Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/cognito_idp/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

module CognitoIdp
class Token
attr_reader :access_token, :id_token, :token_type, :expires_at, :expires_in
attr_reader :access_token, :id_token, :token_type, :expires_at, :expires_in, :refresh_token

def initialize(token_hash)
token_hash.transform_keys(&:to_sym).tap do |values|
@access_token = values[:access_token]
@id_token = values[:id_token]
@token_type = values[:token_type]
@expires_in = values[:expires_in]
@refresh_token = values[:refresh_token]
end
@expires_at = Time.now + expires_in unless expires_in.nil?
end
Expand Down
5 changes: 4 additions & 1 deletion spec/cognito_idp/token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
it { expect(token.token_type).to be_nil }
it { expect(token.expires_in).to be_nil }
it { expect(token.expires_at).to be_nil }
it { expect(token.refresh_token).to be_nil }

context "when token is initialized with values" do
let(:token_hash) do
{
"access_token" => "eyJra1example",
"id_token" => "eyJra2example",
"token_type" => "Bearer",
"expires_in" => 3600
"expires_in" => 3600,
"refresh_token" => "refresh-token-1"
}
end

Expand All @@ -38,5 +40,6 @@
it { expect(token.token_type).to eq("Bearer") }
it { expect(token.expires_in).to eq(3600) }
it { expect(token.expires_at).to eq(Time.now + 3600) }
it { expect(token.refresh_token).to eq("refresh-token-1") }
end
end