Skip to content

Commit

Permalink
fix: null check before trim() (#99)
Browse files Browse the repository at this point in the history
* null check before trim()

* fix: #97 null check before trim()

* fix: apply null check to all trims
  • Loading branch information
esmondmissen committed Nov 7, 2022
1 parent 40120dd commit dfd519a
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions SSO-Auth/Api/SSOController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ public SSOController(ILogger<SSOController> logger, ISessionManager sessionManag
{
var options = new OidcClientOptions
{
Authority = config.OidEndpoint.Trim(),
ClientId = config.OidClientId.Trim(),
ClientSecret = config.OidSecret.Trim(),
Authority = config.OidEndpoint?.Trim(),
ClientId = config.OidClientId?.Trim(),
ClientSecret = config.OidSecret?.Trim(),
RedirectUri = GetRequestBase() + "/sso/OID/r/" + provider,
Scope = string.Join(" ", config.OidScopes.Prepend("openid profile")),
};
options.Policy.Discovery.ValidateEndpoints = false; // For Google and other providers with different endpoints
options.Policy.Discovery.RequireHttps = config.RequireHttps || true;
options.Policy.Discovery.RequireHttps = config?.RequireHttps ?? true;
var oidcClient = new OidcClient(options);
var currentState = StateManager[state].State;
var result = oidcClient.ProcessResponseAsync(Request.QueryString.Value, currentState).Result;
Expand All @@ -110,7 +110,7 @@ public SSOController(ILogger<SSOController> logger, ISessionManager sessionManag

foreach (var claim in result.User.Claims)
{
if (claim.Type == (config.DefaultUsernameClaim.Trim() ?? "preferred_username"))
if (claim.Type == (config.DefaultUsernameClaim?.Trim() ?? "preferred_username"))
{
StateManager[state].Username = claim.Value;
if (config.Roles.Length == 0)
Expand All @@ -122,7 +122,7 @@ public SSOController(ILogger<SSOController> logger, ISessionManager sessionManag
// Role processing
// The regex matches any "." not preceded by a "\": a.b.c will be split into a, b, and c, but a.b\.c will be split into a, b.c (after processing the escaped dots)
// We have to first process the RoleClaim string
string[] segments = Regex.Split(config.RoleClaim.Trim(), "(?<!\\\\)\\.");
string[] segments = Regex.Split(config.RoleClaim?.Trim(), "(?<!\\\\)\\.");
// Now we make sure that any escaped "."s ("\.") are replaced with "."
for (int i = 0; i < segments.Length; i++)
{
Expand Down Expand Up @@ -182,7 +182,7 @@ public SSOController(ILogger<SSOController> logger, ISessionManager sessionManag
{
foreach (FolderRoleMap folderRoleMap in config.FolderRoleMapping)
{
if (role.Equals(folderRoleMap.Role.Trim()))
if (role.Equals(folderRoleMap.Role?.Trim()))
{
StateManager[state].Folders.AddRange(folderRoleMap.Folders);
}
Expand Down Expand Up @@ -255,9 +255,9 @@ public async Task<ActionResult> OidChallenge(string provider, [FromQuery] bool i
{
var options = new OidcClientOptions
{
Authority = config.OidEndpoint.Trim(),
ClientId = config.OidClientId.Trim(),
ClientSecret = config.OidSecret.Trim(),
Authority = config.OidEndpoint?.Trim(),
ClientId = config.OidClientId?.Trim(),
ClientSecret = config.OidSecret?.Trim(),
RedirectUri = GetRequestBase() + "/sso/OID/r/" + provider,
Scope = string.Join(" ", config.OidScopes.Prepend("openid profile")),
};
Expand Down Expand Up @@ -372,7 +372,7 @@ public async Task<ActionResult> OidAuth(string provider, [FromBody] AuthResponse
{
Guid userId = await CreateCanonicalLinkAndUserIfNotExist("oid", provider, kvp.Value.Username);

var authenticationResult = await Authenticate(userId, kvp.Value.Admin, config.EnableAuthorization, config.EnableAllFolders, kvp.Value.Folders.ToArray(), response, config.DefaultProvider.Trim())
var authenticationResult = await Authenticate(userId, kvp.Value.Admin, config.EnableAuthorization, config.EnableAllFolders, kvp.Value.Folders.ToArray(), response, config.DefaultProvider?.Trim())
.ConfigureAwait(false);
return Ok(authenticationResult);
}
Expand Down

0 comments on commit dfd519a

Please sign in to comment.