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.

claims.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package jwt
  2. import (
  3. "crypto/subtle"
  4. "fmt"
  5. "time"
  6. )
  7. // For a type to be a Claims object, it must just have a Valid method that determines
  8. // if the token is invalid for any supported reason
  9. type Claims interface {
  10. Valid() error
  11. }
  12. // Structured version of Claims Section, as referenced at
  13. // https://tools.ietf.org/html/rfc7519#section-4.1
  14. // See examples for how to use this with your own claim types
  15. type StandardClaims struct {
  16. Audience string `json:"aud,omitempty"`
  17. ExpiresAt int64 `json:"exp,omitempty"`
  18. Id string `json:"jti,omitempty"`
  19. IssuedAt int64 `json:"iat,omitempty"`
  20. Issuer string `json:"iss,omitempty"`
  21. NotBefore int64 `json:"nbf,omitempty"`
  22. Subject string `json:"sub,omitempty"`
  23. }
  24. // Validates time based claims "exp, iat, nbf".
  25. // There is no accounting for clock skew.
  26. // As well, if any of the above claims are not in the token, it will still
  27. // be considered a valid claim.
  28. func (c StandardClaims) Valid() error {
  29. vErr := new(ValidationError)
  30. now := TimeFunc().Unix()
  31. // The claims below are optional, by default, so if they are set to the
  32. // default value in Go, let's not fail the verification for them.
  33. if !c.VerifyExpiresAt(now, false) {
  34. delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
  35. vErr.Inner = fmt.Errorf("token is expired by %v", delta)
  36. vErr.Errors |= ValidationErrorExpired
  37. }
  38. if !c.VerifyIssuedAt(now, false) {
  39. vErr.Inner = fmt.Errorf("Token used before issued")
  40. vErr.Errors |= ValidationErrorIssuedAt
  41. }
  42. if !c.VerifyNotBefore(now, false) {
  43. vErr.Inner = fmt.Errorf("token is not valid yet")
  44. vErr.Errors |= ValidationErrorNotValidYet
  45. }
  46. if vErr.valid() {
  47. return nil
  48. }
  49. return vErr
  50. }
  51. // Compares the aud claim against cmp.
  52. // If required is false, this method will return true if the value matches or is unset
  53. func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
  54. return verifyAud([]string{c.Audience}, cmp, req)
  55. }
  56. // Compares the exp claim against cmp.
  57. // If required is false, this method will return true if the value matches or is unset
  58. func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool {
  59. return verifyExp(c.ExpiresAt, cmp, req)
  60. }
  61. // Compares the iat claim against cmp.
  62. // If required is false, this method will return true if the value matches or is unset
  63. func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
  64. return verifyIat(c.IssuedAt, cmp, req)
  65. }
  66. // Compares the iss claim against cmp.
  67. // If required is false, this method will return true if the value matches or is unset
  68. func (c *StandardClaims) VerifyIssuer(cmp string, req bool) bool {
  69. return verifyIss(c.Issuer, cmp, req)
  70. }
  71. // Compares the nbf claim against cmp.
  72. // If required is false, this method will return true if the value matches or is unset
  73. func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
  74. return verifyNbf(c.NotBefore, cmp, req)
  75. }
  76. // ----- helpers
  77. func verifyAud(aud []string, cmp string, required bool) bool {
  78. if len(aud) == 0 {
  79. return !required
  80. }
  81. // use a var here to keep constant time compare when looping over a number of claims
  82. result := false
  83. var stringClaims string
  84. for _, a := range aud {
  85. if subtle.ConstantTimeCompare([]byte(a), []byte(cmp)) != 0 {
  86. result = true
  87. }
  88. stringClaims = stringClaims + a
  89. }
  90. // case where "" is sent in one or many aud claims
  91. if len(stringClaims) == 0 {
  92. return !required
  93. }
  94. return result
  95. }
  96. func verifyExp(exp int64, now int64, required bool) bool {
  97. if exp == 0 {
  98. return !required
  99. }
  100. return now <= exp
  101. }
  102. func verifyIat(iat int64, now int64, required bool) bool {
  103. if iat == 0 {
  104. return !required
  105. }
  106. return now >= iat
  107. }
  108. func verifyIss(iss string, cmp string, required bool) bool {
  109. if iss == "" {
  110. return !required
  111. }
  112. if subtle.ConstantTimeCompare([]byte(iss), []byte(cmp)) != 0 {
  113. return true
  114. } else {
  115. return false
  116. }
  117. }
  118. func verifyNbf(nbf int64, now int64, required bool) bool {
  119. if nbf == 0 {
  120. return !required
  121. }
  122. return now >= nbf
  123. }