簡體   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