diff options
Diffstat (limited to 'src/DotNetOpenAuth/OAuth2/UserAgentClient.cs')
-rw-r--r-- | src/DotNetOpenAuth/OAuth2/UserAgentClient.cs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth/OAuth2/UserAgentClient.cs b/src/DotNetOpenAuth/OAuth2/UserAgentClient.cs new file mode 100644 index 0000000..42e359c --- /dev/null +++ b/src/DotNetOpenAuth/OAuth2/UserAgentClient.cs @@ -0,0 +1,99 @@ +//----------------------------------------------------------------------- +// <copyright file="UserAgentClient.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.OAuth2 { + using System; + using System.Collections.Generic; + using System.Diagnostics.Contracts; + using System.Linq; + using System.Text; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth2.Messages; + + /// <summary> + /// The OAuth client for the user-agent flow, providing services for installed apps + /// and in-browser Javascript widgets. + /// </summary> + public class UserAgentClient : ClientBase { + /// <summary> + /// Initializes a new instance of the <see cref="UserAgentClient"/> class. + /// </summary> + /// <param name="authorizationServer">The token issuer.</param> + public UserAgentClient(AuthorizationServerDescription authorizationServer) + : base(authorizationServer) { + } + + /// <summary> + /// Initializes a new instance of the <see cref="UserAgentClient"/> class. + /// </summary> + /// <param name="authorizationEndpoint">The authorization endpoint.</param> + public UserAgentClient(Uri authorizationEndpoint) + : base(new AuthorizationServerDescription { AuthorizationEndpoint = authorizationEndpoint }) { + Contract.Requires<ArgumentNullException>(authorizationEndpoint != null, "authorizationEndpoint"); + } + + /// <summary> + /// Generates a URL that the user's browser can be directed to in order to authorize + /// this client to access protected data at some resource server. + /// </summary> + /// <param name="authorization">The authorization state that is tracking this particular request. Optional.</param> + /// <param name="immediate">If set to <c>true</c>, the authorization server will return immediately instead of interacting with the user. Authorization will only be granted if the authorization server determines it is safe to do so without asking the user first.</param> + /// <returns>A fully-qualified URL suitable to initiate the authorization flow.</returns> + public Uri RequestUserAuthorization(IAuthorizationState authorization = null, bool immediate = false) { + Contract.Requires<InvalidOperationException>(!string.IsNullOrEmpty(this.ClientIdentifier)); + + if (authorization == null) { + authorization = new AuthorizationState(); + } + + if (authorization.Callback == null) { + authorization.Callback = new Uri("http://localhost/"); + } + + var request = new UserAgentRequest(this.AuthorizationServer) { + ClientIdentifier = this.ClientIdentifier, + Scope = authorization.Scope, + Callback = authorization.Callback, + Immediate = immediate, + }; + + return this.Channel.PrepareResponse(request).GetDirectUriRequest(this.Channel); + } + + /// <summary> + /// Scans the incoming request for an authorization response message. + /// </summary> + /// <param name="actualRedirectUrl">The actual URL of the incoming HTTP request.</param> + /// <param name="authorization">The authorization.</param> + /// <returns>The granted authorization, or <c>null</c> if the incoming HTTP request did not contain an authorization server response or authorization was rejected.</returns> + public IAuthorizationState ProcessUserAuthorization(Uri actualRedirectUrl, IAuthorizationState authorization = null) { + Contract.Requires<ArgumentNullException>(actualRedirectUrl != null, "actualRedirectUrl"); + + if (authorization == null) { + authorization = new AuthorizationState(); + } + + var carrier = new HttpRequestInfo("GET", actualRedirectUrl, actualRedirectUrl.PathAndQuery, new System.Net.WebHeaderCollection(), null); + IDirectedProtocolMessage response = this.Channel.ReadFromRequest(carrier); + if (response == null) { + return null; + } + + UserAgentSuccessResponse success; + UserAgentFailedResponse failure; + if ((success = response as UserAgentSuccessResponse) != null) { + this.UpdateAuthorizationWithResponse(authorization, success); + } else if ((failure = response as UserAgentFailedResponse) != null) { + authorization.Delete(); + return null; + } else { + ErrorUtilities.ThrowProtocol(MessagingStrings.UnexpectedMessageReceivedOfMany); + } + + return authorization; + } + } +} |