示例#1
0
        public async Task <JwtAdto> PasswordAsync(PasswordAdto passwordAdto)
        {
            using (ITransaction transaction = _transactionManager.Create())
            {
                try
                {
                    if (!(await _authenticationServiceCommandRepository.GetSingleAsync(s => s is AuthenticationGrantTypePassword) is AuthenticationGrantTypePassword))
                    {
                        throw new BusinessApplicationException(ExceptionType.BadRequest, ErrorCodes.PasswordLoginNotConfigured, "Password logins are not configured");
                    }

                    Identity identity = await _passwordLoginCommand.ExecuteAsync(new PasswordLoginCommandDdto
                    {
                        Identifier = passwordAdto.Identifier,
                        Password   = passwordAdto.Password
                    });

                    await _identityCommandRepository.UpdateAsync(identity);

                    JwtAdto jwtAdto = await _jwtFactory.GenerateJwtAsync <JwtAdto>(GetClaimsIdentity(identity), identity.Session.Id);

                    transaction.Commit();

                    return(jwtAdto);
                }
                catch (InvalidLoginDomainException)
                {
                    throw new BusinessApplicationException(ExceptionType.BadRequest, ErrorCodes.InvalidLogin, "Your login details are incorrect");
                }
                catch (DomainValidationRuleException e)
                {
                    throw new BusinessValidationRuleApplicationException(e.ValidationResult);
                }
            }
        }
示例#2
0
        public async Task <JwtAdto> FacebookAsync(ClientCredentialAdto clientCredentialAdto)
        {
            using (ITransaction transaction = _transactionManager.Create())
            {
                try
                {
                    AuthenticationGrantTypeFacebook authenticationGrantTypeFacebook =
                        (AuthenticationGrantTypeFacebook)await _authenticationServiceCommandRepository.GetSingleAsync(
                            s => s is AuthenticationGrantTypeFacebook && s.Id == clientCredentialAdto.Id);

                    if (authenticationGrantTypeFacebook == null)
                    {
                        throw new BusinessApplicationException(ExceptionType.BadRequest, ErrorCodes.ClientCredentialLoginNotConfigured, "Client credential login not configured");
                    }

                    IClientCredentialAuthenticationResult clientCredentialAuthenticationResult = await _facebookAuthenticationValidator.Validate(authenticationGrantTypeFacebook, new ValidateClientCredentialAdto
                    {
                        Token       = clientCredentialAdto.Token,
                        RedirectUri = clientCredentialAdto.RedirectUri
                    });

                    if (!clientCredentialAuthenticationResult.Success)
                    {
                        throw new BusinessApplicationException(ExceptionType.Unauthorized, "Could not validate access token");
                    }

                    Identity identity = await _getIdentityByClientCredentialIdentifierQuery.RunAsync(authenticationGrantTypeFacebook, clientCredentialAuthenticationResult.Identifier);

                    if (identity == null)
                    {
                        identity = await _createIdentityCommand.ExecuteAsync();

                        await _registerClientCredentialCommand.ExecuteAsync(identity, authenticationGrantTypeFacebook, new RegisterClientCredentialCommandDdto
                        {
                            Identifier = clientCredentialAuthenticationResult.Identifier
                        });

                        await _identityCommandRepository.AddAsync(identity);
                    }

                    await _clientCredentialLoginCommand.ExecuteAsync(identity);

                    await _identityCommandRepository.UpdateAsync(identity);

                    JwtAdto jwtAdto = await _jwtFactory.GenerateJwtAsync <JwtAdto>(GetClaimsIdentity(identity), identity.Session.Id);

                    transaction.Commit();

                    return(jwtAdto);
                }
                catch (InvalidLoginDomainException)
                {
                    throw new BusinessApplicationException(ExceptionType.Unauthorized, "Your login details are incorrect");
                }
                catch (DomainValidationRuleException e)
                {
                    throw new BusinessValidationRuleApplicationException(e.ValidationResult);
                }
            }
        }
示例#3
0
        public async Task <JwtAdto> RefreshTokenAsync(RefreshTokenAdto refreshTokenAdto)
        {
            using (ITransaction transaction = _transactionManager.Create())
            {
                try
                {
                    if (!(await _authenticationServiceCommandRepository.GetSingleAsync(s => s is AuthenticationGrantTypeRefreshToken) is AuthenticationGrantTypeRefreshToken))
                    {
                        throw new BusinessApplicationException(ExceptionType.BadRequest, ErrorCodes.RefreshTokenLoginNotConfigured, "Refresh token logins are not configured");
                    }

                    Identity identity = await _refreshTokenLoginCommand.ExecuteAsync(new RefreshTokenLoginCommandDdto
                    {
                        SessionId    = refreshTokenAdto.SessionId,
                        RefreshToken = refreshTokenAdto.Token
                    });

                    await _identityCommandRepository.UpdateAsync(identity);

                    JwtAdto jwtAdto = await _jwtFactory.GenerateJwtAsync <JwtAdto>(GetClaimsIdentity(identity), identity.Session.Id);

                    transaction.Commit();

                    return(jwtAdto);
                }
                catch (InvalidLoginDomainException)
                {
                    throw new BusinessApplicationException(ExceptionType.Unauthorized, "Your login details are incorrect");
                }
                catch (DomainValidationRuleException e)
                {
                    throw new BusinessValidationRuleApplicationException(e.ValidationResult);
                }
            }
        }
        public async Task <IActionResult> RefreshToken(RefreshTokenTemplate refreshTokenTemplate)
        {
            JwtAdto jwt = await _authenticationApplicationService.RefreshTokenAsync(new RefreshTokenAdto
            {
                SessionId = refreshTokenTemplate.SessionId,
                Token     = refreshTokenTemplate.Token
            });

            return(Ok(_resourceBuilder.Build(new SessionResource
            {
                Id = jwt.SessionId,
                AuthToken = jwt.AuthToken,
                ExpiresIn = jwt.ExpiresIn
            })));
        }
        public async Task <IActionResult> Password(PasswordTemplate passwordTemplate)
        {
            JwtAdto jwt = await _authenticationApplicationService.PasswordAsync(new PasswordAdto
            {
                Identifier = passwordTemplate.Identifier,
                Password   = passwordTemplate.Password
            });

            return(Ok(_resourceBuilder.Build(new SessionResource
            {
                Id = jwt.SessionId,
                AuthToken = jwt.AuthToken,
                ExpiresIn = jwt.ExpiresIn
            })));
        }
        public async Task <IActionResult> Google(Guid id, AuthenticateGoogleTemplate template)
        {
            JwtAdto jwt = await _authenticationApplicationService.GoogleAsync(new ClientCredentialAdto
            {
                Id          = id,
                Token       = template.Token,
                RedirectUri = template.RedirectUri
            });

            return(Ok(_resourceBuilder.Build(new SessionResource
            {
                Id = jwt.SessionId,
                AuthToken = jwt.AuthToken,
                ExpiresIn = jwt.ExpiresIn
            })));
        }