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

New built-in OAuth Access Token Store: iCloud Key-Value Storage #85

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Heimdallr.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@
E258A16C1CC6BC8600649F5A /* OAuthAccessTokenDefaultParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = E258A1651CC6B66900649F5A /* OAuthAccessTokenDefaultParser.swift */; };
E258A16D1CC6BC8700649F5A /* OAuthAccessTokenDefaultParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = E258A1651CC6B66900649F5A /* OAuthAccessTokenDefaultParser.swift */; };
E258A16E1CC6BC8800649F5A /* OAuthAccessTokenDefaultParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = E258A1651CC6B66900649F5A /* OAuthAccessTokenDefaultParser.swift */; };
E5E39D991D04C5CE00713AC9 /* OAuthAccessTokenICloudKeyValueStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5E39D981D04C5CE00713AC9 /* OAuthAccessTokenICloudKeyValueStore.swift */; };
E5E39D9A1D04D09000713AC9 /* OAuthAccessTokenICloudKeyValueStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5E39D981D04C5CE00713AC9 /* OAuthAccessTokenICloudKeyValueStore.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -237,6 +239,7 @@
DC712A5E1C85AD77009860A5 /* watchOS-StaticLibrary.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "watchOS-StaticLibrary.xcconfig"; sourceTree = "<group>"; };
E258A1651CC6B66900649F5A /* OAuthAccessTokenDefaultParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuthAccessTokenDefaultParser.swift; sourceTree = "<group>"; };
E258A1671CC6B8C500649F5A /* OAuthAccessTokenParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuthAccessTokenParser.swift; sourceTree = "<group>"; };
E5E39D981D04C5CE00713AC9 /* OAuthAccessTokenICloudKeyValueStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuthAccessTokenICloudKeyValueStore.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -366,6 +369,7 @@
DC5220511BEA320B00F37F2A /* OAuthAuthorizationGrant.swift */,
DC5220521BEA320B00F37F2A /* OAuthClientCredentials.swift */,
DC5220531BEA320B00F37F2A /* OAuthError.swift */,
E5E39D981D04C5CE00713AC9 /* OAuthAccessTokenICloudKeyValueStore.swift */,
);
path = Core;
sourceTree = "<group>";
Expand Down Expand Up @@ -892,6 +896,7 @@
DC5220701BEA321300F37F2A /* HeimdallrHTTPClientNSURLSession.swift in Sources */,
DC5220751BEA321300F37F2A /* OAuthAccessToken.swift in Sources */,
DC5220721BEA321300F37F2A /* HeimdallrResourceRequestAuthenticatorHTTPAuthorizationHeader.swift in Sources */,
E5E39D9A1D04D09000713AC9 /* OAuthAccessTokenICloudKeyValueStore.swift in Sources */,
DC52206F1BEA321300F37F2A /* HeimdallrHTTPClient.swift in Sources */,
E258A1691CC6BC7B00649F5A /* OAuthAccessTokenParser.swift in Sources */,
E258A16C1CC6BC8600649F5A /* OAuthAccessTokenDefaultParser.swift in Sources */,
Expand Down Expand Up @@ -927,6 +932,7 @@
DC52207D1BEA321500F37F2A /* HeimdallrHTTPClientNSURLSession.swift in Sources */,
DC5220821BEA321500F37F2A /* OAuthAccessToken.swift in Sources */,
DC52207F1BEA321500F37F2A /* HeimdallrResourceRequestAuthenticatorHTTPAuthorizationHeader.swift in Sources */,
E5E39D991D04C5CE00713AC9 /* OAuthAccessTokenICloudKeyValueStore.swift in Sources */,
E258A1681CC6B8C500649F5A /* OAuthAccessTokenParser.swift in Sources */,
DC52207C1BEA321500F37F2A /* HeimdallrHTTPClient.swift in Sources */,
E258A1661CC6B66900649F5A /* OAuthAccessTokenDefaultParser.swift in Sources */,
Expand Down
82 changes: 82 additions & 0 deletions Heimdallr/Core/OAuthAccessTokenICloudKeyValueStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// OAuthAccessTokenICloudKeyValueStore.swift
// Heimdallr
//
// Created by Fabio Milano on 05/06/16.
// Copyright © 2016 B264 GmbH. All rights reserved.
//

import Foundation

/// A persistent iCloud Key Value based access token store.
@objc public class OAuthAccessTokenICloudKeyValueStore: NSObject, OAuthAccessTokenStore {
private let store = NSUbiquitousKeyValueStore.defaultStore()
private let service: String

public init(service: String = "de.rheinfabrik.heimdallr.oauth") {
self.service = service
}

public func storeAccessToken(accessToken: OAuthAccessToken?) {
if let accessToken = accessToken {
var accessTokenDictionaryRepresentation = [String: String]()

accessTokenDictionaryRepresentation.updateValue(accessToken.accessToken, forKey: "access_token")
accessTokenDictionaryRepresentation.updateValue(accessToken.tokenType, forKey: "token_type")

if let refreshToken = accessToken.refreshToken {
accessTokenDictionaryRepresentation.updateValue(refreshToken, forKey: "refresh_token")
}

accessTokenDictionaryRepresentation.updateValue(accessToken.accessToken, forKey: "access_token")

if let expiresAt = accessToken.expiresAt {
accessTokenDictionaryRepresentation.updateValue(expiresAt.timeIntervalSince1970.description, forKey: "expires_at")
}

store.setDictionary(accessTokenDictionaryRepresentation, forKey: service)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the optional accessToken is nil?

synchronize()
}

public func retrieveAccessToken() -> OAuthAccessToken? {
synchronize()

if let accessTokenDictionaryRepresentation = store.dictionaryForKey(service) {
let accessToken = accessTokenDictionaryRepresentation["access_token"] as? String
let tokenType = accessTokenDictionaryRepresentation["token_type"] as? String
let refreshToken = accessTokenDictionaryRepresentation["refresh_token"] as? String
let expiresAtString = accessTokenDictionaryRepresentation["expires_at"] as? String

let expiresAt = expiresAtString.flatMap { description in
return Double(description).flatMap { expiresAtInSeconds in
return NSDate(timeIntervalSince1970: expiresAtInSeconds)
}
}

if let accessToken = accessToken, tokenType = tokenType {
return OAuthAccessToken(accessToken: accessToken, tokenType: tokenType, expiresAt: expiresAt, refreshToken: refreshToken)
}
}

return nil
}

private func synchronize() -> Void {
#if DEBUG
let synchronized = store.synchronize()

if !synchronized {
let userInfo = [
NSLocalizedDescriptionKey: NSLocalizedString("Could not initialize an iCloud Key Value Store", comment: ""),
NSLocalizedFailureReasonErrorKey: NSLocalizedString("Something went wrong in initializing an iCloud Key Value Store.", comment: "")
]

NSException(name: "OAuthAccessTokenICloudKeyValueStore", reason: NSLocalizedString("Make sure the app has registered to proper iCloud capabilities.", comment: ""), userInfo: userInfo).raise()
}
#else
store.synchronize()
#endif
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Missing newline :)

13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,25 @@ protocol OAuthAccessTokenStore {
}
```

Heimdallr ships with an already built-in persistent keychain-based access token store. The service is configurable:
Heimdallr ships with two already built-in persistent access token store.


#### Keychain-based access token store

The service is configurable:

```swift
var service: String!

let accessTokenStore = OAuthAccessTokenKeychainStore(service: service)
```

#### iCloud Key Value-based access token store

It stores values on the iCloud Key-Value Storage container for the current iCloud account.

Before using this persistent access token store, please make sure your are app has been properly setup for __iCloud Key-Value Storage__ capability. More info [here](https://developer.apple.com/library/mac/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW19).

### HeimdallrHTTPClient

An HTTP client that can be used by Heimdallr for requesting access tokens. It must implement the following `sendRequest` method:
Expand Down