예제 #1
0
 public AuthController(Db db, GoogleOAuthOptions oAuthOptions, JwtOptions jwtOptions,
                       IHttpClientFactory httpClientFactory)
 {
     _db           = db;
     _oAuthOptions = oAuthOptions;
     _jwtOptions   = jwtOptions;
     _httpClient   = httpClientFactory.CreateClient();
 }
예제 #2
0
 public GoogleLoginCommandHandler(DatabaseContext db, GoogleOAuthOptions oAuthOptions,
                                  IHttpClientFactory clientFactory, AuthService authService)
 {
     _db           = db;
     _oAuthOptions = oAuthOptions;
     _authService  = authService;
     _httpClient   = clientFactory.CreateClient();
 }
예제 #3
0
        public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, IConfiguration configuration)
        {
            var jwtOptions = new JwtOptions();

            configuration.GetSection(nameof(JwtOptions)).Bind(jwtOptions);
            services.AddSingleton(jwtOptions);

            var oAuthOptions = new GoogleOAuthOptions();

            configuration.GetSection(nameof(GoogleOAuthOptions)).Bind(oAuthOptions);
            services.AddSingleton(oAuthOptions);

            var tokenValidationParams = new TokenValidationParameters
            {
                IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.Secret)),
                ValidateIssuerSigningKey = true,
                ValidIssuer      = jwtOptions.Issuer,
                ValidateIssuer   = true,
                ValidateAudience = false,
                ValidateLifetime = true,
                ClockSkew        = TimeSpan.Zero
            };

            services.AddSingleton(tokenValidationParams);

            services.AddAuthentication(o =>
            {
                o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(o =>
            {
                o.TokenValidationParameters = tokenValidationParams;

                o.Events = new JwtBearerEvents
                {
                    OnMessageReceived = context =>
                    {
                        var accessToken = context.Request.Query["access_token"];
                        var path        = context.HttpContext.Request.Path;

                        if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments(ChatHub.ApiPath))
                        {
                            context.Token = accessToken;
                        }

                        return(Task.CompletedTask);
                    }
                };

                o.SaveToken = true;
            });

            services.AddScoped <AuthService>();

            return(services);
        }