Ember-Simple-Auth OAuth 2.0不发送正确的参数

时间:2014-07-11 13:36:45

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

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

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上的教程中复制而来的。但是,我无法弄清楚为什么没有正确发送参数。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分: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);
    }
});

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

以下是工作示例的其余文件。

//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的评论。