Beispiel #1
0
        public async Task Invoke(HttpContext context, IOAuthHandler handler, IJsonObjectWriter objectWriter)
        {
            Ensure.IsNotNull(context, nameof(context));
            Ensure.IsNotNull(handler, nameof(handler));
            Ensure.IsNotNull(objectWriter, nameof(objectWriter));

            if (context.Request.Path == options.TokenEndpointPath)
            {
                if (!context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
                {
                    WriteResult(context, objectWriter, OAuthResult.InvalidRequest("Token request should use 'POST' method."));
                    return;
                }

                if (context.Request.ContentType == null)
                {
                    WriteResult(context, objectWriter, OAuthResult.InvalidRequest("Token request should specify Content-Type."));
                    return;
                }

                if (!context.Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase))
                {
                    WriteResult(context, objectWriter, OAuthResult.InvalidRequest("Token request should use 'application/x-www-form-urlencoded' Content-Type."));
                    return;
                }

                var form = await context.Request.ReadFormAsync();

                if (form == null)
                {
                    WriteResult(context, objectWriter, OAuthResult.InvalidRequest("Token request should use 'application/x-www-form-urlencoded' Content-Type."));
                    return;
                }

                foreach (var pair in form)
                {
                    if (pair.Value.Count > 1)
                    {
                        WriteResult(context, objectWriter, OAuthResult.InvalidRequest($"The '{pair.Key}' parameter is repeated."));
                        return;
                    }
                }

                var result = await handler.HandleTokenRequestAsync(new OAuthTokenRequest(form));

                if (result != null)
                {
                    WriteResult(context, objectWriter, result);
                }
                else
                {
                    WriteResult(context, objectWriter, OAuthResult.ServerError("Token request was unhandled."));
                }
            }
            else
            {
                await next(context);
            }
        }
Beispiel #2
0
        private static void WriteResult(HttpContext context, IJsonObjectWriter objectWriter, OAuthResult result)
        {
            var response = context.Response;

            response.StatusCode = result.StatusCode;

            if (result.Body != null)
            {
                response.ContentType = "application/json";

                objectWriter.WriteObject(response.Body, result.Body);
            }
        }