Authentication
server
- http
- grpc
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
return []byte(testKey), nil
}),
),
)
client
- http
conn, err := http.NewClient(
context.Background(),
http.WithEndpoint("127.0.0.1:8000"),
jwt.Client(func(token *jwtv4.Token) (interface{}, error) {
return []byte(serviceTestKey), nil
}),
),
)
- grpc
Options
WithSigningMethod()
Used to set the sigining method.
For examples:
jwt.WithSigningMethod(jwtv4.SigningMethodHS256)
WithClaims()
For examples:
- For
client
:
claims := &jwtv4.StandardClaims{}
jwt.WithClaims(func()jwtv4.Claims{return claims})
- For
server
A simple
In particular, client
is set to visit a service listening the port 9001. And that service should set a key as the same as the client one named .
con, _ := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("dns:///127.0.0.1:9001"),
grpc.WithMiddleware(
jwt.Client(func(token *jwtv4.Token) (interface{}, error) {
return []byte(serviceTestKey), nil
}),
),
)
Extract Users’ Information
Under the hook, after processing by the middleware, the claims
information would be stored into the context. One should assert the claims
as the type that is used to create the token before using it.
Source code:
With selector
middleware, one could setup white list. Ref:
Generate JWT Token
Ref: