JWT

Overview

In go-zero, we declared HTTP service via api language, and then generated HTTP service code via goctl, after our systematic introduction to API norm.

Service authentication is also a frequently used feature in HTTP service development, this document will describe how to declare intermediate in api files.

JWT

JWT (JSON Web Token) is an open standard (RFC 7519) used to transmit declaratory messages between web applications.It is a lightweight JSON-based authentication and authorization mechanism for the safe transmission of information between clients and servers.

For more documentation about jwt

  1. 《JSON Web Tokens》
  2. JWT Certification

Let’s see how to declare jwt authentication in an api file

  1. syntax = "v1"
  2. type LoginReq {
  3. Username string `json:"username"`
  4. Password string `json:"password"`
  5. }
  6. type LoginResp {
  7. ID string `json:"id"`
  8. Name string `json:"name"`
  9. }
  10. type UserInfoReq {
  11. ID string `json:"id"`
  12. }
  13. type UserInfoResp {
  14. Name string `json:"name"`
  15. }
  16. service user-api {
  17. @handler login
  18. post /user/login (LoginReq) returns (LoginResp)
  19. }
  20. @server (
  21. jwt: Auth // Enable jwt authentication
  22. )
  23. service user-api {
  24. @handler userInfo
  25. post /user/info (UserInfoReq) returns (UserInfoResp)
  26. }

In the above, we declared that the jwt authentication is enabled through the jwt keyword in @server, and the jwt authentication is only useful for its corresponding route, as in the jwt above only for /user/info takes effect, but not for /user/login, we use Auth as the value of jwt, after goctl After code generation, it will be converted into Corresponding to jwt configuration.

Below look briefly at the generated jwt code:

  • config.go
  • routes.go
  1. package config
  2. import "github.com/zeromicro/go-zero/rest"
  3. type Config struct {
  4. rest.RestConf
  5. Auth struct {// Key and expiration time configuration required for JWT authentication
  6. AccessSecret string
  7. AccessExpire int64
  8. }
  9. }

The Auth field in the Config structure is the value we declared in the api syntax file, which is the result of code generation

  1. // Code generated by goctl. DO NOT EDIT.
  2. package handler
  3. import (
  4. "net/http"
  5. "go-zero-demo/user/internal/svc"
  6. "github.com/zeromicro/go-zero/rest"
  7. )
  8. func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
  9. server.AddRoutes(
  10. []rest.Route{
  11. {
  12. Method: http.MethodPost,
  13. Path: "/user/login",
  14. Handler: loginHandler(serverCtx),
  15. },
  16. },
  17. )
  18. server.AddRoutes(
  19. []rest.Route{
  20. {
  21. Method: http.MethodPost,
  22. Path: "/user/info",
  23. Handler: userInfoHandler(serverCtx),
  24. },
  25. },
  26. rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
  27. )
  28. }

In the above, we can see that our declared jwt actually generated code by rest.WithJwt to declare jwt authentication.

JWT - 图1takes note of

Jwt authentication after code is generated, the framework only provides server logic and needs to be implemented by the developer for jwt token generation and refresh token.