Ejemplo n.º 1
0
        public async Task <Token> GetTokenAsync(Credentials credentials)
        {
            if (string.IsNullOrEmpty(credentials.Email) || string.IsNullOrEmpty(credentials.Password))
            {
                return(null);
            }

            var userVerification = await authenticationService.VerifyUserAsync(credentials);

            if (userVerification.IsVerified)
            {
                var identity = jwtFactory.GenerateClaimsIdentity(credentials.Email, userVerification.Id);

                var token = new Token
                {
                    Id        = identity.Claims.Single(c => c.Type == "id").Value,
                    AuthToken = await jwtFactory.GenerateEncodedTokenAsync(credentials.Email, identity),
                    ExpiresIn = (int)jwtOptions.ValidFor.TotalSeconds
                };

                return(token);
            }

            return(null);
        }
Ejemplo n.º 2
0
        public async Task <JwtToken> GenerateJwtAsync(ClaimsIdentity identity, string userName)
        {
            var Id        = new Guid(identity.Claims.Single(c => c.Type == "id").Value);
            var AuthToken = await _jwtFactory.GenerateEncodedTokenAsync(userName, identity);

            var ExpiresIn = (int)_jwtOptions.ValidFor.TotalSeconds;

            return(new JwtToken(Id, AuthToken, ExpiresIn));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <LoginResult> > Login(LoginRequest request)
        {
            // Verify that the user & pass are on db and match
            var result = await mediator.Send(request);

            // Create an identity for the user
            var identity = jwtFactory.GenerateClaimsIdentity(request.Email, result.Id, result.Claims);

            // With the identity generate a jwt and a refresh token
            var accessToken = await jwtFactory.GenerateEncodedTokenAsync(request.Email, identity);

            var refreshToken = jwtFactory.GenerateRefreshToken();

            // Save tokens into db
            await mediator.Send(
                new InitSessionRequest(result.Id, accessToken, refreshToken, jwtOptions.Value.RefreshTokenValidFor));

            // Reply with both tokens
            return(Ok(new LoginResult(accessToken, refreshToken)));
        }