Class: HubIdentityRuby::Token

Inherits:
Object
  • Object
show all
Defined in:
app/models/hub_identity_ruby/token.rb

Instance Method Summary collapse

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_userObject



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_claimsObject



21
22
23
# File 'app/models/hub_identity_ruby/token.rb', line 21

def decoded_claims
  decode_and_parse(@claims)
end

#decoded_headersObject



25
26
27
# File 'app/models/hub_identity_ruby/token.rb', line 25

def decoded_headers
  decode_and_parse(@headers)
end

#expiration_timeObject



33
34
35
# File 'app/models/hub_identity_ruby/token.rb', line 33

def expiration_time
  Time.at(decoded_claims[:exp])
end

#expired?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/hub_identity_ruby/token.rb', line 29

def expired?
  Time.now > expiration_time
end

#issuerObject



37
38
39
# File 'app/models/hub_identity_ruby/token.rb', line 37

def issuer
  decoded_claims[:iss]
end

#typeObject



41
42
43
# File 'app/models/hub_identity_ruby/token.rb', line 41

def type
  decoded_claims[:typ]
end

#valid?Boolean

Returns:

  • (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

Returns:

  • (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