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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. exp, ok := m["exp"]
  34. if !ok {
  35. return !req
  36. }
  37. switch expType := exp.(type) {
  38. case float64:
  39. return verifyExp(int64(expType), cmp, req)
  40. case json.Number:
  41. v, _ := expType.Int64()
  42. return verifyExp(v, cmp, req)
  43. }
  44. return false
  45. }
  46. // Compares the iat claim against cmp.
  47. // If required is false, this method will return true if the value matches or is unset
  48. func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
  49. iat, ok := m["iat"]
  50. if !ok {
  51. return !req
  52. }
  53. switch iatType := iat.(type) {
  54. case float64:
  55. return verifyIat(int64(iatType), cmp, req)
  56. case json.Number:
  57. v, _ := iatType.Int64()
  58. return verifyIat(v, cmp, req)
  59. }
  60. return false
  61. }
  62. // Compares the iss claim against cmp.
  63. // If required is false, this method will return true if the value matches or is unset
  64. func (m MapClaims) VerifyIssuer(cmp string, req bool) bool {
  65. iss, _ := m["iss"].(string)
  66. return verifyIss(iss, cmp, req)
  67. }
  68. // Compares the nbf claim against cmp.
  69. // If required is false, this method will return true if the value matches or is unset
  70. func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
  71. nbf, ok := m["nbf"]
  72. if !ok {
  73. return !req
  74. }
  75. switch nbfType := nbf.(type) {
  76. case float64:
  77. return verifyNbf(int64(nbfType), cmp, req)
  78. case json.Number:
  79. v, _ := nbfType.Int64()
  80. return verifyNbf(v, cmp, req)
  81. }
  82. return false
  83. }
  84. // Validates time based claims "exp, iat, nbf".
  85. // There is no accounting for clock skew.
  86. // As well, if any of the above claims are not in the token, it will still
  87. // be considered a valid claim.
  88. func (m MapClaims) Valid() error {
  89. vErr := new(ValidationError)
  90. now := TimeFunc().Unix()
  91. if !m.VerifyExpiresAt(now, false) {
  92. vErr.Inner = errors.New("Token is expired")
  93. vErr.Errors |= ValidationErrorExpired
  94. }
  95. if !m.VerifyIssuedAt(now, false) {
  96. vErr.Inner = errors.New("Token used before issued")
  97. vErr.Errors |= ValidationErrorIssuedAt
  98. }
  99. if !m.VerifyNotBefore(now, false) {
  100. vErr.Inner = errors.New("Token is not valid yet")
  101. vErr.Errors |= ValidationErrorNotValidYet
  102. }
  103. if vErr.valid() {
  104. return nil
  105. }
  106. return vErr
  107. }