Skip to content

How to do externalAuthenticator with 2 step authentication and own oauth-server #82

@cedriclombardot

Description

@cedriclombardot

Hi,

I'm trying to authenticate my data-store and protect my pages using your packages. I follow the sample 7-external-oauth and try to adapt to my problems I use OAuth2 with response_type=code in first call and after call token endpoint with grant_type: authorization_code

So i try todo :

Demo.ExternalAuthenticator = Ember.SimpleAuth.Authenticators.OAuth2.extend({
    serverAuthEndpoint: 'http://connect.dev/oauth/v2/auth',

    client_id: '1_5un1xdsog1wkcgws8ksws48wsg0ow4ckw8g8cscwgwscwwog04',
    client_secret: '2wrfi8rc64kkss80c0ok0www8ss8800c00wo0co4k0048g0gg0',

    redirect_uri: 'http%3A%2F%2Flocalhost%3A9000%2F%23%2Foauth-callback',

    refreshTokenTimeout: null,
    serverTokenEndpoint: 'http://connect.dev/oauth/v2/token',

    restore: function(properties) {
      var _this = this;
      return new Ember.RSVP.Promise(function(resolve, reject) {
        if (!Ember.isEmpty(properties.access_token)) {
          resolve(properties);
        } else {
          reject();
        }
      });
    },
    authenticate: function() {
      var _this = this;
      return new Ember.RSVP.Promise(function(resolve, reject) {
        Demo.ExternalConnector.one('externalAuthenticationSucceeded', function(properties) {
          Ember.run(function() { resolve(properties); });
        });
        Demo.ExternalConnector.one('externalAuthenticationFailed', function(error) {
          Ember.run(function() { reject(error); });
        });
        // open a new window that displays the Facebook login dialog; see the callback handler on the server side
        // (get '/7-external-oauth/callback') in the runner file
        var authWindow = window.open(
          _this.getAuthUrl(),
          '_blank',
          'menubar=no,status=no,height=400,width=800'
        );
      });
    },
    authenticateUsingCode: function(code) {
      var _this = this;
      return new Ember.RSVP.Promise(function(resolve, reject) {
        var data = { grant_type: 'authorization_code', code: code, client_id: _this.client_id, client_secret: this.client_secret };
        _this.makeRequest(data).then(function(response) {
          consle.log(response);

          Ember.run(function() {
            _this.scheduleAccessTokenRefresh(response.expires_in, response.refresh_token);
            resolve(response);
          });
        }, function(xhr, status, error) {
          Ember.run(function() {
            reject(xhr.responseText);
          });
        });
      });
    },
    getAuthUrl: function() {
      return this.serverAuthEndpoint+'?client_id='+this.client_id+'&client_secret='+this.client_secret+'&redirect_uri='+this.redirect_uri+'&response_type=code';
    }
});

this code allow me to generate code and redirect on redirect_uri route wich look like :

Demo.OauthCallbackRoute = Ember.Route.extend({
    setupController: function (controller) {
        var getURLParameter = function getURLParameter(name) {
            return decodeURI(
                (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
            );
        };

        // How to call authenticateUsingCode of Demo.ExternalAuthenticator ?

        controller.set('model', { 'code': getURLParameter('code') });
    }
});

My problem is that i not found how to call Demo.ExternalAuthenticator.authenticateUsingCode() to set the response of my json in properties and continue the authentication process closing the popup and going to the protected page

Cedric

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions