class Lt::Google::Api::Auth::Service

Constants

REDIS_PREFIX

Attributes

context[R]

Public Class Methods

authorizer_for(callback_path) click to toggle source
# File lib/lt/google/api/auth/service.rb, line 16
def authorizer_for(callback_path)
  @authorizer_for ||= begin
    client_id =
      ::Google::Auth::ClientId.new(ENV['GOOGLE_OAUTH2_CLIENT_ID'], ENV['GOOGLE_OAUTH2_CLIENT_SECRET'])
    token_store ||= ::Google::Auth::Stores::RedisTokenStore.new(redis: redis)
    scope = %w(https://www.googleapis.com/auth/drive)
    ::Google::Auth::WebUserAuthorizer.new(client_id, scope, token_store, callback_path)
  end
end
new(context, options = {}) click to toggle source
# File lib/lt/google/api/auth/service.rb, line 33
def initialize(context, options = {})
  @context = context
  @options = options
end
redis() click to toggle source
# File lib/lt/google/api/auth/service.rb, line 26
def redis
  Rails.application.config.redis
end

Public Instance Methods

authorization_url() click to toggle source
# File lib/lt/google/api/auth/service.rb, line 38
def authorization_url
  authorizer.get_authorization_url @options.merge(request: context.request)
end
authorizer() click to toggle source
# File lib/lt/google/api/auth/service.rb, line 42
def authorizer
  self.class.authorizer_for(@options[:callback_path])
end
credentials() click to toggle source
# File lib/lt/google/api/auth/service.rb, line 46
def credentials
  @credentials ||= begin
    remove_expired_token
    authorizer.get_credentials(user_id, context.request)
  end
rescue StandardError => e
  Rails.logger.warn e.message
  nil
end
user_id() click to toggle source
# File lib/lt/google/api/auth/service.rb, line 56
def user_id
  @user_id ||= "#{REDIS_PREFIX}::#{context.current_user.try(:id)}@#{context.request.remote_ip}"
end

Private Instance Methods

redis() click to toggle source
# File lib/lt/google/api/auth/service.rb, line 62
def redis
  self.class.redis
end
remove_expired_token() click to toggle source
# File lib/lt/google/api/auth/service.rb, line 66
def remove_expired_token
  data = JSON(redis.get(user_token)) rescue nil
  return unless data

  expires_at = data['expiration_time_millis'].to_i / 1_000
  return if expires_at.zero?

  redis.del(user_token) if expires_at <= Time.now.to_i
end
user_token() click to toggle source
# File lib/lt/google/api/auth/service.rb, line 76
def user_token
  @user_token ||= "#{REDIS_PREFIX}::#{::Google::Auth::Stores::RedisTokenStore::DEFAULT_KEY_PREFIX}#{user_id}"
end