Method: Google::Auth::UserAuthorizer#get_credentials

Defined in:
lib/googleauth/user_authorizer.rb

#get_credentials(user_id, scope = nil) ⇒ Google::Auth::UserRefreshCredentials

Fetch stored credentials for the user.

Parameters:

  • user_id (String)

    Unique ID of the user for loading/storing credentials.

  • scope (Array<String>, String) (defaults to: nil)

    If specified, only returns credentials that have all the requested scopes

Returns:

Raises:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/googleauth/user_authorizer.rb', line 140

def get_credentials user_id, scope = nil
  saved_token = stored_token user_id
  return nil if saved_token.nil?
  data = MultiJson.load saved_token

  if data.fetch("client_id", @client_id.id) != @client_id.id
    raise CredentialsError.with_details(
      format(MISMATCHED_CLIENT_ID_ERROR, data["client_id"], @client_id.id),
      credential_type_name: self.class.name,
      principal: principal
    )
  end

  credentials = UserRefreshCredentials.new(
    client_id:     @client_id.id,
    client_secret: @client_id.secret,
    scope:         data["scope"] || @scope,
    access_token:  data["access_token"],
    refresh_token: data["refresh_token"],
    expires_at:    data.fetch("expiration_time_millis", 0) / 1000
  )
  scope ||= @scope
  return monitor_credentials user_id, credentials if credentials.includes_scope? scope
  nil
end