Nodes SSO 🔑

Swift Version Vapor Version Circle CI codebeat badge codecov Readme Score GitHub license

📦 安装

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/ViewsPublic 文件夹中复制 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 的签名。

添加 SSO 路由

确保添加相关的 Nodes SSO 路由,例如在您的 configure.swiftroutes.swift

services.register(Router.self) { container -> EngineRouter in
    let router = EngineRouter.default()
    try router.useNodesSSORoutes(MyNodesSSOAuthenticatableUser.self, on: container)
    return router
}

添加 Leaf 标签

为了渲染嵌入 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
    }
}

嵌入 SSO 按钮

在您希望 NodesSSO 按钮出现的页面上,嵌入 sso-button leaf 文件

#embed("NodesSSO/sso-button")

遵循 NodesSSOAuthenticatable 协议

NodesSSOProvider 是泛型的,需要一个遵循 NodesSSOAuthenticatable 的类型。此协议有一个方法,在 SSO 成功完成时会被调用

public static func authenticated(_ user: AuthenticatedUser, req: Request) -> Future<Response>

给定此 AuthenticatedUser,实现者可以查找 email,并在用户不存在时创建用户,如果用户存在,则自动登录用户。

🏆 鸣谢

此软件包由 Nodes 的 Vapor 团队开发和维护。

📄 许可证

此软件包是根据 MIT 许可证 获得许可的开源软件