本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

109 行
3.3KB

  1. package jwt
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "strings"
  6. "time"
  7. )
  8. // TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time).
  9. // You can override it to use another time value. This is useful for testing or if your
  10. // server uses a different time zone than your tokens.
  11. var TimeFunc = time.Now
  12. // Parse methods use this callback function to supply
  13. // the key for verification. The function receives the parsed,
  14. // but unverified Token. This allows you to use properties in the
  15. // Header of the token (such as `kid`) to identify which key to use.
  16. type Keyfunc func(*Token) (interface{}, error)
  17. // A JWT Token. Different fields will be used depending on whether you're
  18. // creating or parsing/verifying a token.
  19. type Token struct {
  20. Raw string // The raw token. Populated when you Parse a token
  21. Method SigningMethod // The signing method used or to be used
  22. Header map[string]interface{} // The first segment of the token
  23. Claims Claims // The second segment of the token
  24. Signature string // The third segment of the token. Populated when you Parse a token
  25. Valid bool // Is the token valid? Populated when you Parse/Verify a token
  26. }
  27. // Create a new Token. Takes a signing method
  28. func New(method SigningMethod) *Token {
  29. return NewWithClaims(method, MapClaims{})
  30. }
  31. func NewWithClaims(method SigningMethod, claims Claims) *Token {
  32. return &Token{
  33. Header: map[string]interface{}{
  34. "typ": "JWT",
  35. "alg": method.Alg(),
  36. },
  37. Claims: claims,
  38. Method: method,
  39. }
  40. }
  41. // Get the complete, signed token
  42. func (t *Token) SignedString(key interface{}) (string, error) {
  43. var sig, sstr string
  44. var err error
  45. if sstr, err = t.SigningString(); err != nil {
  46. return "", err
  47. }
  48. if sig, err = t.Method.Sign(sstr, key); err != nil {
  49. return "", err
  50. }
  51. return strings.Join([]string{sstr, sig}, "."), nil
  52. }
  53. // Generate the signing string. This is the
  54. // most expensive part of the whole deal. Unless you
  55. // need this for something special, just go straight for
  56. // the SignedString.
  57. func (t *Token) SigningString() (string, error) {
  58. var err error
  59. parts := make([]string, 2)
  60. for i, _ := range parts {
  61. var jsonValue []byte
  62. if i == 0 {
  63. if jsonValue, err = json.Marshal(t.Header); err != nil {
  64. return "", err
  65. }
  66. } else {
  67. if jsonValue, err = json.Marshal(t.Claims); err != nil {
  68. return "", err
  69. }
  70. }
  71. parts[i] = EncodeSegment(jsonValue)
  72. }
  73. return strings.Join(parts, "."), nil
  74. }
  75. // Parse, validate, and return a token.
  76. // keyFunc will receive the parsed token and should return the key for validating.
  77. // If everything is kosher, err will be nil
  78. func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
  79. return new(Parser).Parse(tokenString, keyFunc)
  80. }
  81. func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
  82. return new(Parser).ParseWithClaims(tokenString, claims, keyFunc)
  83. }
  84. // Encode JWT specific base64url encoding with padding stripped
  85. func EncodeSegment(seg []byte) string {
  86. return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=")
  87. }
  88. // Decode JWT specific base64url encoding with padding stripped
  89. func DecodeSegment(seg string) ([]byte, error) {
  90. if l := len(seg) % 4; l > 0 {
  91. seg += strings.Repeat("=", 4-l)
  92. }
  93. return base64.URLEncoding.DecodeString(seg)
  94. }
上海开阖软件有限公司 沪ICP备12045867号-1