Perfect-Authentication

Perfect logo

Perfect logo Perfect logo Perfect logo Perfect logo

Swift 4.0 Platforms OS X | Linux License Apache Twitter Join the chat at https://gitter.im/PerfectlySoft/Perfect Slack Status GitHub version

Perfect 的 OAuth2 库

此项目提供 OAuth2 库以及精选的 OAuth2 提供商驱动程序 - Facebook、Google、GitHub。

完整文档请访问 http://www.perfect.org/docs/OAuth2.html

演示应用程序请访问 https://github.com/PerfectExamples/Perfect-Authentication-Demo,其中展示了该系统的用法。

此包使用 Swift Package Manager 构建,并且是 Perfect 项目的一部分。它被编写为独立模块,因此不需要 PerfectLib 或任何其他组件。

请确保您已安装并激活最新的 Swift 4.0 工具链。

添加到您的项目

将此项目作为依赖项添加到您的 Package.swift 文件中。

.Package(url: "https://github.com/PerfectlySoft/Perfect-OAuth2.git", majorVersion: 3)

然后在您的代码中使用 OAuth2 模块

import OAuth2

配置

每个提供商都需要一个 "appid"(也称为 "key")和一个 "secret"。 这些通常由 OAuth 主机生成,例如 Facebook、GitHub 和 Google 开发者控制台。 对于您希望使用的每个提供商,都必须设置这些值以及 "endpointAfterAuth" 和 "redirectAfterAuth" 值。

配置 Facebook 作为提供商

FacebookConfig.appid = "yourAppID"
FacebookConfig.secret = "yourSecret"
FacebookConfig.endpointAfterAuth = "https://:8181/auth/response/facebook"
FacebookConfig.redirectAfterAuth = "https://:8181/"

配置 Google 作为提供商

GoogleConfig.appid = "yourAppID"
GoogleConfig.secret = "yourSecret"
GoogleConfig.endpointAfterAuth = "https://:8181/auth/response/google"
GoogleConfig.redirectAfterAuth = "https://:8181/"

配置 GitHub 作为提供商

GitHubConfig.appid = "yourAppID"
GitHubConfig.secret = "yourSecret"
GitHubConfig.endpointAfterAuth = "https://:8181/auth/response/github"
GitHubConfig.redirectAfterAuth = "https://:8181/"

添加路由

OAuth2 系统依赖于身份验证/交换系统,该系统需要一个专门组装的 URL,用户将被重定向到该 URL,以及用户在提交授权操作后返回的 URL。

下面的第一组路由是将重定向到 OAuth2 提供商系统的操作 URL。 它们可以是您希望的任何内容。 用户永远不会看到它们上的任何内容,因为他们会立即被重定向到正确的位置。

下面的第二组路由是 OAuth2 提供商应该将用户返回到的位置。 请注意,这与 "endpointAfterAuth" 配置选项相同。 一旦 "authResponse" 函数完成,用户将自动转发到 "redirectAfterAuth" 选项中的 URL。

var routes: [[String: Any]] = [[String: Any]]()

routes.append(["method":"get", "uri":"/to/facebook", "handler":Facebook.sendToProvider])
routes.append(["method":"get", "uri":"/to/github", "handler":GitHub.sendToProvider])
routes.append(["method":"get", "uri":"/to/google", "handler":Google.sendToProvider])

routes.append(["method":"get", "uri":"/auth/response/facebook", "handler":Facebook.authResponse])
routes.append(["method":"get", "uri":"/auth/response/github", "handler":GitHub.authResponse])
routes.append(["method":"get", "uri":"/auth/response/google", "handler":Google.authResponse])

返回和提供的可用信息

用户通过身份验证后,会从 OAuth2 提供商处收集某些信息。

请注意,可以使用以下命令检索会话 ID:

request.session?.token

用户特定信息可以作为会话信息的一部分访问

// The UserID as defined by the provider
request.session?.userid

// designates the OAuth2 source - useful if you are allowing multiple OAuth providers
request.session?.data["loginType"]

// The access token obtained in the process
request.session?.data["accessToken"]

// The user's first name as supplied by the provider
request.session?.data["firstName"]

// The user's last name as supplied by the provider
request.session?.data["lastName"]

// The user's profile picture as supplied by the provider
request.session?.data["picture"]

通过访问此信息,您现在可以保存到您选择的数据库。

更多信息

有关 Perfect 项目的更多信息,请访问 perfect.org