# https://github.com/doumart/omniauth-patreon-v2 require 'omniauth/strategies/oauth2' require 'json' module OmniAuth module Strategies class Patreon < OmniAuth::Strategies::OAuth2 DEFAULT_SCOPE = 'identity identity[email] identity[memberships]' 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!({ :first_name => raw_info["data"]["attributes"]['first_name'], :last_name => raw_info["data"]["attributes"]['last_name'], :full_name => raw_info["data"]["attributes"]['full_name'], :nickname => raw_info["data"]["attributes"]['full_name'].gsub('ä','ae').gsub('ö','oe').gsub('ü','ue').gsub(' ','_').downcase, :image_url => raw_info["data"]["attributes"]['image_url'], :email => raw_info["data"]["attributes"]['email'] }) 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('/api/oauth2/v2/identity?include=memberships&fields[pledge]=total_historical_amount_cents,is_paused,outstanding_payment_amount_cents,declined_since', info_options).body) # @raw_info = MultiJson.decode(access_token.get('/api/oauth2/v2/identity?include=campaign&fields[pledge]=total_historical_amount_cents,is_paused,outstanding_payment_amount_cents,declined_since', info_options).body) end def info_options params = {} params.merge!({fields: {user: (options[:info_fields] || 'full_name,first_name,email')}}) params.merge!({locale: options[:locale]}) if options[:locale] { params: params } 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