JWT Authentication
About 2 mincomponentjwtgingrpc
JWT Authentication Component
jwt
is a Token management component based on golang-jwt, providing complete JWT generation and parsing capabilities.
Core Features:
- Supports various signature algorithms such as HS256/HS384/HS512
- Provides complete Token lifecycle management
- Built-in Token validation and expiration check mechanism
Token Generation and Validation
The jwt
component provides two ways to generate and validate JWT tokens.
One Token
Click to view example code
package main
import (
"github.com/go-dev-frame/sponge/pkg/jwt"
"time"
)
func main() {
uid := "123"
// Case 1: default, signKey, signMethod(HS256), expiry time(24 hour)
{
// generate token
jwtID, token, err := jwt.GenerateToken(uid)
// validate token, get claims
claims, err := jwt.ValidateToken(token)
// refresh token
//jwtID, newToken, err := jwt.RefreshToken(token)
}
// Case 2: custom signMethod, signKey, expiry time, fields, claims
{
now := time.Now()
signMethod := jwt.HS384
signKey := "your-secret-key"
// generate token
jwtID, token, err := jwt.GenerateToken(
uid,
jwt.WithGenerateTokenSignMethod(signMethod),
jwt.WithGenerateTokenSignKey(signKey),
jwt.WithGenerateTokenFields(map[string]interface{}{
"name": "john",
"role": "admin",
}),
jwt.WithGenerateTokenClaims([]jwt.RegisteredClaimsOption{
jwt.WithExpires(time.Hour * 12),
jwt.WithIssuedAt(now),
// jwt.WithSubject("123"),
// jwt.WithIssuer("https://auth.example.com"),
// jwt.WithAudience("https://api.example.com"),
// jwt.WithNotBefore(now),
// jwt.WithJwtID("abc1234xxx"),
}...),
)
// validate token, get claims
claims, err := jwt.ValidateToken(token)
// refresh token
//jwtID, newToken, err := jwt.RefreshToken(
// token,
// jwt.WithRefreshTokenSignKey(signKey),
// jwt.WithRefreshTokenExpire(time.Hour*12),
//)
}
}
Tip: jwtID is used to prevent replay attacks. If you need to kick the user offline, you can add it to the blacklist and reject it directly next time you request it.
Two Tokens
Click to view example code
package main
import (
"github.com/go-dev-frame/sponge/pkg/jwt"
"time"
)
func main() {
uid := "123"
// Case 1: default, signKey, signMethod(HS256), expiry time(24 hour)
{
// generate token
tokens, err := jwt.GenerateTwoTokens(uid)
// validate token, get claims
claims, err := jwt.ValidateToken(tokens.AccessToken)
// refresh token, get new access token, if refresh token is expired time is less than 3 hours, will refresh token too.
//newAccessTokens, err := jwt.RefreshTwoTokens(tokens.RefreshToken, tokens.AccessToken)
}
// Case 2: custom signMethod, signKey, expiry time, fields, claims
{
now := time.Now()
signMethod := jwt.HS384
signKey := "your-secret-key"
// generate token
tokens, err := jwt.GenerateTwoTokens(
uid,
jwt.WithGenerateTwoTokensSignMethod(signMethod),
jwt.WithGenerateTwoTokensSignKey(signKey),
jwt.WithGenerateTwoTokensFields(map[string]interface{}{
"name": "john",
"role": "admin",
}),
jwt.WithGenerateTwoTokensRefreshTokenClaims([]jwt.RegisteredClaimsOption{
jwt.WithExpires(time.Hour * 24 * 15),
jwt.WithIssuedAt(now),
// jwt.WithSubject("123"),
// jwt.WithIssuer("https://auth.example.com"),
// jwt.WithAudience("https://api.example.com"),
// jwt.WithNotBefore(now),
// jwt.WithJwtID("abc1234xxx"),
}...),
jwt.WithGenerateTwoTokensAccessTokenClaims([]jwt.RegisteredClaimsOption{
jwt.WithExpires(time.Minute * 15),
jwt.WithIssuedAt(now),
// jwt.WithSubject("123"),
// jwt.WithIssuer("https://auth.example.com"),
// jwt.WithAudience("https://api.example.com"),
// jwt.WithNotBefore(now),
// jwt.WithJwtID("abc1234xxx"),
}...),
)
// validate token, get claims
claims, err := jwt.ValidateToken(tokens.AccessToken)
// refresh token
newTokens, err := jwt.RefreshTwoTokens(
tokens.RefreshToken,
tokens.AccessToken,
jwt.WithRefreshTwoTokensSignKey(signKey),
jwt.WithRefreshTwoTokensRefreshTokenExpires(time.Hour*24*15),
jwt.WithRefreshTwoTokensAccessTokenExpires(time.Minute*15),
)
}
}
Framework Integration
Gin
Provides out-of-the-box JWT authentication middleware, supporting route-level permission control. Click to view Example of using JWT in Gin.
gRPC
Provides gRPC interceptor implementation, supporting service/method-level JWT authentication. Click to view Example of using JWT in gRPC.