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

Added credential manager methods clear and hasValid #133

Merged
merged 5 commits into from
Jul 10, 2017

Conversation

cocojoe
Copy link
Member

@cocojoe cocojoe commented Jul 6, 2017

Additional methods added to CredentialsManager

clear() -> Delete credentials from keychain storage
hasValid() -> Quick check that valid credentials are available & not expired

@cocojoe cocojoe added this to the v1-Next milestone Jul 6, 2017
@cocojoe cocojoe requested review from hzalaz and lbalmaceda July 7, 2017 09:50
hzalaz
hzalaz previously approved these changes Jul 7, 2017
@@ -58,6 +58,26 @@ public struct CredentialsManager {
return self.storage.setData(NSKeyedArchiver.archivedData(withRootObject: credentials), forKey: storeKey)
}

/// Clear credentials stored in keychain
///
/// - Returns: Bool outcome of removal success
Copy link
Member

Choose a reason for hiding this comment

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

no need to state the Bool type, just say true if credentials were removed, false if no credentials were found or it failed to remove`


/// Checks if valid credentials are available
///
/// - Returns: Bool outcome of result
Copy link
Member

Choose a reason for hiding this comment

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

no need to state the Bool type, just say if there are valid and non-expired credentials stored`

let data = self.storage.data(forKey:self.storeKey),
let credentials = NSKeyedUnarchiver.unarchiveObject(with: data) as? Credentials,
credentials.accessToken != nil,
let expiresIn = credentials.expiresIn, expiresIn > Date()
Copy link
Member

Choose a reason for hiding this comment

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

I think it will be clear to have

let expiresIn = credentials.expiresIn else { return false }
return expiresIn > Date()

@hzalaz
Copy link
Member

hzalaz commented Jul 7, 2017

Also I might be inclined to have just clear and hasValid without the Credentials

@cocojoe
Copy link
Member Author

cocojoe commented Jul 8, 2017

@hzalaz These were my original thoughts on naming conventions as it's implied by the context of the class. However the argument is consistency with Android although the Swift API guidelines would say Omit Needless Words 😄

Added `clearCredentials` method
Added relevant tests
@cocojoe cocojoe force-pushed the added-credential-manager-extras branch from bd75222 to bbf5ed4 Compare July 10, 2017 08:29
@cocojoe cocojoe changed the title Added credential manager methods clearCredentials and hasCredentials Added credential manager methods clear and hasValid Jul 10, 2017
Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

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

Just a small missing check. I'm also considering that storing either access_token or id_token should give "valid credentials". Is there a reason to limit it just to access_token? cc @hzalaz

@@ -53,11 +53,31 @@ public struct CredentialsManager {
/// Store credentials instance in keychain
///
/// - Parameter credentials: credentials instance to store
/// - Returns: Bool outcome of success
/// - Returns: if credentials were stored
Copy link
Contributor

Choose a reason for hiding this comment

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

how can this fail?

Copy link
Member Author

Choose a reason for hiding this comment

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

The underlying method can fail, it's just unlikely to ever happen :)

///
/// - Returns: if credentials were removed
public func clear() -> Bool {
return self.storage.deleteEntry(forKey: storeKey)
Copy link
Contributor

Choose a reason for hiding this comment

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

how can this fail?

Copy link
Member Author

Choose a reason for hiding this comment

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

The underlying method can fail, it's just unlikely to ever happen :)

credentials.accessToken != nil,
let expiresIn = credentials.expiresIn
else { return false }
return expiresIn > Date()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is one of the conditions. But if expiresIn <= Date() && credentials.refreshToken != nil you will be able to refresh it, so that's another case that returns "valid credentials". No network call required here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know if the refreshToken is valid or not, could be revoked. This check tells me if the current accessToken has expired or not, if it has expired it is invalid and the user should request fresh credentials with retrieveCredentials.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having the refresh_token revoked is an edge case. Admin has to do that either manually in the user's dashboard or calling the MGMT API with a well-scoped token. I rather pay the price of a 401 and make the user clear the credentials than saying "your token is no longer valid" when I know that I can refresh it. That it's also a valid credentials scenario IMO.

Copy link
Member

@hzalaz hzalaz Jul 10, 2017

Choose a reason for hiding this comment

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

This is just checking the AT (credentials) not if the RT is valid or not, which as @cocojoe said it will imply that the app should call to obtain fresh credentials. In the case it fails the app could prompt to re-login

Copy link
Member

Choose a reason for hiding this comment

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

If it were me I'd have the following props:

  • empty: if there is an AT stored
  • expired: if the AT is expired
  • canRefresh: if there is a RT

@cocojoe cocojoe merged commit 4293564 into master Jul 10, 2017
@cocojoe cocojoe deleted the added-credential-manager-extras branch July 10, 2017 19:09
@cocojoe cocojoe modified the milestones: 1.7.1, v1-Next Jul 11, 2017
@cocojoe cocojoe mentioned this pull request Jul 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants