Esempio n. 1
0
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IHostingEnvironment env,
                              Auth0Settings auth0Settings)
        {
            SetupLogger(env);

            if (env.IsDevelopment())
            {
                SetupForDevelopment(app, loggerFactory);
            }
            else
            {
                SetupForProduction(loggerFactory);
            }
            app.UseJwtAuthentication(auth0Settings);

            app.UseFileServer();
            // app.UseSignalR(routes => routes.MapHub<ListHub>("/listHub"));

            app.UseSwagger();
            app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });

            app.UseHttpException();
            SetupRoutes(app);
        }
Esempio n. 2
0
        public AuthorizedTestFixture(Auth0Settings auth0Settings = null, TestTokenService tokenService = null, TestHttpMessageHandler httpMessageHandler = null, IServiceCollection serviceCollection = null)
        {
            var auth0SettingsOrDefault      = auth0Settings ?? GetDefaultAuth0Settings();
            var tokenServiceOrDefault       = tokenService ?? GetDefaultTokenService(auth0SettingsOrDefault);
            var httpMessageHandlerOrDefault = httpMessageHandler ?? GetDefaultHttpMessageHandler();

            httpMessageHandlerOrDefault.ConfigureFakeAuth0Authority(auth0SettingsOrDefault, tokenServiceOrDefault);

            var webHostBuilder = new WebHostBuilder()
                                 .UseStartup <TStartup>()
                                 .ConfigureServices(services =>
            {
                services.Configure <Auth0Settings>(settings =>
                {
                    settings.Auth0ClientId = auth0SettingsOrDefault.Auth0ClientId;
                    settings.Auth0Domain   = auth0SettingsOrDefault.Auth0Domain;
                });
                services.AddSingleton <IHttpMessageHandlerAccessor>(provider => new HttpMessageHandlerAccessor(httpMessageHandlerOrDefault));

                if (serviceCollection != null)
                {
                    services.Add(serviceCollection);
                }
            });

            var server = new TestServer(webHostBuilder);

            HttpClient   = server.CreateClient();
            TokenService = tokenServiceOrDefault;
        }
        public TestTokenService(Auth0Settings appSettings)
        {
            _appSettings = appSettings;

            var signingInformation = SignatureHelper.GenerateSigningInformation();

            _signingCredentials             = signingInformation.Item1;
            _signatureValidationCertificate = signingInformation.Item2;
        }
Esempio n. 4
0
        public void Auth0Settings_SetData_DataSet()
        {
            var settings = new Auth0Settings
            {
                Audience  = "test audience",
                Authority = "test authority"
            };

            settings.Audience.Should().NotBeNull();
            settings.Authority.Should().NotBeNull();
        }
        public static IServiceCollection AddAuth0(
            this IServiceCollection services,
            Auth0Settings settings)
        {
            Guard.NotNull(services, nameof(services));

            var auth0 = new Auth0Configurator(settings);

            auth0.Apply(services);

            return(services);
        }
Esempio n. 6
0
 private static JwtBearerOptions CreateJwtBearerOptions(Auth0Settings auth0Settings) => new JwtBearerOptions
 {
     Audience  = auth0Settings.ClientId,
     Authority = $"https://{auth0Settings.Domain}",
     TokenValidationParameters =
     {
         IssuerSigningKey = auth0Settings.SigningKey
     },
     // TODO Handle other events, e.g. OnAuthenticationFailed and OnChallenge
     Events = new JwtBearerEvents {
         OnTokenValidated = OnTokenValidated
     }
 };
Esempio n. 7
0
        /// <summary>
        /// Loads the configuration file
        /// </summary>
        /// <returns></returns>
        public static bool Load()
        {
            logger.Info("Loading configuration");

            try {
                StreamReader   stream   = File.OpenText("config.json");
                JsonTextReader reader   = new JsonTextReader(stream);
                JObject        jsonRoot = (JObject)JToken.ReadFrom(reader);

                JObject auth0Root = (JObject)jsonRoot["auth0"];

                auth0Settings = new Auth0Settings()
                {
                    url        = (string)auth0Root["url"],
                    connection = (string)auth0Root["connection"],
                    client     = (string)auth0Root["clientId"],
                    secret     = (string)auth0Root["secret"]
                };

                dbFile = (string)jsonRoot["dbFile"];

                socketUrl  = (string)jsonRoot["socket"]["url"];
                socketPort = (short)jsonRoot["socket"]["port"];

                JObject sslRoot = (JObject)jsonRoot["socket"]["ssl"];
                if (sslRoot != null)
                {
                    hasSSLConfig    = true;
                    sslCertName     = (string)sslRoot["cert"];
                    sslCertPassword = (string)sslRoot["password"];
                }
                else
                {
                    hasSSLConfig = false;
                }

                JToken ud;
                if (jsonRoot.TryGetValue("updateDir", out ud))
                {
                    updateDir = (string)ud;
                }
            } catch (Exception e) {
                logger.Error("Fatal error loading configuration: " + e);
                return(false);
            }

            logger.Info("Done");

            return(true);
        }
Esempio n. 8
0
        public void ConfigureServices(IServiceCollection services)
        {
            var auth0Settings = new Auth0Settings();

            Configuration.GetSection(nameof(Auth0Settings)).Bind(auth0Settings);
            services.AddAuth0(auth0Settings);

            services.AddSingleton <IFileUploadHandler, FileUploadHandler>();
            services.AddSingleton <DocumentService, DocumentService>();
            services.AddSingleton <IFileRepository, AzureStorageFileRepository>();
            services.AddControllersWithViews();
            services.Configure <SearchConfigurationModel>(Configuration.GetSection("Search"));
            services.Configure <ConnectionConfigurationModel>(Configuration.GetSection("Connections"));
        }
Esempio n. 9
0
 public AccountController(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     IMemoryCache memoryCache,
     IEmailSender emailSender,
     IConfiguration configuration,
     ILogger <AccountController> logger,
     IOptions <Auth0Settings> auth0Settings) : base(configuration, memoryCache)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _emailSender   = emailSender;
     _logger        = logger;
     _auth0Settings = auth0Settings?.Value;
 }
Esempio n. 10
0
        public static void ConfigureFakeAuth0Authority(
            this TestHttpMessageHandler testHttpMessageHandler,
            Auth0Settings appSettings,
            TestTokenService tokenService)
        {
            var jwksUri = new Uri(new Uri(appSettings.Auth0Domain), WellKnownJwks);

            testHttpMessageHandler.PushGetResponse(
                jwksUri,
                HttpStatusCode.OK,
                new Jwks
            {
                Keys = new List <Key>
                {
                    new Key
                    {
                        Alg = "RS256",
                        Kty = "RSA",
                        Use = "sig",
                        X5C = new List <string>
                        {
                            tokenService.GetSigningKey()
                        }
                    }
                }
            },
                true
                );

            testHttpMessageHandler.PushGetResponse(
                new Uri(new Uri(appSettings.Auth0Domain), WellKnownOpenIdConfiguartion),
                HttpStatusCode.OK,
                new OpenIdConfiguration
            {
                Issuer  = appSettings.Auth0Domain,
                JwksUri = jwksUri,
                IdTokenSigningAlgValuesSupported = new List <string>
                {
                    "RS256"
                }
            },
                true
                );
        }
Esempio n. 11
0
        public static void AddCustomAuth0Authentication(this IServiceCollection services, IConfiguration configuration)
        {
            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Authority = configuration["Auth0:Authority"];
                options.Audience  = configuration["Auth0:ApiIdentifier"];
            });

            var auth0Settings = new Auth0Settings();

            configuration.GetSection("Auth0").Bind(auth0Settings);
            services.AddSingleton(auth0Settings);
        }
Esempio n. 12
0
 private static TestTokenService GetDefaultTokenService(Auth0Settings auth0SettingsOrDefault)
 {
     return(new TestTokenService(auth0SettingsOrDefault));
 }
 public AccountController(IOptions <Auth0Settings> auth0Settings)
 {
     _auth0Settings = auth0Settings.Value;
 }
 public HomeController(IOptions <Auth0Settings> auth0Settings)
 {
     _auth0 = auth0Settings.Value;
 }
Esempio n. 15
0
 public UserInfoService(IOptions <Auth0Settings> optionsAccessor)
 {
     _auth0Settings = optionsAccessor.Value;
 }
Esempio n. 16
0
        public static IApplicationBuilder UseJwtAuthentication(this IApplicationBuilder app, Auth0Settings auth0Settings)
        {
            var options = CreateJwtBearerOptions(auth0Settings);

            return(app.Use(async(context, next) => {
                AddAuthorizationHeaderFromQueryIfNecessary(context);
                await next();
            }).UseJwtBearerAuthentication(options));
        }
Esempio n. 17
0
 public SettingsController(IOptions <Auth0Settings> settings)
 {
     this.auth0Settings = settings.Options;
 }
Esempio n. 18
0
 public AuthZeroProvider(IOptions <Auth0Settings> settings)
 {
     _settings = settings.Value;
 }
Esempio n. 19
0
 public UserHandler(GaverContext context, IMapperService mapper, Auth0Settings auth0Settings)
 {
     this.context       = context;
     this.mapper        = mapper;
     this.auth0Settings = auth0Settings;
 }
Esempio n. 20
0
        public static IServiceCollection AddAuthentication(this IServiceCollection services, Auth0Settings settings)
        {
            string domain = $"https://{settings.Domain}/";

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.Authority = domain;
                options.Audience  = settings.ApiIdentifier;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = ClaimTypes.NameIdentifier
                };
            });

            return(services);
        }
Esempio n. 21
0
 public AccountController(IOptions <Auth0Settings> auth0Settings, ISecretSantaBl secretSantaBl)
 {
     _auth0Settings = auth0Settings.Value;
     _secretSantaBl = secretSantaBl;
 }
Esempio n. 22
0
 public static IApplicationBuilder UseJwtAuthentication(this IApplicationBuilder app, Auth0Settings auth0Settings)
 {
     return(app.Use(async(context, next) => {
         AddAuthorizationHeaderFromQueryIfNecessary(context);
         await next();
     }));
 }