简体   繁体   English

Django rest + ember simple auth身份验证器“ authenticator:oauth2”被拒绝恢复会话-无效

[英]Django rest + ember simple auth The authenticator “authenticator:oauth2” rejected to restore the session - invalidating

I use django rest framework and token auth as a backend auth. 我使用django rest框架和令牌身份验证作为后端身份验证。 From backend token comes as this format when user credentials submitted 提交用户凭据时,来自后端令牌的格式为

{token: "cKCxxxxxxxxxxxxxxxxxxxxx"} {令牌:“ cKCxxxxxxxxxxxxxxxxxxxxx””}

on the frontend ember-simple auth i use oauth2 as a authenticator when i try to login it says: 在前端ember-simple身份验证上,当我尝试登录时,我使用oauth2作为身份验证器:

The authenticator "authenticator:oauth2" rejected to restore the session - invalidating… 身份验证器“ authenticator:oauth2”被拒绝还原会话-无效…

and session is not saved it logged in but when the route change it logged out. 会话未保存但已登录,但路由更改后已注销。 How do i append token in headers? 如何在标头中附加令牌? It has to be appended automatically when use ember-simple-auth right or i get that wrong??? 当使用ember-simple-auth正确或我得到错误时,它必须自动附加???

login.js 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'));
    });
  } 
}

and authenticator: 和验证者:

import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';

export default OAuth2PasswordGrant.extend({
  serverTokenEndpoint: 'http://127.0.0.1:8000/api/auth/login/',
});

authorizer: import OAuth2Bearer from 'ember-simple-auth/authorizers/oauth2-bearer'; 授权者:从“ ember-simple-auth / authorizers / oauth2-bearer”导入OAuth2Bearer;

export default OAuth2Bearer.extend({
});

adapter: 适配器:

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',
});

TLDR : Ember Social API Looks for access_token while DRF send out token TLDR :Ember Social API在DRF发送token查找access_token

Server-Side Fix 服务器端修复

You need to subclass TokenSerializer and override keyword = Bearer 您需要子类化TokenSerializer并覆盖keyword = Bearer

Client Side Fix 客户端修复

The OAuth2Bearer expects a token value called access_token , and if this value exists it will add the Authorization header with a Bearer prefix. OAuth2Bearer需要一个称为access_token的令牌值,如果该值存在,它将添加带有Bearer前缀的Authorization标头。 However DRF Token expects the Authorization header to be prefixed with Token , see code below to better under stand 但是,DRF Token希望Authorization标头以Token为前缀,请参见下面的代码以更好地理解

Working Authorizer for DRF TOKEN DRF令牌的工作授权者

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

Reference to orignal OAuth2Bearer 对原始OAuth2Bearer的引用

Ember Social Auth - OAuth2Bearer 灰烬社交认证-OAuth2Bearer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM