3

omn​​iauth 用のカスタム プロバイダーを作成する方法をネット全体で検索しました..そして、部分的に成功しました..

私は宝石を作成しましたが、他のプロバイダーが行うように、収集したデータをセッションコントローラーに返す方法を理解できないという部分を除いて、完全に機能しました..

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_hashomn​​iauth インスタンスから呼び出すことができるように、呼び出される新しいメソッドを定義する必要があるようです。

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

omn​​iauth ビルド戦略ガイドを確認しましたか?

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 に答える