Skip to content

Commit f4b0d8a

Browse files
ashishdhingradscpinheiro
authored andcommitted
Changed the default value of SSOAWSCredentialsOptions.SupportsGettingNewToken as false and improved error messaging if required SSO options are missing while generating new credentials.
1 parent b30d4b9 commit f4b0d8a

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"core": {
3+
"changeLogMessages": [
4+
"Changed the default value of SSOAWSCredentialsOptions.SupportsGettingNewToken as false and improved error messaging if required SSO options are missing while generating new credentials."
5+
],
6+
"type": "patch",
7+
"updateMinimum": true
8+
}
9+
}

sdk/src/Core/Amazon.Runtime/Credentials/Internal/_bcl+netstandard/SSOTokenManager.cs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Collections.Concurrent;
18+
using System.Collections.Generic;
1819
using System.Net;
1920
using System.Threading;
2021
using System.Threading.Tasks;
@@ -220,7 +221,8 @@ public SsoToken GetToken(SSOTokenManagerGetTokenOptions options)
220221
}
221222
catch (Exception ex)
222223
{
223-
_logger.Error(ex, $"Refreshing SSOToken for [{options.StartUrl}] failed: {ex.Message}");
224+
// Exception message from SSOIDC client has text along with HTTP Body as JSON string.
225+
_logger.Error(ex, $"Refreshing SSOToken for [{options.StartUrl}] failed: {ex.Message.Replace("{", "{{").Replace("}", "}}")}");
224226
//if refreshing the token failed that means the refresh token was expired.
225227
//if the refresh token is expired and access token is expired and if the user specifies a callback with
226228
//option.SupportsGettingNewToken is true then we will generate a new token.
@@ -484,7 +486,8 @@ public async Task<SsoToken> GetTokenAsync(SSOTokenManagerGetTokenOptions options
484486
}
485487
catch (Exception ex)
486488
{
487-
_logger.Error(ex, $"Refreshing SSOToken for [{options.Session}] failed: {ex.Message}");
489+
// Exception message from SSOIDC client has text along with HTTP Body as JSON string.
490+
_logger.Error(ex, $"Refreshing SSOToken for [{options.Session}] failed: {ex.Message.Replace("{", "{{").Replace("}", "}}")}");
488491
if (ssoToken.IsExpired() && options.SupportsGettingNewToken)
489492
{
490493
return await GenerateNewTokenAsync(options, cancellationToken).ConfigureAwait(false);
@@ -612,24 +615,11 @@ public async Task LogoutAsync(SSOTokenManagerGetTokenOptions options, Cancellati
612615

613616
private async Task<SsoToken> GenerateNewTokenAsync(SSOTokenManagerGetTokenOptions options, CancellationToken cancellationToken = default)
614617
{
615-
if (string.IsNullOrEmpty(options.ClientName))
616-
{
617-
throw new ArgumentNullException($"Options property cannot be empty: {nameof(options.ClientName)}");
618-
}
618+
var emptyProperties = GetEmptySSOTokenOptions(options);
619619

620-
if (options.PkceFlowOptions == null)
621-
{
622-
if (options.SsoVerificationCallback == null)
623-
{
624-
throw new ArgumentNullException($"Options property cannot be empty: {nameof(options.SsoVerificationCallback)}");
625-
}
626-
}
627-
else
620+
if (emptyProperties.Count > 0)
628621
{
629-
if (options.PkceFlowOptions.RetrieveAuthorizationCodeCallbackAsync == null)
630-
{
631-
throw new ArgumentNullException($"Options property cannot be empty: {nameof(options.PkceFlowOptions.RetrieveAuthorizationCodeCallbackAsync)}");
632-
}
622+
throw new AmazonClientException($"Error generating new SSO token. Options properties cannot be empty: {string.Join(", ", emptyProperties)}");
633623
}
634624

635625
var request = new GetSsoTokenRequest
@@ -664,6 +654,33 @@ private async Task<SsoToken> GenerateNewTokenAsync(SSOTokenManagerGetTokenOption
664654

665655
return token;
666656
}
657+
658+
private static List<string> GetEmptySSOTokenOptions(SSOTokenManagerGetTokenOptions options)
659+
{
660+
var emptyProperties = new List<string>();
661+
662+
if (string.IsNullOrEmpty(options.ClientName))
663+
{
664+
emptyProperties.Add(nameof(options.ClientName));
665+
}
666+
667+
if (options.PkceFlowOptions == null)
668+
{
669+
if (options.SsoVerificationCallback == null)
670+
{
671+
emptyProperties.Add(nameof(options.SsoVerificationCallback));
672+
}
673+
}
674+
else
675+
{
676+
if (options.PkceFlowOptions.RetrieveAuthorizationCodeCallbackAsync == null)
677+
{
678+
emptyProperties.Add(nameof(options.PkceFlowOptions.RetrieveAuthorizationCodeCallbackAsync));
679+
}
680+
}
681+
682+
return emptyProperties;
683+
}
667684
#endif
668685

669686
private static SsoToken MapGetSsoTokenResponseToSsoToken(GetSsoTokenResponse response, string session)

sdk/src/Core/Amazon.Runtime/Credentials/_bcl+netstandard/SSOAWSCredentialsOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class SSOAWSCredentialsOptions
9494
/// NOTE: If setting to <c>true</c>, either <see cref="SsoVerificationCallback"/> or <see cref="PkceFlowOptions"/> must
9595
/// also be set for authorization flow to succeed.
9696
/// </summary>
97-
public bool SupportsGettingNewToken { get; set; } = true;
97+
public bool SupportsGettingNewToken { get; set; } = false;
9898

9999
/// <summary>
100100
/// The proxy settings to use when calling SSOOIDC and SSO Services.

0 commit comments

Comments
 (0)