Sha256: 988846a558c2e410b530b4e96b8c63c6227db9409df8e4b89d1a02820c026b27

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'net/http'
require 'uri'
require 'omniauth-oauth2'
require 'json'

module OmniAuth
  module Strategies
    class Zaif < OmniAuth::Strategies::OAuth2
      option :name, 'zaif'

      option :client_options, {
        site: 'https://zaif.jp',
        authorize_url: 'https://zaif.jp/oauth',
        token_url: 'https://oauth.zaif.jp/v1/token'
      }

      def authorize_params
        super.tap do |params|
          params[:scope] ||= 'id_info'
        end
      end

      uid { raw_info['id'] }

      info do
        {
          email: raw_info['email'],
          name: raw_info['name'],
          kana: raw_info['kana'],
          certified: raw_info['certified']
        }
      end

      def raw_info
        return @raw_info if @raw_info

        uri = URI.parse('https://api.zaif.jp/tapi')
        https = Net::HTTP.new(uri.host, uri.port)

        https.use_ssl = true
        req = Net::HTTP::Post.new(uri.request_uri)

        req['token'] = access_token.token
        req.set_form_data({nonce: Time.now.to_f, method: 'get_id_info'})
        res = https.request(req)

        raise "failed to execute get_id_info. http status code: #{res.code}" unless res.code.to_i == 200

        res = JSON.load(res.body)
        raise "failed to execute get_id_info. error: #{res['error']}" unless res['success'] == 1

        @raw_info = res['return']['user']
      end

      def callback_phase
        super
      rescue StandardError => e
        fail!(:unkown_error, e)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omniauth-zaif-1.1.0 lib/omniauth/strategies/zaif.rb