summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OAuth2/OAuth2/Messages/UnauthorizedResponse.cs
blob: 3f4bb5b22b2f017718eaf27d910bb8930075e5f9 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//-----------------------------------------------------------------------
// <copyright file="UnauthorizedResponse.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.OAuth2.Messages {
	using System;
	using System.Diagnostics.Contracts;
	using System.Net;
	using System.Text;
	using DotNetOpenAuth.Messaging;

	/// <summary>
	/// A direct response that is simply a 401 Unauthorized with an 
	/// WWW-Authenticate: OAuth header.
	/// </summary>
	internal class UnauthorizedResponse : MessageBase, IHttpDirectResponse {
		/// <summary>
		/// Initializes a new instance of the <see cref="UnauthorizedResponse"/> class.
		/// </summary>
		/// <param name="exception">The exception.</param>
		/// <param name="version">The protocol version.</param>
		internal UnauthorizedResponse(ProtocolException exception, Version version = null)
			: base(version ?? Protocol.Default.Version) {
			Requires.NotNull(exception, "exception");
			this.ErrorMessage = exception.Message;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UnauthorizedResponse"/> class.
		/// </summary>
		/// <param name="request">The request.</param>
		internal UnauthorizedResponse(IDirectedProtocolMessage request)
			: base(request) {
			this.Realm = "Service";
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UnauthorizedResponse"/> class.
		/// </summary>
		/// <param name="request">The request.</param>
		/// <param name="exception">The exception.</param>
		internal UnauthorizedResponse(IDirectedProtocolMessage request, ProtocolException exception)
			: this(request) {
			Requires.NotNull(exception, "exception");
			this.ErrorMessage = exception.Message;
		}

		#region IHttpDirectResponse Members

		/// <summary>
		/// Gets the HTTP status code that the direct response should be sent with.
		/// </summary>
		HttpStatusCode IHttpDirectResponse.HttpStatusCode {
			get { return HttpStatusCode.Unauthorized; }
		}

		/// <summary>
		/// Gets the HTTP headers to add to the response.
		/// </summary>
		/// <value>May be an empty collection, but must not be <c>null</c>.</value>
		WebHeaderCollection IHttpDirectResponse.Headers {
			get {
				return new WebHeaderCollection() {
					{ HttpResponseHeader.WwwAuthenticate, Protocol.BearerHttpAuthorizationScheme },
				};
			}
		}

		#endregion

		/// <summary>
		/// Gets or sets the error message.
		/// </summary>
		/// <value>The error message.</value>
		[MessagePart("error")]
		internal string ErrorMessage { get; set; }

		/// <summary>
		/// Gets or sets the realm.
		/// </summary>
		/// <value>The realm.</value>
		[MessagePart("realm")]
		internal string Realm { get; set; }

		/// <summary>
		/// Gets or sets the scope.
		/// </summary>
		/// <value>The scope.</value>
		[MessagePart("scope")]
		internal string Scope { get; set; }

		/// <summary>
		/// Gets or sets the algorithms.
		/// </summary>
		/// <value>The algorithms.</value>
		[MessagePart("algorithms")]
		internal string Algorithms { get; set; }

		/// <summary>
		/// Gets or sets the user endpoint.
		/// </summary>
		/// <value>The user endpoint.</value>
		[MessagePart("user-uri")]
		internal Uri UserEndpoint { get; set; }

		/// <summary>
		/// Gets or sets the token endpoint.
		/// </summary>
		/// <value>The token endpoint.</value>
		[MessagePart("token-uri")]
		internal Uri TokenEndpoint { get; set; }
	}
}