2

所以我正在ember-simple-auth使用OAuth 2.0扩展程序进行设置。问题是每当我尝试登录并查看作为表单数据发送的内容时,它只发送grant_typeandpassword参数。但是,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'
  }
  …
}

It's making the call to the right url after this change

在我的初始化程序文件夹中

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

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

4

1 回答 1

1

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

//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 Authenticator 一样使用它,方法是替换simple-auth-authenticator:oauth2-password-grantauthenticator: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'
}

(…)

这个答案来自对 Ember Simple Auth issue tracker 上提到的问题的评论。

于 2015-01-21T19:22:33.023 回答