Amazon Cognito: Enroll an Authenticator for TOTP MFA
See more Amazon Cognito Examples
Enable software-token MFA in the pool first. Choose mode=signed_in to enroll with a user access token containing aws.cognito.signin.user.admin, or mode=challenge to continue the MFA_SETUP saved by the sign-in example. Begin obtains a secret and saves it in private cognito-totp.json. Present secret_code securely to the user for manual entry or an otpauth QR code; never place it in ordinary logs. Then set action=verify with the authenticator's code in private totp-input.json.
Checks VerifySoftwareToken.Status=SUCCESS before activating the preference or finishing sign-in. A signed-in enrollment sets SoftwareTokenMfaSettings.Enabled and PreferredMfa. Challenge enrollment uses the successive Session values from AssociateSoftwareToken and VerifySoftwareToken, then RespondToAuthChallenge(MFA_SETUP). Verifying a replacement authenticator can disassociate the old one. If a later step fails after successful verification, inspect the user's MFA/sign-in state rather than repeatedly beginning enrollment.
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 | AWS documentation 3
Chilkat Delphi ActiveX Downloads
var
success: Integer;
app: TChilkatJsonObject;
clientId: WideString;
endpoint: TChilkatStringBuilder;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
body: TChilkatJsonObject;
result: TChilkatJsonObject;
action: WideString;
mode: WideString;
username: WideString;
credential: TChilkatJsonObject;
newEnrollment: TChilkatJsonObject;
newEnrollmentTOTPenrollment: TCkFileAccess;
enrollment: TChilkatJsonObject;
input: TChilkatJsonObject;
removeEnrollment: TCkFileAccess;
token: TChilkatJsonObject;
obtained: TCkDateTime;
tokenusertokens: TCkFileAccess;
cleanup: TCkFileAccess;
resultauthenticationchallenge: TCkFileAccess;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := 0;
// App settings are trusted configuration. Keep a separate account_key/cache per local user.
app := TChilkatJsonObject.Create(Self);
app.Strict := 1;
success := app.LoadFile('qa_data/cognito/app.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load Cognito app settings.');
Exit;
end;
if ((app.JsonTypeOf('region') <> 1) or (app.StringOf('region') = '')) then
begin
Memo1.Lines.Add('Missing or invalid region.');
Exit;
end;
if ((app.JsonTypeOf('user_pool_id') <> 1) or (app.StringOf('user_pool_id') = '')) then
begin
Memo1.Lines.Add('Missing or invalid user_pool_id.');
Exit;
end;
if ((app.JsonTypeOf('client_id') <> 1) or (app.StringOf('client_id') = '')) then
begin
Memo1.Lines.Add('Missing or invalid client_id.');
Exit;
end;
if ((app.JsonTypeOf('account_key') <> 1) or (app.StringOf('account_key') = '')) then
begin
Memo1.Lines.Add('Missing or invalid account_key.');
Exit;
end;
clientId := app.StringOf('client_id');
endpoint := TChilkatStringBuilder.Create(Self);
endpoint.Append('https://cognito-idp.');
endpoint.Append(app.StringOf('region'));
endpoint.Append('.amazonaws.com/');
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.FollowRedirects := 0;
http.ConnectTimeout := 20;
http.ReadTimeout := 60;
resp := TChilkatHttpResponse.Create(Self);
body := TChilkatJsonObject.Create(Self);
result := TChilkatJsonObject.Create(Self);
result.Strict := 1;
action := 'begin';
mode := 'signed_in';
if ((action <> 'begin') and (action <> 'verify')) then
begin
Memo1.Lines.Add('Choose begin or verify.');
Exit;
end;
if ((mode <> 'signed_in') and (mode <> 'challenge')) then
begin
Memo1.Lines.Add('Choose signed_in or challenge.');
Exit;
end;
username := '';
credential := TChilkatJsonObject.Create(Self);
if (mode = 'signed_in') then
begin
success := credential.LoadFile('qa_data/tokens/cognito-user.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load user tokens.');
Exit;
end;
if ((credential.JsonTypeOf('client_id') <> 1) or (credential.StringOf('client_id') = '')) then
begin
Memo1.Lines.Add('Missing or invalid client_id.');
Exit;
end;
if (credential.StringOf('client_id') <> app.StringOf('client_id')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if ((credential.JsonTypeOf('user_pool_id') <> 1) or (credential.StringOf('user_pool_id') = '')) then
begin
Memo1.Lines.Add('Missing or invalid user_pool_id.');
Exit;
end;
if (credential.StringOf('user_pool_id') <> app.StringOf('user_pool_id')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if ((credential.JsonTypeOf('account_key') <> 1) or (credential.StringOf('account_key') = '')) then
begin
Memo1.Lines.Add('Missing or invalid account_key.');
Exit;
end;
if (credential.StringOf('account_key') <> app.StringOf('account_key')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if ((credential.JsonTypeOf('access_token') <> 1) or (credential.StringOf('access_token') = '')) then
begin
Memo1.Lines.Add('Missing or invalid access_token.');
Exit;
end;
end
else
begin
success := credential.LoadFile('qa_data/tokens/cognito-challenge.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load the MFA_SETUP challenge.');
Exit;
end;
if ((credential.JsonTypeOf('client_id') <> 1) or (credential.StringOf('client_id') = '')) then
begin
Memo1.Lines.Add('Missing or invalid client_id.');
Exit;
end;
if (credential.StringOf('client_id') <> app.StringOf('client_id')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if ((credential.JsonTypeOf('user_pool_id') <> 1) or (credential.StringOf('user_pool_id') = '')) then
begin
Memo1.Lines.Add('Missing or invalid user_pool_id.');
Exit;
end;
if (credential.StringOf('user_pool_id') <> app.StringOf('user_pool_id')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if ((credential.JsonTypeOf('account_key') <> 1) or (credential.StringOf('account_key') = '')) then
begin
Memo1.Lines.Add('Missing or invalid account_key.');
Exit;
end;
if (credential.StringOf('account_key') <> app.StringOf('account_key')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if ((credential.JsonTypeOf('Session') <> 1) or (credential.StringOf('Session') = '')) then
begin
Memo1.Lines.Add('Missing or invalid Session.');
Exit;
end;
if ((credential.JsonTypeOf('username') <> 1) or (credential.StringOf('username') = '')) then
begin
Memo1.Lines.Add('Missing or invalid username.');
Exit;
end;
if (credential.StringOf('ChallengeName') <> 'MFA_SETUP') then
begin
Memo1.Lines.Add('Expected an MFA_SETUP challenge.');
Exit;
end;
username := credential.StringOf('username');
end;
if (action = 'begin') then
begin
if (mode = 'signed_in') then
begin
body.UpdateString('AccessToken',credential.StringOf('access_token'));
end
else
begin
body.UpdateString('Session',credential.StringOf('Session'));
end;
http.SetRequestHeader('X-Amz-Target','AWSCognitoIdentityProviderService.AssociateSoftwareToken');
success := http.HttpJson('POST',endpoint.GetAsString(),body.ControlInterface,'application/x-amz-json-1.0',resp.ControlInterface);
if (resp.StatusCode <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(resp.StatusCode));
success := result.Load(resp.BodyStr);
if (success = 1) then
begin
Memo1.Lines.Add('Error type: ' + result.StringOf('__type'));
Memo1.Lines.Add('OAuth error: ' + result.StringOf('error'));
end;
// Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
Exit;
end;
if (success <> 1) then
begin
Memo1.Lines.Add('The HTTP exchange did not complete.');
Exit;
end;
success := result.Load(resp.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected a JSON response.');
Exit;
end;
if ((result.JsonTypeOf('SecretCode') <> 1) or (result.StringOf('SecretCode') = '')) then
begin
Memo1.Lines.Add('Missing or invalid SecretCode.');
Exit;
end;
newEnrollment := TChilkatJsonObject.Create(Self);
newEnrollment.UpdateString('secret_code',result.StringOf('SecretCode'));
newEnrollment.UpdateString('mode',mode);
newEnrollment.UpdateString('username',username);
if (mode = 'challenge') then
begin
if ((result.JsonTypeOf('Session') <> 1) or (result.StringOf('Session') = '')) then
begin
Memo1.Lines.Add('Missing or invalid Session.');
Exit;
end;
newEnrollment.UpdateString('session',result.StringOf('Session'));
// Bind enrollment to this precise challenge, not just the account.
newEnrollment.UpdateString('original_session',credential.StringOf('Session'));
end;
newEnrollment.UpdateString('client_id',app.StringOf('client_id'));
newEnrollment.UpdateString('user_pool_id',app.StringOf('user_pool_id'));
newEnrollment.UpdateString('account_key',app.StringOf('account_key'));
newEnrollmentTOTPenrollment := TCkFileAccess.Create(Self);
success := newEnrollmentTOTPenrollment.DirEnsureExists('qa_data/tokens');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not create the private cache directory.');
Exit;
end;
if (newEnrollmentTOTPenrollment.FileExists('qa_data/tokens/cognito-totp.json') = 1) then
begin
success := newEnrollmentTOTPenrollment.FileCopy('qa_data/tokens/cognito-totp.json','qa_data/tokens/cognito-totp.json.bak',0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not back up the existing cache.');
Exit;
end;
end;
success := newEnrollment.WriteFile('qa_data/tokens/cognito-totp.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not save TOTP enrollment; preserve any previous backup.');
Exit;
end;
Memo1.Lines.Add('Enrollment saved. Present secret_code privately in your authenticator setup UI.');
// This secret can generate future OTPs. Never log it or email it.
Exit;
end;
enrollment := TChilkatJsonObject.Create(Self);
enrollment.Strict := 1;
success := enrollment.LoadFile('qa_data/tokens/cognito-totp.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load qa_data/tokens/cognito-totp.json.');
Exit;
end;
if ((enrollment.JsonTypeOf('client_id') <> 1) or (enrollment.StringOf('client_id') = '')) then
begin
Memo1.Lines.Add('Missing or invalid client_id.');
Exit;
end;
if (enrollment.StringOf('client_id') <> app.StringOf('client_id')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if ((enrollment.JsonTypeOf('user_pool_id') <> 1) or (enrollment.StringOf('user_pool_id') = '')) then
begin
Memo1.Lines.Add('Missing or invalid user_pool_id.');
Exit;
end;
if (enrollment.StringOf('user_pool_id') <> app.StringOf('user_pool_id')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if ((enrollment.JsonTypeOf('account_key') <> 1) or (enrollment.StringOf('account_key') = '')) then
begin
Memo1.Lines.Add('Missing or invalid account_key.');
Exit;
end;
if (enrollment.StringOf('account_key') <> app.StringOf('account_key')) then
begin
Memo1.Lines.Add('Saved data belongs to another client, pool, or local account.');
Exit;
end;
if (enrollment.StringOf('mode') <> mode) then
begin
Memo1.Lines.Add('Enrollment mode mismatch.');
Exit;
end;
input := TChilkatJsonObject.Create(Self);
input.Strict := 1;
success := input.LoadFile('qa_data/cognito/totp-input.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load qa_data/cognito/totp-input.json.');
Exit;
end;
if ((input.JsonTypeOf('code') <> 1) or (input.StringOf('code') = '')) then
begin
Memo1.Lines.Add('Missing or invalid code.');
Exit;
end;
// Enter a fresh six-digit code from the authenticator; preserve leading zeroes.
body.UpdateString('UserCode',input.StringOf('code'));
body.UpdateString('FriendlyDeviceName','Authenticator');
if (mode = 'signed_in') then
begin
body.UpdateString('AccessToken',credential.StringOf('access_token'));
end
else
begin
if ((enrollment.JsonTypeOf('session') <> 1) or (enrollment.StringOf('session') = '')) then
begin
Memo1.Lines.Add('Missing or invalid session.');
Exit;
end;
if (enrollment.StringOf('original_session') <> credential.StringOf('Session')) then
begin
Memo1.Lines.Add('Enrollment belongs to another sign-in challenge.');
Exit;
end;
body.UpdateString('Session',enrollment.StringOf('session'));
end;
http.SetRequestHeader('X-Amz-Target','AWSCognitoIdentityProviderService.VerifySoftwareToken');
success := http.HttpJson('POST',endpoint.GetAsString(),body.ControlInterface,'application/x-amz-json-1.0',resp.ControlInterface);
if (resp.StatusCode <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(resp.StatusCode));
success := result.Load(resp.BodyStr);
if (success = 1) then
begin
Memo1.Lines.Add('Error type: ' + result.StringOf('__type'));
Memo1.Lines.Add('OAuth error: ' + result.StringOf('error'));
end;
// Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
Exit;
end;
if (success <> 1) then
begin
Memo1.Lines.Add('The HTTP exchange did not complete.');
Exit;
end;
success := result.Load(resp.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected a JSON response.');
Exit;
end;
if (result.StringOf('Status') <> 'SUCCESS') then
begin
Memo1.Lines.Add('Authenticator verification did not succeed; do not enable it.');
Exit;
end;
// Verification succeeded. Discard the enrollment secret and its backup.
removeEnrollment := TCkFileAccess.Create(Self);
removeEnrollment.FileDelete('qa_data/tokens/cognito-totp.json');
if (removeEnrollment.FileExists('qa_data/tokens/cognito-totp.json.bak') = 1) then
begin
removeEnrollment.FileDelete('qa_data/tokens/cognito-totp.json.bak');
end;
success := body.Load('{}');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not reset the request body.');
Exit;
end;
if (mode = 'signed_in') then
begin
body.UpdateString('AccessToken',credential.StringOf('access_token'));
body.UpdateBool('SoftwareTokenMfaSettings.Enabled',1);
body.UpdateBool('SoftwareTokenMfaSettings.PreferredMfa',1);
http.SetRequestHeader('X-Amz-Target','AWSCognitoIdentityProviderService.SetUserMFAPreference');
success := http.HttpJson('POST',endpoint.GetAsString(),body.ControlInterface,'application/x-amz-json-1.0',resp.ControlInterface);
if (resp.StatusCode <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(resp.StatusCode));
success := result.Load(resp.BodyStr);
if (success = 1) then
begin
Memo1.Lines.Add('Error type: ' + result.StringOf('__type'));
Memo1.Lines.Add('OAuth error: ' + result.StringOf('error'));
end;
// Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
Exit;
end;
if (success <> 1) then
begin
Memo1.Lines.Add('The HTTP exchange did not complete.');
Exit;
end;
Memo1.Lines.Add('Authenticator verified and set as the preferred MFA method.');
Exit;
end;
if ((result.JsonTypeOf('Session') <> 1) or (result.StringOf('Session') = '')) then
begin
Memo1.Lines.Add('Missing or invalid Session.');
Exit;
end;
// Submit the NEW Session returned by VerifySoftwareToken, not the earlier one.
body.UpdateString('ClientId',clientId);
body.UpdateString('ChallengeName','MFA_SETUP');
body.UpdateString('Session',result.StringOf('Session'));
body.UpdateString('ChallengeResponses.USERNAME',username);
http.SetRequestHeader('X-Amz-Target','AWSCognitoIdentityProviderService.RespondToAuthChallenge');
success := http.HttpJson('POST',endpoint.GetAsString(),body.ControlInterface,'application/x-amz-json-1.0',resp.ControlInterface);
if (resp.StatusCode <> 200) then
begin
Memo1.Lines.Add('HTTP status: ' + IntToStr(resp.StatusCode));
success := result.Load(resp.BodyStr);
if (success = 1) then
begin
Memo1.Lines.Add('Error type: ' + result.StringOf('__type'));
Memo1.Lines.Add('OAuth error: ' + result.StringOf('error'));
end;
// Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
Exit;
end;
if (success <> 1) then
begin
Memo1.Lines.Add('The HTTP exchange did not complete.');
Exit;
end;
success := result.Load(resp.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Expected a JSON response.');
Exit;
end;
if (result.JsonTypeOf('AuthenticationResult') = 3) then
begin
token := TChilkatJsonObject.Create(Self);
token.UpdateString('access_token',result.StringOf('AuthenticationResult.AccessToken'));
token.UpdateString('id_token',result.StringOf('AuthenticationResult.IdToken'));
token.UpdateString('token_type',result.StringOf('AuthenticationResult.TokenType'));
token.UpdateInt('expires_in',result.IntOf('AuthenticationResult.ExpiresIn'));
if (result.HasMember('AuthenticationResult.RefreshToken') = 1) then
begin
token.UpdateString('refresh_token',result.StringOf('AuthenticationResult.RefreshToken'));
end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOf('access_token') = '')) then
begin
Memo1.Lines.Add('Missing or invalid access_token.');
Exit;
end;
if ((token.JsonTypeOf('id_token') <> 1) or (token.StringOf('id_token') = '')) then
begin
Memo1.Lines.Add('Missing or invalid id_token.');
Exit;
end;
if ((token.JsonTypeOf('expires_in') <> 2) or (token.IntOf('expires_in') <= 0)) then
begin
Memo1.Lines.Add('Expected a positive token lifetime.');
Exit;
end;
if (token.StringOf('token_type') <> 'Bearer') then
begin
Memo1.Lines.Add('Expected Bearer tokens.');
Exit;
end;
if ((token.JsonTypeOf('refresh_token') <> 1) or (token.StringOf('refresh_token') = '')) then
begin
Memo1.Lines.Add('Missing or invalid refresh_token.');
Exit;
end;
token.UpdateString('client_id',app.StringOf('client_id'));
token.UpdateString('user_pool_id',app.StringOf('user_pool_id'));
token.UpdateString('account_key',app.StringOf('account_key'));
obtained := TCkDateTime.Create(Self);
success := obtained.SetFromCurrentSystemTime();
if (success <> 1) then
begin
Memo1.Lines.Add('Could not get the acquisition time.');
Exit;
end;
token.UpdateString('obtained_at',obtained.GetAsTimestamp(0));
tokenusertokens := TCkFileAccess.Create(Self);
success := tokenusertokens.DirEnsureExists('qa_data/tokens');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not create the private cache directory.');
Exit;
end;
if (tokenusertokens.FileExists('qa_data/tokens/cognito-user.json') = 1) then
begin
success := tokenusertokens.FileCopy('qa_data/tokens/cognito-user.json','qa_data/tokens/cognito-user.json.bak',0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not back up the existing cache.');
Exit;
end;
end;
success := token.WriteFile('qa_data/tokens/cognito-user.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not save user tokens; preserve any previous backup.');
Exit;
end;
// A completed flow no longer needs its short-lived challenge session.
cleanup := TCkFileAccess.Create(Self);
if (cleanup.FileExists('qa_data/tokens/cognito-challenge.json') = 1) then
begin
cleanup.FileDelete('qa_data/tokens/cognito-challenge.json');
end;
Memo1.Lines.Add('Sign-in completed; private token cache saved.');
Exit;
end;
if ((result.JsonTypeOf('ChallengeName') <> 1) or (result.StringOf('ChallengeName') = '')) then
begin
Memo1.Lines.Add('Missing or invalid ChallengeName.');
Exit;
end;
if ((result.JsonTypeOf('Session') <> 1) or (result.StringOf('Session') = '')) then
begin
Memo1.Lines.Add('Missing or invalid Session.');
Exit;
end;
// ChallengeParameters may provide the canonical username after alias-based sign-in.
if ((result.JsonTypeOf('ChallengeParameters.USER_ID_FOR_SRP') = 1) and (result.StringOf('ChallengeParameters.USER_ID_FOR_SRP') <> '')) then
begin
username := result.StringOf('ChallengeParameters.USER_ID_FOR_SRP');
end;
if ((result.JsonTypeOf('ChallengeParameters.USERNAME') = 1) and (result.StringOf('ChallengeParameters.USERNAME') <> '')) then
begin
username := result.StringOf('ChallengeParameters.USERNAME');
end;
result.UpdateString('username',username);
result.UpdateString('client_id',app.StringOf('client_id'));
result.UpdateString('user_pool_id',app.StringOf('user_pool_id'));
result.UpdateString('account_key',app.StringOf('account_key'));
resultauthenticationchallenge := TCkFileAccess.Create(Self);
success := resultauthenticationchallenge.DirEnsureExists('qa_data/tokens');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not create the private cache directory.');
Exit;
end;
if (resultauthenticationchallenge.FileExists('qa_data/tokens/cognito-challenge.json') = 1) then
begin
success := resultauthenticationchallenge.FileCopy('qa_data/tokens/cognito-challenge.json','qa_data/tokens/cognito-challenge.json.bak',0);
if (success <> 1) then
begin
Memo1.Lines.Add('Could not back up the existing cache.');
Exit;
end;
end;
success := result.WriteFile('qa_data/tokens/cognito-challenge.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not save authentication challenge; preserve any previous backup.');
Exit;
end;
Memo1.Lines.Add('Further authentication required: ' + result.StringOf('ChallengeName'));
// Collect the requested input in your application's UI; never print the Session value.