【发布时间】: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