예제 #1
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);
                }
            }
        }
예제 #2
0
        public Task ExecuteAsync(AuthenticationGrantTypeFacebook authenticationGrantTypeGoogle,
                                 ChangeAuthenticationGrantTypeFacebookDdto changeAuthenticationGrantTypeFacebookDdto)
        {
            _validator.ValidateAndThrow(changeAuthenticationGrantTypeFacebookDdto);

            authenticationGrantTypeGoogle.Change(changeAuthenticationGrantTypeFacebookDdto);

            return(Task.CompletedTask);
        }
 private static FacebookAdto CreateFacebookAdto(AuthenticationGrantTypeFacebook authenticationGrantTypeFacebook)
 {
     return(new FacebookAdto
     {
         Id = authenticationGrantTypeFacebook.Id,
         Name = authenticationGrantTypeFacebook.Name,
         ClientId = authenticationGrantTypeFacebook.MaskedClientId,
         ClientSecret = authenticationGrantTypeFacebook.MaskedClientSecret,
         ClientGrantAccessTokenUrl = authenticationGrantTypeFacebook.ClientGrantAccessTokenUrl,
         GrantAccessTokenUrl = authenticationGrantTypeFacebook.GrantAccessTokenUrl,
         ValidateAccessTokenUrl = authenticationGrantTypeFacebook.ValidateAccessTokenUrl,
         AppAccessToken = authenticationGrantTypeFacebook.MaskedAppAccessToken,
         Version = ConcurrencyVersionFactory.CreateFromEntity(authenticationGrantTypeFacebook)
     });
 }
        public async Task <FacebookAdto> CreateAsync(CreateFacebookAdto createFacebookAdto)
        {
            using (ITransaction transaction = _transactionManager.Create())
            {
                try
                {
                    AuthenticationGrantTypeFacebook authenticationGrantTypeFacebook =
                        await _createAuthenticationGrantTypeFacebookCommand.ExecuteAsync(
                            _mapper.Map <CreateFacebookAdto, CreateAuthenticationGrantTypeFacebookDdto>(createFacebookAdto));

                    await _commandRepository.AddAsync(authenticationGrantTypeFacebook);

                    transaction.Commit();

                    return(CreateFacebookAdto(authenticationGrantTypeFacebook));
                }
                catch (DomainValidationRuleException e)
                {
                    throw new BusinessValidationRuleApplicationException(e.ValidationResult);
                }
            }
        }
예제 #5
0
        public async Task <IClientCredentialAuthenticationResult> Validate(
            AuthenticationGrantTypeFacebook authenticationGrantTypeFacebook,
            ValidateClientCredentialAdto validateClientCredentialAdto)
        {
            try
            {
                FacebookAccessTokenResponse appAccessTokenResponse = await _httpJson.GetAsync <FacebookAccessTokenResponse>(
                    new Uri(authenticationGrantTypeFacebook.GrantAccessTokenUrl.Format(
                                DictionaryBuilder <string, object> .Create()
                                .Add("clientId", authenticationGrantTypeFacebook.ClientId)
                                .Add("clientSecret", authenticationGrantTypeFacebook.ClientSecret)
                                .Add("redirectUri", validateClientCredentialAdto.RedirectUri)
                                .Add("code", validateClientCredentialAdto.Token)
                                .Build()
                                )), _jsonSerializerSettings
                    );

                ValidateAccessTokenResponse validateAccessTokenResponse = await _httpJson.GetAsync <ValidateAccessTokenResponse>(new Uri(
                                                                                                                                     authenticationGrantTypeFacebook.ValidateAccessTokenUrl.Format(
                                                                                                                                         DictionaryBuilder <string, object> .Create()
                                                                                                                                         .Add("inputToken", appAccessTokenResponse.AccessToken)
                                                                                                                                         .Add("accessToken", authenticationGrantTypeFacebook.AppAccessToken)
                                                                                                                                         .Build()
                                                                                                                                         )), _jsonSerializerSettings);

                return(validateAccessTokenResponse.Data.IsValid ? ClientCredentialAuthenticationResult.Succeed(validateAccessTokenResponse.Data.UserId) : ClientCredentialAuthenticationResult.Fail);
            }
            catch (BadRequestException)
            {
                return(ClientCredentialAuthenticationResult.Fail);
            }
            catch (ServiceUnavailableExcpetion)
            {
                return(ClientCredentialAuthenticationResult.Fail);
            }
        }
예제 #6
0
        public Task <AuthenticationGrantTypeFacebook> ExecuteAsync(CreateAuthenticationGrantTypeFacebookDdto createAuthenticationGrantTypeFacebookDdto)
        {
            _validator.ValidateAndThrow(createAuthenticationGrantTypeFacebookDdto);

            return(Task.FromResult(AuthenticationGrantTypeFacebook.Create(createAuthenticationGrantTypeFacebookDdto)));
        }