jwts.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package logic
  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. // CreateUserJWT - creates a user jwt token
  28. func CreateUserJWT(username string, networks []string, isadmin bool) (response string, err error) {
  29. expirationTime := time.Now().Add(60 * 12 * time.Minute)
  30. claims := &models.UserClaims{
  31. UserName: username,
  32. Networks: networks,
  33. IsAdmin: isadmin,
  34. StandardClaims: jwt.StandardClaims{
  35. ExpiresAt: expirationTime.Unix(),
  36. },
  37. }
  38. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  39. tokenString, err := token.SignedString(jwtSecretKey)
  40. if err == nil {
  41. return tokenString, nil
  42. }
  43. return "", err
  44. }
  45. // VerifyToken func will used to Verify the JWT Token while using APIS
  46. func VerifyUserToken(tokenString string) (username string, networks []string, isadmin bool, err error) {
  47. claims := &models.UserClaims{}
  48. if tokenString == servercfg.GetMasterKey() {
  49. return "masteradministrator", nil, true, nil
  50. }
  51. token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
  52. return jwtSecretKey, nil
  53. })
  54. if token != nil && token.Valid {
  55. // check that user exists
  56. if user, err := GetUser(claims.UserName); user.UserName != "" && err == nil {
  57. return claims.UserName, claims.Networks, claims.IsAdmin, nil
  58. }
  59. err = errors.New("user does not exist")
  60. }
  61. return "", nil, false, err
  62. }
  63. // VerifyToken - gRPC [nodes] Only
  64. func VerifyToken(tokenString string) (macaddress string, network string, err error) {
  65. claims := &models.Claims{}
  66. //this may be a stupid way of serving up a master key
  67. //TODO: look into a different method. Encryption?
  68. if tokenString == servercfg.GetMasterKey() {
  69. return "mastermac", "", nil
  70. }
  71. token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
  72. return jwtSecretKey, nil
  73. })
  74. if token != nil {
  75. return claims.MacAddress, claims.Network, nil
  76. }
  77. return "", "", err
  78. }