summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OAuth2.ClientAuthorization/OAuth2/TokenEndpointProtocolException.cs
blob: d18e7881b12aa61b82ed228148838f95113336d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//-----------------------------------------------------------------------
// <copyright file="TokenEndpointProtocolException.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.OAuth2 {
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.Linq;
	using System.Text;

	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth2.Messages;
	using Validation;

	/// <summary>
	/// Describes an error generated by an Authorization Server's token endpoint.
	/// </summary>
	public class TokenEndpointProtocolException : ProtocolException {
		/// <summary>
		/// The message being processed that caused this exception to be thrown.
		/// </summary>
		private readonly AccessTokenRequestBase requestMessage;

		/// <summary>
		/// The WWW-Authenticate header to add to the response message.
		/// </summary>
		private readonly string authenticateHeader;

		/// <summary>
		/// Initializes a new instance of the <see cref="TokenEndpointProtocolException"/> class.
		/// </summary>
		/// <param name="requestMessage">The message whose processing resulted in this error.</param>
		/// <param name="error">A single error code from <see cref="Protocol.AccessTokenRequestErrorCodes"/>.</param>
		/// <param name="description">A human-readable UTF-8 encoded text providing additional information, used to assist the client developer in understanding the error that occurred.</param>
		/// <param name="moreInformation">A URI identifying a human-readable web page with information about the error, used to provide the client developer with additional information about the error.</param>
		/// <param name="authenticateHeader">The WWW-Authenticate header to add to the response.</param>
		public TokenEndpointProtocolException(AccessTokenRequestBase requestMessage, string error, string description = null, Uri moreInformation = null, string authenticateHeader = null)
			: base(string.Format(CultureInfo.CurrentCulture, ClientAuthorizationStrings.TokenEndpointErrorFormat, error, description)) {
			Requires.NotNull(requestMessage, "requestMessage");
			Requires.NotNullOrEmpty(error, "error");

			this.requestMessage = requestMessage;
			this.Error = error;
			this.Description = description;
			this.MoreInformation = moreInformation;
			this.authenticateHeader = authenticateHeader;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TokenEndpointProtocolException"/> class.
		/// </summary>
		/// <param name="innerException">The inner exception.</param>
		public TokenEndpointProtocolException(Exception innerException)
			: base(Protocol.AccessTokenRequestErrorCodes.InvalidRequest, innerException) {
			this.Error = Protocol.AccessTokenRequestErrorCodes.InvalidRequest;
		}

		/// <summary>
		/// Gets a single error code from <see cref="Protocol.AccessTokenRequestErrorCodes"/>.
		/// </summary>
		public string Error { get; private set; }

		/// <summary>
		/// Gets a human-readable UTF-8 encoded text providing additional information, used to assist the client developer in understanding the error that occurred.
		/// </summary>
		public string Description { get; private set; }

		/// <summary>
		/// Gets a URI identifying a human-readable web page with information about the error, used to provide the client developer with additional information about the error.
		/// </summary>
		public Uri MoreInformation { get; private set; }

		/// <summary>
		/// Gets the response message to send to the client.
		/// </summary>
		/// <returns>A message.</returns>
		public IDirectResponseProtocolMessage GetResponse() {
			var response = this.requestMessage != null
				? new AccessTokenFailedResponse(this.requestMessage, this.authenticateHeader != null)
				: new AccessTokenFailedResponse();
			response.Error = this.Error;
			response.ErrorDescription = this.Description;
			response.ErrorUri = this.MoreInformation;
			if (this.authenticateHeader != null) {
				response.Headers.Add(HttpRequestHeaders.WwwAuthenticate, this.authenticateHeader);
			}

			return response;
		}
	}
}