【问题标题】:ember-cli torii and multiple providersember-cli torii 和多个提供程序
【发布时间】:2015-04-24 15:14:22
【问题描述】:

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

我已经安装了:

* "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/令牌: 我将以下参数传递给服务器:1. grant_type="assertion" 2. provider 3. assertion="3dPartyToken"

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

错误:

【问题讨论】:

  • 你想用你自定义API的token交换第三方token吗?
  • @kunerd 完全在第三方认证的情况下,/oauth/token 必须接收下一个参数(provider="facebook"|"google"、grant_type="assertion" 和 assertion="3dPartyToken ")
  • 在这种情况下,您应该继承 torii 身份验证器,覆盖 authenticate 方法,并在超级解析时向您的服务器发出请求以交换令牌。
  • @marcoow 你的意思是像我在问题中描述的那样吗?我实际上看到它是一样的。如果能举例说明流程就好了。

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


【解决方案1】:

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

【讨论】:

  • @macoow 我实际上有同样的情况,但在通过 torii 进行 facebook 身份验证后,没有请求“/oauth/token”发送到服务器。我现在有 facebook-oauth2 提供程序和 simple-auth-authenticator:torii 但不工作。我应该在某个地方需要牌坊吗?
  • @macoow,我扩展了simple-auth-torii/authenticators/torii 并覆盖了authenticate 方法并在我的新类中调用了“this._super(provider, options)”。但似乎超级类无法找到 torii 对象,它给了我一个错误Cannot read property 'open' of undefined。如何扩展 torii 以覆盖身份验证方法以及如何将 torii 实例获取到我的新类以调用诸如“打开”之类的方法?
【解决方案2】:

目前我正在使用问题中描述的类似方法,但我通过初始化程序将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);
        });
      });
    });
  }

【讨论】:

  • 我喜欢你的解决方案,即使我不确定什么是最好的实现。你能放一些custom-torii-oauth2身份验证器的代码吗?!我不知道如何处理 torii open 方法,torii 对我来说似乎总是未定义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
相关资源
最近更新 更多