public static IApplicationBuilder UseJwtTokenMiddleware(
     this IApplicationBuilder app,
     string schema = "Bearer")
 {
     return(UseExtensions.Use(app, (Func <HttpContext, Func <Task>, Task>)(async(ctx, next) =>
     {
         IIdentity identity = ctx.User.Identity;
         if ((identity != null ? (!identity.IsAuthenticated ? 1 : 0) : 1) != 0)
         {
             AuthenticateResult authenticateResult = await AuthenticationHttpContextExtensions.AuthenticateAsync(ctx, schema);
             if (authenticateResult.Succeeded && authenticateResult.Principal != null)
             {
                 ctx.User = authenticateResult.Principal;
             }
         }
         await next();
     })));
 }
        public static PlatformBuilder ConfigureIdentity(this PlatformBuilder builder, Action <PlatformexIdentityOptions> optionsBuilder)
        {
            var opt = new PlatformexIdentityOptions {
                IdentityServerUri = "https://localhost:5000"
            };

            builder.AddConfigureServicesActions(services =>
            {
                optionsBuilder?.Invoke(opt);

                //services.AddAuthentication("Bearer")
                //    .AddIdentityServerAuthentication("Bearer", options =>
                //    {
                //        // required audience of access tokens
                //        options.ApiName = "platformex";

                //        // auth server base endpoint (this will be used to search for disco doc)
                //        options.Authority = opt.IdentityServerUri;
                //    });
                services.AddAuthentication("Bearer")
                .AddJwtBearer("Bearer", options =>
                {
                    options.Authority = opt.IdentityServerUri;
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateAudience = false,
                        NameClaimType    = JwtClaimTypes.Subject,
                        RoleClaimType    = JwtClaimTypes.Role
                    };
                });
            });
            UseExtensions.AddPreUseAction(app =>
            {
                app.UseAuthentication();
                app.UseAuthorization();
            });
            return(builder);
        }
Beispiel #3
0
        public static PlatformBuilder WithOpenApi(this PlatformBuilder builder, Action <PlatformexOpenApiOptions> optionsBuilder)
        {
            var options = new PlatformexOpenApiOptions("swagger", Assembly.GetEntryAssembly()?.GetName().Name);

            optionsBuilder(options);
            builder.AddConfigureServicesActions(services =>
            {
                services.AddSingleton(options);

                services.TryAdd(ServiceDescriptor
                                .Transient <IApiDescriptionGroupCollectionProvider,
                                            CommandsApiDescriptionGroupCollectionProvider>());

                services.AddSingleton <IApiDescriptionGroupCollectionProvider,
                                       CommandsApiDescriptionGroupCollectionProvider>();

                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo {
                        Title = options.Name + " API", Version = "v1"
                    });
                    c.OperationFilter <DescriptionFilter>();
                    c.OperationFilter <AuthorizeCheckOperationFilter>();
                    c.SchemaFilter <ReadOnlyFilter>();
                    c.CustomSchemaIds(i => i.FullName);
                    c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                    {
                        Type  = SecuritySchemeType.OAuth2,
                        Flows = new OpenApiOAuthFlows
                        {
                            AuthorizationCode = new OpenApiOAuthFlow
                            {
                                AuthorizationUrl = new Uri("https://localhost:5000/connect/authorize"),
                                TokenUrl         = new Uri("https://localhost:5000/connect/token"),
                                Scopes           = new Dictionary <string, string>
                                {
                                    { "openid", "User Profile" },
                                    { "platformex", "Platformex API - full access" }
                                }
                            }
                        }
                    });
                    c.DocInclusionPredicate((docName, apiDesc)
                                            => apiDesc.TryGetMethodInfo(out _) && apiDesc.HttpMethod != null);

                    /*var basePath = AppDomain.CurrentDomain.BaseDirectory;
                     * var files = Directory.GetFiles(basePath, "*.xml");
                     * foreach (var file in files)
                     * {
                     *  c.IncludeXmlComments(file);
                     * }*/
                });
            });
            UseExtensions.AddPreUseAction(app =>
            {
                app.UseSwagger();

                var apiOptions = app.ApplicationServices.GetRequiredService <PlatformexOpenApiOptions>();

                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/" + apiOptions.Url.Trim('/') + "/v1/swagger.json", apiOptions.Name);

                    c.OAuthClientId("swagger");
                    c.OAuthAppName("Platformex Open API");
                    c.OAuthUsePkce();
                });
            });
            return(builder);
        }