Ejemplo n.º 1
0
        public async Task <ActionResult <string> > GetAsync(TokenCredential credential)
        {
            var request = new AdminInitiateAuthRequest
            {
                ClientId   = _options.UserPoolClientId,
                UserPoolId = _options.UserPoolId,
                AuthFlow   = AuthFlowType.ADMIN_NO_SRP_AUTH,
            };

            // For ADMIN_NO_SRP_AUTH: USERNAME (required), SECRET_HASH (if app client is configured
            // with client secret), PASSWORD (required)
            request.AuthParameters.Add("USERNAME", credential.Email);
            request.AuthParameters.Add("PASSWORD", credential.Password);
            request.AuthParameters.Add("SECRET_HASH", _clientSecret.ComputeHash(credential.Email));

            string accessToken = string.Empty;

            try
            {
                var response = await _identityProvider.AdminInitiateAuthAsync(request);

                accessToken = response.AuthenticationResult.AccessToken;
            }
            catch (UserNotFoundException)
            {
                ModelState.AddModelError("UserNotFound", $"A user having email '{credential.Email}' does not exist.");
                return(BadRequest(ModelState));
            }

            return(accessToken);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SignupAsync(Signup signup)
        {
            var request = new SignUpRequest
            {
                ClientId   = _options.UserPoolClientId,
                Password   = signup.Password,
                SecretHash = _clientSecret.ComputeHash(signup.Email),
                Username   = signup.Email
            };

            try
            {
                await _identityProvider.SignUpAsync(request);
            }
            catch (UsernameExistsException)
            {
                ModelState.AddModelError("UsernameExists", $"A user having the email '{signup.Email}' already exists.");
                return(BadRequest(ModelState));
            }
            catch (InvalidParameterException e)
            {
                var key = e.Message.ToLower().Contains("username") ? "InvalidUsername" : "InvalidPassword";
                ModelState.AddModelError(key, e.Message);
                return(BadRequest(ModelState));
            }
            catch (InvalidPasswordException e)
            {
                ModelState.AddModelError("InvalidPassword", e.Message);
                return(BadRequest(ModelState));
            }

            return(Ok());
        }
Ejemplo n.º 3
0
        public async Task<IActionResult> SignupAsync(Signup signup)
        {
            if (signup is null)
                throw new ArgumentNullException(nameof(signup));

            var request = new SignUpRequest
            {
                ClientId = _options.UserPoolClientId,
                Password = signup.Password,
                SecretHash = _clientSecret.ComputeHash(signup.Email),
                UserAttributes = new List<AttributeType>
                {
                    new AttributeType { Name = "email", Value = signup.Email}
                },
                Username = signup.Email
            };

            try
            {
                await _identityProvider.SignUpAsync(request).ConfigureAwait(true);
            }
            catch (UsernameExistsException)
            {
                ModelState.AddModelError("UsernameExists", $"A user having the email '{signup.Email}' already exists.");
                return BadRequest(ModelState);
            }
            catch(InvalidParameterException e)
            {
                var key = e.Message.ToLower().Contains("username") ? "InvalidUsername" : "InvalidPassword";
                ModelState.AddModelError(key, e.Message);
                return BadRequest(ModelState);
            }
            catch(InvalidPasswordException e)
            {
                ModelState.AddModelError("InvalidPassword", e.Message);
                return BadRequest(ModelState);
            }

            return Ok();
        }