You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

map_claims.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package jwt
  2. import (
  3. "encoding/json"
  4. "errors"
  5. // "fmt"
  6. )
  7. // Claims type that uses the map[string]interface{} for JSON decoding
  8. // This is the default claims type if you don't supply one
  9. type MapClaims map[string]interface{}
  10. // VerifyAudience Compares the aud claim against cmp.
  11. // If required is false, this method will return true if the value matches or is unset
  12. func (m MapClaims) VerifyAudience(cmp string, req bool) bool {
  13. var aud []string
  14. switch v := m["aud"].(type) {
  15. case string:
  16. aud = append(aud, v)
  17. case []string:
  18. aud = v
  19. case []interface{}:
  20. for _, a := range v {
  21. vs, ok := a.(string)
  22. if !ok {
  23. return false
  24. }
  25. aud = append(aud, vs)
  26. }
  27. }
  28. return verifyAud(aud, cmp, req)
  29. }
  30. // Compares the exp claim against cmp.
  31. // If required is false, this method will return true if the value matches or is unset
  32. func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
  33. switch exp := m["exp"].(type) {
  34. case float64:
  35. return verifyExp(int64(exp), cmp, req)
  36. case json.Number:
  37. v, _ := exp.Int64()
  38. return verifyExp(v, cmp, req)
  39. }
  40. return !req
  41. }
  42. // Compares the iat claim against cmp.
  43. // If required is false, this method will return true if the value matches or is unset
  44. func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
  45. switch iat := m["iat"].(type) {
  46. case float64:
  47. return verifyIat(int64(iat), cmp, req)
  48. case json.Number:
  49. v, _ := iat.Int64()
  50. return verifyIat(v, cmp, req)
  51. }
  52. return !req
  53. }
  54. // Compares the iss claim against cmp.
  55. // If required is false, this method will return true if the value matches or is unset
  56. func (m MapClaims) VerifyIssuer(cmp string, req bool) bool {
  57. iss, _ := m["iss"].(string)
  58. return verifyIss(iss, cmp, req)
  59. }
  60. // Compares the nbf claim against cmp.
  61. // If required is false, this method will return true if the value matches or is unset
  62. func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
  63. switch nbf := m["nbf"].(type) {
  64. case float64:
  65. return verifyNbf(int64(nbf), cmp, req)
  66. case json.Number:
  67. v, _ := nbf.Int64()
  68. return verifyNbf(v, cmp, req)
  69. }
  70. return !req
  71. }
  72. // Validates time based claims "exp, iat, nbf".
  73. // There is no accounting for clock skew.
  74. // As well, if any of the above claims are not in the token, it will still
  75. // be considered a valid claim.
  76. func (m MapClaims) Valid() error {
  77. vErr := new(ValidationError)
  78. now := TimeFunc().Unix()
  79. if !m.VerifyExpiresAt(now, false) {
  80. vErr.Inner = errors.New("Token is expired")
  81. vErr.Errors |= ValidationErrorExpired
  82. }
  83. if !m.VerifyIssuedAt(now, false) {
  84. vErr.Inner = errors.New("Token used before issued")
  85. vErr.Errors |= ValidationErrorIssuedAt
  86. }
  87. if !m.VerifyNotBefore(now, false) {
  88. vErr.Inner = errors.New("Token is not valid yet")
  89. vErr.Errors |= ValidationErrorNotValidYet
  90. }
  91. if vErr.valid() {
  92. return nil
  93. }
  94. return vErr
  95. }