CredentialsTokenTTL
public protocol CredentialsTokenTTL : AnyObject
Protocol to make it easier to add token TTL (Time To Live) to credentials plugins.
Using this protocol:
Step 1) Conform to the protocol Step 2) Call one of the two getProfileAndCacheIfNeeded methods– probably at the end of your authenticate method:
Either: Step 2a) Typical plugins will call the getProfileAndCacheIfNeeded method with the onSuccess and onFailure closures. I.e., typical plugins will either simply fail or succeed when attempting to generate a user profile when generateNewProfile is called. E.g., see https://github.com/crspybits/CredentialsMicrosoft/blob/master/Sources/CredentialsMicrosoft/CredentialsMicrosoftToken.swift
Or: Step 2b) More complicated plugins will call the getProfileAndCacheIfNeeded method with the single, completion, closure. These plugins (e.g., see https://github.com/Kitura/Kitura-CredentialsJWT/blob/master/Sources/CredentialsJWT/CredentialsJWT.swift) not only either succeed or fail, but they can have a third, unprocessable result.
-
Needed for caching (token, user profile) pairs until their TTL expires or they are evicted from the cache. If nil, no caching of user profiles is carried out.
Declaration
Swift
var usersCache: NSCache<NSString, BaseCacheElement>? { get }
-
The specific TTL value used by the plugin. If nil, the TTL is not used.
Declaration
Swift
var tokenTimeToLive: TimeInterval? { get }
-
Used by the getProfileAndCacheIfNeeded method to generate a profile if one can’t be used from cache.
Declaration
Swift
func generateNewProfile(token: String, options: [String : Any], completion: @escaping (CredentialsTokenTTLResult) -> Void)
Parameters
token
The Oauth2 token, used as a key in the cache.
options
The dictionary of plugin specific options. Effects: Method you implement needs to generate a new profile for a user given the token, if possible. Your method is not reponsible for caching the resulting profile (if successful). Caching is handled by other components of this protocol.
-
getProfileAndCacheIfNeeded(token:
Extension methodoptions: onSuccess: onFailure: ) Calls the completion handler with the profile (from cache or generated with the protocol generateNewProfile method), or failure result. This method should be suited to most plugins that use a TTL.
Declaration
Swift
public func getProfileAndCacheIfNeeded( token: String, options: [String:Any], onSuccess: @escaping (UserProfile) -> Void, onFailure: @escaping (HTTPStatusCode?, [String:String]?) -> Void)
Parameters
token
The Oauth2 token, used as a key in the cache.
options
The dictionary of plugin specific options.
onSuccess
From the authentication method.
onFailure
From the authentication method.
-
getProfileAndCacheIfNeeded(token:
Extension methodoptions: completion: ) Calls the completion handler with the profile (from cache or generated with the protocol generateNewProfile method), or failure result. This method is suited to plugins with more complicated credentials needs. E.g., the Credentials JWT.
Declaration
Swift
public func getProfileAndCacheIfNeeded( token: String, options: [String:Any], completion: @escaping (CredentialsTokenTTLResult) -> Void)
Parameters
token
The Oauth2 token, used as a key in the cache.
options
The dictionary of plugin specific options.
completion
The detailed credentials TTL result.