Ember 简单的 Auth Torri 自定义提供程序

Ember Simple Auth Torri custom Provider

我正在尝试为 Yahoo OAuth 2.0 的 Ember Simple Auth Torri 包装器创建自定义 OAuth 提供程序。 我使用了 Facebook 和 Google 的内置提供程序,没有任何问题,但是由于默认情况下不提供 Yahoo OAuth 包,我正在尝试按照手册创建自己的包。

//app/torri-provider/yahoo-oauth2.js

export default Ember.Object.extend({
  host: 'https://api.login.yahoo.com/oauth2/',
  // create a new authorization
  open: function(options) {
    return new Ember.RSVP.Promise(function(resolve, reject){
      console.log("Hi");
      var authurl="https://api.login.yahoo.com/oauth2/request_auth";

      return $.ajax(authurl, "GET", {
        // CORS
        crossDomain: true,
        xhrFields: {withCredentials: true}
      }).then(function(json) {
        // Massage this demo API endpoint to look like RESTAdapter expects.
        return { things: [json] };
      });


    });
  }
});

在我的控制器中,我称它为 -

'yahoo-share':function(){
  var self=this;
  this.get('session').authenticate('simple-auth-authenticator:torii',"yahoo-oauth2");
},

但是我无法解决 CORS 问题并在我的控制台上收到以下错误-

userhomeinvitemembers:1 XMLHttpRequest cannot load https://api.login.yahoo.com/oauth2/request_auth. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

我已经尝试将 oauth2 端点添加到 ember cli 白名单和内容安全策略白名单,但仍然出现相同的错误。

  ENV.contentSecurityPolicy = {
    'default-src': "'none'",
    'script-src': "'self' http://localhost:4200/",
    'font-src': "'self' http://localhost:4200/",
    'connect-src': "'self' http://localhost:4200/ http://localhost:3000/ http://192.168.1.173:3000/ https://api.login.yahoo.com/oauth2/request_auth",
    'img-src': "'self'",
    'style-src': "'self'",
    'media-src': "'self'"
  },


    ENV['simple-auth'] = {
      crossOriginWhitelist: ['https://api.login.yahoo.com/oauth2/request_auth'],
      authorizer: 'simple-auth-authorizer:oauth2-bearer',
      authenticationRoute: 'index',
      routeIfAlreadyAuthenticated:'userwelcome',

    },

    ENV['torii'] = {
      providers: {
        'facebook-oauth2': {
          apiKey: '799728020115355'
        },
        'google-oauth2': {
          apiKey:'299472944809-sddblocmketamp64sapk51qdrromkj0g.apps.googleusercontent.com',
          scope: 'https://www.google.com/m8/feeds/',
          redirectUri:'http://localhost:4200/userhomeinvitemembers'
        },
        'yahoo-oauth2': {
          apiKey:'dj0yJmk9UmpXWG1odlVlenRSJmQ9WVdrOVdFUkxRbVo2TkdVbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD04Zg--',

        }
      }
    };

我认为您不能通过 AJAX 调用它,而是需要打开一个弹出窗口以允许用户输入他们的 Yahoo 凭据并授权您的应用程序。完成后,雅虎将重定向到您定义的路由,并在查询字符串中包含一个授权代码,然后 torii 读取该代码并将其发回父 window(请参阅默认 OAuth 2.0 提供程序的来源'open方法:https://github.com/Vestorly/torii/blob/master/lib/torii/providers/oauth2-code.js#L118).