Sha256: 22cf223b20b56f866cc800365ea60b62bb2f60f33e3620b0d42f88b21d314b18

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'omniauth-oauth2'

module OmniAuth
  module Strategies
    class StackExchange < OmniAuth::Strategies::OAuth2
      class NotRegisteredForStackExchangeSiteError < StandardError; end

      option :client_options, {
        :site => 'https://api.stackexchange.com/2.0',
        :authorize_url => 'https://stackexchange.com/oauth',
        :token_url => 'https://stackexchange.com/oauth/access_token'
      }

      option :token_params, {
        :parse => :query
      }

      def request_phase
        super
      end

      uid { raw_info['user_id'] }

      info do
        {
          'nickname' => raw_info['display_name'],
          'image' => raw_info['profile_image'],
          'urls' => {
            site => raw_info['link']
          }
        }
      end

      extra do
        { :raw_info => raw_info }
      end

      def raw_info
        @raw_info ||= access_token.get('me', :params => params).parsed['items'].first

        unless @raw_info
          raise NotRegisteredForStackExchangeSiteError, "User is not registered for requested StackExchange site (#{site})"
        end

        @raw_info
      end

      def params
        {
          :site => site,
          :access_token => access_token.token,
          :key => options.public_key
        }
      end

      def site
        request.env['omniauth.params']['site'] || options.site || 'stackoverflow'
      end
    end
  end
end

OmniAuth.config.add_camelization 'stackexchange', 'StackExchange'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omniauth-stackexchange-0.2.0 lib/omniauth/strategies/stackexchange.rb