//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
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;
///
/// The OAuth client for the user-agent flow, providing services for installed apps
/// and in-browser Javascript widgets.
///
public class UserAgentClient : ClientBase {
///
/// Initializes a new instance of the class.
///
/// The token issuer.
/// The client identifier.
/// The client secret.
public UserAgentClient(AuthorizationServerDescription authorizationServer, string clientIdentifier = null, string clientSecret = null)
: base(authorizationServer, clientIdentifier, clientSecret) {
}
///
/// Initializes a new instance of the class.
///
/// The authorization endpoint.
/// The token endpoint.
/// The client identifier.
/// The client secret.
public UserAgentClient(Uri authorizationEndpoint, Uri tokenEndpoint, string clientIdentifier = null, string clientSecret = null)
: this(new AuthorizationServerDescription { AuthorizationEndpoint = authorizationEndpoint, TokenEndpoint = tokenEndpoint }, clientIdentifier, clientSecret) {
Contract.Requires(authorizationEndpoint != null);
Contract.Requires(tokenEndpoint != null);
}
///
/// 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.
///
/// The scope of authorized access requested.
/// The client state that should be returned with the authorization response.
/// The URL that the authorization response should be sent to via a user-agent redirect.
///
/// A fully-qualified URL suitable to initiate the authorization flow.
///
public Uri RequestUserAuthorization(IEnumerable scope = null, string state = null, Uri returnTo = null) {
var authorization = new AuthorizationState(scope) {
Callback = returnTo,
};
return this.RequestUserAuthorization(authorization);
}
///
/// 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.
///
/// The authorization state that is tracking this particular request. Optional.
/// The client state that should be returned with the authorization response.
///
/// A fully-qualified URL suitable to initiate the authorization flow.
///
public Uri RequestUserAuthorization(IAuthorizationState authorization, string state = null) {
Contract.Requires(authorization != null);
Contract.Requires(!string.IsNullOrEmpty(this.ClientIdentifier));
if (authorization.Callback == null) {
authorization.Callback = new Uri("http://localhost/");
}
var request = new EndUserAuthorizationRequest(this.AuthorizationServer) {
ClientIdentifier = this.ClientIdentifier,
Callback = authorization.Callback,
ClientState = state,
};
request.Scope.ResetContents(authorization.Scope);
return this.Channel.PrepareResponse(request).GetDirectUriRequest(this.Channel);
}
///
/// Scans the incoming request for an authorization response message.
///
/// The actual URL of the incoming HTTP request.
/// The authorization.
/// The granted authorization, or null if the incoming HTTP request did not contain an authorization server response or authorization was rejected.
public IAuthorizationState ProcessUserAuthorization(Uri actualRedirectUrl, IAuthorizationState authorizationState = null) {
Contract.Requires(actualRedirectUrl != null);
if (authorizationState == null) {
authorizationState = 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;
}
EndUserAuthorizationSuccessAccessTokenResponse accessTokenSuccess;
EndUserAuthorizationSuccessAuthCodeResponse authCodeSuccess;
EndUserAuthorizationFailedResponse failure;
if ((accessTokenSuccess = response as EndUserAuthorizationSuccessAccessTokenResponse) != null) {
UpdateAuthorizationWithResponse(authorizationState, accessTokenSuccess);
} else if ((authCodeSuccess = response as EndUserAuthorizationSuccessAuthCodeResponse) != null) {
this.UpdateAuthorizationWithResponse(authorizationState, authCodeSuccess);
} else if ((failure = response as EndUserAuthorizationFailedResponse) != null) {
authorizationState.Delete();
return null;
}
return authorizationState;
}
}
}