MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / generateToken

Function generateToken

examples/server/auth-middleware/main.go:70–85  ·  view source on GitHub ↗

generateToken creates a JWT token for testing purposes. In a real application, this would be handled by your authentication service.

(userID string, scopes []string, expiresIn time.Duration)

Source from the content-addressed store, hash-verified

68// generateToken creates a JWT token for testing purposes.
69// In a real application, this would be handled by your authentication service.
70func generateToken(userID string, scopes []string, expiresIn time.Duration) (string, error) {
71 // Create JWT claims with user information and scopes.
72 claims := JWTClaims{
73 UserID: userID,
74 Scopes: scopes,
75 RegisteredClaims: jwt.RegisteredClaims{
76 ExpiresAt: jwt.NewNumericDate(time.Now().Add(expiresIn)), // Token expiration
77 IssuedAt: jwt.NewNumericDate(time.Now()), // Token issuance time
78 NotBefore: jwt.NewNumericDate(time.Now()), // Token validity start time
79 },
80 }
81
82 // Create and sign the JWT token.
83 token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
84 return token.SignedString(jwtSecret)
85}
86
87// verifyJWT verifies JWT tokens and returns TokenInfo for the auth middleware.
88// This function implements the TokenVerifier interface required by auth.RequireBearerToken.

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…