Exemple #1
0
        public async Task <CacheToken> AuthenticateEndpoint(Endpoint endpoint,
                                                            [CallerMemberName] string memberName = "",
                                                            [CallerLineNumber] int lineNumber    = 0)


        {
            if (endpoint.IsActive)
            {
                AuthenticationResult results            = null;
                IPlatformParameters  platformParameters = _azurePlatformParameters.GetPlatformParameters(endpoint.UseBroker);
                try
                {
                    var authContext = new AuthenticationContext(endpoint.Authority);
                    //fixes for security groups in iOS per
                    //https://aka.ms/adal-net-ios-keychain-access

#if iOS
                    authContext.iOSKeychainSecurityGroup = endpoint.iOSKeychainSecurityGroup;
#endif

                    _logBuilder.Clear();
                    LoggerCallbackHandler.LogCallback       = AdalLog;
                    LoggerCallbackHandler.PiiLoggingEnabled = true;

                    _loggingService.LogInformation(typeof(AzureAuthenticatorService),
                                                   $"Before Acquiring Token values",
                                                   memberName,
                                                   lineNumber,
                                                   ComposePropertyValues(endpoint));
                    if (string.IsNullOrEmpty(endpoint.ExtraParameters))
                    {
                        results = await authContext.AcquireTokenAsync(endpoint.ResourceId,
                                                                      endpoint.ApplicationId,
                                                                      new Uri(endpoint.RedirectUri),
                                                                      platformParameters);
                    }
                    else
                    {
                        results = await authContext.AcquireTokenAsync(endpoint.ResourceId,
                                                                      endpoint.ApplicationId,
                                                                      new Uri(endpoint.RedirectUri),
                                                                      platformParameters,
                                                                      UserIdentifier.AnyUser,
                                                                      endpoint.ExtraParameters);
                    }

                    _loggingService.LogInformation(typeof(AzureAuthenticatorService),
                                                   $"{Constants.AzureAuthenticator.AZURELOGTAG} Logs: {_logBuilder?.ToString()}");
                    _loggingService.LogInformation(typeof(AzureAuthenticatorService),
                                                   $"{Constants.AzureAuthenticator.AZURELOGTAG} Log: Access Token: {results?.AccessToken}\n\nAccess Token Type: {results?.AccessTokenType}\n\nExpires On: {results?.ExpiresOn.ToString()}");
                }
                catch (AdalUserMismatchException aume)
                {
                    _loggingService.LogError(typeof(AzureAuthenticatorService),
                                             (System.Exception)aume,
                                             $"{Constants.AzureAuthenticator.AZURELOGERRORTAG} :AdalUserMismatchException",
                                             memberName,
                                             lineNumber,
                                             $"Returned User: {aume?.ReturnedUser} Requested User: {aume?.RequestedUser}",
                                             $"Error Code:: {aume?.ErrorCode}");
                    throw;
                }
                catch (AdalSilentTokenAcquisitionException astae)
                {
                    _loggingService.LogError(typeof(AzureAuthenticatorService),
                                             (System.Exception)astae,
                                             $"AdalSilientTokenAquisitionException {astae?.ErrorCode}",
                                             memberName,
                                             lineNumber,
                                             astae.Data);
                    throw;
                }
                catch (AdalClaimChallengeException acce)
                {
                    _loggingService.LogError(typeof(AzureAuthenticatorService),
                                             (System.Exception)acce,
                                             $"AdalClaimsChallengeException:: Claims: {acce.Claims}",
                                             memberName,
                                             lineNumber,
                                             acce.Data,
                                             acce.Headers,
                                             acce.ServiceErrorCodes,
                                             acce.StatusCode);
                    throw;
                }
                catch (AdalServiceException ase)
                {
                    _loggingService.LogError(typeof(AzureAuthenticatorService),
                                             (System.Exception)ase,
                                             $"{Constants.AzureAuthenticator.AZURELOGERRORTAG}: AdalServiceException::",
                                             memberName,
                                             lineNumber,
                                             ase.Data,
                                             ase.Headers,
                                             ase.ServiceErrorCodes,
                                             ase.StatusCode);
                    throw;
                }
                catch (Exception e)
                {
                    _loggingService.LogError(typeof(AzureAuthenticatorService),
                                             e,
                                             e.Message,
                                             memberName,
                                             lineNumber,
                                             null);
                    throw;
                }

                //
                //add result to token cache
                var cacheToken = AzureTokenCacheService.CreateCacheToken(results, endpoint);
                AzureTokenCacheService.AddToken(cacheToken);
                return(cacheToken);
            }
            else
            {
                throw new Exception("ERROR:  Endpoint not Active, please make Endpoint Active and try again.");
            }
        }
Exemple #2
0
        public async Task <CacheToken> AcquireTokenSilentAsync(Endpoint endpoint,
                                                               [CallerMemberName] string memberName = "",
                                                               [CallerLineNumber] int lineNumber    = 0)

        {
            AuthenticationResult results = null;

            try
            {
                var authContext = new AuthenticationContext(endpoint.Authority);
                //fixes for security groups in iOS per
                //https://aka.ms/adal-net-ios-keychain-access

#if iOS
                authContext.KeychainSecurityGroup = endpoint.iOSKeychainSecurityGroup;
#endif

                _logBuilder.Clear();
                LoggerCallbackHandler.LogCallback       = AdalLog;
                LoggerCallbackHandler.PiiLoggingEnabled = true;

                _loggingService.LogInformation(typeof(AzureAuthenticatorService),
                                               $"Before Acquiring Silent Token values",
                                               memberName,
                                               lineNumber,
                                               ComposePropertyValues(endpoint));

                results = await authContext.AcquireTokenSilentAsync(endpoint.ResourceId, endpoint.ApplicationId);
            }
            catch (AdalUserMismatchException aume)
            {
                _loggingService.LogError(typeof(AzureAuthenticatorService),
                                         (System.Exception)aume,
                                         $"{Constants.AzureAuthenticator.AZURELOGERRORTAG} :AdalUserMismatchException",
                                         memberName,
                                         lineNumber,
                                         $"Returned User: {aume?.ReturnedUser} Requested User: {aume?.RequestedUser}",
                                         $"Error Code:: {aume?.ErrorCode}");
                throw;
            }
            catch (AdalSilentTokenAcquisitionException astae)
            {
                _loggingService.LogError(typeof(AzureAuthenticatorService),
                                         (System.Exception)astae,
                                         $"AdalSilientTokenAquisitionException {astae?.ErrorCode}",
                                         memberName,
                                         lineNumber,
                                         astae.Data);
                throw;
            }
            catch (AdalClaimChallengeException acce)
            {
                _loggingService.LogError(typeof(AzureAuthenticatorService),
                                         (System.Exception)acce,
                                         $"AdalClaimsChallengeException:: Claims: {acce.Claims}",
                                         memberName,
                                         lineNumber,
                                         acce.Data,
                                         acce.Headers,
                                         acce.ServiceErrorCodes,
                                         acce.StatusCode);
                throw;
            }
            catch (AdalServiceException ase)
            {
                _loggingService.LogError(typeof(AzureAuthenticatorService),
                                         (System.Exception)ase,
                                         $"{Constants.AzureAuthenticator.AZURELOGERRORTAG}: AdalServiceException::",
                                         memberName,
                                         lineNumber,
                                         ase.Data,
                                         ase.Headers,
                                         ase.ServiceErrorCodes,
                                         ase.StatusCode);
                throw;
            }
            catch (Exception e)
            {
                _loggingService.LogError(typeof(AzureAuthenticatorService),
                                         e,
                                         e.Message,
                                         memberName,
                                         lineNumber,
                                         null);
                throw;
            }

            //add result to cache
            var cacheToken = AzureTokenCacheService.CreateCacheToken(results, endpoint);
            AzureTokenCacheService.AddToken(cacheToken);
            return(cacheToken);
        }