From 7f0f16e50424e3569fe13a95bec35a63943fc597 Mon Sep 17 00:00:00 2001 From: Piet Brauer Date: Wed, 4 Nov 2015 11:52:25 +0700 Subject: [PATCH 1/2] Add expiration date --- TrashCanKit/OAuthConfiguration.swift | 9 ++------- TrashCanKit/Token.swift | 6 +++--- TrashCanKit/TokenConfiguration.swift | 13 ++++++++++++- TrashCanKitTests/TokenTests.swift | 4 +++- TrashCanKitTests/authorize.json | 3 ++- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/TrashCanKit/OAuthConfiguration.swift b/TrashCanKit/OAuthConfiguration.swift index 4d2c3a3..8869a99 100644 --- a/TrashCanKit/OAuthConfiguration.swift +++ b/TrashCanKit/OAuthConfiguration.swift @@ -57,16 +57,11 @@ public struct OAuthConfiguration: Configuration { guard let data = data else { return nil } do { guard let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [String: AnyObject] else { return nil } - let accessToken = json["access_token"] as? String - let refreshToken = json["refresh_token"] as? String - if let accessToken = accessToken, refreshToken = refreshToken { - let config = TokenConfiguration(accessToken, refreshToken: refreshToken) - return config - } + let config = TokenConfiguration(json: json) + return config } catch { return nil } - return nil } public func handleOpenURL(url: NSURL, completion: (config: TokenConfiguration) -> Void) { diff --git a/TrashCanKit/Token.swift b/TrashCanKit/Token.swift index 785fcfd..ce979f0 100644 --- a/TrashCanKit/Token.swift +++ b/TrashCanKit/Token.swift @@ -4,7 +4,7 @@ import RequestKit // MARK: request public extension TrashCanKit { - public func refreshToken(oauthConfig: OAuthConfiguration, refreshToken: String, completion: (response: Response) -> Void) { + public func refreshToken(oauthConfig: OAuthConfiguration, refreshToken: String, completion: (response: Response) -> Void) { let request = TokenRouter.RefreshToken(oauthConfig, refreshToken).URLRequest if let request = request { let task = oauthConfig.basicAuthSession().dataTaskWithRequest(request) { data, response, err in @@ -18,8 +18,8 @@ public extension TrashCanKit { let error = NSError(domain: BitbucketErrorDomain, code: response.statusCode, userInfo: [NSLocalizedDescriptionKey: errorDescription]) completion(response: Response.Failure(error)) } else { - let accessToken = responseJSON["access_token"] as? String - completion(response: Response.Success(accessToken ?? "")) + let tokenConfig = TokenConfiguration(json: responseJSON) + completion(response: Response.Success(tokenConfig)) } } } diff --git a/TrashCanKit/TokenConfiguration.swift b/TrashCanKit/TokenConfiguration.swift index ceef33c..8f0b79e 100644 --- a/TrashCanKit/TokenConfiguration.swift +++ b/TrashCanKit/TokenConfiguration.swift @@ -4,10 +4,21 @@ public struct TokenConfiguration: Configuration { public var apiEndpoint: String public var accessToken: String? public var refreshToken: String? + public var expirationDate: NSDate? - public init(_ token: String? = nil, refreshToken: String? = nil, url: String = bitbucketBaseURL) { + public init(json: [String: AnyObject], url: String = bitbucketBaseURL) { + apiEndpoint = url + accessToken = json["access_token"] as? String + refreshToken = json["refresh_token"] as? String + let expiresIn = json["expires_in"] as? Int + let currentDate = NSDate() + expirationDate = currentDate.dateByAddingTimeInterval(NSTimeInterval(expiresIn ?? 0)) + } + + public init(_ token: String? = nil, refreshToken: String? = nil, expirationDate: NSDate? = nil, url: String = bitbucketBaseURL) { apiEndpoint = url accessToken = token + self.expirationDate = expirationDate self.refreshToken = refreshToken } } diff --git a/TrashCanKitTests/TokenTests.swift b/TrashCanKitTests/TokenTests.swift index be1fddd..863192b 100644 --- a/TrashCanKitTests/TokenTests.swift +++ b/TrashCanKitTests/TokenTests.swift @@ -23,7 +23,9 @@ class TokenTests: XCTestCase { kit.refreshToken(oauthConfig, refreshToken: tokenConfig.refreshToken!) { response in switch response { case .Success(let newToken): - XCTAssertEqual(newToken, "017ec60f4a182") + XCTAssertEqual(newToken.accessToken, "017ec60f4a182") + XCTAssertNotNil(newToken.expirationDate) + XCTAssertEqual(newToken.refreshToken, "14567") expectation.fulfill() case .Failure: XCTAssertFalse(true) diff --git a/TrashCanKitTests/authorize.json b/TrashCanKitTests/authorize.json index 8159678..d50cacc 100644 --- a/TrashCanKitTests/authorize.json +++ b/TrashCanKitTests/authorize.json @@ -2,5 +2,6 @@ "access_token": "017ec60f4a182", "scope": "read%3Aorg%2Crepo", "token_type": "bearer", - "refresh_token": "14567" + "refresh_token": "14567", + "expires_in": 3600 } \ No newline at end of file From d2264ef1c97cadf2074d67c832991d79a3b4406b Mon Sep 17 00:00:00 2001 From: Piet Brauer Date: Wed, 4 Nov 2015 12:19:58 +0700 Subject: [PATCH 2/2] Update Xcode --- circle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circle.yml b/circle.yml index 8ead812..16515de 100644 --- a/circle.yml +++ b/circle.yml @@ -1,6 +1,6 @@ machine: xcode: - version: "7.0" + version: "7.1" dependencies: override: - make install