internal async Task <DelegatingHandler> GetHandlers() { DelegatingHandler handler = null; switch (CredentialType) { case CredentialType.None: break; case CredentialType.ApiKey: handler = new ApiKeyHandler(ApiKey); handler.InnerHandler = new HttpClientHandler(); break; case CredentialType.ClientCredentials: case CredentialType.ResourceOwner: case CredentialType.RefreshToken: handler = new ApiKeyHandler(ApiKey); var bearerTokenHandler = new BearerTokenHandler(await GetAccessToken()); bearerTokenHandler.InnerHandler = new HttpClientHandler(); handler.InnerHandler = bearerTokenHandler; break; } return(handler); }
public static void AddMyIdentity(this IServiceCollection services, IConfiguration config) { IdentityModelEventSource.ShowPII = true; var identityOptions = config.GetSection("identity").Get <NotifoIdentityOptions>() ?? new NotifoIdentityOptions(); services.Configure <NotifoIdentityOptions>(config, "identity"); services.AddIdentity <NotifoUser, NotifoRole>() .AddDefaultTokenProviders(); services.AddSingletonAs <UserResolver>() .As <IUserResolver>(); AddMyMongoDbIdentity(services); services.AddSingletonAs <UserCreator>() .AsSelf(); services.AddIdentityServer() .AddAspNetIdentity <NotifoUser>() .AddClients() .AddIdentityResources() .AddApiResources(); services.Configure <ApiAuthorizationOptions>(options => { options.Clients.AddIdentityServerSPA("notifo", client => client .WithLogoutRedirectUri("/authentication/logout-callback") .WithRedirectUri("/authentication/login-callback") .WithRedirectUri("/authentication/login-silent-callback.html")); }); services.AddAuthorization(); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = AlternativeSchema; options.DefaultChallengeScheme = AlternativeSchema; }) .AddPolicyScheme(AlternativeSchema, null, options => { options.ForwardDefaultSelector = context => { if (ApiKeyHandler.IsApiKey(context.Request, out _)) { return(ApiKeyDefaults.AuthenticationScheme); } return("IdentityServerJwt"); }; }) .AddGoogle(identityOptions) .AddGithub(identityOptions) .AddApiKey() .AddIdentityServerJwt(); services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <IdentityServerOptions>, IdentityOptions>()); }
public static void AddMyIdentity(this IServiceCollection services, IConfiguration config) { IdentityModelEventSource.ShowPII = true; var identityOptions = config.GetSection("identity").Get <NotifoIdentityOptions>() ?? new NotifoIdentityOptions(); services.Configure <NotifoIdentityOptions>(config, "identity"); services.AddIdentity <IdentityUser, IdentityRole>() .AddDefaultTokenProviders(); services.AddSingletonAs <UserCreator>() .AsSelf(); services.AddSingletonAs <TokenStoreInitializer>() .AsSelf(); services.AddSingletonAs <DefaultUserResolver>() .As <IUserResolver>(); services.AddScopedAs <DefaultUserService>() .As <IUserService>(); services.AddMyOpenIdDict(); services.AddAuthorization(); services.AddAuthentication() .AddPolicyScheme(Constants.IdentityServerOrApiKeyScheme, null, options => { options.ForwardDefaultSelector = context => { if (ApiKeyHandler.IsApiKey(context.Request, out _)) { return(ApiKeyDefaults.AuthenticationScheme); } return(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme); }; }) .AddGoogle(identityOptions) .AddGithub(identityOptions) .AddApiKey(); }
private ApiKeyHandler CreateHandler(HttpContext context) { var keyStore = new FakeKeyStore(); var options = new FakeOptionsMonitor <ApiKeyOptions>() { CurrentValue = new ApiKeyOptions() }; var logger = new FakeLoggerFactory(); var b = new Microsoft.AspNetCore.Authentication.AuthenticationSchemeBuilder(ApiKeyDefaults.Name); b.HandlerType = typeof(ApiKeyHandler); b.DisplayName = "API Key"; var handler = new ApiKeyHandler(keyStore, options, logger, System.Text.Encodings.Web.UrlEncoder.Default, Clock); handler.InitializeAsync(b.Build(), context).ConfigureAwait(false).GetAwaiter().GetResult(); return(handler); }
public static void RegisterServices(this IServiceCollection Services) { //Uses HttpClient Inside -> singleton Services.AddSingleton <ILykkeSigningAPI>((provider) => { var baseSettings = provider.GetService <IBaseSettings>(); var apiKey = baseSettings.SigningServiceApiKey; ApiKeyHandler apiKeyHandler = new ApiKeyHandler(apiKey); var lykkeSigningApi = new LykkeSigningAPI(new Uri(provider.GetService <IBaseSettings>().SignatureProviderUrl , UriKind.Absolute), apiKeyHandler); return(lykkeSigningApi); }); Services.AddSingleton <IEthereumSamuraiAPI>((provider) => { var ethereumSamuraiApi = new EthereumSamuraiAPI(new Uri(provider.GetService <IBaseSettings>().EthereumSamuraiUrl , UriKind.Absolute)); return(ethereumSamuraiApi); }); Services.AddSingleton <Web3>((provider) => { var baseSettings = provider.GetService <IBaseSettings>(); var web3 = new Web3(baseSettings.EthereumUrl); return(web3); }); Services.AddSingleton <IWeb3>((provider) => { var web3 = provider.GetService <Web3>(); return(new Web3Decorator(web3)); }); Services.AddSingleton <ITransactionManager>(provider => { var baseSettings = provider.GetService <IBaseSettings>(); var web3 = provider.GetService <Web3>(); var signatureApi = provider.GetService <ILykkeSigningAPI>(); var nonceCalculator = provider.GetService <INonceCalculator>(); var transactionRouter = provider.GetService <ITransactionRouter>(); var gasPriceRepository = provider.GetService <IGasPriceRepository>(); var transactionManager = new LykkeSignedTransactionManager(baseSettings, nonceCalculator, signatureApi, transactionRouter, web3, gasPriceRepository); web3.TransactionManager = transactionManager; web3.Client.OverridingRequestInterceptor = new SignatureInterceptor(transactionManager); return(transactionManager); }); Services.AddSingleton <IAssetsService>((provider) => { var settings = provider.GetService <AppSettings>(); return(new AssetsService(new Uri(settings.Assets.ServiceUrl))); }); Services.AddSingleton <IBlockPassClient>((provider) => { var settings = provider.GetService <BlockPassClientSettings>(); BlockPassClientFactory factory = new BlockPassClientFactory(); var client = factory.CreateNew(settings, false); return(client); }); }
public static void AddMyIdentity(this IServiceCollection services, IConfiguration config) { IdentityModelEventSource.ShowPII = true; var identityOptions = config.GetSection("identity").Get <NotifoIdentityOptions>() ?? new NotifoIdentityOptions(); services.Configure <NotifoIdentityOptions>(config, "identity"); services.AddIdentity <IdentityUser, IdentityRole>() .AddDefaultTokenProviders(); services.AddSingletonAs <UserCreator>() .AsSelf(); services.AddSingletonAs <DefaultUserResolver>() .As <IUserResolver>(); services.AddScopedAs <DefaultUserService>() .As <IUserService>(); services.AddIdentityServer(options => { options.Authentication.CookieAuthenticationScheme = IdentityConstants.ApplicationScheme; options.Events.RaiseErrorEvents = true; options.Events.RaiseInformationEvents = true; options.Events.RaiseFailureEvents = true; options.Events.RaiseSuccessEvents = true; options.UserInteraction.ErrorUrl = "/account/error"; }) .AddAspNetIdentity <IdentityUser>() .AddClients() .AddIdentityResources() .AddApiResources(); services.Configure <IdentityServerOptions>((c, options) => { var urlBuilder = c.GetRequiredService <IUrlGenerator>(); options.IssuerUri = urlBuilder.BuildUrl(); }); services.Configure <ApiAuthorizationOptions>(options => { options.Clients.AddIdentityServerSPA("notifo", client => client .WithLogoutRedirectUri("/authentication/logout-callback") .WithRedirectUri("/authentication/login-callback") .WithRedirectUri("/authentication/login-silent-callback.html")); }); services.AddAuthorization(); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = Constants.IdentityServerOrApiKeyScheme; options.DefaultChallengeScheme = Constants.IdentityServerOrApiKeyScheme; }) .AddPolicyScheme(Constants.IdentityServerOrApiKeyScheme, null, options => { options.ForwardDefaultSelector = context => { if (ApiKeyHandler.IsApiKey(context.Request, out _)) { return(ApiKeyDefaults.AuthenticationScheme); } return(Constants.IdentityServerScheme); }; }) .AddGoogle(identityOptions) .AddGithub(identityOptions) .AddApiKey() .AddIdentityServerJwt(); }