1

我一直在尝试使用 ember-cli 和 Rails API 后端设置 OAuth2 客户端凭据流,但遇到了死胡同。也许是因为我是 ember 的新手。我目前正在尝试做的是:

bower.json
{
    "ember-simple-auth": "*"
}

Brocfile.js
app.import('vendor/ember-simple-auth/simple-auth.amd.js')
app.import('vendor/ember-simple-auth/simple-auth-oauth2.amd.js')

initializers/login.js
App.initializer({
  name: 'Register Components',
  initialize: function(container, application) {
    registerComponents(container);
    Ember.SimpleAuth.setup(application);
  }
});

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

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

templates/login.hbs
<form {{action authenticate on='submit'}}>
  <label for="identification">Login</label>
  {{view Ember.TextField id='identification' valueBinding='identification' placeholder='Enter Login'}}
  <label for="password">Password</label>
  {{view Ember.TextField id='password' type='password' valueBinding='password' placeholder='Enter Password'}}
  <button type="submit">Login</button>
</form>

感谢您提供这方面的任何指南、教程或更正。

4

4 回答 4

1

我最近在 GitHub 上讨论了这个问题。这就是我最终使用 HTTP 基本身份验证(在 app/app.js 中)对客户端进行身份验证的方法:

import OAuth2Authenticator from 'simple-auth-oauth2/authenticators/oauth2';

OAuth2Authenticator.reopen({
  makeRequest: function(data) {
    var clientId = MyProjectENV.APP.apiClientId;
    var clientSecret = MyProjectENV.APP.apiClientSecret;
    return Ember.$.ajax({
      url:         this.serverTokenEndpoint,
      type:        'POST',
      data:        data,
      dataType:    'json',
      contentType: 'application/x-www-form-urlencoded',
      headers:     { "Authorization": "Basic " + btoa(clientId + ":" + clientSecret) }
    });
  }
});

在 config/environment.js 中:

var ENV = {
  // ...
  APP: {
    apiClientId: "12345",
    apiClientSecret: "abcdefg987654"
  }
于 2014-06-29T22:44:54.337 回答
1

Ember Simple Auth 的最新版本不再需要定义初始化程序,并为库添加了 Ember CLI 插件,这使得设置一切变得更加容易。此外,README 和 API 文档现在专注于将库与 Ember CLI 一起使用,这对您有很大帮助。

查看自述文件:https ://github.com/simplabs/ember-simple-auth#readme

于 2014-06-26T08:18:23.670 回答
0

您可以通过设置 OAuth 2.0 身份验证器的clientId属性来包含客户端 ID(请参阅 API 文档:http ://ember-simple-auth.com/api/classes/OAuth2PasswordGrantAuthenticator.html#property_clientId )。与其他答案在这里建议的相反,您永远不应该包含 client secret。只要您在 Ember 应用程序的任何地方使用秘密,它就不再是秘密,因为它包含在源中并且对有权访问源的每个人(通常是整个互联网上的每个人)都可见。

Web 客户端在 OAuth 方面是不可信任的公共客户端,因此不能使用客户端密码。您可以将客户端 ID 用于分析等,但您甚至不应该在服务器端信任它,因为它可以很容易地被操纵,当然也可以被其他客户端使用,因为它可以简单地从应用程序源中查找。

所以请大家记住:

永远不要在 Ember 应用程序中使用客户端密码!!!1!1!

于 2016-04-29T12:01:26.870 回答
-1

感谢 marcoow Ember-Simple-Auth 的巨大努力,现在支持一种添加 client_it 的简单方法!

ESA 在默认身份验证器中将 clientId 的值设置为 null(请参阅文件node_modules/esa/addon/authenticator/oauth2-password-grant.js

您可以像覆盖自定义服务器令牌端点一样覆盖自定义身份验证器中的值。

// app/authenticators/oauth2.js
import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';
import ENV from '../config/environment';

export default OAuth2PasswordGrant.extend({
  serverTokenEndpoint: `${ENV.api.host}/oauth/token`,
  clientId: `${ENV.APP.apiClientId}`,
});

您可能会考虑作者在这个关于 esa的github 讨论中关于设置 client_id 的意见。

更新

我更新了源代码并删除了客户端密码,因为您不应该将其包含在 ember 应用程序中。

于 2016-04-29T07:41:18.553 回答