Class: Google::APIClient::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/google/api_client/auth/storage.rb

Overview

Represents cached OAuth 2 tokens stored on local disk in a JSON serialized file. Meant to resemble the serialized format google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.file.Storage-class.html

Constant Summary collapse

AUTHORIZATION_URI =
'https://accounts.google.com/o/oauth2/auth'
TOKEN_CREDENTIAL_URI =
'https://accounts.google.com/o/oauth2/token'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Storage

Initializes the Storage object.



39
40
41
42
# File 'lib/google/api_client/auth/storage.rb', line 39

def initialize(store)
  @store= store
  @authorization = nil
end

Instance Attribute Details

#authorizationSignet::OAuth2::Client (readonly)

Returns:

  • (Signet::OAuth2::Client)


33
34
35
# File 'lib/google/api_client/auth/storage.rb', line 33

def authorization
  @authorization
end

#storeObject

Returns Storage object.

Returns:

  • (Object)

    Storage object.



30
31
32
# File 'lib/google/api_client/auth/storage.rb', line 30

def store
  @store
end

Instance Method Details

#authorizeObject

Loads credentials and authorizes an client.

Returns:

  • (Object)

    Signet::OAuth2::Client or NIL



60
61
62
63
64
65
66
67
68
69
# File 'lib/google/api_client/auth/storage.rb', line 60

def authorize
  @authorization = nil
  cached_credentials = load_credentials
  if cached_credentials && cached_credentials.size > 0
    @authorization = Signet::OAuth2::Client.new(cached_credentials)
    @authorization.issued_at = Time.at(cached_credentials['issued_at'].to_i)
    self.refresh_authorization if @authorization.expired?
  end
  return @authorization
end

#refresh_authorizationObject

refresh credentials and save them to store



73
74
75
76
# File 'lib/google/api_client/auth/storage.rb', line 73

def refresh_authorization
  authorization.refresh!
  self.write_credentials
end

#write_credentials(authorization = nil) ⇒ Object

Write the credentials to the specified store.



50
51
52
53
54
55
# File 'lib/google/api_client/auth/storage.rb', line 50

def write_credentials(authorization=nil)
  @authorization = authorization if authorization
  if @authorization.respond_to?(:refresh_token) && @authorization.refresh_token
    store.write_credentials(credentials_hash)
  end
end