我有一组基于ServiceStack的服务需要使用OAuth2进行相互身份验证。具体而言,服务将使用OAuth2客户端凭据流从外部身份验证服务检索引用令牌。
ServiceStack doesn't support与此流程直接集成(也称为令牌身份验证)但我已经能够提供两种不同的实现,这些实现“适合”到框架中并与身份验证服务执行必要的身份验证握手。
建议使用以下两种实现中的哪一种,或者更确切地说,不认为滥用ServiceStack的集成点?
选项1:使用请求过滤器属性验证访问令牌
// Requires registration in AppHost: base.PlugIns.Add(...)
public class TokenAuthProvider : AuthProvider
{
public TokenAuthProvider(params string[] requiredScopes) { ... }
public override object Authenticate(IServiceBase authService, IAuthSession session, Auth request)
{
// 1. Obtain client and secret from "request" (as username and password); return 401 accordingly
// 2. If authenticated, return access token, expiration, etc...
}
public override bool IsAuthorized(IAuthSession session, IOAuthTokens tokens, Auth request = null)
{
// 1. Obtain access token from request header (HttpContext.Current); return false syntax error.
// 2. Send token and scopes to authentication service for validation.
// 3. Return true/false (valid/invalid) accordingly.
}
}
public class Service
{
[Authenticate]
public ResponseType Post(RequestType request) { ... }
[Authenticate]
public ResponseType Get(AnotherRequestType request) { ... }
}
注意:调用者必须直接从auth服务获取令牌,或者API必须提供一个端点来委派auth服务。
选项2:使用自定义身份验证提供程序发布和验证访问令牌
UIButton
注意:所有需要身份验证的方法现在请求相同的范围。
这两种实现在功能方面都是等价的(我们可以解决范围差异)。但是,我倾向于选项1 作为首选,因为它不需要内部ServiceStack身份验证和会话管理组件的初始化开销。这些结构在第二种实现中很明显,但它们从未使用过。
你有什么想法?
答案 0 :(得分:3)
由于您未使用ServiceStack的内置OAuth提供程序或后端UserAuth存储库,我倾向于使用选项1 ,因为它具有最少移动的部分,其中基本上基于令牌的身份验证只是一个验证请求过滤器 - 最终也是ServiceStack的[Authenticate] Request Filter Attribute幕后工作,虽然它更复杂,因为它与ServiceStack和#39;的AuthProvider模型。
2之间的主要区别在于,由于ServiceStack不知道您的请求过滤器是身份验证验证器,因此您无法在元数据页面中获取身份验证密钥图标来指示哪些服务需要身份验证。