Class: Lt::Google::Api::Auth::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/lt/google/api/auth/service.rb

Constant Summary collapse

REDIS_PREFIX =
'lt-google-api'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options = {}) ⇒ Service

Returns a new instance of Service.



33
34
35
36
# File 'lib/lt/google/api/auth/service.rb', line 33

def initialize(context, options = {})
  @context = context
  @options = options
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



31
32
33
# File 'lib/lt/google/api/auth/service.rb', line 31

def context
  @context
end

Class Method Details

.authorizer_for(callback_path) ⇒ Object



16
17
18
19
20
21
22
23
24
# 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

.redisObject



26
27
28
# File 'lib/lt/google/api/auth/service.rb', line 26

def redis
  Rails.application.config.redis
end

Instance Method Details

#authorization_urlObject



38
39
40
# File 'lib/lt/google/api/auth/service.rb', line 38

def authorization_url
  authorizer.get_authorization_url @options.merge(request: context.request)
end

#authorizerObject



42
43
44
# File 'lib/lt/google/api/auth/service.rb', line 42

def authorizer
  self.class.authorizer_for(@options[:callback_path])
end

#credentialsObject



46
47
48
49
50
51
52
53
54
# 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_idObject



56
57
58
# 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