ServiceStack 中提供基于令牌的身份验证的推荐实施模式是什么?
What is the recommended implementation pattern in ServiceStack for providing token-based authentication?
我有一组基于 ServiceStack 的服务需要使用 OAuth2 相互验证。具体来说,服务将使用 OAuth2 客户端凭据流从外部身份验证服务检索引用令牌。
ServiceStack doesn't support 与此流程直接集成(a.k.a 令牌身份验证),但我已经能够提供两种不同的实现 "fit" 到框架中并执行必要的身份验证与身份验证服务握手。
推荐以下两种实现中的哪一种,或者更确切地说,不认为是滥用 ServiceStack 的集成点?
选项 1:使用请求过滤器属性来验证访问令牌
public class TokenAuthenticateAttribute : RequestFilterAttribute
{
public TokenAuthenticateAttribute(params string[] requiredScopes) { ... }
public override void Execute(IHttpRequest request, IHttpResponse response, object requestDto)
{
// 1. Obtain access token from request header; return 401 on syntax error.
// 2. Send token and scopes to authentication service for validation.
// 3. If invalid, return 401/403 accordingly.
}
}
public class Service
{
[TokenAuthenticate("read", "write", "admin")]
public ResponseType Post(RequestType request) { ... }
[TokenAuthenticate("read")]
public ResponseType Get(AnotherRequestType request) { ... }
}
注意:调用方必须直接从身份验证服务获取令牌,或者API必须提供端点以委托给身份验证服务。
选项 2:使用自定义身份验证提供程序颁发和验证访问令牌
// 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) { ... }
}
注意:所有需要身份验证的方法现在都请求相同的范围。
这两种实现在功能方面是等效的(我们可以解决范围差异)。但是,我倾向于 Option 1 作为首选,因为它不需要内部 ServiceStack 身份验证和会话管理组件的初始化开销。这些结构在第二个实现中很明显,尽管它们从未被使用过。
你有什么想法?
相关问题:
How to do token based auth using ServiceStack
由于您没有使用 ServiceStack 的内置 OAuth 提供程序或后端 UserAuth 存储库,我倾向于使用 选项 1,因为它具有最少的移动部分,其中基于令牌的身份验证本质上只是一个验证请求过滤器——这最终也是 ServiceStack 的 [Authenticate] Request Filter Attribute 在幕后的作用,尽管它与 ServiceStack 的 AuthProvider 模型集成后更加复杂。
两者之间的主要区别在于,因为 ServiceStack 不知道您的请求过滤器是身份验证验证器,所以您不会在元数据页面中获得 身份验证密钥 图标指明哪些服务需要身份验证。
我有一组基于 ServiceStack 的服务需要使用 OAuth2 相互验证。具体来说,服务将使用 OAuth2 客户端凭据流从外部身份验证服务检索引用令牌。
ServiceStack doesn't support 与此流程直接集成(a.k.a 令牌身份验证),但我已经能够提供两种不同的实现 "fit" 到框架中并执行必要的身份验证与身份验证服务握手。
推荐以下两种实现中的哪一种,或者更确切地说,不认为是滥用 ServiceStack 的集成点?
选项 1:使用请求过滤器属性来验证访问令牌
public class TokenAuthenticateAttribute : RequestFilterAttribute
{
public TokenAuthenticateAttribute(params string[] requiredScopes) { ... }
public override void Execute(IHttpRequest request, IHttpResponse response, object requestDto)
{
// 1. Obtain access token from request header; return 401 on syntax error.
// 2. Send token and scopes to authentication service for validation.
// 3. If invalid, return 401/403 accordingly.
}
}
public class Service
{
[TokenAuthenticate("read", "write", "admin")]
public ResponseType Post(RequestType request) { ... }
[TokenAuthenticate("read")]
public ResponseType Get(AnotherRequestType request) { ... }
}
注意:调用方必须直接从身份验证服务获取令牌,或者API必须提供端点以委托给身份验证服务。
选项 2:使用自定义身份验证提供程序颁发和验证访问令牌
// 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) { ... }
}
注意:所有需要身份验证的方法现在都请求相同的范围。
这两种实现在功能方面是等效的(我们可以解决范围差异)。但是,我倾向于 Option 1 作为首选,因为它不需要内部 ServiceStack 身份验证和会话管理组件的初始化开销。这些结构在第二个实现中很明显,尽管它们从未被使用过。
你有什么想法?
相关问题: How to do token based auth using ServiceStack
由于您没有使用 ServiceStack 的内置 OAuth 提供程序或后端 UserAuth 存储库,我倾向于使用 选项 1,因为它具有最少的移动部分,其中基于令牌的身份验证本质上只是一个验证请求过滤器——这最终也是 ServiceStack 的 [Authenticate] Request Filter Attribute 在幕后的作用,尽管它与 ServiceStack 的 AuthProvider 模型集成后更加复杂。
两者之间的主要区别在于,因为 ServiceStack 不知道您的请求过滤器是身份验证验证器,所以您不会在元数据页面中获得 身份验证密钥 图标指明哪些服务需要身份验证。