Sha256: cac4eeb6b2df4033462c4301cbd4f3d1ce4592c1cbce40668e186a8fb1a7a1ca

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'omniauth-oauth2'

module OmniAuth
  module Strategies
    class Clef < OmniAuth::Strategies::OAuth2

      option :name, "clef"
      option :client_options, {
        :site => 'https://clef.io/api/v1',
        :request_url => "https://clef.io/iframes/qr",
        :token_url => 'authorize'
      }
      option :provider_ignores_state, true
      option :auth_token_params, {
        param_name: 'access_token',
        mode: :query
      }

      uid { raw_info['id'] }

      info do
        prune!({
          'email' => raw_info['email'],
          'first_name' => raw_info['first_name'],
          'last_name' => raw_info['last_name'],
          'phone_number' => raw_info['phone_number'],
        })
      end

      extra do
        hash = {}
        hash['raw_info'] = raw_info unless skip_info?
        if raw_info['first_name'] && raw_info['last_name']
          hash['raw_info']['name'] = "#{raw_info['first_name']} #{raw_info['last_name']}"
        elsif raw_info['first_name']
          hash['raw_info']['name'] = raw_info['first_name']
        end
        prune! hash
      end

      def request_phase
        url = options.client_options.request_url
        url << "?app_id=#{options.client_id}"
        url << "&redirect_url=#{callback_url}"
        redirect url
      end

      def build_access_token
        verifier = request.params['code']
        client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true)), deep_symbolize(options.auth_token_params))
      end

      def raw_info
        @raw_info ||= access_token.get('info').parsed || {}
        @raw_info = @raw_info['info'] if !@raw_info['info'].nil?
        @raw_info
      end

      private

      def prune!(hash)
        hash.delete_if do |_, value|
          prune!(value) if value.is_a?(Hash)
          value.nil? || (value.respond_to?(:empty?) && value.empty?)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omniauth-clef-1.0.2 lib/omniauth/strategies/clef.rb