【发布时间】:2023-03-20 07:47:02
【问题描述】:
我对 Swift 和 Vapor 完全陌生;
我正在尝试开发一个授权中间件,该中间件是从我的 UserModel(Fluent Model 对象)调用的,并且在所需的访问级别(int)和路径(字符串)和
- 确保用户通过身份验证,
- 检查 UserModel.Accesslevel 的实例是否 => 传入的必需访问级别,如果不是,则将用户重定向到“路径”。
我已经尝试了几天,总是很接近,但从来没有完全得到我所追求的。
我有以下内容(请原谅命名不佳的对象):
import Vapor
public protocol Authorizable: Authenticatable {
associatedtype ProfileAccess: UserAuthorizable
}
/// status cached using `UserAuthorizable'
public protocol UserAuthorizable: Authorizable {
/// Session identifier type.
associatedtype AccessLevel: LosslessStringConvertible
/// Identifier identifier.
var accessLevel: AccessLevel { get }
}
extension Authorizable {
/// Basic middleware to redirect unauthenticated requests to the supplied path
///
/// - parameters:
/// - path: The path to redirect to if the request is not authenticated
public static func authorizeMiddleware(levelrequired: Int, path: String) -> Middleware {
return AuthorizeUserMiddleware<Self>(Self.self, levelrequired: levelrequired, path: path)
}
}
// Helper for creating authorization middleware.
///
//private final class AuthRedirectMiddleware<A>: Middleware
//where A: Authenticatable
final class AuthorizeUserMiddleware<A>: Middleware where A: Authorizable {
let levelrequired: Int
let path: String
let authLevel : Int
init(_ authorizableType: A.Type = A.self, levelrequired: Int, path: String) {
self.levelrequired = levelrequired
self.path = path
}
/// See Middleware.respond
public func respond(to req: Request, chainingTo next: Responder) -> EventLoopFuture<Response> {
if req.auth.has(A.self) {
print("--------")
print(path)
print(levelrequired)
**// print(A.ProfileAccess.AccessLevel) <- list line fails because the A.ProfileAccess.AccessLevel is a type not value**
print("--------")
}
return next.respond(to: req)
}
}
我将以下内容添加到我的用户模型中
extension UserModel: Authorizable
{
typealias ProfileAccess = UserModel
}
extension UserModel: UserAuthorizable {
typealias AccessLevel = Int
var accessLevel: AccessLevel { self.userprofile! }
}
这样的路线
// setup the authentication process
let session = app.routes.grouped([
UserModelSessionAuthenticator(),
UserModelCredentialsAuthenticator(),
UserModel.authorizeMiddleware(levelrequired: 255, path: "/login"), // this will redirect the user when unauthenticted
UserModel.authRedirectMiddleware(path: "/login"), // this will redirect the user when unauthenticted
])
正确传递了路径和所需级别,但我无法从当前用户获取 AccessLevel 的实例。 (我确信我已经戴上了 C++ 帽子,并且从我可以“猜测”的内容来看,即使身份验证已经完成,UserModel 实际上并不是用户的填充实例)
我尝试在使用关联类型传递帐户信息的“SessionAuthenticator”过程中进行合并。
我的另一个想法是检查用户是否经过身份验证,如果是,我可以安全地假设会话 Cookie 包含我的用户 ID,因此我可以(再次)从数据库中提取用户并从那里检查用户访问级别。
我可能会离开这里,几天后我不知道哪种方法是最好的方法,非常感谢任何指导。
干杯
【问题讨论】:
标签: swift middleware vapor vapor-fluent