MCPcopy Create free account
hub / github.com/cloud-native-go/examples / buildToken

Function buildToken

ch12/jwt/token.go:31–48  ·  view source on GitHub ↗
(username string)

Source from the content-addressed store, hash-verified

29}
30
31func buildToken(username string) (string, error) {
32 issuedAt := time.Now()
33 expirationTime := issuedAt.Add(time.Hour)
34
35 // Define our claims map
36 claims := jwt.MapClaims{
37 "iat": issuedAt.Unix(),
38 "exp": expirationTime.Unix(),
39 "name": username,
40 }
41
42 // Create a new token object, specifying signing method and the claims
43 // you would like it to contain.
44 token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
45
46 // Sign and get the complete encoded token as a string using the secret
47 return token.SignedString(secret)
48}
49
50func keyFunc(token *jwt.Token) (any, error) {
51 return secret, nil

Callers 2

TestBuildAndVerifyTokenFunction · 0.85
authenticateHandlerFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestBuildAndVerifyTokenFunction · 0.68