Kitura

Docs Build Status - Master macOS Linux Apache 2 Slack Status

Kitura-CredentialsGitHub

使用 GitHub 进行身份验证的 Credentials 框架的插件

摘要

Kitura-Credentials 框架的插件,它使用 GitHub OAuth2 Web 登录 API 进行身份验证。

目录

Swift 版本

最新版本的 Kitura-CredentialsGitHub 需要 Swift 4.0 或更高版本。 您可以按照此链接下载此版本的 Swift 二进制文件。 不保证与其他 Swift 版本的兼容性。

GitHub Web 登录示例

本指南假定您对 Kitura 应用程序路由有基本的了解。

首先,设置会话中间件

import KituraSession

router.all(middleware: Session(secret: "Very very secret..."))

创建 CredentialsGitHub 插件的实例,并将其注册到 Credentials 框架

import Credentials
import CredentialsGitHub

let credentials = Credentials()
let gitCredentials = CredentialsGitHub(clientId: gitClientId, clientSecret: gitClientSecret, callbackUrl: serverUrl + "/login/github/callback", userAgent: "my-kitura-app", options: ["scopes": ["user:email"]])
credentials.register(gitCredentials)

其中

接下来,指定将未经身份验证的请求重定向到哪里

credentials.options["failureRedirect"] = "/login/github"

连接 credentials 中间件以处理对服务器上的受保护路径的请求,例如 /private

router.all("/private", middleware: credentials)
router.get("/private/data", handler: { request, response, next in
  ...  
  next()
})

并调用 authenticate 以使用 GitHub 登录并处理成功登录后从 GitHub 登录网页的重定向(回调)

router.get("/login/github", handler: credentials.authenticate(gitCredentials.name))

router.get("/login/github/callback", handler: credentials.authenticate(gitCredentials.name))

许可证

此库在 Apache 2.0 许可证下获得许可。 完整的许可证文本可在 LICENSE 中找到。