Django rest + ember simple auth验证者“authenticator:oauth2”被拒绝恢复会话 - 使

时间:2016-07-13 06:06:15

标签: django authentication ember.js django-rest-framework ember-simple-auth

我使用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';

导入OAuth2Bearer
export 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',
});

1 个答案:

答案 0 :(得分:1)

TLDR :当DRF发送access_token时,Ember Social API会查找token

服务器端修复

您需要继承TokenSerializer并覆盖keyword = Bearer

客户端修复

OAuth2Bearer需要一个名为access_token的令牌值,如果此值存在,则会添加带有Bearer前缀的Authorization标头。 但是,DRF Token期望Authorization标头以Token为前缀,请参阅下面的代码以更好地展示

DRF 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}`);
    }
  }
});

参考orignal OAuth2Bearer

Ember Social Auth - OAuth2Bearer