Exemplo n.º 1
0
        public JwtSigningOptions(IJwtOptions jwtOptions)
        {
            byte[] key = Encoding.UTF8.GetBytes(jwtOptions.SecretKey);
            SymmetricSecurityKey securityKey = new SymmetricSecurityKey(key);

            SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Generates tokens for users
 /// </summary>
 /// <param name="logger">A logger</param>
 /// <param name="context">The DB</param>
 /// <param name="options">JWT config options</param>
 public TokenGenerator(ILogger <TokenGenerator> logger, Db context, IJwtOptions options)
 {
     this._tokenHandler = new JwtSecurityTokenHandler();
     this._logger       = logger;
     this._options      = options;
     this._context      = context;
 }
Exemplo n.º 3
0
 public GenerateWebTokenHandler(IJwtSigningOptions jwtSigningOptions, IJwtOptions jwtOptions, IRefreshTokenRepository refreshTokenRepository)
 {
     _jwtSigningOptions       = jwtSigningOptions;
     _jwtOptions              = jwtOptions;
     _jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
     _refreshTokenRepository  = refreshTokenRepository;
 }
Exemplo n.º 4
0
 public JwtService(
     IJwtOptions jwtOptions,
     UserManager <LocalistUser?> userManager)
 {
     this.jwtOptions  = jwtOptions;
     this.userManager = userManager;
 }
Exemplo n.º 5
0
        public JwtService(IJwtOptions jwtOptions)
        {
            _jwtOptions = jwtOptions;
            var issuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.SecretKey));

            _signingCredentials = new SigningCredentials(issuerSigningKey, SecurityAlgorithms.HmacSha256);
        }
Exemplo n.º 6
0
 public AuthService(IJwtHelper jwtHelper,
                    IDnsWatcherDbContext context,
                    IRandomGenerator randomGenerator,
                    IDateTime dateTime,
                    IJwtOptions jwtOptions)
 {
     _jwtHelper       = jwtHelper;
     _context         = context;
     _randomGenerator = randomGenerator;
     _dateTime        = dateTime;
     _jwtOptions      = jwtOptions;
 }
Exemplo n.º 7
0
 public static void ConfigureJwtAuth(this IServiceCollection services, IJwtOptions jwtOptions)
 {
     services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
     .AddJwtBearer(options =>
                   options.TokenValidationParameters = new TokenValidationParameters()
     {
         ValidateIssuer           = true,
         ValidIssuer              = jwtOptions.Issuer,
         ValidateAudience         = true,
         ValidAudience            = jwtOptions.Audience,
         IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SecretKey)),
         ValidateIssuerSigningKey = true
     });
 }
Exemplo n.º 8
0
        private static JwtSecurityToken GenerateToken(IJwtOptions jwtOptions, IEnumerable <Claim> claims)
        {
            if (jwtOptions == null)
            {
                throw new ArgumentNullException(nameof(jwtOptions));
            }

            var now = DateTime.UtcNow;

            return(new JwtSecurityToken(
                       jwtOptions.Issuer,
                       jwtOptions.Audience,
                       notBefore: now,
                       claims: claims,
                       expires: now.Add(TimeSpan.FromMinutes(jwtOptions.Lifetime)),
                       signingCredentials: new SigningCredentials(jwtOptions.GetSymmetricSecurityKey(),
                                                                  SecurityAlgorithms.HmacSha256)));
        }
Exemplo n.º 9
0
        private static void ThrowIfInvalidOptions(IJwtOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.ValidFor <= TimeSpan.Zero)
            {
                throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(JwtIssuerOptions.ValidFor));
            }

            if (options.SigningCredentials == null)
            {
                throw new ArgumentNullException(nameof(JwtIssuerOptions.SigningCredentials));
            }

            if (options.JtiGenerator == null)
            {
                throw new ArgumentNullException(nameof(JwtIssuerOptions.JtiGenerator));
            }
        }
Exemplo n.º 10
0
        private void ConfigureAppSettings(IServiceCollection services)
        {
            void ConfigureSection <Interface, Implementation>(string sectionName)
                where Implementation : Interface, new() where Interface : class
            {
                Implementation configSection = new Implementation();

                Configuration.GetSection(sectionName).Bind(configSection);
                services.AddSingleton <Interface>(configSection);
            }

            ConfigureSection <IJwtOptions, JwtOptions>(nameof(JwtOptions));
            ConfigureSection <IConnectionStrings, ConnectionStrings>(nameof(ConnectionStrings));
            ConfigureSection <IMongodbDatabaseSettings, MongodbDatabaseSettings>(nameof(MongodbDatabaseSettings));

            services.AddSingleton <IAppSettings>(factory =>
            {
                IJwtOptions jwtOptionsSection = factory.GetService <IJwtOptions>();
                IConnectionStrings connectionStringsSection      = factory.GetService <IConnectionStrings>();
                IMongodbDatabaseSettings mongodbDatabaseSettings = factory.GetService <IMongodbDatabaseSettings>();

                return(new AppSettings(jwtOptionsSection, connectionStringsSection, mongodbDatabaseSettings));
            });
        }
Exemplo n.º 11
0
 public JwtHelper(IJwtOptions jwtOptions, IDateTime dateTime)
 {
     _jwtOptions = jwtOptions;
     _dateTime   = dateTime;
 }
Exemplo n.º 12
0
 public JwtAccessTokenGenerator(IJwtOptions jwtOptions)
 {
     _jwtOptions = jwtOptions;
 }
Exemplo n.º 13
0
 public AuthenticationService(IJwtOptions jwtOptions)
 {
     _jwtOptions = jwtOptions;
 }
Exemplo n.º 14
0
 public AppSettings(IJwtOptions jwtOptions, IConnectionStrings connectionStrings, IMongodbDatabaseSettings mongodbDatabaseSettings)
 {
     JwtOptions              = jwtOptions;
     ConnectionStrings       = connectionStrings;
     MongodbDatabaseSettings = mongodbDatabaseSettings;
 }