简体   繁体   English

devise_token_auth omniauth JSON响应?

[英]devise_token_auth omniauth JSON response?

I have a Rails 5 site which consists of 2 parts: 我有一个Rails 5站点,它由2部分组成:

  1. Admin area 管理区域
  2. API-only client area 仅限API的客户区域

I'm using Devise for both parts and https://github.com/lynndylanhurley/devise_token_auth gem for the API frontend. 我在两个部分使用Devise,为API前端使用https://github.com/lynndylanhurley/devise_token_auth gem。

The problem is about using the omniauth authentication. 问题是关于使用omniauth身份验证。 When I omniauth authenticate into the admin area - everything is ok - I get back some successful HTML-response. 当omniauth对管理区域进行身份验证时 - 一切正常 - 我得到了一些成功的HTML响应。

But the problem is that I'm getting the same HTML-response in the API-area - but I need some JSON-response - not HTML one. 但问题是我在API领域得到了相同的HTML响应 - 但我需要一些JSON响应 - 而不是HTML响应。

Here is my code: 这是我的代码:

config/routes.rb 配置/ routes.rb中

Rails.application.routes.draw do

    devise_for :users, controllers: { sessions: 'users/sessions', :omniauth_callbacks => 'users/omniauth_callbacks' }

    namespace :api do   

        mount_devise_token_auth_for 'User', at: 'auth', controllers: { sessions: 'api/users/sessions', :omniauth_callbacks => 'api/users/omniauth_callbacks' }    

    end
end

app/models/user.rb 应用程序/模型/ user.rb

class User < ApplicationRecord

  # Include default devise modules.
  devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable,
          :omniauthable,
          :omniauth_providers => [:facebook, :vkontakte]

  include DeviseTokenAuth::Concerns::User

  devise :omniauthable

  def self.from_omniauth_vkontakte(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.extra.raw_info.first_name.to_s + "." + auth.extra.raw_info.last_name.to_s + '@vk.com'      
      user.password = Devise.friendly_token[0,20]
    end
  end

end

app/controllers/users/omniauth_callbacks_controller.rb 应用程序/控制器/用户/ omn​​iauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def vkontakte
    @user = User.from_omniauth_vkontakte(request.env["omniauth.auth"])

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Vkontakte") if is_navigational_format?
    else
      session["devise.vkontakte_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end    
  end

end

config/initializers/devise.rb 配置/初始化/ devise.rb

Devise.setup do |config|

  config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_APP_SECRET"], provider_ignores_state: true

  config.omniauth :vkontakte, ENV["VKONTAKTE_APP_ID"], ENV["VKONTAKTE_APP_SECRET"]

end

Gemfile 的Gemfile

gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-vkontakte'

Gemfile.lock Gemfile.lock的

devise (4.3.0)
devise_token_auth (0.1.42)

Here's my log: 这是我的日志:

Started GET "/api/auth/vkontakte" for 127.0.0.1 at 2017-06-20 17:34:23
+0300 
Started GET "/omniauth/vkontakte?namespace_name=api&resource_class=User" for
127.0.0.1 at 2017-06-20 17:34:23 +0300 
I, [2017-06-20T17:34:23.237270 #15747]  INFO -- omniauth: (vkontakte) Request phase initiated. 
Started GET "/omniauth/vkontakte/callback?code=0b8446c5fe6873bb12&state=52254649eb899e3b743779a1a4afc0304f249a6dd90b4415" for 127.0.0.1 at 2017-06-20 17:34:23 +0300 
I, [2017-06-20T17:34:23.672200 #15747]  INFO -- omniauth: (vkontakte) Callback phase initiated. Processing by Users::OmniauthCallbacksController#vkontakte as */*   Parameters: {"code"=>"0b8446c5fe6873bb12", "state"=>"52254649eb899e3b743779a1a4afc0304f249a6dd90b4415"}

I guess that the problem is about a so-called "callback" url. 我猜这个问题是关于一个所谓的“回调”网址。 I don't understand where it is set. 我不明白它的位置。 It is obvious from the log that at the end of the auth process the GET "/omniauth/vkontakte/callback..." query is called. 从日志中可以明显看出,在auth过程结束时, GET "/omniauth/vkontakte/callback..."GET "/omniauth/vkontakte/callback..."查询。 And probably it is called always - no matter if I initiated the oath sequence from admin or api client area. 并且可能它总是被调用 - 无论我是从admin还是api客户区发起誓序。

I use Chrome Postman to make the API query http://localhost:3000/api/auth/vkontakte - and I get the HTML -response back ("successful login etc.") - but I need surely some JSON -response. 我使用Chrome Postman进行API查询http://localhost:3000/api/auth/vkontakte - 我得到了HTML -response(“成功登录等”) - 但我肯定需要一些JSON响应。

Is there a way to dynamically change the callback path depending on some precondition? 有没有办法根据一些前提条件动态更改回调路径? Is the callback query somewhat different depending on from where the oath procedure was initiated? 回调查询是否有所不同,具体取决于启动誓言程序的位置?

EDIT1: EDIT1:

This is not a single problem here unfortunately. 不幸的是,这不是一个单一的问题。 Looks like the oauth is simply not implemented in the https://github.com/lynndylanhurley/devise_token_auth gem. 看起来oauth根本没有在https://github.com/lynndylanhurley/devise_token_auth gem中实现。 So, even if I succeed to switch the oauth login procedure to the JSON way - how do I login the user the devise_token_auth-way - generating 3 tokens etc...? 因此,即使我成功将oauth登录过程切换为JSON方式 - 如何以devise_token_auth-way方式登录用户 - 生成3个令牌等...? The app/controllers/users/omniauth_callbacks_controller.rb needs to be totally reimlemented. app / controllers / users / omniauth_callbacks_controller.rb需要完全重新发布。

You can render json from your OmniauthCallbacksController based on some extra parameter provided when your request a connection from the API for example. 您可以根据从API请求连接时提供的一些额外参数,从OmniauthCallbacksController渲染json。 These extra parameters will be availables in this hash request.env["omniauth.params"] . 这些额外参数将在此哈希request.env["omniauth.params"]可用。

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def vkontakte
    @user = User.from_omniauth_vkontakte(request.env["omniauth.auth"])

    if @user.persisted?
      sign_in @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Vkontakte") if is_navigational_format?

      if request.env["omniauth.params"]["apiRequest"]
        render status: 200, json: { message: "Login success" }
      else
        redirect_to after_sign_in_path_for(@user)
      end
    else
      session["devise.vkontakte_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

end

You can this extra parameters by calling the auth helper with additional parameters, they will be passed to your OmniauthController : user_vkontakte_omniauth_authorize_path(api_request: true) (Or whatever your route helper is) 您可以通过使用其他参数调用auth helper来获取这些额外的参数,它们将被传递给您的OmniauthController: user_vkontakte_omniauth_authorize_path(api_request: true) (或者您的路径助手是什么)

I ended up implementing my own oauth callback procedure - instead of using one from the devise_token_auth gem. 我最终实现了自己的oauth回调过程 - 而不是使用devise_token_auth gem中的一个。

The devise_token_auth gem does contain the oauth authentication - but it appears to be not working properly. devise_token_auth gem确实包含oauth身份验证 - 但它似乎无法正常工作。

Here are my code changes: 这是我的代码更改:

config/routes.rb 配置/ routes.rb中

Rails.application.routes.draw do

    devise_for :users, controllers: { sessions: 'users/sessions', :omniauth_callbacks => 'users/omniauth_callbacks' }

    namespace :api do   

        mount_devise_token_auth_for 'User', at: 'auth', controllers: { sessions: 'api/users/sessions'}   

    end
end

app/controllers/users/omniauth_callbacks_controller.rb 应用程序/控制器/用户/ omn​​iauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  include DeviseTokenAuth::Concerns::SetUserByToken

  def vkontakte
    @user = User.from_omniauth_vkontakte(request.env["omniauth.auth"])

    namespace_name = request.env["omniauth.params"]["namespace_name"]

    if @user.persisted?

      if namespace_name && namespace_name == "api"

        @client_id = SecureRandom.urlsafe_base64(nil, false)
        @token     = SecureRandom.urlsafe_base64(nil, false)

        @user.tokens[@client_id] = {
          token: BCrypt::Password.create(@token),
          expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
        }
        @user.save

        @resource = @user # trade-off for "update_auth_header" defined in "DeviseTokenAuth::Concerns::SetUserByToken"

        sign_in(:user, @user, store: false, bypass: false)        

        render json: @user

      else

        sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
        set_flash_message(:notice, :success, :kind => "Vkontakte") if is_navigational_format?

      end

    else
      session["devise.vkontakte_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end    
  end

end

The inclusion of include DeviseTokenAuth::Concerns::SetUserByToken provides 5 auth headers in response: include DeviseTokenAuth::Concerns::SetUserByToken 5个auth标头作为响应:

access-token →BeX35KJfYVheKifFdwMPag
client →96a_7jXewCThas3mpe-NhA
expiry →1499340863
token-type →Bearer
uid →376449571

But the response still lacks these headers (available at a common sign-in): 但响应仍然缺少这些标头(可在常见的登录中使用):

Access-Control-Allow-Credentials →true
Access-Control-Allow-Methods →GET, POST, PUT, PATCH, DELETE, OPTIONS
Access-Control-Allow-Origin →chrome-extension://aicmkgpgakddgnaphhhpliifpcfhicfo
Access-Control-Max-Age →1728000

I don't know whether they are important and if yes - how to provide them. 我不知道它们是否重要,如果是的话 - 如何提供它们。

PS The same identical approach works with Facebook too. PS同样的方法也适用于Facebook。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM