Skip to content

Commit bd72278

Browse files
authored
Fix CognitoIdentityCredentials (#2944)
1 parent acbe006 commit bd72278

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

gems/aws-sdk-cognitoidentity/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Issue - Pass provided `logins` when a `CognitoIdentityCredentials` client is created (#2941).
5+
46
1.49.0 (2023-09-27)
57
------------------
68

gems/aws-sdk-cognitoidentity/lib/aws-sdk-cognitoidentity/customizations/cognito_identity_credentials.rb

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module CognitoIdentity
2323
#
2424
# ## Refreshing Credentials from Identity Service
2525
#
26-
# The CognitoIdentityCredentials will auto-refresh the AWS credentials from
26+
# The `CognitoIdentityCredentials` will auto-refresh the AWS credentials from
2727
# Cognito. In addition to AWS credentials expiring after a given amount of
2828
# time, the login token from the identity provider will also expire.
2929
# Once this token expires, it will not be usable to refresh AWS credentials,
@@ -32,19 +32,18 @@ module CognitoIdentity
3232
# supported by most identity providers. Consult the documentation for
3333
# the identity provider for refreshing tokens. Once the refreshed token is
3434
# acquired, you should make sure to update this new token in the
35-
# CognitoIdentityCredentials object's {logins} property. The following
35+
# `CognitoIdentityCredentials` object's {logins} property. The following
3636
# code will update the WebIdentityToken, assuming you have retrieved
3737
# an updated token from the identity provider:
3838
#
39-
# AWS.config.credentials.logins['graph.facebook.com'] = updatedToken;
40-
# AWS.config.credentials.refresh! # required only if authentication state has changed
39+
# cognito_credentials.logins['graph.facebook.com'] = updatedToken;
40+
# cognito_credentials.refresh! # required only if authentication state has changed
4141
#
42-
# The CognitoIdentityCredentials also provides a `before_refresh` callback
42+
# The `CognitoIdentityCredentials` also provides a `before_refresh` callback
4343
# that can be used to help manage refreshing identity provider tokens.
4444
# `before_refresh` is called when AWS credentials are required and need
4545
# to be refreshed and it has access to the CognitoIdentityCredentials object.
4646
class CognitoIdentityCredentials
47-
4847
include CredentialProvider
4948
include RefreshingCredentials
5049

@@ -54,8 +53,8 @@ class CognitoIdentityCredentials
5453
# identifier in the format REGION:GUID
5554
#
5655
# @option options [String] :identity_pool_id Required unless identity_id
57-
# is provided. A Amazon Cognito
58-
# Identity Pool ID)in the format REGION:GUID.
56+
# is provided. An Amazon Cognito Identity Pool ID in the
57+
# format REGION:GUID.
5958
#
6059
# @option options [Hash<String,String>] :logins A set of optional
6160
# name-value pairs that map provider names to provider tokens.
@@ -69,16 +68,15 @@ class CognitoIdentityCredentials
6968
# that do not support role customization.
7069
#
7170
# @option options [Callable] before_refresh Proc called before
72-
# credentials are refreshed from Cognito. Useful for updating logins/
73-
# auth tokens. `before_refresh` is called when AWS credentials are
74-
# required and need to be refreshed. Login tokens can be refreshed using
75-
# the following example:
71+
# credentials are refreshed from Cognito. `before_refresh` is called
72+
# when AWS credentials are required and need to be refreshed.
73+
# Login tokens can be refreshed using the following example:
7674
#
7775
# before_refresh = Proc.new do |cognito_credentials| do
7876
# cognito_credentials.logins['graph.facebook.com'] = update_token
7977
# end
8078
#
81-
# @option options [STS::CognitoIdentity] :client Optional CognitoIdentity
79+
# @option options [CognitoIdentity::Client] :client Optional CognitoIdentity
8280
# client. If not provided, a client will be constructed.
8381
def initialize(options = {})
8482
@identity_pool_id = options.delete(:identity_pool_id)
@@ -88,9 +86,9 @@ def initialize(options = {})
8886
@async_refresh = false
8987

9088
client_opts = {}
91-
options.each_pair { |k,v| client_opts[k] = v unless CLIENT_EXCLUDE_OPTIONS.include?(k) }
89+
options.each_pair { |k, v| client_opts[k] = v unless CLIENT_EXCLUDE_OPTIONS.include?(k) }
9290

93-
if !@identity_pool_id && !@identity_id
91+
unless @identity_pool_id || @identity_id
9492
raise ArgumentError,
9593
'Must provide either identity_pool_id or identity_id'
9694
end
@@ -109,19 +107,21 @@ def initialize(options = {})
109107

110108
# @return [String]
111109
def identity_id
112-
@identity_id ||= @client
113-
.get_id(identity_pool_id: @identity_pool_id)
114-
.identity_id
110+
@identity_id ||= @client.get_id(
111+
identity_pool_id: @identity_pool_id,
112+
logins: @logins
113+
).identity_id
115114
end
116115

117116
private
118117

119118
def refresh
120-
@before_refresh.call(self) if @before_refresh
119+
@before_refresh&.call(self)
121120

122121
resp = @client.get_credentials_for_identity(
123122
identity_id: identity_id,
124-
custom_role_arn: @custom_role_arn
123+
custom_role_arn: @custom_role_arn,
124+
logins: @logins
125125
)
126126

127127
@credentials = Credentials.new(
@@ -134,4 +134,3 @@ def refresh
134134
end
135135
end
136136
end
137-

gems/aws-sdk-cognitoidentity/spec/cognito_identity_credentials_spec.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ module CognitoIdentity
2929

3030
let(:identity_id) { 'identity_id' }
3131
let(:identity_pool_id) { 'pool_id' }
32-
32+
let(:logins) do
33+
{ 'login_provider' => 'login_token' }
34+
end
3335
let(:resp) { double('client-resp', credentials: cognito_creds) }
3436

3537
describe '#initialize' do
@@ -88,11 +90,13 @@ module CognitoIdentity
8890

8991
it 'gets identity_id from the identity_pool_id' do
9092
expect(client).to receive(:get_id)
91-
.with(identity_pool_id: identity_pool_id)
93+
.with(identity_pool_id: identity_pool_id, logins: logins)
9294
.and_return(double("getid", identity_id: identity_id))
9395

9496
creds = CognitoIdentityCredentials.new(
95-
client: client, identity_pool_id: identity_pool_id
97+
client: client,
98+
identity_pool_id: identity_pool_id,
99+
logins: logins
96100
)
97101

98102
expect(creds.identity_id).to eq(identity_id)
@@ -102,7 +106,7 @@ module CognitoIdentity
102106
describe '#refresh' do
103107
it 'extracts credentials and expiration from the response' do
104108
expect(client).to receive(:get_credentials_for_identity)
105-
.with(identity_id: identity_id, custom_role_arn: nil)
109+
.with(identity_id: identity_id, custom_role_arn: nil, logins: {})
106110
.and_return(resp)
107111

108112
creds = CognitoIdentityCredentials.new(
@@ -134,8 +138,21 @@ module CognitoIdentity
134138

135139
expect(before_refresh_called).to be(true)
136140
end
137-
end
138141

142+
it 'passes logins to the credentials' do
143+
expect(client).to receive(:get_credentials_for_identity)
144+
.with(identity_id: identity_id, logins: logins, custom_role_arn: nil)
145+
.and_return(resp)
146+
147+
creds = CognitoIdentityCredentials.new(
148+
client: client,
149+
identity_id: identity_id,
150+
logins: logins
151+
)
152+
153+
expect(creds.logins).to eq(logins)
154+
end
155+
end
139156
end
140157
end
141158
end

0 commit comments

Comments
 (0)