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