0. 起源
开放授权(OAuth)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用。
而作为第三方软件,为用户提供 OAuth 登录是更好的选择,可以有效打消用户对于个人账户密码泄露的顾虑,同时也能有效避免用户反复登录,进而增加用户的舒适度,提高用户粘性。
1. 环境
项目使用 MVVM 架构,引入了 Rx 全家桶,网络请求框架使用了 Moya ,以及处理 Oauth 相关的库 OAuth2 。
2. OAuth2 部分
参阅 OAuth2 库的 README ,完成 OAuth 的信息配置:
?
|
1
2
3
4
5
6
7
8
9
10
|
let oauth2 = OAuth2CodeGrant(settings: [
"client_id": "my_swift_app",
"client_secret": "C7447242",
"authorize_uri": "https://github.com/login/oauth/authorize",
"token_uri": "https://github.com/login/oauth/access_token",
"redirect_uris": ["myapp://oauth/callback"],
"scope": "user repo:status",
"secret_in_body": true,
"keychain": false,
] as OAuth2JSON)
|
同时因为 Moya 的底层网络请求实现是基于 Alamofire,因此我们可以参照 OAuth2 提供的说明文档 Alamofire 4 · p2/OAuth2 Wiki · GitHub 完成相关配置,关键代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import Foundation
import OAuth2
import Alamofire
class OAuth2RetryHandler: RequestRetrier, RequestAdapter {
let loader: OAuth2DataLoader
init(oauth2: OAuth2) {
loader = OAuth2DataLoader(oauth2: oauth2)
}
/// Intercept 401 and do an OAuth2 authorization.
public func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
if let response = request.task?.response as? HTTPURLResponse, 401 == response.statusCode, let req = request.request {
var dataRequest = OAuth2DataRequest(request: req, callback: { _ in })
dataRequest.context = completion
loader.enqueue(request: dataRequest)
loader.attemptToAuthorize() { authParams, error in
self.loader.dequeueAndApply() { req in
if let comp = req.context as? RequestRetryCompletion {
comp(nil != authParams, 0.0)
}
}
}
}
else {
completion(false, 0.0) // not a 401, not our problem
}
}
/// Sign the request with the access token.
public func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
guard nil != loader.oauth2.accessToken else {
return urlRequest
}
return try urlRequest.signed(with: loader.oauth2) // "try" added in 3.0.2
}
}
|
3. Moya 部分
Moya 的 provider 在初始化时可以传入 SessionManager ,因此参照文档,可以先使用 OAuth2 生成一个特定的 SessionManager :
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
func getManager() -> SessionManager {
let oauth2 = OAuth2CodeGrant(settings: [
"client_id": "my_swift_app",
"client_secret": "C7447242",
"authorize_uri": "https://github.com/login/oauth/authorize",
"token_uri": "https://github.com/login/oauth/access_token",
"redirect_uris": ["myapp://oauth/callback"],
"scope": "user repo:status",
"secret_in_body": true,
"keychain": false,
] as OAuth2JSON)
let sessionManager = SessionManager()
let oauthHandler = OAuth2Handler(oauth2: oauth2)
sessionManager.adapter = oauthHandler
sessionManager.retrier = oauthHandler
return sessionManager
}
|
进而生成带 OAuth 的 provider:
?
|
1
2
3
4
|
fileprivate lazy var provider: MoyaProvider = {
return MoyaProvider<API>(manager: self.getManager(),
plugins: [NetworkLoggerPlugin()])
}()
|
相关文章
猜你喜欢
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
云服务器上建站:操作系统选型(Linux vs Windows)的影响
2025-06-04 45 -
2025-05-25 69
-
2025-05-25 46
-
2025-05-29 59
-
2025-06-04 86
热门评论

