class Google::APIClient::Storage

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

Constants

AUTHORIZATION_URI
TOKEN_CREDENTIAL_URI

Attributes

authorization[R]

@return [Signet::OAuth2::Client]

store[RW]

@return [Object] Storage object.

Public Class Methods

new(store) click to toggle source

Initializes the Storage object.

@params [Object] Storage object

# File lib/google/api_client/auth/storage.rb, line 39
def initialize(store)
  @store= store
  @authorization = nil
end

Public Instance Methods

authorize() click to toggle source

Loads credentials and authorizes an client. @return [Object] Signet::OAuth2::Client or NIL

# 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_authorization() click to toggle source

refresh credentials and save them to store

# File lib/google/api_client/auth/storage.rb, line 73
def refresh_authorization
  authorization.refresh!
  self.write_credentials
end
write_credentials(authorization=nil) click to toggle source

Write the credentials to the specified store.

@params [Signet::OAuth2::Client] authorization

Optional authorization instance. If not provided, the authorization
already associated with this instance will be written.
# 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

Private Instance Methods

credentials_hash() click to toggle source

@return [Hash] with credentials

# File lib/google/api_client/auth/storage.rb, line 88
def credentials_hash
  {
    :access_token          => authorization.access_token,
    :authorization_uri     => AUTHORIZATION_URI,
    :client_id             => authorization.client_id,
    :client_secret         => authorization.client_secret,
    :expires_in            => authorization.expires_in,
    :refresh_token         => authorization.refresh_token,
    :token_credential_uri  => TOKEN_CREDENTIAL_URI,
    :issued_at             => authorization.issued_at.to_i
  }
end
load_credentials() click to toggle source

Attempt to read in credentials from the specified store.

# File lib/google/api_client/auth/storage.rb, line 82
def load_credentials
  store.load_credentials
end