2022-12-02 20:40:58 -07:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-12-03 18:15:19 -07:00
|
|
|
"fmt"
|
2022-12-02 20:40:58 -07:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/models"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-12-03 18:15:19 -07:00
|
|
|
// JwtGenerateFastenTokenFromUser Note: these functions are duplicated, in Fasten Cloud
|
|
|
|
//Any changes here must be replicated in that repo
|
2022-12-02 20:40:58 -07:00
|
|
|
func JwtGenerateFastenTokenFromUser(user models.User, issuerSigningKey string) (string, error) {
|
|
|
|
log.Printf("ISSUER KEY: " + issuerSigningKey)
|
|
|
|
userClaims := UserRegisteredClaims{
|
|
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
|
|
// In JWT, the expiry time is expressed as unix milliseconds
|
|
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
|
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
|
|
Issuer: "docker-fastenhealth",
|
|
|
|
Subject: user.Username,
|
|
|
|
},
|
2022-12-03 18:15:19 -07:00
|
|
|
UserMetadata: UserMetadata{
|
|
|
|
FullName: user.FullName,
|
|
|
|
Picture: "",
|
|
|
|
Email: user.ID.String(),
|
|
|
|
},
|
2022-12-02 20:40:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//FASTEN_JWT_ISSUER_KEY
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, userClaims)
|
|
|
|
//token.Header["kid"] = "docker"
|
|
|
|
tokenString, err := token.SignedString([]byte(issuerSigningKey))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return tokenString, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func JwtValidateFastenToken(encryptionKey string, signedToken string) (*UserRegisteredClaims, error) {
|
|
|
|
token, err := jwt.ParseWithClaims(
|
|
|
|
signedToken,
|
|
|
|
&UserRegisteredClaims{},
|
|
|
|
func(token *jwt.Token) (interface{}, error) {
|
|
|
|
if jwt.SigningMethodHS256 != token.Method {
|
2022-12-03 18:15:19 -07:00
|
|
|
return nil, fmt.Errorf("invalid signing algorithm: %s", token.Method)
|
2022-12-02 20:40:58 -07:00
|
|
|
}
|
|
|
|
return []byte(encryptionKey), nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
claims, ok := token.Claims.(*UserRegisteredClaims)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("couldn't parse claims")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if claims.ExpiresAt.Unix() < time.Now().Local().Unix() {
|
|
|
|
err = errors.New("token expired")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return claims, nil
|
|
|
|
}
|