Class: HubIdentityRuby::Token
- Inherits:
-
Object
- Object
- HubIdentityRuby::Token
- Defined in:
- app/models/hub_identity_ruby/token.rb
Instance Method Summary collapse
- #current_user ⇒ Object
- #decoded_claims ⇒ Object
- #decoded_headers ⇒ Object
- #expiration_time ⇒ Object
- #expired? ⇒ Boolean
-
#initialize(jwt_token) ⇒ Token
constructor
A new instance of Token.
- #issuer ⇒ Object
- #type ⇒ Object
- #valid? ⇒ Boolean
- #valid_signature? ⇒ Boolean
Constructor Details
#initialize(jwt_token) ⇒ Token
Returns a new instance of Token.
6 7 8 9 10 11 |
# File 'app/models/hub_identity_ruby/token.rb', line 6 def initialize(jwt_token) token_array = jwt_token.split(".") @headers = token_array[0] @claims = token_array[1] @signature = Base64.urlsafe_decode64(token_array[2]) end |
Instance Method Details
#current_user ⇒ Object
13 14 15 16 17 18 19 |
# File 'app/models/hub_identity_ruby/token.rb', line 13 def current_user if valid? && type == "access" user_params else nil end end |
#decoded_claims ⇒ Object
21 22 23 |
# File 'app/models/hub_identity_ruby/token.rb', line 21 def decoded_claims decode_and_parse(@claims) end |
#decoded_headers ⇒ Object
25 26 27 |
# File 'app/models/hub_identity_ruby/token.rb', line 25 def decoded_headers decode_and_parse(@headers) end |
#expiration_time ⇒ Object
33 34 35 |
# File 'app/models/hub_identity_ruby/token.rb', line 33 def expiration_time Time.at(decoded_claims[:exp]) end |
#expired? ⇒ Boolean
29 30 31 |
# File 'app/models/hub_identity_ruby/token.rb', line 29 def expired? Time.now > expiration_time end |
#issuer ⇒ Object
37 38 39 |
# File 'app/models/hub_identity_ruby/token.rb', line 37 def issuer decoded_claims[:iss] end |
#type ⇒ Object
41 42 43 |
# File 'app/models/hub_identity_ruby/token.rb', line 41 def type decoded_claims[:typ] end |
#valid? ⇒ Boolean
45 46 47 |
# File 'app/models/hub_identity_ruby/token.rb', line 45 def valid? valid_signature? && !expired? && issuer == "HubIdentity" end |
#valid_signature? ⇒ Boolean
49 50 51 52 53 54 55 |
# File 'app/models/hub_identity_ruby/token.rb', line 49 def valid_signature? begin rsa_public_key.verify(OpenSSL::Digest.new('sha256'), @signature, @headers + "." + @claims) rescue false end end |