ember.jstorii

torii gets authorization code instead access token


I have an ember app that connects with github, but the authentication flow returns the authorization code, not the access token, and I don't really know how to do the exchange...well, at least I didn't found any examples on the internet.

I'm kinda new to ember, this is what I got so far

authenticator/torii.js

import Ember from 'ember';
import ToriiAuthenticator from 'ember-simple-auth/authenticators/torii';

export default ToriiAuthenticator.extend({
  torii: Ember.inject.service()
});

torii-providers/github.js

import GithubOauth2Provider from 'torii/providers/github-oauth2';

export default GithubOauth2Provider.extend({
  fetch(data) {
    return data;
  }
});

I know I may have to change something in the provider, but I don't really know where to start


Solution

  • I've used Torii to do GitHub auth myself. Here's my advice:

    1. Drop ember-simple-auth and just use Torii directly. Ironically, ember-simple-auth's Torii wrapper isn't "simple".
    2. You should go over Torii's docs to familiarize yourself with the library.
    3. In your config/environment.js, configure Torii. Example:

      torii: {
        sessionServiceName: 'session',
        providers: {
          'github-oauth2': {
             // your api key goes here
             apiKey: '',
      
             // link to your app goes here
             // in development mode, it should be http://localhost:4200
             redirectUri: '',
      
             // specify OAuth scope here
             scope: ''
          }
        }
      }
      
    4. Create a file called torii-adapters/application.js. Here you will need to implement the three methods .open(), .fetch(), and .close(). Note that you will receive the authorizationCode as a parameter for .open(), which you should exchange (with your auth backend) for an access token.

    5. Oh, and you'll need an OAuth backend that keeps your client secret private. You send the authorization code from your Ember app to your OAuth backend, and the OAuth backend responds with an access token.

    If none of that made any sense to you, check out this blog post, which has a good summary of OAuth. You should understand the big picture so that filling in the details is easy. :)