Пример #1
0
        public ActionResult Index()
        {
            // Have the authorization server handle the token request.
            // It will use the passed-in request to determine what the actual token request is.
            // If the request does not contain a valid token request, an exception is thrown
            OutgoingWebResponse outgoingWebResponse = this.authorizationServer.HandleTokenRequest(this.Request);

            // Convert the outgoing web response to an ActionResult to correctly integrate with ASP.NET MVC flow
            return(outgoingWebResponse.AsActionResult());
        }
Пример #2
0
 /// <summary>
 /// The OAuth 2.0 token endpoint.
 /// </summary>
 /// <returns>The response to the Client.</returns>
 public ActionResult Token()
 {
     using (OAuth2AuthorizationServer server = (new OAuth2AuthorizationServer(new X509Certificate2(ConfigurationManager.AppSettings["AbsolutePathToPfx"], ConfigurationManager.AppSettings["CertificatePassword"]),
                                                                              new X509Certificate2(ConfigurationManager.AppSettings["AbsolutePathToCertificate"]))))
     {
         AuthorizationServer authorizationServer = new AuthorizationServer(server);
         OutgoingWebResponse response            = authorizationServer.HandleTokenRequest(this.Request);
         return(response.AsActionResult());
     }
 }
Пример #3
0
 public override ActionResult AsActionResult()
 {
     return(_response.AsActionResult());
 }
Пример #4
0
        /// <summary>
        /// Create a authorise token from the request. Returns the bdy html result.
        /// </summary>
        /// <param name="httpRequest">The current http request.</param>
        /// <param name="rawUri">A System.Uri object containing information regarding the URL of the current request.</param>
        /// <param name="queryString">The collection of HTTP query string variables.</param>
        /// <param name="form">The collection of form variables.</param>
        /// <param name="headers">The collection of HTTP headers.</param>
        /// <param name="cookies">The collection of cookies sent by the client.</param>
        /// <param name="returnType">The type of response to return.</param>
        /// <param name="responseHeaders">The response headers for the request.</param>
        /// <param name="isApprovedByUser">Has the user approved the client to access the resources.</param>
        /// <returns>The formatted redirect url; else null.</returns>
        private object CreateAuthorise(HttpRequestBase httpRequest, Uri rawUri, NameValueCollection queryString,
                                       NameValueCollection form, NameValueCollection headers, HttpCookieCollection cookies, int returnType,
                                       out System.Net.WebHeaderCollection responseHeaders, bool isApprovedByUser)
        {
            IDirectedProtocolMessage response    = null;
            OutgoingWebResponse      webResponse = null;
            string clientID = null;
            string nonce    = null;
            string codeKey  = null;

            try
            {
                // Make sure that all the passed parameters are valid.
                if (httpRequest == null)
                {
                    throw new ArgumentNullException("httpRequest");
                }
                if (rawUri == null)
                {
                    throw new ArgumentNullException("rawUri");
                }
                if (queryString == null)
                {
                    throw new ArgumentNullException("queryString");
                }
                if (form == null)
                {
                    throw new ArgumentNullException("form");
                }
                if (headers == null)
                {
                    throw new ArgumentNullException("headers");
                }
                if (cookies == null)
                {
                    throw new ArgumentNullException("cookies");
                }

                // Read the request make sure it is valid.
                EndUserAuthorizationRequest pendingRequest = _authorizationServer.ReadAuthorizationRequest(httpRequest);
                if (pendingRequest == null)
                {
                    throw new Exception("Missing authorization request.");
                }

                // Only process if the user has approved the request.
                if (isApprovedByUser)
                {
                    // Make sure all maditor parameters are present.
                    _oAuthAuthorizationServer.ValidateAuthoriseRequestParametersAbsent(queryString);
                    if (_oAuthAuthorizationServer.ParametersAbsent.Count() > 0)
                    {
                        throw new Exception("Some authorisation request parameters are missing.");
                    }

                    // Assign each query string parameter.
                    clientID = pendingRequest.ClientIdentifier;
                    string callback            = pendingRequest.Callback.ToString();
                    string state               = pendingRequest.ClientState;
                    string scope               = OAuthUtilities.JoinScopes(pendingRequest.Scope);
                    string responseType        = (pendingRequest.ResponseType == EndUserAuthorizationResponseType.AccessToken ? "token" : "code");
                    string companyUniqueUserID = queryString["com_unique_uid"];

                    // Set the crytography key store values.
                    _authorizationServer.AuthorizationServerServices.CryptoKeyStore.ExpiryDateTime   = DateTime.UtcNow.AddYears(1);
                    _authorizationServer.AuthorizationServerServices.CryptoKeyStore.ClientIndetifier = clientID;
                    _authorizationServer.AuthorizationServerServices.CryptoKeyStore.GetCodeKey       = false;

                    // Create a new nonce and store it in the nonce store.
                    nonce = _nonceStore.GenerateNonce();
                    _nonceStore.StoreNonce(DateTime.UtcNow, nonce, clientID);

                    // Create the access token from the stores, and create a new verification code.
                    string verifier = _consumerStore.SetVerificationCode(clientID, nonce, companyUniqueUserID, scope);
                    EndUserAuthorizationSuccessAccessTokenResponse successAccessTokenResponse = null;

                    // Prepare the request. pass the nonce and join the userID and nonce of
                    // the user that approved the resource access request.
                    response = _authorizationServer.PrepareApproveAuthorizationRequest(
                        pendingRequest,
                        companyUniqueUserID + "_" + nonce,
                        nonce,
                        out successAccessTokenResponse);

                    // Prepare the authorisation response.
                    webResponse = _authorizationServer.Channel.PrepareResponse(response);

                    // Create the query collection of the code request
                    // and extract the code value that is to be sent
                    // the the client.
                    NameValueCollection queryResponseString = new NameValueCollection();
                    Uri uriRequest = webResponse.GetDirectUriRequest(_authorizationServer.Channel);

                    // For each query item.
                    string[] queries = uriRequest.Query.Split(new char[] { '&' });
                    foreach (string query in queries)
                    {
                        // Add the query name and value to the collection.
                        string[] queriesNameValue = query.Split(new char[] { '=' });
                        queryResponseString.Add(queriesNameValue[0].TrimStart(new char[] { '?' }), queriesNameValue[1]);
                    }

                    // What type of response is to be handled.
                    switch (pendingRequest.ResponseType)
                    {
                    case EndUserAuthorizationResponseType.AuthorizationCode:
                        // The user has requested a code, this is
                        // used so the client can get a token later.
                        // If the code response type exits.
                        if (queryResponseString["code"] != null)
                        {
                            codeKey = HttpUtility.UrlDecode(queryResponseString["code"]);
                        }

                        // Insert the code key (code or token);
                        if (!String.IsNullOrEmpty(codeKey))
                        {
                            _tokenStore.StoreCodeKey(clientID, nonce, codeKey);
                        }
                        break;

                    case EndUserAuthorizationResponseType.AccessToken:
                        // This is used so the client is approved and a token is sent back.
                        // Update the access token.
                        if (successAccessTokenResponse != null)
                        {
                            if (!String.IsNullOrEmpty(successAccessTokenResponse.AccessToken))
                            {
                                _tokenStore.UpdateAccessToken(successAccessTokenResponse.AccessToken, nonce);
                            }
                        }
                        break;
                    }
                }
                else
                {
                    // Send an error response.
                    response = _authorizationServer.PrepareRejectAuthorizationRequest(pendingRequest);
                }

                // What type should be returned.
                switch (returnType)
                {
                case 0:
                    // A URI request redirect only.
                    responseHeaders = webResponse.Headers;
                    return(webResponse.GetDirectUriRequest(_authorizationServer.Channel));

                case 1:
                    // The complete html body.
                    responseHeaders = webResponse.Headers;
                    return(webResponse.Body);

                case 2:
                    // The action result.
                    responseHeaders = webResponse.Headers;
                    return(webResponse.AsActionResult());

                default:
                    // Default is the complete html body.
                    responseHeaders = webResponse.Headers;
                    return(webResponse.Body);
                }
            }
            catch (Exception ex)
            {
                // Get the current token errors.
                responseHeaders = null;
                _tokenError     = ex.Message;
                return(null);
            }
        }
Пример #5
0
        /// <summary>
        /// Create a request token from the request.
        /// </summary>
        /// <param name="httpRequest">The current http request.</param>
        /// <param name="rawUri">A System.Uri object containing information regarding the URL of the current request.</param>
        /// <param name="queryString">The collection of HTTP query string variables.</param>
        /// <param name="form">The collection of form variables.</param>
        /// <param name="headers">The collection of HTTP headers.</param>
        /// <param name="cookies">The collection of cookies sent by the client.</param>
        /// <param name="responseHeaders">The response headers for the request.</param>
        /// <param name="returnType">The type of response to return.</param>
        /// <returns>The token if successful; else null.</returns>
        private object CreateToken(HttpRequestBase httpRequest, Uri rawUri, NameValueCollection queryString,
                                   NameValueCollection form, NameValueCollection headers, HttpCookieCollection cookies,
                                   out System.Net.WebHeaderCollection responseHeaders, int returnType)
        {
            OutgoingWebResponse        outgoingWebResponse        = null;
            AccessTokenSuccessResponse accessTokenSuccessResponse = null;
            IProtocolMessage           message = null;
            string codeKey      = null;
            string refreshToken = null;
            string clientID     = null;
            string nonce        = null;

            try
            {
                // Make sure that all the passed parameters are valid.
                if (httpRequest == null)
                {
                    throw new ArgumentNullException("httpRequest");
                }
                if (rawUri == null)
                {
                    throw new ArgumentNullException("rawUri");
                }
                if (queryString == null)
                {
                    throw new ArgumentNullException("queryString");
                }
                if (form == null)
                {
                    throw new ArgumentNullException("form");
                }
                if (headers == null)
                {
                    throw new ArgumentNullException("headers");
                }
                if (cookies == null)
                {
                    throw new ArgumentNullException("cookies");
                }

                // Set the crytography key store values.
                _authorizationServer.AuthorizationServerServices.CryptoKeyStore.ExpiryDateTime = DateTime.UtcNow.AddYears(1);
                _authorizationServer.AuthorizationServerServices.CryptoKeyStore.GetCodeKey     = true;

                // Attempt to find the 'code' parameter in the form.
                IEnumerable <string> codeKeys = form.AllKeys.Where(u => u.EndsWith("code"));
                if (codeKeys == null || codeKeys.Count() < 1)
                {
                    // Attempt to find the 'code' parameter in the query string.
                    if (queryString != null || queryString.Keys.Count > 0)
                    {
                        if (queryString["code"] != null)
                        {
                            codeKey = queryString["code"];
                        }
                    }
                }
                else
                {
                    codeKey = form["code"];
                }

                // If a code value exists.
                if (!String.IsNullOrEmpty(codeKey))
                {
                    // Get the nonce data for the code value.
                    nonce = _tokenStore.GetNonce(codeKey);
                }

                // Attempt to find the 'refresh_token' parameter in the form.
                IEnumerable <string> refreshTokens = form.AllKeys.Where(u => u.EndsWith("refresh_token"));
                if (refreshTokens == null || refreshTokens.Count() < 1)
                {
                    // Attempt to find the 'refresh_token' parameter in the query string.
                    if (queryString != null || queryString.Keys.Count > 0)
                    {
                        if (queryString["refresh_token"] != null)
                        {
                            refreshToken = queryString["refresh_token"];
                        }
                    }
                }
                else
                {
                    refreshToken = form["refresh_token"];
                }

                // Pass a refresh token
                if (!String.IsNullOrEmpty(refreshToken))
                {
                    string clientIdentifier = null;
                    string clientSecret     = null;

                    // Get the refresh token data from the http request.
                    _oAuthAuthorizationServer.GetRefreshTokenData(queryString, form, out clientIdentifier, out clientSecret);

                    // Get the nonce data for the code value.
                    nonce = _tokenStore.GetNonce(refreshToken, clientIdentifier, clientSecret);
                }

                // Handles an incoming request to the authorization server's token endpoint.
                message = _authorizationServer.HandleTokenRequest(nonce, out clientID, out accessTokenSuccessResponse, httpRequest);

                // Set the crytography key store values after finding the client identifier.
                _authorizationServer.AuthorizationServerServices.CryptoKeyStore.ClientIndetifier = clientID;

                // Handles an incoming request to the authorization server's token endpoint.
                outgoingWebResponse = _authorizationServer.HandleTokenRequestPrepareResponse(message);

                // Update the access token.
                if (accessTokenSuccessResponse != null)
                {
                    if (!String.IsNullOrEmpty(accessTokenSuccessResponse.AccessToken))
                    {
                        _tokenStore.UpdateAccessToken(accessTokenSuccessResponse.AccessToken, nonce, accessTokenSuccessResponse.RefreshToken);
                    }
                }

                // What type should be returned.
                switch (returnType)
                {
                case 0:
                    // The complete html body.
                    responseHeaders = outgoingWebResponse.Headers;
                    return(outgoingWebResponse.Body);

                case 1:
                    // The action result.
                    responseHeaders = outgoingWebResponse.Headers;
                    return(outgoingWebResponse.AsActionResult());

                default:
                    // Default is html body.
                    responseHeaders = outgoingWebResponse.Headers;
                    return(outgoingWebResponse.Body);
                }
            }
            catch (Exception ex)
            {
                // Get the current token errors.
                responseHeaders = null;
                _tokenError     = ex.Message;
                return(null);
            }
        }