/// <inheritdoc/> public async Task <AuthenticationResult> AcquireToken(TargetUri targetUri, string username, string password, AuthenticationResultType resultType, TokenScope scope) { if (resultType == AuthenticationResultType.TwoFactor) { // a previous attempt to aquire a token failed in a way that suggests the user has // Bitbucket 2FA turned on. so attempt to run the OAuth dance... OAuth.OAuthAuthenticator oauth = new OAuth.OAuthAuthenticator(); try { var result = await oauth.GetAuthAsync(targetUri, scope, CancellationToken.None); if (!result.IsSuccess) { Trace.WriteLine($"oauth authentication failed"); return(new AuthenticationResult(AuthenticationResultType.Failure)); } // we got a toke but lets check to see the usernames match var restRootUri = new Uri(_restRootUrl); var authHeader = GetBearerHeaderAuthHeader(result.Token.Value); var userResult = await RestClient.TryGetUser(targetUri, RequestTimeout, restRootUri, authHeader); if (!userResult.IsSuccess) { Trace.WriteLine($"oauth user check failed"); return(new AuthenticationResult(AuthenticationResultType.Failure)); } if (!string.IsNullOrWhiteSpace(userResult.RemoteUsername) && !username.Equals(userResult.RemoteUsername)) { Trace.WriteLine($"Remote username [{userResult.RemoteUsername}] != [{username}] supplied username"); // make sure the 'real' username is returned return(new AuthenticationResult(AuthenticationResultType.Success, result.Token, result.RefreshToken, userResult.RemoteUsername)); } // everything is hunky dory return(result); } catch (Exception ex) { Trace.WriteLine($"oauth authentication failed [{ex.Message}]"); return(new AuthenticationResult(AuthenticationResultType.Failure)); } } else { BasicAuthAuthenticator basicauth = new BasicAuthAuthenticator(); try { var restRootUri = new Uri(_restRootUrl); return(await basicauth.GetAuthAsync(targetUri, scope, RequestTimeout, restRootUri, username, password)); } catch (Exception ex) { Trace.WriteLine($"basic auth authentication failed [{ex.Message}]"); return(new AuthenticationResult(AuthenticationResultType.Failure)); } } }
/// <summary> /// Internal method for updating the CognitoUser SessionTokens property if properly authenticated /// </summary> private void UpdateSessionIfAuthenticationComplete(ChallengeNameType challengeName, AuthenticationResultType authResult) { if (string.IsNullOrEmpty(challengeName)) { CognitoUserSession cognitoUserSession = GetCognitoUserSession(authResult); this.SessionTokens = cognitoUserSession; } }