FWOAuthCredential


@interface FWOAuthCredential : NSObject <NSCoding>

FWOAuthCredential models the credentials returned from an OAuth server, storing the token type, access & refresh tokens, and whether the token is expired.

OAuth credentials can be stored in the user’s keychain, and retrieved on subsequent launches.

Accessing Credential Properties

  • The OAuth access token.

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NSString *_Nonnull accessToken;
  • The OAuth token type (e.g. “bearer”).

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NSString *_Nonnull tokenType;
  • The OAuth refresh token.

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NSString *_Nonnull refreshToken;
  • Whether the OAuth credentials are expired.

    Declaration

    Objective-C

    @property (nonatomic, readonly, getter=isExpired) BOOL expired;

Creating and Initializing Credentials

  • Create an OAuth credential from a token string, with a specified type.

    Declaration

    Objective-C

    + (nonnull instancetype)credentialWithOAuthToken:(nonnull NSString *)token
                                           tokenType:(nonnull NSString *)type;

    Parameters

    token

    The OAuth token string.

    type

    The OAuth token type.

  • Initialize an OAuth credential from a token string, with a specified type.

    Declaration

    Objective-C

    - (nonnull id)initWithOAuthToken:(nonnull NSString *)token
                           tokenType:(nonnull NSString *)type;

    Parameters

    token

    The OAuth token string.

    type

    The OAuth token type.

Setting Refresh Token

  • Set the expiration on the access token. If no expiration is given by the OAuth2 provider, you may pass in [NSDate distantFuture]

    Declaration

    Objective-C

    - (void)setExpiration:(nonnull NSDate *)expiration;

    Parameters

    expiration

    The expiration of the access token. This must not be nil.

  • Set the credential refresh token, with a specified expiration.

    Declaration

    Objective-C

    - (void)setRefreshToken:(nonnull NSString *)refreshToken
                 expiration:(nonnull NSDate *)expiration;

    Parameters

    refreshToken

    The OAuth refresh token.

    expiration

    The expiration of the access token. This must not be nil.

Storing and Retrieving Credentials

  • Whether to store the credential in the Keychain. Default is No, store in NSUserDefaults. Must be set before use.

    Declaration

    Objective-C

    @property (class, nonatomic) BOOL storeCredentialInKeychain;
  • Stores the specified OAuth credential for a given web service identifier in the Keychain. with the default Keychain Accessibilty of kSecAttrAccessibleWhenUnlocked.

    Declaration

    Objective-C

    + (BOOL)storeCredential:(nonnull FWOAuthCredential *)credential
             withIdentifier:(nonnull NSString *)identifier;

    Parameters

    credential

    The OAuth credential to be stored.

    identifier

    The service identifier associated with the specified credential.

    Return Value

    Whether or not the credential was stored in the keychain.

  • Stores the specified OAuth token for a given web service identifier in the Keychain.

    Declaration

    Objective-C

    + (BOOL)storeCredential:(nonnull FWOAuthCredential *)credential
             withIdentifier:(nonnull NSString *)identifier
          withAccessibility:(nullable id)securityAccessibility;

    Parameters

    credential

    The OAuth credential to be stored.

    identifier

    The service identifier associated with the specified token.

    securityAccessibility

    The Keychain security accessibility to store the credential with.

    Return Value

    Whether or not the credential was stored in the keychain.

  • Retrieves the OAuth credential stored with the specified service identifier from the Keychain.

    Declaration

    Objective-C

    + (nullable FWOAuthCredential *)retrieveCredentialWithIdentifier:
        (nonnull NSString *)identifier;

    Parameters

    identifier

    The service identifier associated with the specified credential.

    Return Value

    The retrieved OAuth credential.

  • Deletes the OAuth credential stored with the specified service identifier from the Keychain.

    Declaration

    Objective-C

    + (BOOL)deleteCredentialWithIdentifier:(nonnull NSString *)identifier;

    Parameters

    identifier

    The service identifier associated with the specified credential.

    Return Value

    Whether or not the credential was deleted from the keychain.