Example #1
0
        internal async Task <AccountSession> SendTokenRequestAsync(string requestBodyString)
        {
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, this.ServiceInfo.TokenServiceUrl);

            httpRequestMessage.Content = new StringContent(requestBodyString, Encoding.UTF8, Constants.Headers.FormUrlEncodedContentType);

            using (var authResponse = await this.ServiceInfo.HttpProvider.SendAsync(httpRequestMessage))
                using (var responseStream = await authResponse.Content.ReadAsStreamAsync())
                {
                    var responseValues =
                        this.ServiceInfo.HttpProvider.Serializer.DeserializeObject <IDictionary <string, string> >(
                            responseStream);

                    if (responseValues != null)
                    {
                        OAuthErrorHandler.ThrowIfError(responseValues);
                        return(new AccountSession(responseValues, this.ServiceInfo.AppId, AccountType.MicrosoftAccount));
                    }

                    throw new OneDriveException(
                              new Error
                    {
                        Code    = OneDriveErrorCode.AuthenticationFailure.ToString(),
                        Message = "Authentication failed. No response values returned from authentication flow."
                    });
                }
        }
Example #2
0
        private async Task <string> GetAuthorizationCodeAsync()
        {
            if (this.ServiceInfo.WebAuthenticationUi != null)
            {
                var requestUriString = string.Format(
                    "{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}",
                    this.ServiceInfo.AuthenticationServiceUrl,
                    Constants.Authentication.RedirectUriKeyName,
                    this.ServiceInfo.ReturnUrl,
                    Constants.Authentication.ClientIdKeyName,
                    this.ServiceInfo.AppId,
                    Constants.Authentication.ScopeKeyName,
                    string.Join("%20", this.ServiceInfo.Scopes),
                    Constants.Authentication.ResponseTypeKeyName,
                    Constants.Authentication.CodeKeyName);

                var requestUri = new Uri(requestUriString);

                var authenticationResponseValues = await this.ServiceInfo.WebAuthenticationUi.AuthenticateAsync(
                    requestUri,
                    this.callbackUri);

                OAuthErrorHandler.ThrowIfError(authenticationResponseValues);

                string code;
                if (authenticationResponseValues != null && authenticationResponseValues.TryGetValue("code", out code))
                {
                    return(code);
                }
            }

            return(null);
        }
        internal async Task <AccountSession> GetAccountSessionAsync()
        {
            var returnUrlForRequest = string.IsNullOrEmpty(this.ServiceInfo.ReturnUrl)
                ? WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString()
                : this.ServiceInfo.ReturnUrl;

            var requestUriStringBuilder = new StringBuilder();

            requestUriStringBuilder.Append(this.ServiceInfo.AuthenticationServiceUrl);
            requestUriStringBuilder.AppendFormat("?{0}={1}", Constants.Authentication.RedirectUriKeyName, returnUrlForRequest);
            requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ClientIdKeyName, this.ServiceInfo.AppId);
            requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ScopeKeyName, string.Join("%20", this.ServiceInfo.Scopes));
            requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ResponseTypeKeyName, Constants.Authentication.TokenResponseTypeValueName);

            var requestUri = new Uri(requestUriStringBuilder.ToString());

            var authenticationResponseValues = await this.ServiceInfo.WebAuthenticationUi.AuthenticateAsync(
                requestUri,
                string.IsNullOrEmpty(this.ServiceInfo.ReturnUrl)
                ?null
                : new Uri(this.ServiceInfo.ReturnUrl));

            OAuthErrorHandler.ThrowIfError(authenticationResponseValues);

            return(new AccountSession(authenticationResponseValues, this.ServiceInfo.AppId, AccountType.MicrosoftAccount)
            {
                CanSignOut = true
            });
        }
Example #4
0
        public static void ThrowIfError(IDictionary <string, string> responseValues)
        {
            if (responseValues != null)
            {
                string error            = null;
                string errorDescription = null;

                if (responseValues.TryGetValue(Constants.Authentication.ErrorDescriptionKeyName, out errorDescription) ||
                    responseValues.TryGetValue(Constants.Authentication.ErrorKeyName, out error))
                {
                    OAuthErrorHandler.ParseAuthenticationError(error, errorDescription);
                }
            }
        }
        internal async Task <string> GetAuthorizationCodeAsync(string returnUrl = null)
        {
            if (this.ServiceInfo.WebAuthenticationUi != null)
            {
                returnUrl = returnUrl ?? this.ServiceInfo.ReturnUrl;

                var requestUri = new Uri(this.OAuthRequestStringBuilder.GetAuthorizationCodeRequestUrl(returnUrl));

                var authenticationResponseValues = await this.ServiceInfo.WebAuthenticationUi.AuthenticateAsync(
                    requestUri,
                    new Uri(returnUrl));

                OAuthErrorHandler.ThrowIfError(authenticationResponseValues);

                string code;
                if (authenticationResponseValues != null && authenticationResponseValues.TryGetValue("code", out code))
                {
                    return(code);
                }
            }

            return(null);
        }
        internal async Task <string> GetAuthorizationCodeAsync(string returnUrl = null)
        {
            if (this.ServiceInfo.WebAuthenticationUi != null)
            {
                returnUrl = returnUrl ?? this.ServiceInfo.ReturnUrl;

                var requestUriStringBuilder = new StringBuilder();
                requestUriStringBuilder.Append(this.ServiceInfo.AuthenticationServiceUrl);
                requestUriStringBuilder.AppendFormat("?{0}={1}", Constants.Authentication.RedirectUriKeyName, returnUrl);
                requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ClientIdKeyName, this.ServiceInfo.AppId);
                requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ScopeKeyName, WebUtility.UrlEncode(string.Join(" ", this.ServiceInfo.Scopes)));

                if (!string.IsNullOrEmpty(this.ServiceInfo.UserId))
                {
                    requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.UserIdKeyName, this.ServiceInfo.UserId);
                }

                requestUriStringBuilder.AppendFormat("&{0}={1}", Constants.Authentication.ResponseTypeKeyName, Constants.Authentication.CodeKeyName);

                var requestUri = new Uri(requestUriStringBuilder.ToString());

                var authenticationResponseValues = await this.ServiceInfo.WebAuthenticationUi.AuthenticateAsync(
                    requestUri,
                    new Uri(returnUrl));

                OAuthErrorHandler.ThrowIfError(authenticationResponseValues);

                string code;
                if (authenticationResponseValues != null && authenticationResponseValues.TryGetValue("code", out code))
                {
                    return(code);
                }
            }

            return(null);
        }
Example #7
0
        protected override async Task <IAuthenticationResult> AuthenticateResourceAsync(string resource)
        {
            IAuthenticationResult authenticationResult = null;

            var adalServiceInfo = this.ServiceInfo as AdalServiceInfo;

            ClientAssertionCertificate clientAssertionCertificate = null;
            ClientCredential           clientCredential           = this.GetClientCredentialForAuthentication();

            var userIdentifier = this.GetUserIdentifierForAuthentication();

            try
            {
                if (adalServiceInfo != null && adalServiceInfo.ClientCertificate != null)
                {
                    clientAssertionCertificate = new ClientAssertionCertificate(this.serviceInfo.AppId, adalServiceInfo.ClientCertificate);

                    authenticationResult = await this.authenticationContextWrapper.AcquireTokenSilentAsync(resource, clientAssertionCertificate, userIdentifier);
                }
                else if (clientCredential != null)
                {
                    authenticationResult = await this.authenticationContextWrapper.AcquireTokenSilentAsync(resource, clientCredential, userIdentifier);
                }
                else
                {
                    authenticationResult = await this.authenticationContextWrapper.AcquireTokenSilentAsync(resource, this.serviceInfo.AppId);
                }
            }
            catch (Exception)
            {
                // If an exception happens during silent authentication try interactive authentication.
            }

            if (authenticationResult != null)
            {
                return(authenticationResult);
            }

            try
            {
                var redirectUri = new Uri(this.ServiceInfo.ReturnUrl);

                if (clientAssertionCertificate != null || clientCredential != null)
                {
                    var webAuthenticationUi = this.serviceInfo.WebAuthenticationUi ?? new FormsWebAuthenticationUi();

                    var requestUri = new Uri(this.OAuthRequestStringBuilder.GetAuthorizationCodeRequestUrl());

                    var authenticationResponseValues = await webAuthenticationUi.AuthenticateAsync(
                        requestUri,
                        redirectUri);

                    OAuthErrorHandler.ThrowIfError(authenticationResponseValues);

                    string code;
                    if (authenticationResponseValues != null && authenticationResponseValues.TryGetValue("code", out code))
                    {
                        if (clientAssertionCertificate != null)
                        {
                            authenticationResult = await this.authenticationContextWrapper.AcquireTokenByAuthorizationCodeAsync(
                                code,
                                redirectUri,
                                clientAssertionCertificate,
                                resource);
                        }
                        else
                        {
                            authenticationResult = await this.authenticationContextWrapper.AcquireTokenByAuthorizationCodeAsync(
                                code,
                                redirectUri,
                                clientCredential,
                                resource);
                        }
                    }
                }
                else
                {
                    authenticationResult = this.authenticationContextWrapper.AcquireToken(
                        resource,
                        this.ServiceInfo.AppId,
                        redirectUri,
                        PromptBehavior.Auto,
                        userIdentifier);
                }
            }
            catch (Exception exception)
            {
                AuthenticationExceptionHelper.HandleAuthenticationException(exception);
            }

            if (authenticationResult == null)
            {
                AuthenticationExceptionHelper.HandleAuthenticationException(null);
            }

            return(authenticationResult);
        }