//----------------------------------------------------------------------- // // Copyright (c) Outercurve Foundation. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OAuth2.Messages { using System; using System.Diagnostics.Contracts; using System.Net; using System.Text; using DotNetOpenAuth.Messaging; /// /// A direct response that is simply a 401 Unauthorized with an /// WWW-Authenticate: OAuth header. /// internal class UnauthorizedResponse : MessageBase, IHttpDirectResponse { /// /// Initializes a new instance of the class. /// /// The exception. /// The protocol version. internal UnauthorizedResponse(ProtocolException exception, Version version = null) : base(version ?? Protocol.Default.Version) { Requires.NotNull(exception, "exception"); this.ErrorMessage = exception.Message; } /// /// Initializes a new instance of the class. /// /// The request. internal UnauthorizedResponse(IDirectedProtocolMessage request) : base(request) { this.Realm = "Service"; } /// /// Initializes a new instance of the class. /// /// The request. /// The exception. internal UnauthorizedResponse(IDirectedProtocolMessage request, ProtocolException exception) : this(request) { Requires.NotNull(exception, "exception"); this.ErrorMessage = exception.Message; } #region IHttpDirectResponse Members /// /// Gets the HTTP status code that the direct response should be sent with. /// HttpStatusCode IHttpDirectResponse.HttpStatusCode { get { return HttpStatusCode.Unauthorized; } } /// /// Gets the HTTP headers to add to the response. /// /// May be an empty collection, but must not be null. WebHeaderCollection IHttpDirectResponse.Headers { get { return new WebHeaderCollection() { { HttpResponseHeader.WwwAuthenticate, Protocol.BearerHttpAuthorizationScheme }, }; } } #endregion /// /// Gets or sets the error message. /// /// The error message. [MessagePart("error")] internal string ErrorMessage { get; set; } /// /// Gets or sets the realm. /// /// The realm. [MessagePart("realm")] internal string Realm { get; set; } /// /// Gets or sets the scope. /// /// The scope. [MessagePart("scope")] internal string Scope { get; set; } /// /// Gets or sets the algorithms. /// /// The algorithms. [MessagePart("algorithms")] internal string Algorithms { get; set; } /// /// Gets or sets the user endpoint. /// /// The user endpoint. [MessagePart("user-uri")] internal Uri UserEndpoint { get; set; } /// /// Gets or sets the token endpoint. /// /// The token endpoint. [MessagePart("token-uri")] internal Uri TokenEndpoint { get; set; } } }