我使用django rest框架和令牌身份验证作为后端身份验证。在提交用户凭据时,来自后端令牌的格式为
{token:" cKCxxxxxxxxxxxxxxxxxxxxx"}
在前端ember-simple auth上我尝试登录时使用oauth2作为身份验证器说:
验证者"验证者:oauth2"拒绝恢复会话 - 无效......
并且会话未保存它已登录但当路由更改时它已注销。如何在标题中附加标记?当使用ember-simple-auth时,它必须自动附加或我得到错误???
login.js
actions: {
authenticate(username, password) {
var controller = this.controller;
this.get('session').authenticate('authenticator:oauth2', username, password).catch((reason) => {
controller.set('errorMessage', reason.detail || reason);
console.log(this.get('session.data.authenticated'));
});
}
}
和验证者:
import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';
export default OAuth2PasswordGrant.extend({
serverTokenEndpoint: 'http://127.0.0.1:8000/api/auth/login/',
});
授权人: 从' ember-simple-auth / authorizers / oauth2-bearer';
导入OAuth2Bearerexport default OAuth2Bearer.extend({
});
适配器:
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.RESTAdapter.extend(DataAdapterMixin, {
host: 'http://127.0.0.1:8000',
namespace: 'api',
authorizer: 'authorizer:oauth2',
});
答案 0 :(得分:1)
TLDR :当DRF发送access_token
时,Ember Social API会查找token
您需要继承TokenSerializer并覆盖keyword = Bearer
OAuth2Bearer需要一个名为access_token
的令牌值,如果此值存在,则会添加带有Bearer
前缀的Authorization标头。
但是,DRF Token期望Authorization标头以Token
为前缀,请参阅下面的代码以更好地展示
import Ember from 'ember';
import Base from 'ember-simple-auth/authorizers/base';
const { isEmpty } = Ember;
export default Base.extend({
authorize(data, block) {
const accessToken = data['token'];
if (!isEmpty(accessToken)) {
block('Authorization', `Token ${accessToken}`);
}
}
});