AWFOAuthCredential

Objective-C

@interface AWFOAuthCredential : NSObject <NSCoding>

Swift

class AWFOAuthCredential : NSObject, NSCoding

AWFOAuthCredential 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;

    Swift

    var accessToken: String { get }
  • The OAuth token type (e.g. “bearer”).

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NSString *_Nonnull tokenType;

    Swift

    var tokenType: String { get }
  • The OAuth refresh token.

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NSString *_Nonnull refreshToken;

    Swift

    var refreshToken: String { get }
  • Whether the OAuth credentials are expired.

    Declaration

    Objective-C

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

    Swift

    var isExpired: Bool { get }

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;

    Swift

    init(oAuthToken token: String, tokenType type: String)

    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;

    Swift

    func setExpiration(_ expiration: Date)

    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;

    Swift

    func setRefreshToken(_ refreshToken: String, expiration: Date)

    Parameters

    refreshToken

    The OAuth refresh token.

    expiration

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

Storing and Retrieving Credentials

  • 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 AWFOAuthCredential *)credential
             withIdentifier:(nonnull NSString *)identifier;

    Swift

    class func store(_ credential: AWFOAuthCredential, withIdentifier identifier: String) -> Bool

    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 AWFOAuthCredential *)credential
             withIdentifier:(nonnull NSString *)identifier
          withAccessibility:(nonnull id)securityAccessibility;

    Swift

    class func store(_ credential: AWFOAuthCredential, withIdentifier identifier: String, withAccessibility securityAccessibility: Any) -> Bool

    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 AWFOAuthCredential *)retrieveCredentialWithIdentifier:
        (nonnull NSString *)identifier;

    Swift

    class func retrieveCredential(withIdentifier identifier: String) -> AWFOAuthCredential?

    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;

    Swift

    class func delete(withIdentifier identifier: String) -> Bool

    Parameters

    identifier

    The service identifier associated with the specified credential.

    Return Value

    Whether or not the credential was deleted from the keychain.