public async Task <Token> AuthenticationByPassword(String login, String password, CancellationToken cancellationToken)
        {
            var user = await _userGetter.Get(login, cancellationToken);

            if (user == null)
            {
                throw new UnauthorizedException();
            }

            if (!user.IsActive)
            {
                throw new UnconfirmedException();
            }

            if (!await _passwordHasher.VerifyHashedPassword(user.Password, password, cancellationToken))
            {
                throw new UnauthorizedException();
            }

            var refreshToken = await _refreshTokenStore.Add(user.Id);

            var accessToken = await _accessTokenFactory.Create(user, cancellationToken);

            return(new Token(
                       accessToken: accessToken.Value,
                       expiresIn: accessToken.ExpiresIn,
                       refreshToken: refreshToken));
        }
Beispiel #2
0
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var clientid = context.Ticket.Properties.Dictionary["as:client_id"];

            if (string.IsNullOrEmpty(clientid))
            {
                return;
            }

            var refreshTokenId       = Guid.NewGuid().ToString("n");
            var refreshTokenLifeTime = context.OwinContext.Get <string>("as:clientRefreshTokenLifeTime");

            var token = new RefreshToken()
            {
                Id         = refreshTokenId,
                ClientId   = clientid,
                Subject    = context.Ticket.Identity.Name,
                IssuedUtc  = DateTime.UtcNow,
                ExpiresUtc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime))
            };

            context.Ticket.Properties.IssuedUtc  = token.IssuedUtc;
            context.Ticket.Properties.ExpiresUtc = token.ExpiresUtc;

            token.ProtectedTicket = context.SerializeTicket();

            _refrshTokenStore.Add(token);
            context.SetToken(refreshTokenId);
        }
        public async Task <Token> AuthenticationByPassword(string email, string password,
                                                           CancellationToken cancellationToken)
        {
            var user = await _userRepository.FindByEmail(email, cancellationToken);

            if (user == null)
            {
                throw new UnauthorizedException();
            }

            if (user.EmailState == EmailState.Unconfirmed)
            {
                throw new UnconfirmedException();
            }

            if (!_passwordHasher.VerifyHashedPassword(user.PasswordHash, password))
            {
                throw new UnauthorizedException();
            }

            var refreshToken = await _refreshTokenStore.Add(user.Id, cancellationToken);

            var accessToken = await _accessTokenFactory.Create(user, cancellationToken);

            return(new Token(accessToken.Value, accessToken.ExpiresIn, refreshToken));
        }