public IActionResult ShowLoginPage()
        {
            var responseTypes = Request.Query["response_type"];
            var clientIds     = Request.Query["client_id"];
            var redirectUris  = Request.Query["redirect_uri"];

            if (responseTypes.Count != 1 || clientIds.Count != 1 || redirectUris.Count != 1)
            {
                var error = new JsonResult(new ErrorResponse
                {
                    Error = ErrorTypeEnum.InvalidRequest
                })
                {
                    StatusCode = (int)HttpStatusCode.Unauthorized
                };
                return(error);
            }

            var clientId = clientIds[0];

            if (!_clientManager.IsValidClient(clientId))
            {
                var error = new JsonResult(new ErrorResponse
                {
                    Error = ErrorTypeEnum.InvalidClient
                })
                {
                    StatusCode = (int)HttpStatusCode.Unauthorized
                };
                return(error);
            }

            var responseType = responseTypes[0];

            if (responseType != "token" || responseType == "implicit")
            {
                return(_flowResponses.InvalidGrant());
            }

            // todo Think about moving such logic into its own scope. Maybe into the enum? Also, think about separating these to ResponseType and GrantType enums... Might be useful.
            var parsedGrantType = responseType switch
            {
                "token" => GrantType.Implicit,
                "code" => GrantType.AuthorizationCode
            };

            if (!_clientGrantManager.ClientHasGrantType(clientId, parsedGrantType))
            {
                return(_flowResponses.InvalidGrant());
            }

            ViewData["RedirectUri"]  = redirectUris[0];
            ViewData["ResponseType"] = responseTypes[0];
            ViewData["ClientId"]     = clientId;

            return(View("AuthorizationLogin"));
        }
Exemple #2
0
        public IActionResult ProcessFlow(HttpRequest request)
        {
            var(clientSecret, validCredentials) = ExtractAndValidateClientCredentials(request);

            if (!validCredentials)
            {
                return(_flowResponses.InvalidClient());
            }

            if (!request.Form.ContainsKey("username") ||
                !request.Form.ContainsKey("password") ||
                string.IsNullOrWhiteSpace(request.Form["username"]) ||
                string.IsNullOrWhiteSpace(request.Form["password"]))
            {
                return(_flowResponses.InvalidRequest());
            }

            var username = request.Form["username"];
            var password = request.Form["password"];

            if (!_userCredentialValidator.ValidateCredentials(username, password))
            {
                return(_flowResponses.InvalidGrant());
            }

            var success = new JsonResult(new AccessTokenResponse
            {
                AccessToken = _jwtGenerator.GenerateToken(clientSecret),
                ExpiresIn   = (int)TimeSpan.FromMinutes(10).TotalSeconds,
                TokenType   = "Bearer"
            })
            {
                StatusCode = (int)HttpStatusCode.OK
            };

            return(success);
        }