Amazon Cognito: Exchange an ID Token for Temporary AWS Credentials
See more Amazon Cognito Examples
Configure an Identity Pool with the User Pool and app client as an authenticated provider, an authenticated IAM role, and the required trust/permissions policies. Supply the separate Identity Pool ID and region in the code. GetId and GetCredentialsForIdentity use the cognito-identity service and the user's ID token in Logins; they are not signed with administrative IAM credentials. An access token is not a substitute for this ID token.
The provider key is exactly cognito-idp.REGION.amazonaws.com/USER_POOL_ID, with no https:// prefix; it is a literal JSON property name. Both requests carry Logins. Persist the returned IdentityId because it can differ after linking, and retain the identity-pool context. The resulting access key, secret key, session token, and expiration describe temporary AWS credentials, not OAuth tokens. Save them separately and renew before expiration using a current user token. Their permissions come from Identity Pool role mapping/IAM policy; they do not automatically grant Cognito administrator access.
Create qa_data/cognito/app.json with string fields region (for example us-west-2), user_pool_id, client_id, and account_key (your local account identifier). OAuth examples also require domain, the trusted HTTPS base URL of your Cognito domain, without a trailing slash. The region and user pool must match the app client. These examples use commercial AWS endpoints.
Sample cache/input files contain secrets. Use an access-controlled directory outside web content and source control, separate each local user/account, and serialize operations that modify the same cache. In a real application obtain passwords and codes through private UI instead of leaving them in files. Cache writes retain a .bak copy; protect and remove obsolete backups and challenge files after the flow. Production storage should provide atomic replacement and OS-backed secret protection. Never restore an old refresh token automatically after rotation. These examples do not require a hosted web server.
Checks transport completion, HTTP status, and required response fields before using results. Cognito often reports throttling, invalid codes, and authentication failures as HTTP 400. Present generic sign-in/recovery errors to users; keep detailed diagnostics private. Retry throttled reads with bounded backoff, but reconcile uncertain writes and refresh rotations before repeating them.
AWS documentation 1 | AWS documentation 2 | AWS documentation 3
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @sTmp1 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
-- App settings are trusted configuration. Keep a separate account_key/cache per local user.
DECLARE @app int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @app OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @app, 'Strict', 1
EXEC sp_OAMethod @app, 'LoadFile', @success OUT, 'qa_data/cognito/app.json'
IF @success <> 1
BEGIN
PRINT 'Could not load Cognito app settings.'
EXEC @hr = sp_OADestroy @app
RETURN
END
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'region'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid region.'
EXEC @hr = sp_OADestroy @app
RETURN
END
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid user_pool_id.'
EXEC @hr = sp_OADestroy @app
RETURN
END
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'client_id'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid client_id.'
EXEC @hr = sp_OADestroy @app
RETURN
END
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'account_key'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'account_key'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid account_key.'
EXEC @hr = sp_OADestroy @app
RETURN
END
DECLARE @clientId nvarchar(4000)
EXEC sp_OAMethod @app, 'StringOf', @clientId OUT, 'client_id'
DECLARE @endpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://cognito-idp.'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com/'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
EXEC sp_OASetProperty @http, 'ReadTimeout', 60
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @body int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
DECLARE @cached int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @cached OUT
EXEC sp_OASetProperty @cached, 'Strict', 1
EXEC sp_OAMethod @cached, 'LoadFile', @success OUT, 'qa_data/tokens/cognito-user.json'
IF @success <> 1
BEGIN
PRINT 'Could not load qa_data/tokens/cognito-user.json.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'client_id'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid client_id.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'client_id'
EXEC sp_OAMethod @app, 'StringOf', @sTmp1 OUT, 'client_id'
IF @sTmp0 <> @sTmp1
BEGIN
PRINT 'Saved data belongs to another client, pool, or local account.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'user_pool_id'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid user_pool_id.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @app, 'StringOf', @sTmp1 OUT, 'user_pool_id'
IF @sTmp0 <> @sTmp1
BEGIN
PRINT 'Saved data belongs to another client, pool, or local account.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'account_key'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'account_key'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid account_key.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'account_key'
EXEC sp_OAMethod @app, 'StringOf', @sTmp1 OUT, 'account_key'
IF @sTmp0 <> @sTmp1
BEGIN
PRINT 'Saved data belongs to another client, pool, or local account.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'id_token'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'id_token'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid id_token.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
RETURN
END
-- Identity Pool region may differ from the User Pool region.
DECLARE @identityRegion nvarchar(4000)
SELECT @identityRegion = 'us-west-2'
DECLARE @identityPoolId nvarchar(4000)
SELECT @identityPoolId = 'us-west-2:11111111-2222-3333-4444-555555555555'
DECLARE @identityEndpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @identityEndpoint OUT
EXEC sp_OAMethod @identityEndpoint, 'Append', @success OUT, 'https://cognito-identity.'
EXEC sp_OAMethod @identityEndpoint, 'Append', @success OUT, @identityRegion
EXEC sp_OAMethod @identityEndpoint, 'Append', @success OUT, '.amazonaws.com/'
DECLARE @provider int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @provider OUT
EXEC sp_OAMethod @provider, 'Append', @success OUT, 'cognito-idp.'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
EXEC sp_OAMethod @provider, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @provider, 'Append', @success OUT, '.amazonaws.com/'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @provider, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'IdentityPoolId', @identityPoolId
EXEC sp_OAMethod @body, 'UpdateNewObject', @success OUT, 'Logins'
DECLARE @logins int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @logins OUT
EXEC sp_OAMethod @body, 'ObjectOf2', @success OUT, 'Logins', @logins
IF @success <> 1
BEGIN
PRINT 'Could not create the Logins map.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
-- AppendString takes a literal property name, preserving periods and the slash.
EXEC sp_OAMethod @provider, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @cached, 'StringOf', @sTmp1 OUT, 'id_token'
EXEC sp_OAMethod @logins, 'AppendString', @success OUT, @sTmp0, @sTmp1
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityService.GetId'
EXEC sp_OAMethod @identityEndpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.1', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'Error type: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
PRINT 'OAuth error: ' + @sTmp0
END
-- Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP exchange did not complete.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a JSON response.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'IdentityId'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'IdentityId'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid IdentityId.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
DECLARE @identityId nvarchar(4000)
EXEC sp_OAMethod @result, 'StringOf', @identityId OUT, 'IdentityId'
EXEC sp_OAMethod @body, 'Delete', @success OUT, 'IdentityPoolId'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'IdentityId', @identityId
-- Include the same Logins map again; do not accidentally request unauthenticated access.
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityService.GetCredentialsForIdentity'
EXEC sp_OAMethod @identityEndpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.1', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
PRINT 'Error type: ' + @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
PRINT 'OAuth error: ' + @sTmp0
END
-- Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
IF @success <> 1
BEGIN
PRINT 'The HTTP exchange did not complete.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a JSON response.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'IdentityId'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'IdentityId'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid IdentityId.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Credentials.AccessKeyId'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Credentials.AccessKeyId'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid Credentials.AccessKeyId.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Credentials.SecretKey'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Credentials.SecretKey'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid Credentials.SecretKey.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Credentials.SessionToken'
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Credentials.SessionToken'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid Credentials.SessionToken.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Credentials.Expiration'
IF @iTmp0 <> 2
BEGIN
PRINT 'Missing credential expiration.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
RETURN
END
DECLARE @temporary int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @temporary OUT
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Credentials.AccessKeyId'
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'access_key', @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Credentials.SecretKey'
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'secret_key', @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Credentials.SessionToken'
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'session_token', @sTmp0
-- Expiration is a Unix timestamp; preserve its decimal text rather than using int64.
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Credentials.Expiration'
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'expiration_unix', @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'IdentityId'
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'identity_id', @sTmp0
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'identity_pool_id', @identityPoolId
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'client_id'
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'client_id', @sTmp0
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'user_pool_id', @sTmp0
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'account_key'
EXEC sp_OAMethod @temporary, 'UpdateString', @success OUT, 'account_key', @sTmp0
DECLARE @temporarytemporaryAWScredentials int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @temporarytemporaryAWScredentials OUT
EXEC sp_OAMethod @temporarytemporaryAWScredentials, 'DirEnsureExists', @success OUT, 'qa_data/tokens'
IF @success <> 1
BEGIN
PRINT 'Could not create the private cache directory.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
EXEC @hr = sp_OADestroy @temporary
EXEC @hr = sp_OADestroy @temporarytemporaryAWScredentials
RETURN
END
EXEC sp_OAMethod @temporarytemporaryAWScredentials, 'FileExists', @iTmp0 OUT, 'qa_data/tokens/cognito-identity.json'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @temporarytemporaryAWScredentials, 'FileCopy', @success OUT, 'qa_data/tokens/cognito-identity.json', 'qa_data/tokens/cognito-identity.json.bak', 0
IF @success <> 1
BEGIN
PRINT 'Could not back up the existing cache.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
EXEC @hr = sp_OADestroy @temporary
EXEC @hr = sp_OADestroy @temporarytemporaryAWScredentials
RETURN
END
END
EXEC sp_OAMethod @temporary, 'WriteFile', @success OUT, 'qa_data/tokens/cognito-identity.json'
IF @success <> 1
BEGIN
PRINT 'Could not save temporary AWS credentials; preserve any previous backup.'
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
EXEC @hr = sp_OADestroy @temporary
EXEC @hr = sp_OADestroy @temporarytemporaryAWScredentials
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'IdentityId'
PRINT 'Identity ID: ' + @sTmp0
PRINT 'Temporary AWS credentials saved privately.'
-- Renew by repeating the exchange with a current ID token before Expiration.
-- Use these credentials only for actions allowed by the selected Identity Pool IAM role.
EXEC @hr = sp_OADestroy @app
EXEC @hr = sp_OADestroy @endpoint
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @body
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @cached
EXEC @hr = sp_OADestroy @identityEndpoint
EXEC @hr = sp_OADestroy @provider
EXEC @hr = sp_OADestroy @logins
EXEC @hr = sp_OADestroy @temporary
EXEC @hr = sp_OADestroy @temporarytemporaryAWScredentials
END
GO