jwt.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package security
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "time"
  7. jwt "github.com/golang-jwt/jwt/v5"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. )
  10. type EncodedJwt string
  11. type SigningKey []byte
  12. // SeaweedFileIdClaims is created by Master server(s) and consumed by Volume server(s),
  13. // restricting the access this JWT allows to only a single file.
  14. type SeaweedFileIdClaims struct {
  15. Fid string `json:"fid"`
  16. jwt.RegisteredClaims
  17. }
  18. // SeaweedFilerClaims is created e.g. by S3 proxy server and consumed by Filer server.
  19. // Right now, it only contains the standard claims; but this might be extended later
  20. // for more fine-grained permissions.
  21. type SeaweedFilerClaims struct {
  22. jwt.RegisteredClaims
  23. }
  24. func GenJwtForVolumeServer(signingKey SigningKey, expiresAfterSec int, fileId string) EncodedJwt {
  25. if len(signingKey) == 0 {
  26. return ""
  27. }
  28. claims := SeaweedFileIdClaims{
  29. fileId,
  30. jwt.RegisteredClaims{},
  31. }
  32. if expiresAfterSec > 0 {
  33. claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(expiresAfterSec)))
  34. }
  35. t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  36. encoded, e := t.SignedString([]byte(signingKey))
  37. if e != nil {
  38. glog.V(0).Infof("Failed to sign claims %+v: %v", t.Claims, e)
  39. return ""
  40. }
  41. return EncodedJwt(encoded)
  42. }
  43. // GenJwtForFilerServer creates a JSON-web-token for using the authenticated Filer API. Used f.e. inside
  44. // the S3 API
  45. func GenJwtForFilerServer(signingKey SigningKey, expiresAfterSec int) EncodedJwt {
  46. if len(signingKey) == 0 {
  47. return ""
  48. }
  49. claims := SeaweedFilerClaims{
  50. jwt.RegisteredClaims{},
  51. }
  52. if expiresAfterSec > 0 {
  53. claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(expiresAfterSec)))
  54. }
  55. t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  56. encoded, e := t.SignedString([]byte(signingKey))
  57. if e != nil {
  58. glog.V(0).Infof("Failed to sign claims %+v: %v", t.Claims, e)
  59. return ""
  60. }
  61. return EncodedJwt(encoded)
  62. }
  63. func GetJwt(r *http.Request) EncodedJwt {
  64. // Get token from query params
  65. tokenStr := r.URL.Query().Get("jwt")
  66. // Get token from authorization header
  67. if tokenStr == "" {
  68. bearer := r.Header.Get("Authorization")
  69. if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
  70. tokenStr = bearer[7:]
  71. }
  72. }
  73. // Get token from http only cookie
  74. if tokenStr == "" {
  75. token, err := r.Cookie("AT")
  76. if err == nil {
  77. tokenStr = token.Value
  78. }
  79. }
  80. return EncodedJwt(tokenStr)
  81. }
  82. func DecodeJwt(signingKey SigningKey, tokenString EncodedJwt, claims jwt.Claims) (token *jwt.Token, err error) {
  83. // check exp, nbf
  84. return jwt.ParseWithClaims(string(tokenString), claims, func(token *jwt.Token) (interface{}, error) {
  85. if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
  86. return nil, fmt.Errorf("unknown token method")
  87. }
  88. return []byte(signingKey), nil
  89. })
  90. }