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) ⇒ void
Validates the expires claim.
-
#validate_iss(valid_iss) ⇒ void
Validates the issuer claim.
-
#validate_nbf(max_clock_skew) ⇒ void
Validates the not-before claim.
Instance Method Details
#validate_aud(valid_aud) ⇒ void
This method returns an undefined value.
Validates the audience claim.
59 60 61 62 63 64 65 |
# File 'lib/sandal/claims.rb', line 59 def validate_aud(valid_aud) if valid_aud && valid_aud.length > 0 aud = self['aud'] aud = [aud] unless aud.is_a?(Array) raise Sandal::ClaimError, 'The audence is invalid.' unless (aud & valid_aud).length > 0 end end |
#validate_claims(options) ⇒ Hash
Validates the set of claims.
10 11 12 13 14 15 16 |
# File 'lib/sandal/claims.rb', line 10 def validate_claims() validate_exp([:max_clock_skew]) if [:validate_exp] validate_nbf([:max_clock_skew]) if [:validate_nbf] validate_iss([:valid_iss]) validate_aud([:valid_aud]) self end |
#validate_exp(max_clock_skew) ⇒ void
This method returns an undefined value.
Validates the expires claim.
23 24 25 26 27 28 29 |
# File 'lib/sandal/claims.rb', line 23 def validate_exp(max_clock_skew) exp = time_claim('exp') if exp && exp <= (Time.now - max_clock_skew) raise Sandal::ClaimError, 'The token has expired.' end nil end |
#validate_iss(valid_iss) ⇒ void
This method returns an undefined value.
Validates the issuer claim.
48 49 50 51 52 |
# File 'lib/sandal/claims.rb', line 48 def validate_iss(valid_iss) if valid_iss && valid_iss.length > 0 raise Sandal::ClaimError, 'The issuer is invalid.' unless valid_iss.include?(self['iss']) end end |
#validate_nbf(max_clock_skew) ⇒ void
This method returns an undefined value.
Validates the not-before claim.
36 37 38 39 40 41 |
# File 'lib/sandal/claims.rb', line 36 def validate_nbf(max_clock_skew) nbf = time_claim('nbf') if nbf && nbf > (Time.now + max_clock_skew) raise Sandal::ClaimError, 'The token is not valid yet.' end end |