Sha256: 8545891017caadbd686d850388905fec74b928b978b416eddfff8cccbd940b78

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require 'omniauth/oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    # OAuth 2.0 based authentication with GitHub. In order to
    # sign up for an application, you need to [register an application](http://github.com/account/applications/new)
    # and provide the proper credentials to this middleware.
    class Yammer < OmniAuth::Strategies::OAuth2
      def initialize(app, client_id=nil, client_secret=nil, options={}, &block)
        client_options = {
          :token_url => '/oauth2/access_token.json',
          :authorize_url => '/dialog/oauth',
          :site => 'https://www.yammer.com'
        }
        super(app, :yammer, client_id, client_secret, client_options, options, &block)
      end

      def auth_hash
        OmniAuth::Utils.deep_merge(
          super, {
            'uid' => user_hash['id'],
            'user_info' => user_info,
            'extra' => {
              'user_hash' => user_hash,
            },
          }
        )
      end

      def user_info
        user_hash = self.user_hash
        {
          'nickname' => user_hash['name'],
          'name' => user_hash['full_name'],
          'location' => user_hash['location'],
          'image' => user_hash['mugshot_url'],
          'description' => user_hash['job_title'],
          'email' => user_hash['contact']['email_addresses'][0]['address'],
          'urls' => {
            'Yammer' => user_hash['web_url'],
          },
        }
      end

      def build_access_token
        # Dance to get the real token out of the object returned by Yammer
        verifier = request.params['code']
        temp_access_token = client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(options))
        token = eval(temp_access_token.token)['token']
        @access_token = ::OAuth2::AccessToken.new(client, token, temp_access_token.params)
      end

      def user_hash
        @user_hash ||= MultiJson.decode(@access_token.get('/api/v1/users/current.json').body)
      end


    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oa-oauth-0.3.2 lib/omniauth/strategies/oauth2/yammer.rb