实现自己的OAuth2服务器和API服务器

5
我们正在尝试实现OAuth 2服务器和API服务器(两者是不同的服务器)。 (使用Node.js进行所有操作)

enter image description here

我们正在使用https://github.com/FrankHassanabad/Oauth2orizeRecipes授权码流。
在OAuth服务器中,我们需要编写新的validateToken函数,并从API端调用它以仅验证该用户。
我们打算将用户和角色保存在OAuth端,但在向API调用响应之前,我们需要在API端对其进行检查。
我们试图将其用于CMS和移动应用程序的身份验证目的。我们走在正确的轨道上还是缺少了什么?
2个回答

0

(我在 .Net 中遇到了类似的情况,因此在这个背景下)

不,如果你正在使用 OAuth,你不需要编写新的验证令牌方法。 因为 OAuthBearerAuthenticationProvider 在幕后完成了这个过程。

app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                AllowedAudiences = new[] { audience },
                IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                {
                    new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
                },
                Provider = new OAuthBearerAuthenticationProvider
                    {
                        OnValidateIdentity = context =>
                        {
                            context.Ticket.Identity.AddClaim(new System.Security.Claims.Claim("newCustomClaim", "newValue"));
                            return Task.FromResult<object>(null);
                        }
                    }

            });

(根据我的经验)但是如果你想的话,可以在你的“启动”文件中配置提供程序选项:
app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                AllowedAudiences = new[] { audience },
                IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                {
                    new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
                },
                Provider = new CustomOAuthBearerProvider()                        

            });

"CustomOAuthBearerProvider" 继承了 "IOAuthBearerAuthenticationProvider" 接口,该接口具有 RequestToken() 方法的预定义签名,并且在任何令牌验证之前调用此方法。因此,我认为您可以使用它来进行自定义验证操作并将令牌发送到 OAuth 验证。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接