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.



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

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

Class Method Details

.authorizer_for(callback_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# 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.fetch('GOOGLE_OAUTH2_CLIENT_ID', nil),
                                   ENV.fetch('GOOGLE_OAUTH2_CLIENT_SECRET', nil))
    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



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

def redis
  Rails.application.config.redis
end

Instance Method Details

#authorization_urlObject



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

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

#authorizerObject



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

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

#credentialsObject



47
48
49
50
51
52
53
54
55
# File 'lib/lt/google/api/auth/service.rb', line 47

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



57
58
59
# File 'lib/lt/google/api/auth/service.rb', line 57

def user_id
  @user_id ||= "#{REDIS_PREFIX}::#{context.current_user.try(:id)}@#{context.request.remote_ip}"
end