Skip to content

Commit

Permalink
Fix - Ensure existing refreshToken returned in Credentials Manager (#146
Browse files Browse the repository at this point in the history
)

* Fix - Ensure existing refreshToken returned in Credentials Manager
Added - Renewed credentials are autosaved in Credentials Manager

* Update for latest Swiftlint 0.22

* Updated Fastlane, Settings, had issues with codecov
  • Loading branch information
cocojoe authored Sep 7, 2017
1 parent 9ceb2cc commit a454345
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 56 deletions.
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

12 changes: 10 additions & 2 deletions Auth0/CredentialsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public struct CredentialsManager {
}

/// Retrieve credentials from keychain and yield new credentials using refreshToken if accessToken has expired
/// otherwise the retrieved credentails will be returned as they have not expired.
/// otherwise the retrieved credentails will be returned as they have not expired. Renewed credentials will be
/// stored in the keychain.
///
///
/// ```
Expand Down Expand Up @@ -120,7 +121,14 @@ public struct CredentialsManager {
self.authentication.renew(withRefreshToken: refreshToken, scope: scope).start {
switch $0 {
case .success(let credentials):
callback(nil, credentials)
let newCredentials = Credentials(accessToken: credentials.accessToken,
tokenType: credentials.tokenType,
idToken: credentials.idToken,
refreshToken: refreshToken,
expiresIn: credentials.expiresIn,
scope: credentials.scope)
_ = self.store(credentials: newCredentials)
callback(nil, newCredentials)
case .failure(let error):
callback(.failedRefresh(error), nil)
}
Expand Down
1 change: 0 additions & 1 deletion Auth0/Identity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class Identity: NSObject, JSONObjectPayload {
return "<identity: \(identifier) provider: \(provider) connection: \(connection)>"
}

// swiftlint:disable:next function_parameter_count
public required init(identifier: String, provider: String, connection: String, social: Bool, profileData: [String: Any], accessToken: String?, expiresIn: Date?, accessTokenSecret: String?) {
self.identifier = identifier
self.provider = provider
Expand Down
1 change: 0 additions & 1 deletion Auth0/OAuth2Grant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ struct PKCE: OAuth2Grant {
self.init(authentication: authentication, redirectURL: redirectURL, verifier: generator.verifier, challenge: generator.challenge, method: generator.method, responseType: reponseType, nonce: nonce)
}

// swiftlint:disable:next function_parameter_count
init(authentication: Authentication, redirectURL: URL, verifier: String, challenge: String, method: String, responseType: [ResponseType], nonce: String? = nil) {
self.authentication = authentication
self.redirectURL = redirectURL
Expand Down
1 change: 0 additions & 1 deletion Auth0/Profile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public class Profile: NSObject, JSONObjectPayload {
return self["app_metadata"] as? [String: Any] ?? [:]
}

// swiftlint:disable:next function_parameter_count
required public init(id: String, name: String, nickname: String, pictureURL: URL, createdAt: Date, email: String?, emailVerified: Bool, givenName: String?, familyName: String?, attributes: [String: Any], identities: [Identity]) {
self.id = id
self.name = name
Expand Down
1 change: 0 additions & 1 deletion Auth0/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public struct Request<T, E: Auth0Error>: Requestable {
let logger: Logger?
let telemetry: Telemetry

// swiftlint:disable:next function_parameter_count
init(session: URLSession, url: URL, method: String, handle: @escaping (Response<E>, Callback) -> Void, payload: [String: Any] = [:], headers: [String: String] = [:], logger: Logger?, telemetry: Telemetry) {
self.session = session
self.url = url
Expand Down
1 change: 0 additions & 1 deletion Auth0/_ObjectiveManagementAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public class _ObjectiveManagementAPI: NSObject {
}

@objc(unlinkUserWithIdentifier:provider:fromUserId:callback:)
// swiftlint:disable:next function_parameter_count
public func unlink(identifier: String, provider: String, fromUserId userId: String, callback: @escaping (NSError?, [[String: Any]]?) -> Void) {
self.users
.unlink(identityId: identifier, provider: provider, fromUserId:userId)
Expand Down
26 changes: 23 additions & 3 deletions Auth0Tests/CredentialsManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import LocalAuthentication
@testable import Auth0

private let AccessToken = UUID().uuidString.replacingOccurrences(of: "-", with: "")
private let NewAccessToken = UUID().uuidString.replacingOccurrences(of: "-", with: "")
private let TokenType = "bearer"
private let IdToken = UUID().uuidString.replacingOccurrences(of: "-", with: "")
private let NewIdToken = UUID().uuidString.replacingOccurrences(of: "-", with: "")
private let RefreshToken = UUID().uuidString.replacingOccurrences(of: "-", with: "")
private let ExpiresIn: TimeInterval = 3600
private let ClientId = "CLIENT_ID"
Expand Down Expand Up @@ -118,7 +120,7 @@ class CredentialsManagerSpec: QuickSpec {
beforeEach {
error = nil
newCredentials = nil
stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authResponse(accessToken: AccessToken) }.name = "renew success"
stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authResponse(accessToken: NewAccessToken, idToken: NewIdToken, expiresIn: 86400) }.name = "renew success"
}

afterEach {
Expand Down Expand Up @@ -175,18 +177,36 @@ class CredentialsManagerSpec: QuickSpec {

context("renew") {

it("should yield new credentials") {
it("should yield new credentials, maintain refresh token") {
credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: Date(timeIntervalSinceNow: -3600))
_ = credentialsManager.store(credentials: credentials)
waitUntil(timeout: 2) { done in
credentialsManager.credentials { error = $0; newCredentials = $1
expect(error).to(beNil())
expect(newCredentials?.accessToken) == AccessToken
expect(newCredentials?.accessToken) == NewAccessToken
expect(newCredentials?.refreshToken) == RefreshToken
expect(newCredentials?.idToken) == NewIdToken
done()
}
}
}

it("should store new credentials") {
credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: Date(timeIntervalSinceNow: -3600))
_ = credentialsManager.store(credentials: credentials)
waitUntil(timeout: 2) { done in
credentialsManager.credentials { error = $0; newCredentials = $1
expect(error).to(beNil())
credentialsManager.credentials {
expect($1!.accessToken) == NewAccessToken
expect($1!.refreshToken) == RefreshToken
expect($1!.idToken) == NewIdToken
done()
}
}
}
}

it("should yield error on failed renew") {
stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authFailure(code: "invalid_request", description: "missing_params") }.name = "renew failed"
credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: Date(timeIntervalSinceNow: -3600))
Expand Down
6 changes: 5 additions & 1 deletion Auth0Tests/Responses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let Sub = "auth0|\(UUID().uuidString.replacingOccurrences(of: "-", with: ""))"
let LocaleUS = "en-US"
let ZoneEST = "US/Eastern"

func authResponse(accessToken: String, idToken: String? = nil) -> OHHTTPStubsResponse {
func authResponse(accessToken: String, idToken: String? = nil, expiresIn: Double? = nil) -> OHHTTPStubsResponse {
var json = [
"access_token": accessToken,
"token_type": "bearer",
Expand All @@ -50,6 +50,10 @@ func authResponse(accessToken: String, idToken: String? = nil) -> OHHTTPStubsRes
if let token = idToken {
json["id_token"] = token
}

if let expires = expiresIn {
json["expires_in"] = String(expires)
}
return OHHTTPStubsResponse(jsonObject: json, statusCode: 200, headers: ["Content-Type": "application/json"])
}

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

gem 'fastlane'
gem 'fastlane', '~> 2.5'
gem 'semantic', '~> 1.5'

plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
Expand Down
87 changes: 45 additions & 42 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.5.1)
public_suffix (~> 2.0, >= 2.0.2)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
babosa (1.0.2)
claide (1.0.1)
claide (1.0.2)
cocoapods (1.2.1)
activesupport (>= 4.0.2, < 5)
claide (>= 1.0.1, < 2.0)
Expand Down Expand Up @@ -46,68 +46,70 @@ GEM
cocoapods-try (1.1.0)
colored (1.2)
colored2 (3.1.2)
commander-fastlane (4.4.4)
commander-fastlane (4.4.5)
highline (~> 1.7.2)
declarative (0.0.9)
declarative-option (0.1.0)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.2.0)
dotenv (2.2.1)
escape (0.0.4)
excon (0.55.0)
faraday (0.12.0.1)
excon (0.59.0)
faraday (0.13.1)
multipart-post (>= 1.2, < 3)
faraday-cookie_jar (0.0.6)
faraday (>= 0.7.4)
http-cookie (~> 1.0.0)
faraday_middleware (0.11.0.1)
faraday_middleware (0.12.2)
faraday (>= 0.7.4, < 1.0)
fastimage (2.1.0)
fastlane (2.27.0)
fastlane (2.55.0)
CFPropertyList (>= 2.3, < 3.0.0)
addressable (>= 2.3, < 3.0.0)
babosa (>= 1.0.2, < 2.0.0)
bundler (>= 1.12.0, < 2.0.0)
colored
commander-fastlane (>= 4.4.0, < 5.0.0)
commander-fastlane (>= 4.4.5, < 5.0.0)
dotenv (>= 2.1.1, < 3.0.0)
excon (>= 0.45.0, < 1.0.0)
faraday (~> 0.9)
faraday-cookie_jar (~> 0.0.6)
faraday_middleware (~> 0.9)
fastimage (>= 1.6)
fastimage (>= 2.1.0, < 3.0.0)
gh_inspector (>= 1.0.1, < 2.0.0)
google-api-client (~> 0.9.2)
google-api-client (>= 0.13.1, < 0.14.0)
highline (>= 1.7.2, < 2.0.0)
json (< 3.0.0)
mini_magick (~> 4.5.1)
multi_json
multi_xml (~> 0.5)
multipart-post (~> 2.0.0)
plist (>= 3.1.0, < 4.0.0)
public_suffix (~> 2.0.0)
rubyzip (>= 1.1.0, < 2.0.0)
security (= 0.1.3)
slack-notifier (>= 1.3, < 2.0.0)
terminal-notifier (>= 1.6.2, < 2.0.0)
terminal-table (>= 1.4.5, < 2.0.0)
tty-screen (~> 0.5.0)
word_wrap (~> 1.0.0)
xcodeproj (>= 1.4.4, < 2.0.0)
xcodeproj (>= 1.5.0, < 2.0.0)
xcpretty (>= 0.2.4, < 1.0.0)
xcpretty-travis-formatter (>= 0.0.3)
fastlane-plugin-auth0_shipper (0.2.2)
semantic (~> 1.5)
fourflusher (2.0.1)
fuzzy_match (2.0.4)
gh_inspector (1.0.3)
google-api-client (0.9.28)
addressable (~> 2.3)
google-api-client (0.13.5)
addressable (~> 2.5, >= 2.5.1)
googleauth (~> 0.5)
httpclient (~> 2.7)
hurley (~> 0.1)
memoist (~> 0.11)
mime-types (>= 1.6)
representable (~> 2.3.0)
retriable (~> 2.0)
googleauth (0.5.1)
faraday (~> 0.9)
httpclient (>= 2.8.1, < 3.0)
mime-types (~> 3.0)
representable (~> 3.0)
retriable (>= 2.0, < 4.0)
googleauth (0.5.3)
faraday (~> 0.12)
jwt (~> 1.4)
logging (~> 2.0)
memoist (~> 0.12)
Expand All @@ -118,34 +120,35 @@ GEM
http-cookie (1.0.3)
domain_name (~> 0.5)
httpclient (2.8.3)
hurley (0.2)
i18n (0.8.4)
json (2.0.4)
json (2.1.0)
jwt (1.5.6)
little-plugger (1.1.4)
logging (2.2.2)
little-plugger (~> 1.1)
multi_json (~> 1.10)
memoist (0.15.0)
memoist (0.16.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_magick (4.5.1)
minitest (5.10.2)
molinillo (0.5.7)
multi_json (1.12.1)
multi_json (1.12.2)
multi_xml (0.6.0)
multipart-post (2.0.0)
nanaimo (0.2.3)
nap (1.1.0)
netrc (0.7.8)
os (0.9.6)
plist (3.2.0)
plist (3.3.0)
public_suffix (2.0.5)
representable (2.3.0)
uber (~> 0.0.7)
retriable (2.1.0)
rouge (1.11.1)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
uber (< 0.2.0)
retriable (3.1.1)
rouge (2.0.7)
ruby-macho (1.1.0)
rubyzip (1.2.1)
security (0.1.3)
Expand All @@ -156,26 +159,26 @@ GEM
jwt (~> 1.5)
multi_json (~> 1.10)
slack-notifier (1.5.1)
terminal-notifier (1.7.1)
terminal-table (1.7.3)
unicode-display_width (~> 1.1.1)
terminal-notifier (1.8.0)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
thread_safe (0.3.6)
tty-screen (0.5.0)
tzinfo (1.2.3)
thread_safe (~> 0.1)
uber (0.0.15)
uber (0.1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.3)
unicode-display_width (1.1.3)
unf_ext (0.0.7.4)
unicode-display_width (1.3.0)
word_wrap (1.0.0)
xcodeproj (1.4.4)
xcodeproj (1.5.1)
CFPropertyList (~> 2.3.3)
claide (>= 1.0.1, < 2.0)
claide (>= 1.0.2, < 2.0)
colored2 (~> 3.1)
nanaimo (~> 0.2.3)
xcpretty (0.2.6)
rouge (~> 1.8)
xcpretty (0.2.8)
rouge (~> 2.0.7)
xcpretty-travis-formatter (0.0.4)
xcpretty (~> 0.2, >= 0.0.7)

Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
cache_directories:
- "Carthage/Build"
override:
- bundle install
- bundle install --without=development
- bundle exec fastlane ios bootstrap

test:
Expand Down
1 change: 1 addition & 0 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ platform :ios do
scheme: scheme,
device: device,
clean: true,
skip_build: true,
output_types: 'junit'
)
end
Expand Down

0 comments on commit a454345

Please sign in to comment.