3

我在网上搜索了所有内容,如何为omniauth创建自定义提供程序..我部分成功了..

我创建了一个 gem,它工作得很好,除了我无法理解如何将收集到的数据返回到会话控制器的部分,就像其他提供者一样..

这是 auth gem 中的代码:

require 'multi_json'
require 'digest/md5'
require 'rest-client'

module OmniAuth
  module Strategies
    class Providername < OmniAuth::Strategies::OAuth
      attr_accessor :app_id, :api_key, :auth

      def initialize(app, app_id = nil, api_key = nil, options = {})
        super(app, :providername)
        @app_id = app_id
        @api_key = api_key
      end

      protected

      def request_phase
        redirect "http://valid_url"
      end

      def callback_phase
        if request.params['code'] && request.params['status'] == 'ok'
          response = RestClient.get("http://valid_url2/?code=#{request.params['auth_code']}")
          auth = MultiJson.decode(response.to_s)
          unless auth['error']
            @auth_data = auth

            if @auth_data
              @return_data = OmniAuth::Utils.deep_merge(super, {
                  'uid' => @auth_data['uid'],
                  'nickname' => @auth_data['nick'],
                  'user_info' => {
                    'first_name' => @auth_data['name'],
                    'last_name' => @auth_data['surname'],
                    'location' => @auth_data['place'],
                  },
                  'credentials' => {
                    'apikey' => @auth_data['apikey']
                  },
                  'extra' => {'user_hash' => @auth_data}
                })
            end

          end
        else
          fail!(:invalid_request)
        end
      rescue Exception => e
        fail!(:invalid_response, e)
      end

    end
  end
end

在这里我在我的初始化程序中调用它:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider "providername", Settings.providers.providername.app_id, Settings.providers.providername.app_secret
end

在这段代码中,到目前为止一切正常,提供者被调用,我从提供者那里获取信息,我用信息创建了一个哈希(@auth_data),但是我如何返回它

4

1 回答 1

0

看起来您必须定义一个名为的新方法,auth_hash以便您可以从omniauth 实例调用它。

https://gist.github.com/dira/722793

您是否查看了omniauth 构建策略指南?

https://github.com/intridea/omniauth/wiki/Strategy-Contribution-Guide

其他指南将您指向 auth_hash 方法的想法:

http://anti-pattern.com/adding-a-new-strategy-to-omniauth

于 2014-11-28T20:14:46.430 回答