# https://github.com/doumart/omniauth-patreon-v2 require 'omniauth/strategies/oauth2' require 'json' module OmniAuth module Strategies class Patreon < OmniAuth::Strategies::OAuth2 DEFAULT_SCOPE = 'users pledges-to-me my-campaign' option :name, 'patreon' option :client_options, { :authorize_url => '/oauth2/authorize', :site => 'https://www.patreon.com', :token_url => '/api/oauth2/token' } uid { raw_info["data"]["id"] } info do prune!({ :email => raw_info['data']['attributes']['email'], :name => raw_info['data']['attributes']['full_name'], :nickname => raw_info['data']['attributes']['full_name'].gsub('ä','ae').gsub('ö','oe').gsub('ü','ue').gsub(' ','_').downcase, :access_token => access_token.token, :refresh_token => access_token.refresh_token }) end 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 extra do hash = {} hash['raw_info'] = raw_info unless skip_info? prune! hash end # def raw_info # @raw_info = MultiJson.decode(access_token.get('/oauth2/api/current_user', info_options).body) # end def raw_info @raw_info ||= begin response = client.request( :get, 'https://api.patreon.com/oauth2/api/current_user', headers: { 'Authorization' => "Bearer #{access_token.token}" }, parse: :json ) response.parsed end end def callback_url options[:redirect_uri] || full_host + script_name + callback_path end def get_scope(params) raw_scope = params[:scope] || DEFAULT_SCOPE scope_list = raw_scope.split(' ').map { |item| item.split(',') }.flatten scope_list.join(' ') end 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