OhhAuth
(RFC-5849)
纯 Swift 实现 OAuth 1.0 协议,作为用于 URLRequest 类型的易用扩展。尽管其继任者已经指定,OAuth 1.0 协议仍在广泛应用。此 URLRequest 类型的易用扩展实现了最常见的 OAuth 客户端请求变体。
需求
OhhAuth 依赖于 libCommonCrypto
,该库已经安装在所有常见的 Apple 操作系统(macOS、iOS、tvOS、watchOS)上。不幸的是,目前不支持 Linux,但预计在未来可能会添加。
使用示例
经典用法示例是发布一条推文集锦到Twitter
要获取消费者凭证(密钥和密钥),首先必须在新Twitter应用程序上注册,请访问https://apps.twitter.com
有许多方法可以获取用户凭证(如oauth反向认证),但测试起来最简便的方法是从您的Twitter应用程序页面直接使用本指南。
有关api.twitter.com/1.1/statuses/update.json
的更多信息,请参阅Twitter API文档。
let cc = (key: "<YOUR APP CONSUMER KEY>", secret: "<YOUR APP CONSUMER SECRET>")
let uc = (key: "<YOUR USER KEY>", secret: "<YOUR USER SECRET>")
var req = URLRequest(url: URL(string: "https://api.twitter.com/1.1/statuses/update.json")!)
let paras = ["status": "Hey Twitter! \u{1F6A7} Take a look at this sweet UUID: \(UUID())"]
req.oAuthSign(method: "POST", urlFormParameters: paras, consumerCredentials: cc, userCredentials: uc)
let task = URLSession(configuration: .ephemeral).dataTask(with: req) { (data, response, error) in
if let error = error {
print(error)
}
else if let data = data {
print(String(data: data, encoding: .utf8) ?? "Does not look like an utf8 response :(")
}
}
task.resume()
安装
Cocoa Pods
要使用CocoaPods将OhhAuth集成到您的Xcode项目中,请在您的Podfile
中添加它
target '<your_target_name>' do
pod 'OhhAuth'
end
然后,运行以下命令
$ pod install
然后,您需要在Swift源文件的开头添加import OhhAuth
以使用此扩展。
Swift Package Manager
要使用Swift包管理器将OhhAuth集成到您的Xcode项目中,请将其作为依赖项添加到您的Package.swift
文件中
import PackageDescription
let package = Package(
name: "<your_package_name>",
dependencies: [
.Package(url: "https://github.com/mw99/OhhAuth.git", majorVersion: 1)
]
)
然后,您需要在Swift源文件的开头添加import OhhAuth
以使用此扩展。
或者只需将文件复制到项目中
您只需要一个位于 Sources/OhhAuth.swift
的文件。只需将其拖放到 Xcode 的项目导航器中。
接口
URLRequest
扩展
作为 /// Easy to use method to sign a URLRequest which includes url-form parameters with OAuth.
/// The request needs a valid URL with all query parameters etc. included.
/// After calling this method the HTTP header fields: "Authorization", "Content-Type"
/// and "Content-Length" should not be overwritten.
///
/// - Parameters:
/// - method: HTTP Method
/// - paras: url-form parameters
/// - consumerCredentials: consumer credentials
/// - userCredentials: user credentials (nil if this is a request without user association)
public mutating func oAuthSign(method: String, urlFormParameters paras: [String: String],
consumerCredentials cc: OhhAuth.Credentials, userCredentials uc: OhhAuth.Credentials? = nil)
/// Easy to use method to sign a URLRequest which includes plain body data with OAuth.
/// The request needs a valid URL with all query parameters etc. included.
/// After calling this method the HTTP header fields: "Authorization", "Content-Type"
/// and "Content-Length" should not be overwritten.
///
/// - Parameters:
/// - method: HTTP Method
/// - body: HTTP request body (default: nil)
/// - contentType: HTTP header "Content-Type" entry (default: nil)
/// - consumerCredentials: consumer credentials
/// - userCredentials: user credentials (nil if this is a request without user association)
public mutating func oAuthSign(method: String, body: Data? = nil, contentType: String? = nil,
consumerCredentials cc: OhhAuth.Credentials, userCredentials uc: OhhAuth.Credentials? = nil)
OhhAuth
类直接访问
从 /// Function to calculate the OAuth protocol parameters and signature ready to be added
/// as the HTTP header "Authorization" entry. A detailed explanation of the procedure
/// can be found at: [RFC-5849 Section 3](https://tools.ietf.org/html/rfc5849#section-3)
///
/// - Parameters:
/// - url: Request url (with all query parameters etc.)
/// - method: HTTP method
/// - parameter: url-form parameters
/// - consumerCredentials: consumer credentials
/// - userCredentials: user credentials (nil if this is a request without user association)
///
/// - Returns: OAuth HTTP header entry for the Authorization field.
open static func calculateSignature(url: URL, method: String, parameter: [String: String],
consumerCredentials cc: Credentials, userCredentials uc: Credentials?) -> String
/// Function to perform the right percentage encoding for url form parameters.
///
/// - Parameter paras: url-form parameters
/// - Parameter encoding: used string encoding (default: .utf8)
/// - Returns: correctly percentage encoded url-form parameters
open static func httpBody(forFormParameters paras: [String: String],
encoding: String.Encoding = .utf8) -> Data?