用于 Kitura-Credentials 框架的插件,该插件使用 Google 网络登录和 Google OAuth 令牌进行身份验证。
用于 Kitura-Credentials 框架的插件,该插件使用 Google 网络登录(使用 OAuth 2.0) 和 Google OAuth 令牌 进行身份验证,该令牌由移动应用程序或基于 Kitura 后端的其他客户端获取。
最新版本的 Kitura-CredentialsGoogle 需要 Swift 4.0 或更高版本。 您可以通过点击此链接下载此版本的 Swift 二进制文件。 不保证与其他 Swift 版本的兼容性。
将 Kitura-CredentialsGoogle
和 Credentials
包添加到应用程序 Package.swift
文件中的依赖项中。 将 "x.x.x"
替换为最新的 Kitura-CredentialsGoogle
版本 和最新的 Kitura-Credentials
版本。
.package(url: "https://github.com/Kitura/Kitura-Credentials.git", from: "x.x.x")
.package(url: "https://github.com/Kitura/Kitura-CredentialsGoogle.git", from: "x.x.x")
将 CredentialsGoogle
和 Credentials
添加到你的目标依赖项中
.target(name: "example", dependencies: ["CredentialsGoogle", "Credentials"]),
import Credentials
import CredentialsGoogle
完整的示例可以在 Kitura-Sample 中找到。
首先设置会话
import KituraSession
router.all(middleware: Session(secret: "Very very secret..."))
创建 CredentialsGoogle
插件的实例,并将其注册到 Credentials
框架
import Credentials
import CredentialsGoogle
let credentials = Credentials()
let googleCredentials = CredentialsGoogle(clientId: googleClientId,
clientSecret: googleClientSecret,
callbackUrl: serverUrl + "/login/google/callback",
options: options)
credentials.register(googleCredentials)
其中
CredentialsGoogleOptions
中列出注意: 上面的 callbackUrl 参数用于告知 Google 网络登录页面,当登录成功时,用户的浏览器应该重定向到哪里。 它应该是你正在编写的服务器处理的 URL。
指定将未经验证的请求重定向到哪里
credentials.options["failureRedirect"] = "/login/google"
将 credentials
中间件连接到对 /private
的请求
router.all("/private", middleware: credentials)
router.get("/private/data", handler:
{ request, response, next in
...
next()
})
并调用 authenticate
以使用 Google 登录,并在成功登录后处理来自 Google 登录网页的重定向(回调)
router.get("/login/google",
handler: credentials.authenticate(googleCredentials.name))
router.get("/login/google/callback",
handler: credentials.authenticate(googleCredentials.name))
此示例展示了如何使用 CredentialsGoogleToken
插件来验证 post 请求,它展示了所涉及请求的服务器端和客户端。
首先创建 Credentials
的实例和 CredentialsGoogleToken
插件的实例
import Credentials
import CredentialsGoogle
let credentials = Credentials()
let googleCredentials = CredentialsGoogleToken(options: options)
其中
CredentialsGoogleOptions
中列出现在注册插件
credentials.register(googleCredentials)
将 credentials
中间件连接到 post 请求
router.post("/collection/:new", middleware: credentials)
如果身份验证成功,request.userProfile
将包含从 Google 收到的用户个人资料信息
router.post("/collection/:new") {request, response, next in
...
let profile = request.userProfile
let userId = profile.id
let userName = profile.displayName
...
next()
}
客户端需要将 Google 访问令牌 放在请求的 access_token
HTTP 标头字段中,并将 "GoogleToken" 放在 X-token-type
字段中
let urlRequest = NSMutableURLRequest(URL: NSURL(string: "http://\(serverUrl)/collection/\(name)"))
urlRequest.HTTPMethod = "POST"
urlRequest.HTTPBody = ...
urlRequest.addValue(googleAccessToken, forHTTPHeaderField: "id_token")
urlRequest.addValue("GoogleToken", forHTTPHeaderField: "X-token-type")
Alamofire.request(urlRequest).responseJSON {response in
...
}
有关更多信息,请访问我们的 API 参考。
我们喜欢讨论服务器端 Swift 和 Kitura。 加入我们的 Slack 与团队会面!
此库在 Apache 2.0 许可证下获得许可。 完整许可证文本可在 LICENSE 中找到。