Skip to content

Commit

Permalink
simplify login name check
Browse files Browse the repository at this point in the history
  • Loading branch information
gord5500 committed Sep 20, 2024
1 parent f46d800 commit fa1fd6d
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/D2L.Bmx/OktaAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,19 @@ consider running the command again with the '--experimental-bypass-browser-secur

var oktaAuthenticatedClient = oktaClientFactory.CreateAuthenticatedClient( orgUrl, sessionId );
var oktaSession = await oktaAuthenticatedClient.GetCurrentOktaSessionAsync();
if( !OktaUserMatchesProvided( oktaSession.Login, user ) ) {
string sessionLogin = oktaSession.Login.Split( "@" )[0];
string providedLogin = user.Split( "@" )[0];
if( !sessionLogin.Equals( providedLogin, StringComparison.OrdinalIgnoreCase ) ) {
consoleWriter.WriteWarning(
"WARNING: Could not automatically sign in to Okta as provided Okta user "
+ $"'{StripLoginDomain( user )}' does not match user '{StripLoginDomain( oktaSession.Login )}'." );
+ $"'{sessionLogin}' does not match user '{providedLogin}'." );
return null;
}

TryCacheOktaSession( user, orgUrl.Host, sessionId, oktaSession.ExpiresAt );
return oktaAuthenticatedClient;
}

private static string StripLoginDomain( string email ) {
return email.Contains( '@' ) ? email.Split( '@' )[0] : email;
}

private static bool OktaUserMatchesProvided( string oktaLogin, string providedUser ) {
string adName = StripLoginDomain( oktaLogin );
string normalizedUser = StripLoginDomain( providedUser );
return adName.Equals( normalizedUser, StringComparison.OrdinalIgnoreCase );
}

private bool TryCacheOktaSession( string userId, string org, string sessionId, DateTimeOffset expiresAt ) {
if( File.Exists( BmxPaths.CONFIG_FILE_NAME ) ) {
CacheOktaSession( userId, org, sessionId, expiresAt );
Expand Down

0 comments on commit fa1fd6d

Please sign in to comment.