44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
type TestAuthenticator struct {
|
|
}
|
|
|
|
const testToken = "test-token"
|
|
|
|
var testClaims = jwt.MapClaims{
|
|
"aud": "test-audience",
|
|
"iss": "test-issuer",
|
|
"sub": 1,
|
|
"exp": time.Now().Add(time.Hour * 24).Unix(),
|
|
}
|
|
|
|
func (a *TestAuthenticator) GenerateToken(claims jwt.Claims) (string, error) {
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, testClaims)
|
|
|
|
return token.SignedString([]byte(testToken))
|
|
}
|
|
|
|
func (a *TestAuthenticator) GenerateRefreshToken(claims jwt.Claims) (string, error) {
|
|
refreshToken := jwt.NewWithClaims(jwt.SigningMethodHS256, testClaims)
|
|
|
|
return refreshToken.SignedString([]byte(testToken))
|
|
}
|
|
|
|
func (a *TestAuthenticator) ValidateToken(token string) (*jwt.Token, error) {
|
|
return jwt.Parse(token, func(token *jwt.Token) (any, error) {
|
|
return []byte(testToken), nil
|
|
})
|
|
}
|
|
|
|
func (a *TestAuthenticator) ValidateRefreshToken(token string) (*jwt.Token, error) {
|
|
return jwt.Parse(token, func(token *jwt.Token) (any, error) {
|
|
return []byte(testToken), nil
|
|
})
|
|
}
|