netmaker/logic/jwts.go

137 lines
3.8 KiB
Go
Raw Normal View History

2021-10-27 00:27:29 +08:00
package logic
2021-03-26 00:17:52 +08:00
import (
2021-08-10 04:18:24 +08:00
"errors"
2022-02-15 22:51:21 +08:00
"fmt"
2023-03-31 03:10:17 +08:00
"strings"
2021-08-10 04:18:24 +08:00
"time"
2021-10-09 03:07:12 +08:00
2021-08-10 05:57:40 +08:00
"github.com/golang-jwt/jwt/v4"
2022-02-15 22:51:21 +08:00
"github.com/gravitl/netmaker/logger"
2021-08-10 04:18:24 +08:00
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
2021-03-26 00:17:52 +08:00
)
2022-02-15 22:51:21 +08:00
var jwtSecretKey []byte
// SetJWTSecret - sets the jwt secret on server startup
func SetJWTSecret() {
currentSecret, jwtErr := FetchJWTSecret()
if jwtErr != nil {
newValue := RandomString(64)
2022-02-16 10:50:47 +08:00
jwtSecretKey = []byte(newValue) // 512 bit random password
2022-02-15 22:51:21 +08:00
if err := StoreJWTSecret(string(jwtSecretKey)); err != nil {
logger.FatalLog("something went wrong when configuring JWT authentication")
}
} else {
jwtSecretKey = []byte(currentSecret)
}
}
2021-03-26 00:17:52 +08:00
// CreateJWT func will used to create the JWT while signing in and signing out
2022-01-11 06:52:21 +08:00
func CreateJWT(uuid string, macAddress string, network string) (response string, err error) {
2021-08-10 04:18:24 +08:00
expirationTime := time.Now().Add(5 * time.Minute)
claims := &models.Claims{
2022-01-11 06:52:21 +08:00
ID: uuid,
2021-08-10 04:18:24 +08:00
Network: network,
2022-01-11 06:52:21 +08:00
MacAddress: macAddress,
2022-06-27 22:47:28 +08:00
RegisteredClaims: jwt.RegisteredClaims{
2022-02-15 22:51:21 +08:00
Issuer: "Netmaker",
Subject: fmt.Sprintf("node|%s", uuid),
2022-06-27 22:47:28 +08:00
IssuedAt: jwt.NewNumericDate(time.Now()),
ExpiresAt: jwt.NewNumericDate(expirationTime),
2021-08-10 04:18:24 +08:00
},
}
2021-03-26 00:17:52 +08:00
2021-08-10 04:18:24 +08:00
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString(jwtSecretKey)
if err == nil {
return tokenString, nil
}
return "", err
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// CreateUserJWT - creates a user jwt token
func CreateUserJWT(username string, issuperadmin, isadmin bool) (response string, err error) {
expirationTime := time.Now().Add(servercfg.GetServerConfig().JwtValidityDuration)
2021-08-10 04:18:24 +08:00
claims := &models.UserClaims{
UserName: username,
IsSuperAdmin: issuperadmin,
IsAdmin: isadmin,
2022-06-27 22:47:28 +08:00
RegisteredClaims: jwt.RegisteredClaims{
2022-02-15 22:51:21 +08:00
Issuer: "Netmaker",
Subject: fmt.Sprintf("user|%s", username),
2022-06-27 22:47:28 +08:00
IssuedAt: jwt.NewNumericDate(time.Now()),
ExpiresAt: jwt.NewNumericDate(expirationTime),
2021-08-10 04:18:24 +08:00
},
}
2021-03-26 00:17:52 +08:00
2021-08-10 04:18:24 +08:00
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, err := token.SignedString(jwtSecretKey)
if err == nil {
return tokenString, nil
}
return "", err
2021-03-26 00:17:52 +08:00
}
2023-03-31 03:10:17 +08:00
// VerifyJWT verifies Auth Header
func VerifyJWT(bearerToken string) (username string, issuperadmin, isadmin bool, err error) {
2023-03-31 03:10:17 +08:00
token := ""
tokenSplit := strings.Split(bearerToken, " ")
if len(tokenSplit) > 1 {
token = tokenSplit[1]
} else {
return "", false, false, errors.New("invalid auth header")
2023-03-31 03:10:17 +08:00
}
return VerifyUserToken(token)
}
// VerifyUserToken func will used to Verify the JWT Token while using APIS
func VerifyUserToken(tokenString string) (username string, issuperadmin, isadmin bool, err error) {
2021-08-10 04:18:24 +08:00
claims := &models.UserClaims{}
2021-03-26 00:17:52 +08:00
if tokenString == servercfg.GetMasterKey() && servercfg.GetMasterKey() != "" {
return MasterUser, true, true, nil
2021-08-10 04:18:24 +08:00
}
2021-04-15 10:59:25 +08:00
2021-08-10 04:18:24 +08:00
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return jwtSecretKey, nil
})
2021-03-26 00:17:52 +08:00
2021-08-10 04:18:24 +08:00
if token != nil && token.Valid {
var user *models.User
2021-08-10 04:18:24 +08:00
// check that user exists
user, err = GetUser(claims.UserName)
if err != nil {
return "", false, false, err
}
if user.UserName != "" {
2023-11-27 18:43:12 +08:00
return user.UserName, user.IsSuperAdmin, user.IsAdmin, nil
2021-08-10 04:18:24 +08:00
}
err = errors.New("user does not exist")
}
return "", false, false, err
2021-03-26 00:17:52 +08:00
}
2023-03-23 02:47:13 +08:00
// VerifyHostToken - [hosts] Only
func VerifyHostToken(tokenString string) (hostID string, mac string, network string, err error) {
2021-08-10 04:18:24 +08:00
claims := &models.Claims{}
2021-03-26 00:17:52 +08:00
// this may be a stupid way of serving up a master key
// TODO: look into a different method. Encryption?
if tokenString == servercfg.GetMasterKey() && servercfg.GetMasterKey() != "" {
2022-01-11 06:52:21 +08:00
return "mastermac", "", "", nil
2021-08-10 04:18:24 +08:00
}
2021-03-26 00:17:52 +08:00
2021-08-10 04:18:24 +08:00
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return jwtSecretKey, nil
})
2021-03-26 00:17:52 +08:00
2021-08-10 04:18:24 +08:00
if token != nil {
2022-01-11 06:52:21 +08:00
return claims.ID, claims.MacAddress, claims.Network, nil
2021-08-10 04:18:24 +08:00
}
2022-01-11 06:52:21 +08:00
return "", "", "", err
2021-03-26 00:17:52 +08:00
}