Ejemplo n.º 1
0
 /// <summary> Initializes a new instance of the <see cref="InvalidGrantTypeException" /> class </summary>
 /// <param name="type">Http Error type</param>
 /// <param name="property">The property which caused the exception</param>
 public InvalidGrantTypeException(HTTPErrorType type, string property = null)
 {
     err = HTTPErrorCollection.Instance[type];
     this.Type = err.Type;
     this.Message = this.FormatMessage(property);
     this.Code = err.HTTPStatusCode;
 }
Ejemplo n.º 2
0
        /// <summary>Gets all headers that have to be send with the error response </summary>
        /// <param name="error">The error message key</param>
        /// <returns>List{System.String} with header values</returns>
        public static List<string> GetExceptionHttpHeaders(HTTPErrorType error)
        {
            var headers = new List<string>();
            switch (HTTPErrorCollection.Instance[error].HTTPStatusCode)
            {
                case 401:
                    headers.Add("HTTP/1.1 401 Unauthorized");
                    break;
                case 500:
                    headers.Add("HTTP/1.1 500 Internal Server Error");
                    break;
                case 501:
                    headers.Add("HTTP/1.1 501 Not Implemented");
                    break;
                default:
                    headers.Add("HTTP/1.1 400 Bad Request");
                    break;
            }

            // Add "WWW-Authenticate" header
            //
            // RFC 6749, section 5.2.:
            // "If the client attempted to authenticate via the 'Authorization'
            // request header field, the authorization server MUST
            // respond with an HTTP 401 (Unauthorized) status code and
            // include the "WWW-Authenticate" response header field
            // matching the authentication scheme used by the client.
            if (error == HTTPErrorType.invalid_client)
            {
                string authScheme = null;
                var request = new Request();
                if (request.Server("AUTH_USER") != null)
                    authScheme = "Basic";
                else
                {
                    var authHeader = request.Header("Authorization");
                    if (authHeader != null)
                    {
                        if (authHeader.IndexOf("Bearer", StringComparison.Ordinal) == 0)
                            authScheme = "Bearer";
                        else if (authHeader.IndexOf("Basic", StringComparison.Ordinal) == 0)
                            authScheme = "Basic";
                    }
                }

                if (authScheme != null)
                    headers.Add(string.Format("WWW-Authenticate: {0} realm=\"\"", authScheme));
            }

            return headers;
        }
Ejemplo n.º 3
0
 /// <summary> Gets the <see cref="HTTPError" /> with the specified type </summary>
 /// <param name="type">The type to search</param>
 /// <returns>HTTPError based on type </returns>
 public HTTPError this[HTTPErrorType type]
 {
     get { return this.errors.Single(e => e.Type.Equals(type)); }
 }