require 'multi_json' require 'omniauth/strategies/oauth2' module OmniAuth module Strategies class Slooob < OmniAuth::Strategies::OAuth2 DEFAULT_SCOPE = 'email public' option :name, :slooob option :authorize_options, %i[incremental_authorization image_size scope state] option :client_options, { site: 'https://api.slooob.com', authorize_url: '/oauth/authorize' } def authorize_params super.tap do |params| options[:authorize_options].each do |k| params[k] = request.params[k.to_s] unless [nil, ''].include?(request.params[k.to_s]) end params[:scope] = get_scope params session['omniauth.state'] = params[:state] if params[:state] end end uid { raw_info[:id] } info do prune!( email: raw_info[:email], username: raw_info[:username], name: raw_info[:name], first_name: raw_info[:first_name], last_name: raw_info[:last_name], description: raw_info[:name], image: image_url, location: raw_info[:location], confirmed: raw_info[:email_confirmed], urls: { website: raw_info[:website], slooob: raw_info[:profile] } ) end extra do hash = {} hash[:raw_info] = raw_info unless skip_info? prune! hash end def raw_info @raw_info ||= access_token.get('/identity/v1/resource.json').parsed end private # https://github.com/intridea/omniauth-oauth2/issues/81 def callback_url options[:redirect_uri] || (full_host + script_name + callback_path) end def prune! hash hash.delete_if do |_, v| prune!(v) if v.is_a?(Hash) v.nil? || (v.respond_to?(:empty?) && v.empty?) end end def get_scope params raw_scope = params[:scope] || DEFAULT_SCOPE scope_list = raw_scope.split(' ').map { |item| item.split(',') }.flatten scope_list.map! { |s| s =~ %r{^https?://} } scope_list.join(' ') end def image_url return nil unless raw_info['avatar'] image_size = options[:image_size] || 'raw' image_url = raw_info['avatar'].has_key?(image_size) ? raw_info['avatar'][image_size] : raw_info['avatar'] return image_url end end end end