我正在尝试设置一个对许多提供者有效的身份验证器,因为在后端,我使用了门卫断言方法来处理整个流程。
我安装了:
* "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"我看了这个问题,Ember-simple-auth,Torii和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'
}
}
};我不确定这是否更适合我的要求,因为现在我遇到的问题是我不能运行开放的托利方法,有人知道我做错了什么吗?如果你有一个更好的解决这个问题的办法,将不胜感激。
错误:

发布于 2015-04-24 18:23:45
您不应该将OAuth 2.0身份验证器扩展到使用OAuth 2.0进行身份验证,因为OAuth 2.0和torii与Ember简单的8月观点有很大的不同,尽管大多数OAuth提供程序都连接到OAuth 2.0后端。只需使用torii身份验证器并将您想要用作Session.authenticate的第二个参数的torii提供程序传递。请参阅这个例子,以了解它是如何工作的。
发布于 2015-04-26 08:26:18
目前,我正在使用类似于问题中描述的方法,但我通过初始化器将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(...)我还考虑了他在评论中提到的解决方案,但对我来说,这会导致与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);
});
});
});
}https://stackoverflow.com/questions/29851214
复制相似问题