mirror of
https://github.com/Unkn0wnCat/calapi.git
synced 2025-05-30 07:36:10 +02:00
Add authentication and metrics
This commit is contained in:
parent
7667ea7b90
commit
6abea91d7c
16 changed files with 894 additions and 31 deletions
60
internal/auth/jwtHelpers.go
Normal file
60
internal/auth/jwtHelpers.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/spf13/viper"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ParseJWT(tokenString string) (*User, error) {
|
||||
claims := JwtClaims{}
|
||||
jwtSigningKey := []byte(viper.GetString("auth.secret"))
|
||||
|
||||
token, err := jwt.ParseWithClaims(tokenString, &claims, func(token *jwt.Token) (interface{}, error) {
|
||||
return jwtSigningKey, nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !token.Valid {
|
||||
return nil, errors.New("invalid token")
|
||||
}
|
||||
|
||||
user := &User{
|
||||
ID: claims.Subject,
|
||||
Username: claims.Username,
|
||||
Name: claims.Name,
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func MakeJWT(user *User) (string, error) {
|
||||
if user == nil {
|
||||
return "", errors.New("no user provided")
|
||||
}
|
||||
|
||||
claims := JwtClaims{
|
||||
Username: user.Username,
|
||||
Name: user.Name,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Issuer: "calapi",
|
||||
Subject: user.ID,
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour * 24)),
|
||||
NotBefore: jwt.NewNumericDate(time.Now()),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
},
|
||||
}
|
||||
|
||||
jwtSigningKey := []byte(viper.GetString("auth.secret"))
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
ss, err := token.SignedString(jwtSigningKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ss, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue