Exemplo n.º 1
0
        public async Task <ActionResult> Authorize(GetAuthorizeRequest request, CancellationToken ct)
        {
            var client = await _ioAuthClientsService.GetByClientIdAsync(request.client_id, ct);

            if (!client.IsValid())
            {
                return(BadRequest("Client not found"));
            }

            if (!client.IsMatchRedirectUri(request))
            {
                return(BadRequest("Invalid redirect uri"));
            }

            if (!client.IsScopesInclude(request.scope))
            {
                return(BadRequest("Invalid scopes"));
            }

            var model = new AuthorizeViewModel(
                request.client_id,
                request.response_type,
                request.scope,
                request.redirect_uri,
                request.state,
                request.IsInvalidCredentials);

            return(View("~/OAuth/Views/Authorize.cshtml", model));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Authorize([FromForm] PostAuthorizeRequest request, CancellationToken ct)
        {
            var client = await _ioAuthClientsService.GetByClientIdAsync(request.client_id, ct);

            if (!client.IsValid())
            {
                return(BadRequest("Client not found"));
            }

            if (!client.IsMatchRedirectUri(request))
            {
                return(BadRequest("Invalid redirect uri"));
            }

            if (!client.IsScopesInclude(request.scope))
            {
                return(BadRequest("Invalid scopes"));
            }

            var response = await _oauthService.AuthorizeAsync(
                request.Country,
                request.Login,
                request.Password,
                request.response_type,
                request.redirect_uri,
                request.state,
                IpAddress,
                UserAgent,
                request.scope.ToScopeList(),
                client.Audience,
                ct);

            if (response.IsInvalidCredentials)
            {
                var newRequest = new GetAuthorizeRequest
                {
                    client_id            = request.client_id,
                    response_type        = request.response_type,
                    scope                = request.scope,
                    state                = request.state,
                    redirect_uri         = request.redirect_uri,
                    IsInvalidCredentials = true
                };

                return(RedirectToAction("Authorize", newRequest));
            }

            return(Redirect(response.CallbackUri));
        }
Exemplo n.º 3
0
 public static bool IsMatchRedirectUri(this OAuthClient oAuthClient, GetAuthorizeRequest request)
 {
     return(IsMatchRedirectUri(oAuthClient, request.redirect_uri));
 }
Exemplo n.º 4
0
        public async Task <ActionResult> Register([FromForm] PostRegisterRequest request, CancellationToken ct)
        {
            var client = await _ioAuthClientsService.GetByClientIdAsync(request.client_id, ct);

            if (!client.IsValid())
            {
                return(BadRequest("Client not found"));
            }

            if (!client.IsMatchRedirectUri(request))
            {
                return(BadRequest("Invalid redirect uri"));
            }

            if (!client.IsScopesInclude(request.scope))
            {
                return(BadRequest("Invalid scopes"));
            }

            if (request.Password != request.PasswordConfirmation)
            {
                var newRegisterRequest = new GetRegisterRequest
                {
                    client_id           = request.client_id,
                    response_type       = request.response_type,
                    scope               = request.scope,
                    state               = request.state,
                    redirect_uri        = request.redirect_uri,
                    Surname             = request.Surname,
                    Name                = request.Name,
                    Login               = request.Login,
                    Email               = request.Email,
                    Phone               = request.Phone,
                    IsPasswordsNotEqual = true
                };

                return(RedirectToAction("Register", newRegisterRequest));
            }

            var isLoginExists = await _identityStatusService.IsLoginExistsAsync(request.Login, ct);

            var isEmailExists = await _identityStatusService.IsEmailExistsAsync(request.Email, ct);

            var isPhoneExists = await _identityStatusService.IsPhoneExistsAsync(request.Phone, ct);

            if (isLoginExists || isEmailExists || isPhoneExists)
            {
                var newRegisterRequest = new GetRegisterRequest
                {
                    client_id     = request.client_id,
                    response_type = request.response_type,
                    scope         = request.scope,
                    state         = request.state,
                    redirect_uri  = request.redirect_uri,
                    Surname       = request.Surname,
                    Name          = request.Name,
                    Login         = request.Login,
                    Email         = request.Email,
                    Phone         = request.Phone,
                    IsLoginExists = isLoginExists,
                    IsEmailExists = isEmailExists,
                    IsPhoneExists = isPhoneExists
                };

                return(RedirectToAction("Register", newRegisterRequest));
            }

            var phoneIdentityTokenId = await _registrationService.RegisterAsync(
                request.Country,
                request.Surname,
                request.Name,
                request.Login,
                request.Email,
                request.Phone,
                request.Password,
                IpAddress,
                UserAgent,
                ct);

            var authorizeResponse = await _oauthService.AuthorizeAsync(
                request.Country,
                request.Login,
                request.Password,
                request.response_type,
                request.redirect_uri,
                request.state,
                IpAddress,
                UserAgent,
                request.scope.ToScopeList(),
                client.Audience,
                ct);

            if (authorizeResponse.IsInvalidCredentials)
            {
                var newAuthorizeRequest = new GetAuthorizeRequest
                {
                    client_id            = request.client_id,
                    response_type        = request.response_type,
                    scope                = request.scope,
                    state                = request.state,
                    redirect_uri         = request.redirect_uri,
                    IsInvalidCredentials = true
                };

                return(RedirectToAction("Authorize", newAuthorizeRequest));
            }

            var getVerifyPhoneRequest = new GetVerifyPhoneRequest
            {
                TokenId       = phoneIdentityTokenId,
                CallbackUri   = authorizeResponse.CallbackUri,
                IsInvalidCode = false
            };

            if (_webHostEnvironment.IsDevelopment())
            {
                var phoneIdentityToken = await _identityTokensService.GetAsync(phoneIdentityTokenId, ct);

                getVerifyPhoneRequest.Code = phoneIdentityToken?.Value;
            }

            return(RedirectToAction("VerifyPhone", getVerifyPhoneRequest));
        }