refactor: prefix all jwt helpers

This commit is contained in:
Derrick Hammer 2024-02-13 22:31:44 -05:00
parent 50c4d8b945
commit f645499c7f
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 6 additions and 6 deletions

View File

@ -253,7 +253,7 @@ func (s AccountServiceDefault) PinByID(uploadId uint, accountID uint) error {
} }
func (s AccountServiceDefault) doLogin(user *models.User, ip string) (string, error) { func (s AccountServiceDefault) doLogin(user *models.User, ip string) (string, error) {
token, err := GenerateToken(s.config.GetString("core.domain"), s.identity, user.ID, JWTPurposeLogin) token, err := JWTGenerateToken(s.config.GetString("core.domain"), s.identity, user.ID, JWTPurposeLogin)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -27,11 +27,11 @@ const (
JWTPurpose2FA JWTPurpose = "2fa" JWTPurpose2FA JWTPurpose = "2fa"
) )
func GenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint, purpose JWTPurpose) (string, error) { func JWTGenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint, purpose JWTPurpose) (string, error) {
return GenerateTokenWithDuration(domain, privateKey, userID, time.Hour*24, purpose) return JWTGenerateTokenWithDuration(domain, privateKey, userID, time.Hour*24, purpose)
} }
func GenerateTokenWithDuration(domain string, privateKey ed25519.PrivateKey, userID uint, duration time.Duration, purpose JWTPurpose) (string, error) { func JWTGenerateTokenWithDuration(domain string, privateKey ed25519.PrivateKey, userID uint, duration time.Duration, purpose JWTPurpose) (string, error) {
// Define the claims // Define the claims
claims := jwt.RegisteredClaims{ claims := jwt.RegisteredClaims{
@ -54,7 +54,7 @@ func GenerateTokenWithDuration(domain string, privateKey ed25519.PrivateKey, use
return tokenString, nil return tokenString, nil
} }
func VerifyToken(token string, domain string, privateKey ed25519.PrivateKey, verifyFunc VerifyTokenFunc) (*jwt.RegisteredClaims, error) { func JWTVerifyToken(token string, domain string, privateKey ed25519.PrivateKey, verifyFunc VerifyTokenFunc) (*jwt.RegisteredClaims, error) {
validatedToken, err := jwt.ParseWithClaims(token, jwt.RegisteredClaims{}, func(token *jwt.Token) (interface{}, error) { validatedToken, err := jwt.ParseWithClaims(token, jwt.RegisteredClaims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodEd25519); !ok { if _, ok := token.Method.(*jwt.SigningMethodEd25519); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])

View File

@ -120,7 +120,7 @@ func AuthMiddleware(options AuthMiddlewareOptions) func(http.Handler) http.Handl
return return
} }
claim, err := account.VerifyToken(authToken, domain, options.Identity, func(claim jwt.RegisteredClaims) error { claim, err := account.JWTVerifyToken(authToken, domain, options.Identity, func(claim jwt.RegisteredClaims) error {
aud, _ := claim.GetAudience() aud, _ := claim.GetAudience()
if slices.Contains[jwt.ClaimStrings, string](aud, string(options.Purpose)) == false { if slices.Contains[jwt.ClaimStrings, string](aud, string(options.Purpose)) == false {