Firebase Google Auth in Swagger(swashbuckle.aspnetcore)

发布于 2025-02-09 13:09:22 字数 3020 浏览 2 评论 0原文

试图在Swagger

Onboard:Asp.net Core 5,swashbuckle.aspnetcore 6.3.1, 在Fairbase Console>身份验证>登录方法>通过Google进行的身份验证是

在ServiceConfigure方法上启用的:

services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyApi", Version = "v1" });
            c.UseInlineDefinitionsForEnums();
            
            c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
            {
                Type = SecuritySchemeType.OAuth2,
                Flows = new OpenApiOAuthFlows
                {
                    //email and password authentication - works fine
                    Password = new OpenApiOAuthFlow 
                    {
                        TokenUrl = new Uri("/api/v1/auth/password", UriKind.Relative), //here my backend endpoint
                        Extensions = new Dictionary<string, IOpenApiExtension>
                        {
                            { "returnSecureToken", new OpenApiBoolean(true) },
                        },
                    },
                    //try add google auth - troble here
                    Implicit = new OpenApiOAuthFlow()
                    {
                        //Not sure about the endpoints. Its not work with 404 err
                        AuthorizationUrl = new Uri("https://securetoken.google.com/MY-PROJECT-FIREBASE-NAME"),
                        TokenUrl = new Uri("https://securetoken.google.com/MY-PROJECT-FIREBASE-NAME"),
                        Scopes = new Dictionary<string, string>
                        {                                
                            { "profile", "profile" },

                        }                            
                    }
                }
            });
            c.OperationFilter<AuthorizeCheckOperationFilter>();
        });

类过滤器:

public class AuthorizeCheckOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        var requiredScopes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
            .OfType<AuthorizeAttribute>()
            .Select(attr => attr.Policy)
            .Distinct();

        if (requiredScopes.Any())
        {

            var oAuthScheme = new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
            };

            operation.Security = new List<OpenApiSecurityRequirement>
            {
                new OpenApiSecurityRequirement
                {
                    [ oAuthScheme ] = requiredScopes.ToList()
                }
            };
        }
    }
}

在配置方法中:

app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyApi v1");

授权的端点 / tokenurl应该致电哪些端点? 有什么swagger的附加选项吗? 我是Firebase的新手。 MB需要在Firebase控制台中进行宣传调整吗? 我将非常感谢代码样本。

Trying to implement firebase google authentication in the swagger

onboard: asp.net core 5, Swashbuckle.AspNetCore 6.3.1,
In fairbase console > authentication > Sign-in method > authentication via google is enabled

On ServiceConfigure method:

services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyApi", Version = "v1" });
            c.UseInlineDefinitionsForEnums();
            
            c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
            {
                Type = SecuritySchemeType.OAuth2,
                Flows = new OpenApiOAuthFlows
                {
                    //email and password authentication - works fine
                    Password = new OpenApiOAuthFlow 
                    {
                        TokenUrl = new Uri("/api/v1/auth/password", UriKind.Relative), //here my backend endpoint
                        Extensions = new Dictionary<string, IOpenApiExtension>
                        {
                            { "returnSecureToken", new OpenApiBoolean(true) },
                        },
                    },
                    //try add google auth - troble here
                    Implicit = new OpenApiOAuthFlow()
                    {
                        //Not sure about the endpoints. Its not work with 404 err
                        AuthorizationUrl = new Uri("https://securetoken.google.com/MY-PROJECT-FIREBASE-NAME"),
                        TokenUrl = new Uri("https://securetoken.google.com/MY-PROJECT-FIREBASE-NAME"),
                        Scopes = new Dictionary<string, string>
                        {                                
                            { "profile", "profile" },

                        }                            
                    }
                }
            });
            c.OperationFilter<AuthorizeCheckOperationFilter>();
        });

class filter:

public class AuthorizeCheckOperationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        var requiredScopes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
            .OfType<AuthorizeAttribute>()
            .Select(attr => attr.Policy)
            .Distinct();

        if (requiredScopes.Any())
        {

            var oAuthScheme = new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
            };

            operation.Security = new List<OpenApiSecurityRequirement>
            {
                new OpenApiSecurityRequirement
                {
                    [ oAuthScheme ] = requiredScopes.ToList()
                }
            };
        }
    }
}

in Configure method:

app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyApi v1");

What endpoints for AuthorizationUrl / TokenUrl should call?
Any additinal options for swagger?
I`m new in Firebase. M.b. need aditional adjustments in firebase console?
I would be very appreciate for a code sample.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2025-02-16 13:09:22

我为您找到了一个很好的回购。这个简单的是.NET 5 WebAPI项目基础,其实现了以下功能:

用firebase登录摇晃
Serilog记录到Amazon CloudWatch
AppSettings中的加密字段
软删除和审核列
多重支持
数据播种
本地化

代码示例:

I found a good repo for you. This simple is .NET 5 WebApi project base with the following features implemented:

Swagger with Firebase login
Serilog Logging to Amazon CloudWatch
Encrypted fields in AppSettings
Soft Delete and Audit Columns
Multitenancy support
Data Seeding
Localization

code sample:clean-base-api

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文