将 NodesSSO
添加到包依赖项中(在您的 Package.swift
文件中)
dependencies: [
...,
.package(url: "https://github.com/nodes-vapor/nodes-sso.git", from: "1.0.0")
]
以及您的目标(例如 “App”)
targets: [
...
.target(
name: "App",
dependencies: [... "NodesSSO" ...]
),
...
]
从这个仓库的 Resources/Views
和 Public
文件夹中复制 NodesSSO
文件夹,并将它们粘贴到您项目中的相同目录中。您可以将此仓库下载为 zip 文件,然后将文件移动到上述目录中。
首先确保您在所有需要的地方都导入了 NodesSSO
import NodesSSO
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
try services.register(NodesSSOProvider<MyNodesSSOAuthenticatableUser>(config: NodesSSOConfig(
projectURL: "https://myproject.com",
redirectURL: "https://url-for-sso.com",
salt: "MY-SECRET-HASH-FOR-SSO",
environment: env
)))
}
还有一些参数用于设置应在您的项目中启用 SSO 的路由。有关更多信息,请查看 NodesSSOConfig
的签名。
确保添加相关的 Nodes SSO 路由,例如在您的 configure.swift
或 routes.swift
中
services.register(Router.self) { container -> EngineRouter in
let router = EngineRouter.default()
try router.useNodesSSORoutes(MyNodesSSOAuthenticatableUser.self, on: container)
return router
}
为了渲染嵌入 SSO 按钮,您需要添加 NodesSSO Leaf 标签
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
services.register { _ -> LeafTagConfig in
var tags = LeafTagConfig.default()
tags.useNodesSSOLeafTags()
return tags
}
}
在您希望 NodesSSO 按钮出现的页面上,嵌入 sso-button
leaf 文件
#embed("NodesSSO/sso-button")
NodesSSOProvider
是泛型的,需要一个遵循 NodesSSOAuthenticatable
的类型。此协议有一个方法,在 SSO 成功完成时会被调用
public static func authenticated(_ user: AuthenticatedUser, req: Request) -> Future<Response>
给定此 AuthenticatedUser
,实现者可以查找 email
,并在用户不存在时创建用户,如果用户存在,则自动登录用户。
此软件包由 Nodes 的 Vapor 团队开发和维护。
此软件包是根据 MIT 许可证 获得许可的开源软件