jwt.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package functions
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/golang-jwt/jwt/v4"
  6. "github.com/gravitl/netmaker/models"
  7. "github.com/gravitl/netmaker/servercfg"
  8. )
  9. var jwtSecretKey = []byte("(BytesOverTheWire)")
  10. // CreateJWT func will used to create the JWT while signing in and signing out
  11. func CreateJWT(macaddress string, network string) (response string, err error) {
  12. expirationTime := time.Now().Add(5 * time.Minute)
  13. claims := &models.Claims{
  14. MacAddress: macaddress,
  15. Network: network,
  16. StandardClaims: jwt.StandardClaims{
  17. ExpiresAt: expirationTime.Unix(),
  18. },
  19. }
  20. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  21. tokenString, err := token.SignedString(jwtSecretKey)
  22. if err == nil {
  23. return tokenString, nil
  24. }
  25. return "", err
  26. }
  27. func CreateUserJWT(username string, networks []string, isadmin bool) (response string, err error) {
  28. expirationTime := time.Now().Add(60 * 12 * time.Minute)
  29. claims := &models.UserClaims{
  30. UserName: username,
  31. Networks: networks,
  32. IsAdmin: isadmin,
  33. StandardClaims: jwt.StandardClaims{
  34. ExpiresAt: expirationTime.Unix(),
  35. },
  36. }
  37. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  38. tokenString, err := token.SignedString(jwtSecretKey)
  39. if err == nil {
  40. return tokenString, nil
  41. }
  42. return "", err
  43. }
  44. // VerifyToken func will used to Verify the JWT Token while using APIS
  45. func VerifyUserToken(tokenString string) (username string, networks []string, isadmin bool, err error) {
  46. claims := &models.UserClaims{}
  47. if tokenString == servercfg.GetMasterKey() {
  48. return "masteradministrator", nil, true, nil
  49. }
  50. token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
  51. return jwtSecretKey, nil
  52. })
  53. if token != nil && token.Valid {
  54. // check that user exists
  55. if user, err := GetUser(claims.UserName); user.UserName != "" && err == nil {
  56. return claims.UserName, claims.Networks, claims.IsAdmin, nil
  57. }
  58. err = errors.New("user does not exist")
  59. }
  60. return "", nil, false, err
  61. }
  62. // GRPC [nodes] Only
  63. func VerifyToken(tokenString string) (macaddress string, network string, err error) {
  64. claims := &models.Claims{}
  65. //this may be a stupid way of serving up a master key
  66. //TODO: look into a different method. Encryption?
  67. if tokenString == servercfg.GetMasterKey() {
  68. return "mastermac", "", nil
  69. }
  70. token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
  71. return jwtSecretKey, nil
  72. })
  73. if token != nil {
  74. return claims.MacAddress, claims.Network, nil
  75. }
  76. return "", "", err
  77. }