【问题标题】:Ember-simple-auth: Authenticating from URLEmber-simple-auth:从 URL 进行身份验证
【发布时间】:2014-05-01 00:41:06
【问题描述】:

我有一个要求,我可以将?auth_token=x 附加到我的应用程序中的任何 URL,它会恢复会话,就好像你有一个 cookie 或详细信息存储在本地存储中一样。

在过去的几个小时里,我已经尝试了所有我能想到的方法来实现这一点,但我没有得到任何结果。这可能吗?我应该在哪里添加此类功能?

【问题讨论】:

  • 是的,一旦一切都被初始化并且转换正在进行/完成,这似乎工作正常。在我的情况下,我需要更早地处理身份验证,所以我有自己的查询参数处理工作正常。问题是在 ember-simple-auth 中我可以在它尝试重定向到登录页面之前触发身份验证。
  • 如果您使用 Ember.SimpleAuth,您已经将会话状态存储在 localStorage 中。我不确定我是否了解您要在这里实现的目标。如果您在 localStorage 中有一个有效的会话,该会话将自动恢复。当您想在 URL 中而不是 Authorization 标头中将令牌传递给服务器时,您可以实现自定义授权方(参见此处:github.com/simplabs/…)。
  • 我有一个用例,其中没有存储会话,但用户需要能够单击链接或从外部服务自动通过身份验证并为他们创建会话。我设法做到了这一点,我会尽快在答案中发布我的代码
  • @marcoow 我已经发布了我的代码,如果您有时间看一下,Ember.SimpleAuth 中是否有比我使用的更好的入口点?

标签: ember.js ember-simple-auth


【解决方案1】:

我在这里回答我自己的问题,因为我设法找到了一个解决方案,虽然我不确定它有多正确!

application.js:

// Really simple query parameter access
(function($) {
  $.QueryString = (function(a) {
    if (a == "") return {};
    var b = {};
    for (var i = 0; i < a.length; ++i)
    {
      var p=a[i].split('=');
      if (p.length != 2) continue;
      b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
  })(window.location.search.substr(1).split('&'))
})(jQuery);

初始化程序/authentication.js.coffee:

LocalStorageWithURLAuth = Ember.SimpleAuth.Stores.LocalStorage.extend
  init: ->
    if $.QueryString['auth_token']
      auth_token = $.QueryString['auth_token']
      Ember.$.ajax(
        async: false
        url: '/users/sign_in'
        type: 'POST'
        data: { auth_token: auth_token }
        dataType: 'json'
      ).then (data) =>
        @persist
          auth_token: data.auth_token
          auth_email: data.auth_email
          user_id: data.user_id
          authenticatorFactory: 'authenticator:devise'
        @_super()
      , (error) =>
        @_super()
    else
      @_super()

Ember.Application.initializer
  name: 'authentication'
  initialize: (container, application) ->
    Ember.SimpleAuth.Session.reopen
      user: (->
        userId = @get 'user_id'
        if !Ember.isEmpty userId
          container.lookup('store:main').find 'user', userId
      ).property('user_id')

    container.register 'session-store:local-storage-url', LocalStorageWithURLAuth

    Ember.SimpleAuth.setup container, application,
      storeFactory: 'session-store:local-storage-url'
      authenticatorFactory: 'authenticator:devise'
      authorizerFactory: 'authorizer:devise'

【讨论】:

    【解决方案2】:

    我不确定我是否理解你在做什么。您似乎正在从查询字符串中的身份验证令牌恢复会话?这实际上就是验证器的 restore 方法的用途(请参阅此处的文档:http://ember-simple-auth.simplabs.com/ember-simple-auth-devise-api-docs.html#Ember-SimpleAuth-Authenticators-Devise-restore)。还有当应用启动时查询字符串不是空的吗?

    【讨论】:

    • 查询字符串中有一个一次性的 auth_token 参数,用于创建新会话。我花了很多时间试图让restore 方法工作,因为我认为这是最适合它的地方,但我只有恢复会话所需的部分属性并在该方法中运行额外的 ajax 请求证明很棘手.查询字符串在应用程序启动时出现,因为它用于直接从电子邮件中的链接或作为外部服务中重定向链的一部分对用户进行身份验证。
    • 现在我想起来了,我认为restore 方法的问题是它实际上并没有在我需要的时候被调用,所以我放在那里的任何代码都是无用的。我最终采用的方法是预先填充 LocalStorage,以便恢复功能运行并恢复会话。
    • 应用程序启动时以及商店中发生任何变化时都会调用restore 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2018-09-24
    • 1970-01-01
    相关资源
    最近更新 更多