javascript - Ember-Simple-Auth OAuth 2.0 没有发送正确的参数

标签 javascript ember.js oauth ember-simple-auth

因此,我正在使用 OAuth 2.0 扩展设置 ember-simple-auth。问题是每当我尝试登录并查看作为表单数据发送的内容时,它只发送 grant_typepassword 参数。但是,password 参数始终为空,有时甚至不显示。 username 参数总是丢失,目前我还没有看到。

这是我的 login.hbs 代码(顺便说一句,我正在使用 ember-cli)

<form {{action 'authenticate' on='submit'}}>
  <label for="identification">Login</label>
  {{input class="form-control" id='identification' placeholder='Enter Login' value=identification}}
  <label for="password">Password</label>
  {{input class="form-control" id='password' placeholder='Enter Password' type='password' value=password}}
  <button type="submit">Login</button>
</form>

我的 login.js 代码在 controllers

import Ember from 'ember';
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';

export default Ember.Controller.extend(LoginControllerMixin, {
  authenticator: 'simple-auth-authenticator:oauth2-password-grant'
});

我的 application.js 代码在 controllers

// app/routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin);

在我的config/environment.js 文件中

if (environment === 'development') {
  …
  ENV['simple-auth-oauth2'] = {
    serverTokenEndpoint: '/api/v1/oauth/token'
  }
  …
}

在此更改后调用正确的 url

在我的初始化文件夹中

// app/initializers/simple-auth-config.js
export default {
  name:       'simple-auth-config',
  before:     'simple-auth',
  initialize: function() {
    window.ENV = FrontENV;
  }
};

很容易注意到大部分代码是从 simplelabs 上的教程复制而来的,并进行了一些自定义。但是,我无法弄清楚为什么没有正确发送参数。任何帮助将不胜感激!

最佳答案

缺少的参数是client_idclient_secret。 Ember-Simple-Auth OAuth 2.0 不包括这些,因为它们在 ember 应用程序中使用时不是 secret 的。您需要像这样创建一个自定义身份验证器以包含两个参数:

//app/authenticators/mine.js
import Authenticator from 'simple-auth-oauth2/authenticators/oauth2';

export default Authenticator.extend({
    makeRequest: function(url, data) {
        data.client_id = '…';
        data.client_secret = '…';
        return this._super(url, data);
    }
});

您可以像使用 OAuth 2.0 身份验证器一样使用它,只需将 simple-auth-authenticator:oauth2-password-grant 替换为 authenticator:mine

下面是一个工作示例的其余文件。

//app/controllers/login.js
import Ember from 'ember';
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';

export default Ember.Controller.extend(LoginControllerMixin, {
    authenticator: 'authenticator:mine'
});

//app/routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(
    ApplicationRouteMixin
);

//app/routes/index.js
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(
    AuthenticatedRouteMixin
);

//app/routes/login.js
import Ember from 'ember';
import UnauthenticatedRouteMixin from 'simple-auth/mixins/unauthenticated-route-mixin';

export default Ember.Route.extend(
    UnauthenticatedRouteMixin
);

<!-- app/templates/application.hbs -->
{{#if session.isAuthenticated}}
  <h2 id="title">Welcome to Ember.js</h2>
  <a {{ action 'invalidateSession' }}>Logout</a>
{{/if}}

{{outlet}}

<!-- app/templates/index.hbs -->
<p>Private page</p>

<!-- app/templates/login.hbs -->
<form {{action 'authenticate' on='submit'}}>
  <label for="identification">Login</label>
  {{input value=identification placeholder='Enter Login'}}
  <label for="password">Password</label>
  {{input value=password placeholder='Enter Password' type='password'}}
  <button type="submit">Login</button>
</form>

//config/environment.js
(…)

ENV['simple-auth'] = {
    authorizer: 'simple-auth-authorizer:oauth2-bearer'
}

ENV['simple-auth-oauth2'] = {
    serverTokenEndpoint: '/api/v1/oauth/token'
}

(…)

此答案源自对上述 question on the Ember Simple Auth issue tracker 的评论.

关于javascript - Ember-Simple-Auth OAuth 2.0 没有发送正确的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24699086/

相关文章:

javascript - 如何在 Next.js 中最好地导入 "server-only"代码?

javascript - 在 JavaScript 中使用 reduceRight 的真实示例

javascript - JS 数组 - 高级赋值

javascript - 为什么我的 List<string> 没有从 $ajax 调用的 MVC Controller 返回

ember.js - Ember 对测试路由钩子(Hook)和操作感到困惑

javascript - EmberJS Action 以数组中的对象为目标

ember.js - 如何在 Ember 1.7 中从路由访问查询参数

php - 无法使用 PHP OAuth 扩展使用已知的 CA 证书对对等证书进行身份验证

java - 为什么在谷歌的响应中缺少使用 openid4java 的 OAuth request_token?

php - 无法使用php表单在google oauth上交换 token 的授权码