Authentication

    server

    • http
    • grpc
    1. grpcSrv := grpc.NewServer(
    2. grpc.Address(":9000"),
    3. grpc.Middleware(
    4. return []byte(testKey), nil
    5. }),
    6. ),
    7. )

    client

    • http
    1. conn, err := http.NewClient(
    2. context.Background(),
    3. http.WithEndpoint("127.0.0.1:8000"),
    4. jwt.Client(func(token *jwtv4.Token) (interface{}, error) {
    5. return []byte(serviceTestKey), nil
    6. }),
    7. ),
    8. )
    • grpc

    Options

    WithSigningMethod()

    Used to set the sigining method.

    For examples:

    1. jwt.WithSigningMethod(jwtv4.SigningMethodHS256)

    WithClaims()

    For examples:

    • For client:
    1. claims := &jwtv4.StandardClaims{}
    2. 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 .

    1. con, _ := grpc.DialInsecure(
    2. context.Background(),
    3. grpc.WithEndpoint("dns:///127.0.0.1:9001"),
    4. grpc.WithMiddleware(
    5. jwt.Client(func(token *jwtv4.Token) (interface{}, error) {
    6. return []byte(serviceTestKey), nil
    7. }),
    8. ),
    9. )

    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: