繁体   English   中英

灰烬简单身份验证-自定义授权者破坏身份验证

[英]Ember Simple Auth - Custom Authorizer Breaks Authentication

我将Torer-cli-simple-auth插件与Torii一起用于身份验证流程。

到目前为止,我已经成功地使用定制的Torii Provider和定制的Simple Auth Authenticator进行身份验证。

现在,我想使用自定义的简单身份验证授权器将访问令牌注入请求中。

按照文档https://github.com/simplabs/ember-simple-auth#implementing-a-custom-authorizer,我添加了一个自定义授权者和初始化程序

authorizers/myservice.js

import Base from 'simple-auth/authorizers/base';
import Ember from 'ember';

export default Base.extend({
  /**
    @method authorize
    @param {jqXHR} jqXHR The XHR request to authorize (see http://api.jquery.com/jQuery.ajax/#jqXHR)
    @param {Object} requestOptions The options as provided to the `$.ajax` method (see http://api.jquery.com/jQuery.ajaxPrefilter/)
  */
  authorize: function(jqXHR) {
    var accessToken = this.get('session.content.token');
    if (this.get('session.isAuthenticated') && !Ember.isEmpty(accessToken)) {
      jqXHR.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    }
  }
});

initializers/authorization.js

import MyserviceAuthorizer from '../authorizers/myservice';

export function initialize(container, application) {
  container.register('authorizer:myservice', MyserviceAuthorizer);
}

export default {
  name: 'authorization',
  before: 'simple-auth',
  initialize: initialize
};

并包含在开发环境中的config/environment.js

ENV['simple-auth'] = {
  authorizer: 'authorizer:myservice',
  crossOriginWhitelist: ['*']
}

不幸的是,通过添加它,它现在破坏了身份验证。

看来Torii不再收到响应。

The response from the provider is missing these required response params: access_token, token_type, expires_in

我也在此处同时包括了Torii提供程序代码和简单身份验证器代码。

任何建议或帮助将不胜感激,我对此有点坚持。

torii-providers/myservice.js

import Provider from 'torii/providers/oauth2-bearer';
import {configurable} from 'torii/configuration';
import env from '../config/environment';

export default Provider.extend({
  name: 'myservice',
  baseUrl: (env.api_host + '/oauth/authorize'),

  responseParams: ['access_token', 'token_type', 'expires_in'],

  redirectUri: configurable('redirectUri', function(){
    // A hack that allows redirectUri to be configurable
    // but default to the superclass
    return this._super();
  })
});

和自定义的简单身份验证器

authenticators/myservice.js

import Ember from 'ember';
import Base from 'simple-auth/authenticators/base';
import ajax from 'ic-ajax';

export default Base.extend({
  restore: function(data) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      if(!Ember.isEmpty(data.currentUser)) {
        resolve(data);
      } else {
        reject();
      }
    });
  },

  authenticate: function(options) {
    return this.fetchOauthData(options).then(this.fetchUserData.bind(this));
  },

  fetchUserData: function(oauthData) {
    var token = oauthData.token.access_token;
    return ajax({
        url: '/api/v1/users/me',
        type: "GET",
        beforeSend: function (xhr) {
          xhr.setRequestHeader("Authorization", "Bearer " + token);
        }
    }).then(function(userJSON){
      return {
        currentUser: userJSON.user,
        token: token
      };
    });
  },

  fetchOauthData: function(options) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      options.torii.open(options.provider).then(function(oauthData) {
        resolve({
          provider: oauthData.provider,
          token: oauthData.authorizationToken
        });
      }, function(error) {
        reject(error);
      });
    });
  }
});

这可能与以下事实有关:Ember CLI自动将所有内容注册在容器中的app文件夹下。 尽管Ember CLI文档中的以下引用并没有清楚说明,但提供了提示:

app文件夹中的所有模块都可以由解析程序加载,但是通常应使用import语句手动加载诸如mixins和utils之类的类。

如果您的授权者文件为app/authorizers/myservice.js ,则Ember CLI将在容器上以'authorizer:myservice'名称注册该文件。 依次查找时,容器将创建一个单例实例。 由于您在初始化程序中进行了相同的注册,因此可能存在某种冲突。

暂无
暂无

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

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