netmaker/logic/jwts.go

95 lines
2.6 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"
"time"
2021-10-09 03:07:12 +08:00
2021-08-10 05:57:40 +08:00
"github.com/golang-jwt/jwt/v4"
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
)
var jwtSecretKey = []byte("(BytesOverTheWire)")
// 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,
2021-08-10 04:18:24 +08:00
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}
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
2021-07-02 12:03:46 +08:00
func CreateUserJWT(username string, networks []string, isadmin bool) (response string, err error) {
2021-08-10 05:57:40 +08:00
expirationTime := time.Now().Add(60 * 12 * time.Minute)
2021-08-10 04:18:24 +08:00
claims := &models.UserClaims{
UserName: username,
Networks: networks,
IsAdmin: isadmin,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
}
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
}
// VerifyToken func will used to Verify the JWT Token while using APIS
2021-07-02 12:03:46 +08:00
func VerifyUserToken(tokenString string) (username string, networks []string, isadmin bool, err error) {
2021-08-10 04:18:24 +08:00
claims := &models.UserClaims{}
2021-03-26 00:17:52 +08:00
2021-08-10 04:18:24 +08:00
if tokenString == servercfg.GetMasterKey() {
return "masteradministrator", nil, true, nil
}
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 {
// check that user exists
if user, err := GetUser(claims.UserName); user.UserName != "" && err == nil {
return claims.UserName, claims.Networks, claims.IsAdmin, nil
}
err = errors.New("user does not exist")
}
return "", nil, false, err
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// VerifyToken - gRPC [nodes] Only
2022-01-11 06:52:21 +08:00
func VerifyToken(tokenString string) (nodeID 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
2021-08-10 04:18:24 +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() {
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
}