Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Compatibility with Swift 3.0 (up to Xcode 8 GM) #267

Closed
wants to merge 19 commits into from
2 changes: 1 addition & 1 deletion OAuthSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Pod::Spec.new do |s|
s.homepage = 'https://github.com/OAuthSwift/OAuthSwift'
s.social_media_url = 'http://twitter.com/dongrify'
s.authors = { 'Dongri Jin' => 'dongrify@gmail.com' }
s.source = { :git => 'https://github.com/OAuthSwift/OAuthSwift.git', :tag => s.version }
s.source = { git: 'https://github.com/OAuthSwift/OAuthSwift.git', tag: s.version }
s.source_files = 'OAuthSwift/*.swift'
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.10'
Expand Down
63 changes: 38 additions & 25 deletions OAuthSwift.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions OAuthSwift/Dictionary+OAuthSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ extension Dictionary {
return joinedDictionary
}

func filter(_ predicate: (key: Key, value: Value) -> Bool) -> Dictionary {
func filter(_ predicate: (_ key: Key, _ value: Value) -> Bool) -> Dictionary {
var filteredDictionary = Dictionary()

for (key, value) in self {
if predicate(key: key, value: value) {
if predicate(key, value) {
filteredDictionary.updateValue(value, forKey: key)
}
}
Expand Down
4 changes: 2 additions & 2 deletions OAuthSwift/HMAC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

import Foundation

public class HMAC {
open class HMAC {

let key:[UInt8] = []

class internal func sha1(key: Data, message: Data) -> Data? {
class internal func sha1(_ key: Data, message: Data) -> Data? {
var key = key.bytes()
let message = message.bytes()

Expand Down
12 changes: 6 additions & 6 deletions OAuthSwift/Int+OAuthSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
import Foundation

extension Int {
public func bytes(_ totalBytes: Int = sizeof(Int)) -> [UInt8] {
public func bytes(_ totalBytes: Int = MemoryLayout<Int>.size) -> [UInt8] {
return arrayOfBytes(self, length: totalBytes)
}
}

func arrayOfBytes<T>(_ value:T, length:Int? = nil) -> [UInt8] {
let totalBytes = length ?? (sizeofValue(value) * 8)
let totalBytes = length ?? (MemoryLayout<T>.size * 8)

let valuePointer = UnsafeMutablePointer<T>(allocatingCapacity: 1)
let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
valuePointer.pointee = value

let bytesPointer = UnsafeMutablePointer<UInt8>(valuePointer)
let bytesPointer = valuePointer.withMemoryRebound(to: UInt8.self, capacity: 1) { return $0 }
var bytes = [UInt8](repeating: 0, count: totalBytes)
for j in 0..<min(sizeof(T),totalBytes) {
for j in 0..<min(MemoryLayout<T>.size,totalBytes) {
bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
}

valuePointer.deinitialize()
valuePointer.deallocateCapacity(1)
valuePointer.deallocate(capacity: 1)

return bytes
}
4 changes: 2 additions & 2 deletions OAuthSwift/NSData+OAuthSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ extension NSMutableData {

extension Data {
func bytes() -> [UInt8] {
let count = self.count / sizeof(UInt8)
let count = self.count / MemoryLayout<UInt8>.size
var bytesArray = [UInt8](repeating: 0, count: count)
(self as NSData).getBytes(&bytesArray, length:count * sizeof(UInt8))
(self as NSData).getBytes(&bytesArray, length:count * MemoryLayout<UInt8>.size)
return bytesArray
}

Expand Down
6 changes: 1 addition & 5 deletions OAuthSwift/NSURL+OAuthSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ extension URL {
}

var unsafeAbsoluteString: String {
#if swift(>=2.3)
return self.absoluteString!
#else
return self.absoluteString
#endif
return self.absoluteString
}

}
44 changes: 21 additions & 23 deletions OAuthSwift/OAuth1Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import Foundation


public class OAuth1Swift: OAuthSwift {
open class OAuth1Swift: OAuthSwift {

// If your oauth provider doesn't provide `oauth_verifier`
// set value to true (default: false)
public var allowMissingOauthVerifier: Bool = false
open var allowMissingOauthVerifier: Bool = false

var consumer_key: String
var consumer_secret: String
Expand All @@ -29,12 +29,12 @@ public class OAuth1Swift: OAuthSwift {
self.authorize_url = authorizeUrl
self.access_token_url = accessTokenUrl
super.init(consumerKey: consumerKey, consumerSecret: consumerSecret)
self.client.credential.version = .oAuth1
self.client.credential.version = .OAuth1
}

public convenience init?(parameters: [String:String]){
guard let consumerKey = parameters["consumerKey"], consumerSecret = parameters["consumerSecret"],
requestTokenUrl = parameters["requestTokenUrl"], authorizeUrl = parameters["authorizeUrl"], accessTokenUrl = parameters["accessTokenUrl"] else {
guard let consumerKey = parameters["consumerKey"], let consumerSecret = parameters["consumerSecret"],
let requestTokenUrl = parameters["requestTokenUrl"], let authorizeUrl = parameters["authorizeUrl"], let accessTokenUrl = parameters["accessTokenUrl"] else {
return nil
}
self.init(consumerKey:consumerKey, consumerSecret: consumerSecret,
Expand All @@ -43,7 +43,7 @@ public class OAuth1Swift: OAuthSwift {
accessTokenUrl: accessTokenUrl)
}

public var parameters: [String: String] {
open var parameters: [String: String] {
return [
"consumerKey": consumer_key,
"consumerSecret": consumer_secret,
Expand All @@ -55,7 +55,7 @@ public class OAuth1Swift: OAuthSwift {

// MARK: functions
// 0. Start
public func authorizeWithCallbackURL(_ callbackURL: URL, success: TokenSuccessHandler, failure: FailureHandler?) {
open func authorizeWithCallbackURL(_ callbackURL: URL, success: @escaping TokenSuccessHandler, failure: FailureHandler?) {
self.postOAuthRequestTokenWithCallbackURL(callbackURL, success: { [unowned self]
credential, response, _ in

Expand All @@ -64,7 +64,7 @@ public class OAuth1Swift: OAuthSwift {
if let query = url.query {
responseParameters += query.parametersFromQueryString()
}
if let fragment = url.fragment where !fragment.isEmpty {
if let fragment = url.fragment , !fragment.isEmpty {
responseParameters += fragment.parametersFromQueryString()
}
if let token = responseParameters["token"] {
Expand All @@ -79,29 +79,27 @@ public class OAuth1Swift: OAuthSwift {
self.postOAuthAccessTokenWithRequestToken(success, failure: failure)
} else {
let userInfo = [NSLocalizedDescriptionKey: "Oauth problem. oauth_token or oauth_verifier not returned"]
failure?(error: NSError(domain: OAuthSwiftErrorDomain, code: -1, userInfo: userInfo))
failure?(NSError(domain: OAuthSwiftErrorDomain, code: -1, userInfo: userInfo))
return
}
}
// 2. Authorize
let urlString = self.authorize_url + (self.authorize_url.has("?") ? "&" : "?") + "oauth_token=\(credential.oauth_token.urlQueryEncoded)"
let urlString = self.authorize_url + (self.authorize_url.has("?") ? "&" : "?") + "oauth_token=\(credential.oauth_token.urlQueryEncoded!)"
if let queryURL = URL(string: urlString) {
self.authorize_url_handler.handle(queryURL)
}
else {
let errorInfo = [NSLocalizedFailureReasonErrorKey: NSLocalizedString("Failed to create URL", comment: "\(urlString) not convertible to URL, please encode.")]
failure?(error: NSError(domain: OAuthSwiftErrorDomain, code: -1, userInfo: errorInfo))
failure?(NSError(domain: OAuthSwiftErrorDomain, code: -1, userInfo: errorInfo))
}
}, failure: failure)
}

// 1. Request token
func postOAuthRequestTokenWithCallbackURL(_ callbackURL: URL, success: TokenSuccessHandler, failure: FailureHandler?) {
var parameters = Dictionary<String, AnyObject>()
if let callbackURLString: String = callbackURL.absoluteString {
parameters["oauth_callback"] = callbackURLString
}
self.client.post(self.request_token_url, parameters: parameters, success: {
func postOAuthRequestTokenWithCallbackURL(_ callbackURL: URL, success: @escaping TokenSuccessHandler, failure: FailureHandler?) {
var parameters = Dictionary<String, Any>()
parameters["oauth_callback"] = callbackURL.absoluteString
let _ = self.client.post(self.request_token_url, parameters: parameters, success: {
data, response in
let responseString = String(data: data, encoding: String.Encoding.utf8)!
let parameters = responseString.parametersFromQueryString()
Expand All @@ -111,16 +109,16 @@ public class OAuth1Swift: OAuthSwift {
if let oauthTokenSecret=parameters["oauth_token_secret"] {
self.client.credential.oauth_token_secret = oauthTokenSecret.safeStringByRemovingPercentEncoding
}
success(credential: self.client.credential, response: response, parameters: parameters)
success(self.client.credential, response, parameters as [String: Any])
}, failure: failure)
}

// 3. Get Access token
func postOAuthAccessTokenWithRequestToken(_ success: TokenSuccessHandler, failure: FailureHandler?) {
var parameters = Dictionary<String, AnyObject>()
func postOAuthAccessTokenWithRequestToken(_ success: @escaping TokenSuccessHandler, failure: FailureHandler?) {
var parameters = Dictionary<String, Any>()
parameters["oauth_token"] = self.client.credential.oauth_token
parameters["oauth_verifier"] = self.client.credential.oauth_verifier
self.client.post(self.access_token_url, parameters: parameters, success: {
let _ = self.client.post(self.access_token_url, parameters: parameters, success: {
data, response in
let responseString = String(data: data, encoding: String.Encoding.utf8)!
let parameters = responseString.parametersFromQueryString()
Expand All @@ -130,12 +128,12 @@ public class OAuth1Swift: OAuthSwift {
if let oauthTokenSecret=parameters["oauth_token_secret"] {
self.client.credential.oauth_token_secret = oauthTokenSecret.safeStringByRemovingPercentEncoding
}
success(credential: self.client.credential, response: response, parameters: parameters)
success(self.client.credential, response, parameters as [String: Any])
}, failure: failure)
}

@available(*, deprecated:0.5.0, message:"Use OAuthSwift.handleOpenURL()")
public override class func handleOpenURL(_ url: URL) {
open override class func handleOpenURL(_ url: URL) {
super.handleOpenURL(url)
}

Expand Down