Module: Sandal::Claims
- Defined in:
- lib/sandal/claims.rb
Overview
A module that can be mixed into Hash-like objects to provide claims-related functionality.
Instance Method Summary collapse
-
#validate_aud(valid_aud) ⇒ void
Validates the audience claim.
-
#validate_claims(options = {}) ⇒ Hash
Validates the set of claims.
-
#validate_exp(max_clock_skew = 0) ⇒ void
Validates the expires claim.
-
#validate_iss(valid_iss) ⇒ void
Validates the issuer claim.
-
#validate_nbf(max_clock_skew = 0) ⇒ void
Validates the not-before claim.
Instance Method Details
#validate_aud(valid_aud) ⇒ void
This method returns an undefined value.
Validates the audience claim.
69 70 71 72 73 74 75 76 77 |
# File 'lib/sandal/claims.rb', line 69 def validate_aud(valid_aud) return unless valid_aud && valid_aud.length > 0 aud = self["aud"] aud = [aud] unless aud.is_a?(Array) unless (aud & valid_aud).length > 0 raise Sandal::ClaimError, "The audence is invalid." end end |
#validate_claims(options = {}) ⇒ Hash
Validates the set of claims.
12 13 14 15 16 17 18 |
# File 'lib/sandal/claims.rb', line 12 def validate_claims( = {}) validate_exp([:max_clock_skew]) unless [:ignore_exp] validate_nbf([:max_clock_skew]) unless [:ignore_nbf] validate_iss([:valid_iss]) validate_aud([:valid_aud]) self end |
#validate_exp(max_clock_skew = 0) ⇒ void
This method returns an undefined value.
Validates the expires claim.
26 27 28 29 30 31 32 33 |
# File 'lib/sandal/claims.rb', line 26 def validate_exp(max_clock_skew = 0) max_clock_skew ||= 0 exp = time_claim("exp") if exp && exp <= (Time.now - max_clock_skew) raise Sandal::ExpiredTokenError, "The token has expired." end end |
#validate_iss(valid_iss) ⇒ void
This method returns an undefined value.
Validates the issuer claim.
55 56 57 58 59 60 61 |
# File 'lib/sandal/claims.rb', line 55 def validate_iss(valid_iss) return unless valid_iss && valid_iss.length > 0 unless valid_iss.include?(self["iss"]) raise Sandal::ClaimError, "The issuer is invalid." end end |
#validate_nbf(max_clock_skew = 0) ⇒ void
This method returns an undefined value.
Validates the not-before claim.
41 42 43 44 45 46 47 48 |
# File 'lib/sandal/claims.rb', line 41 def validate_nbf(max_clock_skew = 0) max_clock_skew ||= 0 nbf = time_claim("nbf") if nbf && nbf > (Time.now + max_clock_skew) raise Sandal::ClaimError, "The token is not valid yet." end end |