GithubHelp home page GithubHelp logo

oauthtokenmanager's Introduction

OAuthTokenManager

OAuthTokenManager is a simple, opinionated way to handle authentication and token management with Swift and Apple's Keychain. It uses a bit of SwiftKeychainWrapper to handle communicating with the Keychain and begins the authentication flow with SFAuthenticationSession.

This manager works only with the OAuth Code Grant Flow.

Installation

Just add OAuthTokenManager.swift, KeychainWrapper.swift, and KeychainItemAccessibility.swift to your Xcode project.

How To Use

Initialization

You'll need to initialize a manager for any OAuth Service you use.

let tokenManager = OAuthTokenManager(
    authorizationUrl: exampleAuthUrl,
    tokenExchangeUrl: exampleExchangeUrl,
    redirectUri: "exampleapp://auth",
    clientId: "your_client_id",
    clientSecret: "super_secret_string",
    keychainPrefix: "example"
)

To be notified of a successful login, you will need to register your redirect URI scheme ("exampleapp://" in the example above) in your app's Info.plist.

The keychainPrefix is used when storing your session in Keychain. If no value is provided, OAuthTokenManager uses a default prefix. You should override this if you are using more than one OAuth service in your app (or if you might). You can access the complete key string through the managers keychainKeys param.

Note: Be sure to maintain a reference to this manager at least throughout the complete authentication process. If you see a "Your App" Wants to Use "X Service" to Sign In alert that immediately disappears, not maintaining this reference is probably your issue.

Authentication

To authenticate, simply call authenticate(callback:) with your manager. You'll receive the session after successful authentication. This same method handles storing the current auth session securely in Keychain.

tokenManager.authenticate { session, error in
    let authToken = session.authToken
    // Do something with your session
}

Current Session

You can access the current session at any time with your manager.

let session = tokenManager.currentSession

The first time you access this after initializing your manager (before calling authenticate(callback:)), the manager will check for an existing session in Keychain. This should be checked prior to authenticating to prevent unnecessary re-authentication. If a previous session has expired, currentSession will retrun nil and be removed from Keychain upon first access.

Refreshing Your Token

You can refresh an auth token at any time by simply calling refresh(callback:) with your manager.

tokenManager.refresh { session, error in
    let authToken = session.authToken
    // Do something with your session
}

You can check expiresAt on your session if you'd like to manage how often you refresh more carefully.

oauthtokenmanager's People

Contributors

dustinburge avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.