Esempio n. 1
0
        public async Task <IActionResult> Post([FromForm] TokenRequest tokenRequest)
        {
            var trp = new TokenRequestProcessor(tokenRequest);

            if (trp.CheckState() is ErrorTokenResponse badState)
            {
                return(BadRequest(badState));
            }
            if (await trp.LoadAndCheckLoginRequest(_loginRegistry, _refreshTokenRegistry) is ErrorTokenResponse badRequest)
            {
                return(BadRequest(badRequest));
            }

            var refreshToken = RefreshToken.Generate(_options.RefreshTokenLifeTimeDays);

            await trp.RegisterRefreshToken(refreshToken, _refreshTokenRegistry);

            var succResp = trp.CreateSuccessResponse(
                refreshToken,
                _options.Secret,
                _options.Issuer,
                _options.AccessTokenLifeTimeMin);


            return(Ok(succResp));
        }
Esempio n. 2
0
        private async Task <AccessTokenModel> GenerateToken(
            string login,
            SigningConfiguration signingConfigurations,
            TokenConfiguration tokenConfigurations,
            IDistributedCache cache)
        {
            var account = await _accountService.GetAccountInformation(login);

            ClaimsIdentity identity = new ClaimsIdentity(
                new GenericIdentity(login, "Login"),
                new[] {
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
                new Claim("user", login),
                new Claim(ClaimTypes.Role, account.group_id.ToString())
            }
                );

            DateTime creationDate   = DateTime.Now;
            DateTime expirationDate = creationDate +
                                      TimeSpan.FromSeconds(tokenConfigurations.TokenExpiration);
            TimeSpan finalExpiration =
                TimeSpan.FromSeconds(tokenConfigurations.RefreshTokenExpiration);

            var handler       = new JwtSecurityTokenHandler();
            var securityToken = handler.CreateToken(new SecurityTokenDescriptor
            {
                Issuer             = tokenConfigurations.Issuer,
                Audience           = tokenConfigurations.Audience,
                SigningCredentials = signingConfigurations.SigningCredentials,
                Subject            = identity,
                NotBefore          = creationDate,
                Expires            = expirationDate
            });
            var token = handler.WriteToken(securityToken);

            var result = new AccessTokenModel()
            {
                Authenticated = true,
                Created       = creationDate.ToString("yyyy-MM-dd HH:mm:ss"),
                Expiration    = expirationDate.ToString("yyyy-MM-dd HH:mm:ss"),
                AccessToken   = token,
                RefreshToken  = RefreshToken.Generate(),
                Message       = "OK"
            };
            var refreshTokenData = new RefreshTokenModel();

            refreshTokenData.RefreshToken = result.RefreshToken;
            refreshTokenData.Login        = login;

            DistributedCacheEntryOptions cacheOptions =
                new DistributedCacheEntryOptions();

            cacheOptions.SetAbsoluteExpiration(finalExpiration);
            await cache.SetStringAsync(result.RefreshToken,
                                       JsonConvert.SerializeObject(refreshTokenData),
                                       cacheOptions);

            return(result);
        }
Esempio n. 3
0
        public void ShouldGenerateRefreshToken()
        {
            //Arrange

            //Act
            var oneDayRefreshToken = RefreshToken.Generate(1);

            _output.WriteLine(oneDayRefreshToken.Body);

            //Assert
        }