Sha256: 3d2a75e567ec723a676d9c79ae7051ecd634a00dfbd64adccff1b64306a7df4b

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Tenzing
  module OauthExtract
    extend ActiveSupport::Concern

    included do
      before_filter :parse_request
      before_filter :get_oauth_data
    end

    def parse_request
      @oauth = request.env['omniauth.auth']
    end

    def get_oauth_data
      return nil unless @oauth.present?
      @oauth_data = {
          user: {
              email:  @oauth.info['email'],
              name:   @oauth.info['name'],
              image:  @oauth.info['image'],
          },
          auth: {
              provider:     @oauth.provider,
              uid:          @oauth.uid,
              username:     @oauth.info['nickname'],
              auth_token:   @oauth.credentials['token'],
              auth_secret:  @oauth.credentials['secret'],
              expires_at:   parse_expiration_date(@oauth),
          }
      }
      raise 'Unable to get OAuth2 data' if @oauth_data.empty?
    end

    private
    def parse_expiration_date(data)
      expires_at = data.credentials['expires_at']
      return expires_at if expires_at.present?

      # LinkedIn only indicates expires_in attribute
      expires_in = data.extra.access_token.params['oauth_expires_in']
      expires_at = Time.now.to_i + expires_in.to_i
      return expires_at if expires_at.present?

      raise 'Unable to parse OAuth2 expires_at attribute'
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tenzing-0.0.1 lib/tenzing/oauth_extract.rb