Amazon Cognito: Refresh Tokens with Rotation Support
See more Amazon Cognito Examples
Renew a user session without asking for the password again. Choose the default GetTokensFromRefreshToken JSON API or the OAuth refresh_token grant. Both support refresh-token rotation. REFRESH_TOKEN_AUTH is incompatible with rotation and is intentionally not used. With rotation disabled, retain the old refresh token when the response omits a replacement; with rotation enabled, save the new one before another refresh.
Use a public client without a secret, matching the initial sign-in. Managed-login sessions can use the OAuth endpoint. API-origin sessions can use OAuth refresh only when device remembering is disabled; otherwise use the API with the confirmed DeviceKey. This collection's password sign-in example assumes device remembering is disabled. Coordinate simultaneous refreshes and handle the configured retry grace period; expiration or revocation requires a new sign-in. Rotating does not extend the original refresh-token lifetime.
Uses direct Chilkat.Http requests to the regional Cognito User Pools JSON API. Public app-client operations do not use IAM signing. These examples use a public app client without a client secret; do not add administrative AWS credentials. For a confidential app client, implement the operation-specific SecretHash/SECRET_HASH or ClientSecret requirements on a trusted system.
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
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 @iTmp1 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
-- "api" supports API sign-in and refresh rotation. "oauth" uses the Cognito domain.
DECLARE @method nvarchar(4000)
SELECT @method = 'api'
IF (@method <> 'api') and (@method <> 'oauth')
BEGIN
PRINT 'Choose api or oauth.'
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
RETURN
END
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, 'refresh_token'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'refresh_token'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid refresh_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
IF @method = 'api'
BEGIN
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'ClientId', @clientId
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'RefreshToken', @sTmp0
-- If device remembering is enabled, supply the confirmed DeviceKey from your device flow.
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'device_key'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'device_key'
IF (@iTmp0 = 1) and (@sTmp0 <> '')
BEGIN
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'device_key'
EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'DeviceKey', @sTmp0
END
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.GetTokensFromRefreshToken'
EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @body, 'application/x-amz-json-1.0', @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
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
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
RETURN
END
END
ELSE
BEGIN
EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'domain'
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid domain.'
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
-- This URL comes from trusted configuration, never from a token or user-supplied URL.
DECLARE @tokenEndpoint int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @tokenEndpoint OUT
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, '/oauth2/token'
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'grant_type', 'refresh_token'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'client_id', @clientId
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'refresh_token', @sTmp0
EXEC sp_OAMethod @tokenEndpoint, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @sTmp0, @req, @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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
RETURN
END
END
DECLARE @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
IF @method = 'api'
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'AuthenticationResult.AccessToken'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'access_token', @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'AuthenticationResult.IdToken'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'id_token', @sTmp0
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'AuthenticationResult.TokenType'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'token_type', @sTmp0
EXEC sp_OAMethod @result, 'IntOf', @iTmp0 OUT, 'AuthenticationResult.ExpiresIn'
EXEC sp_OAMethod @token, 'UpdateInt', @success OUT, 'expires_in', @iTmp0
EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'AuthenticationResult.RefreshToken'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'AuthenticationResult.RefreshToken'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'refresh_token', @sTmp0
END
END
ELSE
BEGIN
EXEC sp_OAMethod @result, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @token, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not copy the token 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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
RETURN
END
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid access_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
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'id_token'
EXEC sp_OAMethod @token, '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
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'expires_in'
EXEC sp_OAMethod @token, 'IntOf', @iTmp1 OUT, 'expires_in'
IF (@iTmp0 <> 2) or (@iTmp1 <= 0)
BEGIN
PRINT 'Expected a positive token lifetime.'
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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'token_type'
IF @sTmp0 <> 'Bearer'
BEGIN
PRINT 'Expected Bearer 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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
RETURN
END
-- Rotation enabled: save the replacement. Rotation disabled: retain the old refresh token.
EXEC sp_OAMethod @token, 'HasMember', @iTmp0 OUT, 'refresh_token'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'refresh_token'
IF (@iTmp0 <> 1) or (@sTmp0 = '')
BEGIN
PRINT 'Missing or invalid refresh_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
EXEC @hr = sp_OADestroy @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
RETURN
END
END
ELSE
BEGIN
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'refresh_token', @sTmp0
END
EXEC sp_OAMethod @cached, 'JsonTypeOf', @iTmp0 OUT, 'device_key'
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'device_key'
IF (@iTmp0 = 1) and (@sTmp0 <> '')
BEGIN
EXEC sp_OAMethod @cached, 'StringOf', @sTmp0 OUT, 'device_key'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'device_key', @sTmp0
END
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'client_id'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'client_id', @sTmp0
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'user_pool_id', @sTmp0
EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'account_key'
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'account_key', @sTmp0
DECLARE @obtained int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @obtained OUT
EXEC sp_OAMethod @obtained, 'SetFromCurrentSystemTime', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not get the acquisition time.'
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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
RETURN
END
EXEC sp_OAMethod @obtained, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'obtained_at', @sTmp0
DECLARE @tokenrefreshedusertokens int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @tokenrefreshedusertokens OUT
EXEC sp_OAMethod @tokenrefreshedusertokens, '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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
EXEC @hr = sp_OADestroy @tokenrefreshedusertokens
RETURN
END
EXEC sp_OAMethod @tokenrefreshedusertokens, 'FileExists', @iTmp0 OUT, 'qa_data/tokens/cognito-user.json'
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @tokenrefreshedusertokens, 'FileCopy', @success OUT, 'qa_data/tokens/cognito-user.json', 'qa_data/tokens/cognito-user.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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
EXEC @hr = sp_OADestroy @tokenrefreshedusertokens
RETURN
END
END
EXEC sp_OAMethod @token, 'WriteFile', @success OUT, 'qa_data/tokens/cognito-user.json'
IF @success <> 1
BEGIN
PRINT 'Could not save refreshed user tokens; 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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
EXEC @hr = sp_OADestroy @tokenrefreshedusertokens
RETURN
END
PRINT 'Tokens refreshed; private cache updated.'
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 @tokenEndpoint
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
EXEC @hr = sp_OADestroy @tokenrefreshedusertokens
END
GO