ember-cli torii和多个提供商

时间:2015-04-24 15:14:23

标签: ember.js ember-cli ember-simple-auth torii

我正在尝试设置一个对许多提供程序有效的身份验证器,因为在后端我使用了管理整个流程的门卫断言方法。

我已安装:

* "ember-cli-simple-auth": "0.8.0-beta.1"
* "ember-cli-simple-auth-oauth2": "^0.8.0-beta.2"
* "ember-cli-simple-auth-torii": "^0.8.0-beta.2"
* "torii": "^0.3.4"

我正在研究这个问题Workflow for Ember-simple-auth, Torii and Facebook Oauth2所以我可以这样写:

# templates/login
<a {{action 'oauth2Authenticate' 'facebook-oauth2'}}>Login with facebook</a>
<a {{action 'oauth2Authenticate' 'google-oauth2'}}>Login with google</a>

# controllers/login
actions: {
  oauth2Authenticate: function(provider) {
    this.get('session').authenticate('authenticator:oauth2', { torii: this.get('torii'), provider: provider });
  }
}

# initializers/authentication
import Oauth2Authenticator from '../authenticators/oauth2';
export function initialize(container) {
  container.register('authenticator:oauth2', Oauth2Authenticator);
}
export default {
  name: 'authentication',
  initialize: initialize
};

# authenticators/oauth2
import Ember from 'ember';
import OAuth2 from 'simple-auth-oauth2/authenticators/oauth2';

export default OAuth2.extend({  
  authenticate: function(options) {
    var self = this;
    console.log(options.provider);
    return new Ember.RSVP.Promise(function(resolve, reject) {
      options.torii.open(options.provider).then(function(data) {
        var data = {
          grant_type: 'assertion',
          provider: options.provider,
          assertion: data.authorizationCode         
        };
        self.makeRequest(self.serverTokenEndpoint, data).then(function(response) {
          Ember.run(function() {
            var expiresAt = self.absolutizeExpirationTime(response.expires_in);
            self.scheduleAccessTokenRefresh(response.expires_in, expiresAt, response.refresh_token);
            resolve(Ember.$.extend(response, { expires_at: expiresAt }));
          });
        }, function(xhr, status, error) {
          Ember.run(function() {
            reject(xhr.responseJSON || xhr.responseText);
          });
        });
      }, reject);
    });
  }
});

# config/environment
ENV['simple-auth'] = {
  authorizer: 'simple-auth-authorizer:oauth2-bearer',
  crossOriginWhitelist: ['*']
};

ENV['simple-auth-oauth2'] = {
  serverTokenEndpoint: ENV.host + '/oauth/token',
  refreshAccessTokens: true
};

ENV['torii'] = {
  providers: {
    'facebook-oauth2': {
      apiKey:      '631252926924840',
      redirectUri: 'http://localhost:4200'
    }, 
    'google-oauth2': {
      apiKey:      '631252926924840',
      redirectUri: 'http://localhost:4200'
    }
  }
};
  • POST / oauth / token: 我将以下参数传递给服务器:1。grant_type =&#34;断言&#34; 2.提供者3.断言=&#34; 3dPartyToken&#34;

我不确定这是否是更好的方式来满足我的要求,因为现在我遇到的问题是我无法运行开放的torii方法,任何人都知道我做错了什么?如果您对此问题有更好的解决方案,将非常感激。

错误: enter image description here

2 个答案:

答案 0 :(得分:1)

您不应该扩展OAuth 2.0身份验证器以使用torii进行身份验证,因为OAuth 2.0和Torii与Ember Simple Auth的观点非常不同,尽管大多数Torii提供程序连接到OAuth 2.0后端。只需使用torii身份验证器,并将要用作第二个参数的torii提供程序传递给Session.authenticate。请参阅this example以了解其工作原理。

答案 1 :(得分:0)

目前我正在使用问题中描述的类似方法,但我通过初始化程序将torii注入我的身份验证器中:

export default {
  name: 'custom-torii-oauth2-config',
  before: 'simple-auth',
  after: 'torii',

  initialize: function(container, application) {
    application.inject('authenticator:custom-torii-oauth2', 'torii', 'torii:main');
  }
};

之后,它可以在身份验证器中使用,如下所示:

this.torii.open(...)

我也考虑过他的评论中提到的marcoow解决方案,但对我而言,oauth2-authenticator会产生很多重复的代码。 torii-authenticator的扩展名不是很好,这正在处理这个流程吗?

修改 custom-torii-oauth2身份验证者的重要部分。

  fetchOauthData: function(options) {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
      _this.torii.open(options.provider).then(function(oauthData) {
        Ember.run(function() {
          resolve({
            grant_type: 'authorization_code',
            provider: oauthData.provider,
            code: oauthData.authorizationCode,
          });
        });
      }, function(error) {
        Ember.run(function() {
          reject(error);
        });
      });
    });
  }