Exemple #1
0
        public InteractiveRequest(
            IServiceBundle serviceBundle,
            AuthenticationRequestParameters authenticationRequestParameters,
            ApiEvent.ApiIds apiId,
            IEnumerable <string> extraScopesToConsent,
            string loginHint,
            UIBehavior uiBehavior,
            IWebUI webUi)
            : base(serviceBundle, authenticationRequestParameters, apiId)
        {
            RedirectUriHelper.Validate(authenticationRequestParameters.RedirectUri);
            webUi?.ValidateRedirectUri(authenticationRequestParameters.RedirectUri);

            _extraScopesToConsent = new SortedSet <string>();
            if (!extraScopesToConsent.IsNullOrEmpty())
            {
                _extraScopesToConsent = ScopeHelper.CreateSortedSetFromEnumerable(extraScopesToConsent);
            }

            ValidateScopeInput(_extraScopesToConsent);

            authenticationRequestParameters.LoginHint = loginHint;
            if (!string.IsNullOrWhiteSpace(authenticationRequestParameters.ExtraQueryParameters) &&
                authenticationRequestParameters.ExtraQueryParameters[0] == '&')
            {
                authenticationRequestParameters.ExtraQueryParameters =
                    authenticationRequestParameters.ExtraQueryParameters.Substring(1);
            }

            _webUi      = webUi;
            _uiBehavior = uiBehavior;
            AuthenticationRequestParameters.RequestContext.Logger.Info(
                "Additional scopes - " + _extraScopesToConsent.AsSingleString() + ";" +
                "UIBehavior - " + _uiBehavior.PromptValue);
        }
Exemple #2
0
        public AuthenticationRequestParameters CreateAuthenticationRequestParameters(
            string authority,
            IEnumerable <string> scopes    = null,
            ITokenCacheInternal tokenCache = null,
            IAccount account = null,
            IDictionary <string, string> extraQueryParameters = null,
            string claims          = null,
            ApiEvent.ApiIds apiId  = ApiEvent.ApiIds.None,
            bool validateAuthority = false)
        {
            scopes     = scopes ?? TestConstants.s_scope;
            tokenCache = tokenCache ?? new TokenCache(ServiceBundle, false);

            var commonParameters = new AcquireTokenCommonParameters
            {
                Scopes = scopes ?? TestConstants.s_scope,
                ExtraQueryParameters = extraQueryParameters ?? new Dictionary <string, string>(),
                Claims = claims,
                ApiId  = apiId
            };

            return(new AuthenticationRequestParameters(
                       ServiceBundle,
                       tokenCache,
                       commonParameters,
                       new RequestContext(ServiceBundle, Guid.NewGuid()))
            {
                Account = account,
                Authority = Authority.CreateAuthority(authority, validateAuthority)
            });
        }
Exemple #3
0
        public AuthenticationRequestParameters CreateAuthenticationRequestParameters(
            string authority,
            IEnumerable <string> scopes,
            ITokenCacheInternal tokenCache,
            IAccount account = null,
            IDictionary <string, string> extraQueryParameters = null,
            string claims         = null,
            ApiEvent.ApiIds apiId = ApiEvent.ApiIds.None)
        {
            if (tokenCache == null)
            {
                throw new ArgumentNullException(nameof(tokenCache));
            }

            var commonParameters = new AcquireTokenCommonParameters
            {
                Scopes = scopes ?? TestConstants.s_scope,
                ExtraQueryParameters = extraQueryParameters ?? new Dictionary <string, string>(),
                Claims = claims,
                ApiId  = apiId
            };

            return(new AuthenticationRequestParameters(
                       ServiceBundle,
                       tokenCache,
                       commonParameters,
                       new RequestContext(ServiceBundle, Guid.NewGuid()))
            {
                Account = account,
                Authority = Authority.CreateAuthority(authority)
            });
        }
Exemple #4
0
        private async Task <AuthenticationResult> AcquireTokenForLoginHintCommonAsync(
            Authority authority,
            IEnumerable <string> scopes,
            IEnumerable <string> extraScopesToConsent,
            string loginHint,
            UIBehavior behavior,
            string extraQueryParameters,
            UIParent parent,
            ApiEvent.ApiIds apiId)
        {
            var requestParams = CreateRequestParameters(authority, scopes, null, UserTokenCache);

            requestParams.ExtraQueryParameters = extraQueryParameters;

            var handler = new InteractiveRequest(
                ServiceBundle,
                requestParams,
                apiId,
                extraScopesToConsent,
                loginHint,
                behavior,
                CreateWebAuthenticationDialog(
                    parent,
                    behavior,
                    requestParams.RequestContext));

            return(await handler.RunAsync(CancellationToken.None).ConfigureAwait(false));
        }
Exemple #5
0
 public DeviceCodeRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId,
     Func <DeviceCodeResult, Task> deviceCodeResultCallback)
     : base(serviceBundle, authenticationRequestParameters, apiId)
 {
     _deviceCodeResultCallback = deviceCodeResultCallback;
 }
Exemple #6
0
 public SilentRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId,
     bool forceRefresh)
     : base(serviceBundle, authenticationRequestParameters, apiId)
 {
     ForceRefresh = forceRefresh;
 }
Exemple #7
0
 public ByRefreshTokenRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId,
     string userProvidedRefreshToken)
     : base(serviceBundle, authenticationRequestParameters, apiId)
 {
     _userProvidedRefreshToken = userProvidedRefreshToken;
 }
Exemple #8
0
 public OnBehalfOfRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId)
     : base(serviceBundle, authenticationRequestParameters, apiId)
 {
     if (authenticationRequestParameters.UserAssertion == null)
     {
         throw new ArgumentNullException(nameof(authenticationRequestParameters.UserAssertion));
     }
 }
Exemple #9
0
        public AuthorizationCodeRequest(
            IServiceBundle serviceBundle,
            AuthenticationRequestParameters authenticationRequestParameters,
            ApiEvent.ApiIds apiId)
            : base(serviceBundle, authenticationRequestParameters, apiId)
        {
            if (string.IsNullOrWhiteSpace(authenticationRequestParameters.AuthorizationCode))
            {
                throw new ArgumentNullException(nameof(authenticationRequestParameters.AuthorizationCode));
            }

            RedirectUriHelper.Validate(authenticationRequestParameters.RedirectUri);
        }
Exemple #10
0
 public UsernamePasswordRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId,
     UsernamePasswordInput usernamePasswordInput)
     : base(serviceBundle, authenticationRequestParameters, apiId)
 {
     _usernamePasswordInput       = usernamePasswordInput ?? throw new ArgumentNullException(nameof(usernamePasswordInput));
     _commonNonInteractiveHandler = new CommonNonInteractiveHandler(
         authenticationRequestParameters.RequestContext,
         usernamePasswordInput,
         serviceBundle);
 }
 public IntegratedWindowsAuthRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId,
     IntegratedWindowsAuthInput iwaInput)
     : base(serviceBundle, authenticationRequestParameters, apiId)
 {
     _iwaInput = iwaInput ?? throw new ArgumentNullException(nameof(iwaInput));
     _commonNonInteractiveHandler = new CommonNonInteractiveHandler(
         authenticationRequestParameters.RequestContext,
         _iwaInput,
         serviceBundle);
 }
 public NonInteractiveRegisterAccountRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId,
     IEnumerable <string> extraScopesToConsent,
     string loginHint,
     UIBehavior uiBehavior,
     IWebUI webUi,
     string email)
     : base(serviceBundle, authenticationRequestParameters, apiId, extraScopesToConsent, loginHint, uiBehavior, webUi)
 {
     Init(email);
 }
        internal override ApiEvent.ApiIds CalculateApiEventId()
        {
            ApiEvent.ApiIds apiId = ApiEvent.ApiIds.AcquireTokenWithScope;
            if (Parameters.Account != null)
            {
                apiId = ApiEvent.ApiIds.AcquireTokenWithScopeUser;
            }
            else if (!string.IsNullOrWhiteSpace(Parameters.LoginHint))
            {
                apiId = ApiEvent.ApiIds.AcquireTokenWithScopeHint;
            }

            return(apiId);
        }
Exemple #14
0
 public NonInteractiveLoginRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId,
     IEnumerable <string> extraScopesToConsent,
     string loginHint,
     UIBehavior uiBehavior,
     IWebUI webUi,
     string userName,
     string password)
     : base(serviceBundle, authenticationRequestParameters, apiId, extraScopesToConsent, loginHint, uiBehavior, webUi)
 {
     Init(userName, password);
 }
Exemple #15
0
 public InteractiveRequest(
     IServiceBundle serviceBundle,
     AuthenticationRequestParameters authenticationRequestParameters,
     ApiEvent.ApiIds apiId,
     IEnumerable <string> extraScopesToConsent,
     UIBehavior uiBehavior,
     IWebUI webUi)
     : this(
         serviceBundle,
         authenticationRequestParameters,
         apiId,
         extraScopesToConsent,
         authenticationRequestParameters.Account?.Username,
         uiBehavior,
         webUi)
 {
 }
        public void ValidateCommonParameters(
            ApiEvent.ApiIds expectedApiId,
            string expectedAuthorityOverride = null,
            Dictionary <string, string> expectedExtraQueryParameters = null,
            IEnumerable <string> expectedScopes = null)
        {
            Assert.IsNotNull(CommonParametersReceived);

            Assert.AreEqual(expectedApiId, CommonParametersReceived.ApiId);
            Assert.AreEqual(expectedAuthorityOverride, CommonParametersReceived.AuthorityOverride);

            CoreAssert.AreScopesEqual(
                (expectedScopes ?? MsalTestConstants.Scope).AsSingleString(),
                CommonParametersReceived.Scopes.AsSingleString());

            CollectionAssert.AreEqual(
                expectedExtraQueryParameters,
                CommonParametersReceived.ExtraQueryParameters?.ToList());
        }
Exemple #17
0
        public AuthenticationRequestParameters CreateAuthenticationRequestParameters(
            string authority,
            IEnumerable <string> scopes    = null,
            ITokenCacheInternal tokenCache = null,
            IAccount account = null,
            IDictionary <string, string> extraQueryParameters = null,
            string claims          = null,
            ApiEvent.ApiIds apiId  = ApiEvent.ApiIds.None,
            bool validateAuthority = false)
        {
            scopes     = scopes ?? TestConstants.s_scope;
            tokenCache = tokenCache ?? new TokenCache(ServiceBundle, false);

            var commonParameters = new AcquireTokenCommonParameters
            {
                Scopes = scopes ?? TestConstants.s_scope,
                ExtraQueryParameters = extraQueryParameters ?? new Dictionary <string, string>(),
                Claims = claims,
                ApiId  = apiId
            };

            var authorityObj   = Authority.CreateAuthority(authority, validateAuthority);
            var requestContext = new RequestContext(ServiceBundle, Guid.NewGuid());
            AuthenticationRequestParameters authenticationRequestParameters =
                new AuthenticationRequestParameters(
                    ServiceBundle,
                    tokenCache,
                    commonParameters,
                    requestContext,
                    authorityObj)
            {
                Account = account,
            };

            authenticationRequestParameters.RequestContext.ApiEvent = new ApiEvent(
                authenticationRequestParameters.RequestContext.Logger,
                ServiceBundle.PlatformProxy.CryptographyManager,
                Guid.NewGuid().AsMatsCorrelationId());

            return(authenticationRequestParameters);
        }
Exemple #18
0
        private async Task <AuthenticationResult> AcquireTokenByAuthorizationCodeCommonAsync(string authorizationCode,
                                                                                             IEnumerable <string> scopes, Uri redirectUri, ApiEvent.ApiIds apiId)
        {
            Authority authority     = Internal.Instance.Authority.CreateAuthority(Authority, ValidateAuthority);
            var       requestParams = CreateRequestParameters(authority, scopes, null, UserTokenCache);

            requestParams.AuthorizationCode = authorizationCode;
            requestParams.RedirectUri       = redirectUri;
            var handler =
                new AuthorizationCodeRequest(requestParams)
            {
                ApiId = apiId, IsConfidentialClient = true
            };

            return(await handler.RunAsync().ConfigureAwait(false));
        }
Exemple #19
0
        internal async Task <AuthenticationResult> AcquireTokenSilentCommonAsync(Authority authority,
                                                                                 IEnumerable <string> scopes, IUser user, bool forceRefresh, ApiEvent.ApiIds apiId)
        {
            var handler = new SilentRequest(
                CreateRequestParameters(authority, scopes, user, UserTokenCache),
                forceRefresh)
            {
                ApiId = apiId
            };

            return(await handler.RunAsync().ConfigureAwait(false));
        }
Exemple #20
0
        private async Task <AuthenticationResult> AcquireTokenForUserCommonAsync(Authority authority, IEnumerable <string> scopes,
                                                                                 IEnumerable <string> extraScopesToConsent, IUser user, UIBehavior behavior, string extraQueryParameters, UIParent parent, ApiEvent.ApiIds apiId)
        {
            var requestParams = CreateRequestParameters(authority, scopes, user, UserTokenCache);

            requestParams.ExtraQueryParameters = extraQueryParameters;

            var handler =
                new InteractiveRequest(requestParams, extraScopesToConsent, behavior,
                                       CreateWebAuthenticationDialog(parent, behavior, requestParams.RequestContext))
            {
                ApiId = apiId
            };

            return(await handler.RunAsync().ConfigureAwait(false));
        }
Exemple #21
0
        private async Task <AuthenticationResult> AcquireTokenForClientCommonAsync(IEnumerable <string> scopes, bool forceRefresh, ApiEvent.ApiIds apiId)
        {
            Authority authority = Internal.Instance.Authority.CreateAuthority(Authority, ValidateAuthority);
            AuthenticationRequestParameters parameters = CreateRequestParameters(authority, scopes, null,
                                                                                 AppTokenCache);

            parameters.IsClientCredentialRequest = true;
            var handler = new ClientCredentialRequest(parameters, forceRefresh)
            {
                ApiId = apiId, IsConfidentialClient = true
            };

            return(await handler.RunAsync().ConfigureAwait(false));
        }
Exemple #22
0
        private async Task <AuthenticationResult> AcquireTokenOnBehalfCommonAsync(Authority authority,
                                                                                  IEnumerable <string> scopes, UserAssertion userAssertion, ApiEvent.ApiIds apiId)
        {
            var requestParams = CreateRequestParameters(authority, scopes, null, UserTokenCache);

            requestParams.UserAssertion = userAssertion;
            var handler = new OnBehalfOfRequest(requestParams)
            {
                ApiId = apiId, IsConfidentialClient = true
            };

            return(await handler.RunAsync().ConfigureAwait(false));
        }