Skip to content

Commit

Permalink
Merge pull request #3 from nerdishbynature/add_expiry_date
Browse files Browse the repository at this point in the history
Add expiration date
  • Loading branch information
pietbrauer committed Nov 4, 2015
2 parents 4148e0b + d2264ef commit 06b6789
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
9 changes: 2 additions & 7 deletions TrashCanKit/OAuthConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions TrashCanKit/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import RequestKit
// MARK: request

public extension TrashCanKit {
public func refreshToken(oauthConfig: OAuthConfiguration, refreshToken: String, completion: (response: Response<String>) -> Void) {
public func refreshToken(oauthConfig: OAuthConfiguration, refreshToken: String, completion: (response: Response<TokenConfiguration>) -> Void) {
let request = TokenRouter.RefreshToken(oauthConfig, refreshToken).URLRequest
if let request = request {
let task = oauthConfig.basicAuthSession().dataTaskWithRequest(request) { data, response, err in
Expand All @@ -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))
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion TrashCanKit/TokenConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 3 additions & 1 deletion TrashCanKitTests/TokenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion TrashCanKitTests/authorize.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"access_token": "017ec60f4a182",
"scope": "read%3Aorg%2Crepo",
"token_type": "bearer",
"refresh_token": "14567"
"refresh_token": "14567",
"expires_in": 3600
}
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
machine:
xcode:
version: "7.0"
version: "7.1"
dependencies:
override:
- make install
Expand Down

0 comments on commit 06b6789

Please sign in to comment.