| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170 |
- # Copyright 2014 Google Inc. All rights reserved.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- """An OAuth 2.0 client.
- Tools for interacting with OAuth 2.0 protected resources.
- """
- import collections
- import copy
- import datetime
- import json
- import logging
- import os
- import shutil
- import socket
- import sys
- import tempfile
- import six
- from six.moves import http_client
- from six.moves import urllib
- import oauth2client
- from oauth2client import _helpers
- from oauth2client import _pkce
- from oauth2client import clientsecrets
- from oauth2client import transport
- HAS_OPENSSL = False
- HAS_CRYPTO = False
- try:
- from oauth2client import crypt
- HAS_CRYPTO = True
- HAS_OPENSSL = crypt.OpenSSLVerifier is not None
- except ImportError: # pragma: NO COVER
- pass
- logger = logging.getLogger(__name__)
- # Expiry is stored in RFC3339 UTC format
- EXPIRY_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
- # Which certs to use to validate id_tokens received.
- ID_TOKEN_VERIFICATION_CERTS = 'https://www.googleapis.com/oauth2/v1/certs'
- # This symbol previously had a typo in the name; we keep the old name
- # around for now, but will remove it in the future.
- ID_TOKEN_VERIFICATON_CERTS = ID_TOKEN_VERIFICATION_CERTS
- # Constant to use for the out of band OAuth 2.0 flow.
- OOB_CALLBACK_URN = 'urn:ietf:wg:oauth:2.0:oob'
- # The value representing user credentials.
- AUTHORIZED_USER = 'authorized_user'
- # The value representing service account credentials.
- SERVICE_ACCOUNT = 'service_account'
- # The environment variable pointing the file with local
- # Application Default Credentials.
- GOOGLE_APPLICATION_CREDENTIALS = 'GOOGLE_APPLICATION_CREDENTIALS'
- # The ~/.config subdirectory containing gcloud credentials. Intended
- # to be swapped out in tests.
- _CLOUDSDK_CONFIG_DIRECTORY = 'gcloud'
- # The environment variable name which can replace ~/.config if set.
- _CLOUDSDK_CONFIG_ENV_VAR = 'CLOUDSDK_CONFIG'
- # The error message we show users when we can't find the Application
- # Default Credentials.
- ADC_HELP_MSG = (
- 'The Application Default Credentials are not available. They are '
- 'available if running in Google Compute Engine. Otherwise, the '
- 'environment variable ' +
- GOOGLE_APPLICATION_CREDENTIALS +
- ' must be defined pointing to a file defining the credentials. See '
- 'https://developers.google.com/accounts/docs/'
- 'application-default-credentials for more information.')
- _WELL_KNOWN_CREDENTIALS_FILE = 'application_default_credentials.json'
- # The access token along with the seconds in which it expires.
- AccessTokenInfo = collections.namedtuple(
- 'AccessTokenInfo', ['access_token', 'expires_in'])
- DEFAULT_ENV_NAME = 'UNKNOWN'
- # If set to True _get_environment avoid GCE check (_detect_gce_environment)
- NO_GCE_CHECK = os.getenv('NO_GCE_CHECK', 'False')
- # Timeout in seconds to wait for the GCE metadata server when detecting the
- # GCE environment.
- try:
- GCE_METADATA_TIMEOUT = int(os.getenv('GCE_METADATA_TIMEOUT', 3))
- except ValueError: # pragma: NO COVER
- GCE_METADATA_TIMEOUT = 3
- _SERVER_SOFTWARE = 'SERVER_SOFTWARE'
- _GCE_METADATA_URI = 'http://' + os.getenv('GCE_METADATA_IP', '169.254.169.254')
- _METADATA_FLAVOR_HEADER = 'metadata-flavor' # lowercase header
- _DESIRED_METADATA_FLAVOR = 'Google'
- _GCE_HEADERS = {_METADATA_FLAVOR_HEADER: _DESIRED_METADATA_FLAVOR}
- # Expose utcnow() at module level to allow for
- # easier testing (by replacing with a stub).
- _UTCNOW = datetime.datetime.utcnow
- # NOTE: These names were previously defined in this module but have been
- # moved into `oauth2client.transport`,
- clean_headers = transport.clean_headers
- MemoryCache = transport.MemoryCache
- REFRESH_STATUS_CODES = transport.REFRESH_STATUS_CODES
- class SETTINGS(object):
- """Settings namespace for globally defined values."""
- env_name = None
- class Error(Exception):
- """Base error for this module."""
- class FlowExchangeError(Error):
- """Error trying to exchange an authorization grant for an access token."""
- class AccessTokenRefreshError(Error):
- """Error trying to refresh an expired access token."""
- class HttpAccessTokenRefreshError(AccessTokenRefreshError):
- """Error (with HTTP status) trying to refresh an expired access token."""
- def __init__(self, *args, **kwargs):
- super(HttpAccessTokenRefreshError, self).__init__(*args)
- self.status = kwargs.get('status')
- class TokenRevokeError(Error):
- """Error trying to revoke a token."""
- class UnknownClientSecretsFlowError(Error):
- """The client secrets file called for an unknown type of OAuth 2.0 flow."""
- class AccessTokenCredentialsError(Error):
- """Having only the access_token means no refresh is possible."""
- class VerifyJwtTokenError(Error):
- """Could not retrieve certificates for validation."""
- class NonAsciiHeaderError(Error):
- """Header names and values must be ASCII strings."""
- class ApplicationDefaultCredentialsError(Error):
- """Error retrieving the Application Default Credentials."""
- class OAuth2DeviceCodeError(Error):
- """Error trying to retrieve a device code."""
- class CryptoUnavailableError(Error, NotImplementedError):
- """Raised when a crypto library is required, but none is available."""
- def _parse_expiry(expiry):
- if expiry and isinstance(expiry, datetime.datetime):
- return expiry.strftime(EXPIRY_FORMAT)
- else:
- return None
- class Credentials(object):
- """Base class for all Credentials objects.
- Subclasses must define an authorize() method that applies the credentials
- to an HTTP transport.
- Subclasses must also specify a classmethod named 'from_json' that takes a
- JSON string as input and returns an instantiated Credentials object.
- """
- NON_SERIALIZED_MEMBERS = frozenset(['store'])
- def authorize(self, http):
- """Take an httplib2.Http instance (or equivalent) and authorizes it.
- Authorizes it for the set of credentials, usually by replacing
- http.request() with a method that adds in the appropriate headers and
- then delegates to the original Http.request() method.
- Args:
- http: httplib2.Http, an http object to be used to make the refresh
- request.
- """
- raise NotImplementedError
- def refresh(self, http):
- """Forces a refresh of the access_token.
- Args:
- http: httplib2.Http, an http object to be used to make the refresh
- request.
- """
- raise NotImplementedError
- def revoke(self, http):
- """Revokes a refresh_token and makes the credentials void.
- Args:
- http: httplib2.Http, an http object to be used to make the revoke
- request.
- """
- raise NotImplementedError
- def apply(self, headers):
- """Add the authorization to the headers.
- Args:
- headers: dict, the headers to add the Authorization header to.
- """
- raise NotImplementedError
- def _to_json(self, strip, to_serialize=None):
- """Utility function that creates JSON repr. of a Credentials object.
- Args:
- strip: array, An array of names of members to exclude from the
- JSON.
- to_serialize: dict, (Optional) The properties for this object
- that will be serialized. This allows callers to
- modify before serializing.
- Returns:
- string, a JSON representation of this instance, suitable to pass to
- from_json().
- """
- curr_type = self.__class__
- if to_serialize is None:
- to_serialize = copy.copy(self.__dict__)
- else:
- # Assumes it is a str->str dictionary, so we don't deep copy.
- to_serialize = copy.copy(to_serialize)
- for member in strip:
- if member in to_serialize:
- del to_serialize[member]
- to_serialize['token_expiry'] = _parse_expiry(
- to_serialize.get('token_expiry'))
- # Add in information we will need later to reconstitute this instance.
- to_serialize['_class'] = curr_type.__name__
- to_serialize['_module'] = curr_type.__module__
- for key, val in to_serialize.items():
- if isinstance(val, bytes):
- to_serialize[key] = val.decode('utf-8')
- if isinstance(val, set):
- to_serialize[key] = list(val)
- return json.dumps(to_serialize)
- def to_json(self):
- """Creating a JSON representation of an instance of Credentials.
- Returns:
- string, a JSON representation of this instance, suitable to pass to
- from_json().
- """
- return self._to_json(self.NON_SERIALIZED_MEMBERS)
- @classmethod
- def new_from_json(cls, json_data):
- """Utility class method to instantiate a Credentials subclass from JSON.
- Expects the JSON string to have been produced by to_json().
- Args:
- json_data: string or bytes, JSON from to_json().
- Returns:
- An instance of the subclass of Credentials that was serialized with
- to_json().
- """
- json_data_as_unicode = _helpers._from_bytes(json_data)
- data = json.loads(json_data_as_unicode)
- # Find and call the right classmethod from_json() to restore
- # the object.
- module_name = data['_module']
- try:
- module_obj = __import__(module_name)
- except ImportError:
- # In case there's an object from the old package structure,
- # update it
- module_name = module_name.replace('.googleapiclient', '')
- module_obj = __import__(module_name)
- module_obj = __import__(module_name,
- fromlist=module_name.split('.')[:-1])
- kls = getattr(module_obj, data['_class'])
- return kls.from_json(json_data_as_unicode)
- @classmethod
- def from_json(cls, unused_data):
- """Instantiate a Credentials object from a JSON description of it.
- The JSON should have been produced by calling .to_json() on the object.
- Args:
- unused_data: dict, A deserialized JSON object.
- Returns:
- An instance of a Credentials subclass.
- """
- return Credentials()
- class Flow(object):
- """Base class for all Flow objects."""
- pass
- class Storage(object):
- """Base class for all Storage objects.
- Store and retrieve a single credential. This class supports locking
- such that multiple processes and threads can operate on a single
- store.
- """
- def __init__(self, lock=None):
- """Create a Storage instance.
- Args:
- lock: An optional threading.Lock-like object. Must implement at
- least acquire() and release(). Does not need to be
- re-entrant.
- """
- self._lock = lock
- def acquire_lock(self):
- """Acquires any lock necessary to access this Storage.
- This lock is not reentrant.
- """
- if self._lock is not None:
- self._lock.acquire()
- def release_lock(self):
- """Release the Storage lock.
- Trying to release a lock that isn't held will result in a
- RuntimeError in the case of a threading.Lock or multiprocessing.Lock.
- """
- if self._lock is not None:
- self._lock.release()
- def locked_get(self):
- """Retrieve credential.
- The Storage lock must be held when this is called.
- Returns:
- oauth2client.client.Credentials
- """
- raise NotImplementedError
- def locked_put(self, credentials):
- """Write a credential.
- The Storage lock must be held when this is called.
- Args:
- credentials: Credentials, the credentials to store.
- """
- raise NotImplementedError
- def locked_delete(self):
- """Delete a credential.
- The Storage lock must be held when this is called.
- """
- raise NotImplementedError
- def get(self):
- """Retrieve credential.
- The Storage lock must *not* be held when this is called.
- Returns:
- oauth2client.client.Credentials
- """
- self.acquire_lock()
- try:
- return self.locked_get()
- finally:
- self.release_lock()
- def put(self, credentials):
- """Write a credential.
- The Storage lock must be held when this is called.
- Args:
- credentials: Credentials, the credentials to store.
- """
- self.acquire_lock()
- try:
- self.locked_put(credentials)
- finally:
- self.release_lock()
- def delete(self):
- """Delete credential.
- Frees any resources associated with storing the credential.
- The Storage lock must *not* be held when this is called.
- Returns:
- None
- """
- self.acquire_lock()
- try:
- return self.locked_delete()
- finally:
- self.release_lock()
- class OAuth2Credentials(Credentials):
- """Credentials object for OAuth 2.0.
- Credentials can be applied to an httplib2.Http object using the authorize()
- method, which then adds the OAuth 2.0 access token to each request.
- OAuth2Credentials objects may be safely pickled and unpickled.
- """
- @_helpers.positional(8)
- def __init__(self, access_token, client_id, client_secret, refresh_token,
- token_expiry, token_uri, user_agent, revoke_uri=None,
- id_token=None, token_response=None, scopes=None,
- token_info_uri=None, id_token_jwt=None):
- """Create an instance of OAuth2Credentials.
- This constructor is not usually called by the user, instead
- OAuth2Credentials objects are instantiated by the OAuth2WebServerFlow.
- Args:
- access_token: string, access token.
- client_id: string, client identifier.
- client_secret: string, client secret.
- refresh_token: string, refresh token.
- token_expiry: datetime, when the access_token expires.
- token_uri: string, URI of token endpoint.
- user_agent: string, The HTTP User-Agent to provide for this
- application.
- revoke_uri: string, URI for revoke endpoint. Defaults to None; a
- token can't be revoked if this is None.
- id_token: object, The identity of the resource owner.
- token_response: dict, the decoded response to the token request.
- None if a token hasn't been requested yet. Stored
- because some providers (e.g. wordpress.com) include
- extra fields that clients may want.
- scopes: list, authorized scopes for these credentials.
- token_info_uri: string, the URI for the token info endpoint.
- Defaults to None; scopes can not be refreshed if
- this is None.
- id_token_jwt: string, the encoded and signed identity JWT. The
- decoded version of this is stored in id_token.
- Notes:
- store: callable, A callable that when passed a Credential
- will store the credential back to where it came from.
- This is needed to store the latest access_token if it
- has expired and been refreshed.
- """
- self.access_token = access_token
- self.client_id = client_id
- self.client_secret = client_secret
- self.refresh_token = refresh_token
- self.store = None
- self.token_expiry = token_expiry
- self.token_uri = token_uri
- self.user_agent = user_agent
- self.revoke_uri = revoke_uri
- self.id_token = id_token
- self.id_token_jwt = id_token_jwt
- self.token_response = token_response
- self.scopes = set(_helpers.string_to_scopes(scopes or []))
- self.token_info_uri = token_info_uri
- # True if the credentials have been revoked or expired and can't be
- # refreshed.
- self.invalid = False
- def authorize(self, http):
- """Authorize an httplib2.Http instance with these credentials.
- The modified http.request method will add authentication headers to
- each request and will refresh access_tokens when a 401 is received on a
- request. In addition the http.request method has a credentials
- property, http.request.credentials, which is the Credentials object
- that authorized it.
- Args:
- http: An instance of ``httplib2.Http`` or something that acts
- like it.
- Returns:
- A modified instance of http that was passed in.
- Example::
- h = httplib2.Http()
- h = credentials.authorize(h)
- You can't create a new OAuth subclass of httplib2.Authentication
- because it never gets passed the absolute URI, which is needed for
- signing. So instead we have to overload 'request' with a closure
- that adds in the Authorization header and then calls the original
- version of 'request()'.
- """
- transport.wrap_http_for_auth(self, http)
- return http
- def refresh(self, http):
- """Forces a refresh of the access_token.
- Args:
- http: httplib2.Http, an http object to be used to make the refresh
- request.
- """
- self._refresh(http)
- def revoke(self, http):
- """Revokes a refresh_token and makes the credentials void.
- Args:
- http: httplib2.Http, an http object to be used to make the revoke
- request.
- """
- self._revoke(http)
- def apply(self, headers):
- """Add the authorization to the headers.
- Args:
- headers: dict, the headers to add the Authorization header to.
- """
- headers['Authorization'] = 'Bearer ' + self.access_token
- def has_scopes(self, scopes):
- """Verify that the credentials are authorized for the given scopes.
- Returns True if the credentials authorized scopes contain all of the
- scopes given.
- Args:
- scopes: list or string, the scopes to check.
- Notes:
- There are cases where the credentials are unaware of which scopes
- are authorized. Notably, credentials obtained and stored before
- this code was added will not have scopes, AccessTokenCredentials do
- not have scopes. In both cases, you can use refresh_scopes() to
- obtain the canonical set of scopes.
- """
- scopes = _helpers.string_to_scopes(scopes)
- return set(scopes).issubset(self.scopes)
- def retrieve_scopes(self, http):
- """Retrieves the canonical list of scopes for this access token.
- Gets the scopes from the OAuth2 provider.
- Args:
- http: httplib2.Http, an http object to be used to make the refresh
- request.
- Returns:
- A set of strings containing the canonical list of scopes.
- """
- self._retrieve_scopes(http)
- return self.scopes
- @classmethod
- def from_json(cls, json_data):
- """Instantiate a Credentials object from a JSON description of it.
- The JSON should have been produced by calling .to_json() on the object.
- Args:
- json_data: string or bytes, JSON to deserialize.
- Returns:
- An instance of a Credentials subclass.
- """
- data = json.loads(_helpers._from_bytes(json_data))
- if (data.get('token_expiry') and
- not isinstance(data['token_expiry'], datetime.datetime)):
- try:
- data['token_expiry'] = datetime.datetime.strptime(
- data['token_expiry'], EXPIRY_FORMAT)
- except ValueError:
- data['token_expiry'] = None
- retval = cls(
- data['access_token'],
- data['client_id'],
- data['client_secret'],
- data['refresh_token'],
- data['token_expiry'],
- data['token_uri'],
- data['user_agent'],
- revoke_uri=data.get('revoke_uri', None),
- id_token=data.get('id_token', None),
- id_token_jwt=data.get('id_token_jwt', None),
- token_response=data.get('token_response', None),
- scopes=data.get('scopes', None),
- token_info_uri=data.get('token_info_uri', None))
- retval.invalid = data['invalid']
- return retval
- @property
- def access_token_expired(self):
- """True if the credential is expired or invalid.
- If the token_expiry isn't set, we assume the token doesn't expire.
- """
- if self.invalid:
- return True
- if not self.token_expiry:
- return False
- now = _UTCNOW()
- if now >= self.token_expiry:
- logger.info('access_token is expired. Now: %s, token_expiry: %s',
- now, self.token_expiry)
- return True
- return False
- def get_access_token(self, http=None):
- """Return the access token and its expiration information.
- If the token does not exist, get one.
- If the token expired, refresh it.
- """
- if not self.access_token or self.access_token_expired:
- if not http:
- http = transport.get_http_object()
- self.refresh(http)
- return AccessTokenInfo(access_token=self.access_token,
- expires_in=self._expires_in())
- def set_store(self, store):
- """Set the Storage for the credential.
- Args:
- store: Storage, an implementation of Storage object.
- This is needed to store the latest access_token if it
- has expired and been refreshed. This implementation uses
- locking to check for updates before updating the
- access_token.
- """
- self.store = store
- def _expires_in(self):
- """Return the number of seconds until this token expires.
- If token_expiry is in the past, this method will return 0, meaning the
- token has already expired.
- If token_expiry is None, this method will return None. Note that
- returning 0 in such a case would not be fair: the token may still be
- valid; we just don't know anything about it.
- """
- if self.token_expiry:
- now = _UTCNOW()
- if self.token_expiry > now:
- time_delta = self.token_expiry - now
- # TODO(orestica): return time_delta.total_seconds()
- # once dropping support for Python 2.6
- return time_delta.days * 86400 + time_delta.seconds
- else:
- return 0
- def _updateFromCredential(self, other):
- """Update this Credential from another instance."""
- self.__dict__.update(other.__getstate__())
- def __getstate__(self):
- """Trim the state down to something that can be pickled."""
- d = copy.copy(self.__dict__)
- del d['store']
- return d
- def __setstate__(self, state):
- """Reconstitute the state of the object from being pickled."""
- self.__dict__.update(state)
- self.store = None
- def _generate_refresh_request_body(self):
- """Generate the body that will be used in the refresh request."""
- body = urllib.parse.urlencode({
- 'grant_type': 'refresh_token',
- 'client_id': self.client_id,
- 'client_secret': self.client_secret,
- 'refresh_token': self.refresh_token,
- })
- return body
- def _generate_refresh_request_headers(self):
- """Generate the headers that will be used in the refresh request."""
- headers = {
- 'content-type': 'application/x-www-form-urlencoded',
- }
- if self.user_agent is not None:
- headers['user-agent'] = self.user_agent
- return headers
- def _refresh(self, http):
- """Refreshes the access_token.
- This method first checks by reading the Storage object if available.
- If a refresh is still needed, it holds the Storage lock until the
- refresh is completed.
- Args:
- http: an object to be used to make HTTP requests.
- Raises:
- HttpAccessTokenRefreshError: When the refresh fails.
- """
- if not self.store:
- self._do_refresh_request(http)
- else:
- self.store.acquire_lock()
- try:
- new_cred = self.store.locked_get()
- if (new_cred and not new_cred.invalid and
- new_cred.access_token != self.access_token and
- not new_cred.access_token_expired):
- logger.info('Updated access_token read from Storage')
- self._updateFromCredential(new_cred)
- else:
- self._do_refresh_request(http)
- finally:
- self.store.release_lock()
- def _do_refresh_request(self, http):
- """Refresh the access_token using the refresh_token.
- Args:
- http: an object to be used to make HTTP requests.
- Raises:
- HttpAccessTokenRefreshError: When the refresh fails.
- """
- body = self._generate_refresh_request_body()
- headers = self._generate_refresh_request_headers()
- logger.info('Refreshing access_token')
- resp, content = transport.request(
- http, self.token_uri, method='POST',
- body=body, headers=headers)
- content = _helpers._from_bytes(content)
- if resp.status == http_client.OK:
- d = json.loads(content)
- self.token_response = d
- self.access_token = d['access_token']
- self.refresh_token = d.get('refresh_token', self.refresh_token)
- if 'expires_in' in d:
- delta = datetime.timedelta(seconds=int(d['expires_in']))
- self.token_expiry = delta + _UTCNOW()
- else:
- self.token_expiry = None
- if 'id_token' in d:
- self.id_token = _extract_id_token(d['id_token'])
- self.id_token_jwt = d['id_token']
- else:
- self.id_token = None
- self.id_token_jwt = None
- # On temporary refresh errors, the user does not actually have to
- # re-authorize, so we unflag here.
- self.invalid = False
- if self.store:
- self.store.locked_put(self)
- else:
- # An {'error':...} response body means the token is expired or
- # revoked, so we flag the credentials as such.
- logger.info('Failed to retrieve access token: %s', content)
- error_msg = 'Invalid response {0}.'.format(resp.status)
- try:
- d = json.loads(content)
- if 'error' in d:
- error_msg = d['error']
- if 'error_description' in d:
- error_msg += ': ' + d['error_description']
- self.invalid = True
- if self.store is not None:
- self.store.locked_put(self)
- except (TypeError, ValueError):
- pass
- raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
- def _revoke(self, http):
- """Revokes this credential and deletes the stored copy (if it exists).
- Args:
- http: an object to be used to make HTTP requests.
- """
- self._do_revoke(http, self.refresh_token or self.access_token)
- def _do_revoke(self, http, token):
- """Revokes this credential and deletes the stored copy (if it exists).
- Args:
- http: an object to be used to make HTTP requests.
- token: A string used as the token to be revoked. Can be either an
- access_token or refresh_token.
- Raises:
- TokenRevokeError: If the revoke request does not return with a
- 200 OK.
- """
- logger.info('Revoking token')
- query_params = {'token': token}
- token_revoke_uri = _helpers.update_query_params(
- self.revoke_uri, query_params)
- resp, content = transport.request(http, token_revoke_uri)
- if resp.status == http_client.METHOD_NOT_ALLOWED:
- body = urllib.parse.urlencode(query_params)
- resp, content = transport.request(http, token_revoke_uri,
- method='POST', body=body)
- if resp.status == http_client.OK:
- self.invalid = True
- else:
- error_msg = 'Invalid response {0}.'.format(resp.status)
- try:
- d = json.loads(_helpers._from_bytes(content))
- if 'error' in d:
- error_msg = d['error']
- except (TypeError, ValueError):
- pass
- raise TokenRevokeError(error_msg)
- if self.store:
- self.store.delete()
- def _retrieve_scopes(self, http):
- """Retrieves the list of authorized scopes from the OAuth2 provider.
- Args:
- http: an object to be used to make HTTP requests.
- """
- self._do_retrieve_scopes(http, self.access_token)
- def _do_retrieve_scopes(self, http, token):
- """Retrieves the list of authorized scopes from the OAuth2 provider.
- Args:
- http: an object to be used to make HTTP requests.
- token: A string used as the token to identify the credentials to
- the provider.
- Raises:
- Error: When refresh fails, indicating the the access token is
- invalid.
- """
- logger.info('Refreshing scopes')
- query_params = {'access_token': token, 'fields': 'scope'}
- token_info_uri = _helpers.update_query_params(
- self.token_info_uri, query_params)
- resp, content = transport.request(http, token_info_uri)
- content = _helpers._from_bytes(content)
- if resp.status == http_client.OK:
- d = json.loads(content)
- self.scopes = set(_helpers.string_to_scopes(d.get('scope', '')))
- else:
- error_msg = 'Invalid response {0}.'.format(resp.status)
- try:
- d = json.loads(content)
- if 'error_description' in d:
- error_msg = d['error_description']
- except (TypeError, ValueError):
- pass
- raise Error(error_msg)
- class AccessTokenCredentials(OAuth2Credentials):
- """Credentials object for OAuth 2.0.
- Credentials can be applied to an httplib2.Http object using the
- authorize() method, which then signs each request from that object
- with the OAuth 2.0 access token. This set of credentials is for the
- use case where you have acquired an OAuth 2.0 access_token from
- another place such as a JavaScript client or another web
- application, and wish to use it from Python. Because only the
- access_token is present it can not be refreshed and will in time
- expire.
- AccessTokenCredentials objects may be safely pickled and unpickled.
- Usage::
- credentials = AccessTokenCredentials('<an access token>',
- 'my-user-agent/1.0')
- http = httplib2.Http()
- http = credentials.authorize(http)
- Raises:
- AccessTokenCredentialsExpired: raised when the access_token expires or
- is revoked.
- """
- def __init__(self, access_token, user_agent, revoke_uri=None):
- """Create an instance of OAuth2Credentials
- This is one of the few types if Credentials that you should contrust,
- Credentials objects are usually instantiated by a Flow.
- Args:
- access_token: string, access token.
- user_agent: string, The HTTP User-Agent to provide for this
- application.
- revoke_uri: string, URI for revoke endpoint. Defaults to None; a
- token can't be revoked if this is None.
- """
- super(AccessTokenCredentials, self).__init__(
- access_token,
- None,
- None,
- None,
- None,
- None,
- user_agent,
- revoke_uri=revoke_uri)
- @classmethod
- def from_json(cls, json_data):
- data = json.loads(_helpers._from_bytes(json_data))
- retval = AccessTokenCredentials(
- data['access_token'],
- data['user_agent'])
- return retval
- def _refresh(self, http):
- """Refreshes the access token.
- Args:
- http: unused HTTP object.
- Raises:
- AccessTokenCredentialsError: always
- """
- raise AccessTokenCredentialsError(
- 'The access_token is expired or invalid and can\'t be refreshed.')
- def _revoke(self, http):
- """Revokes the access_token and deletes the store if available.
- Args:
- http: an object to be used to make HTTP requests.
- """
- self._do_revoke(http, self.access_token)
- def _detect_gce_environment():
- """Determine if the current environment is Compute Engine.
- Returns:
- Boolean indicating whether or not the current environment is Google
- Compute Engine.
- """
- # NOTE: The explicit ``timeout`` is a workaround. The underlying
- # issue is that resolving an unknown host on some networks will take
- # 20-30 seconds; making this timeout short fixes the issue, but
- # could lead to false negatives in the event that we are on GCE, but
- # the metadata resolution was particularly slow. The latter case is
- # "unlikely".
- http = transport.get_http_object(timeout=GCE_METADATA_TIMEOUT)
- try:
- response, _ = transport.request(
- http, _GCE_METADATA_URI, headers=_GCE_HEADERS)
- return (
- response.status == http_client.OK and
- response.get(_METADATA_FLAVOR_HEADER) == _DESIRED_METADATA_FLAVOR)
- except socket.error: # socket.timeout or socket.error(64, 'Host is down')
- logger.info('Timeout attempting to reach GCE metadata service.')
- return False
- def _in_gae_environment():
- """Detects if the code is running in the App Engine environment.
- Returns:
- True if running in the GAE environment, False otherwise.
- """
- if SETTINGS.env_name is not None:
- return SETTINGS.env_name in ('GAE_PRODUCTION', 'GAE_LOCAL')
- try:
- import google.appengine # noqa: unused import
- except ImportError:
- pass
- else:
- server_software = os.environ.get(_SERVER_SOFTWARE, '')
- if server_software.startswith('Google App Engine/'):
- SETTINGS.env_name = 'GAE_PRODUCTION'
- return True
- elif server_software.startswith('Development/'):
- SETTINGS.env_name = 'GAE_LOCAL'
- return True
- return False
- def _in_gce_environment():
- """Detect if the code is running in the Compute Engine environment.
- Returns:
- True if running in the GCE environment, False otherwise.
- """
- if SETTINGS.env_name is not None:
- return SETTINGS.env_name == 'GCE_PRODUCTION'
- if NO_GCE_CHECK != 'True' and _detect_gce_environment():
- SETTINGS.env_name = 'GCE_PRODUCTION'
- return True
- return False
- class GoogleCredentials(OAuth2Credentials):
- """Application Default Credentials for use in calling Google APIs.
- The Application Default Credentials are being constructed as a function of
- the environment where the code is being run.
- More details can be found on this page:
- https://developers.google.com/accounts/docs/application-default-credentials
- Here is an example of how to use the Application Default Credentials for a
- service that requires authentication::
- from googleapiclient.discovery import build
- from oauth2client.client import GoogleCredentials
- credentials = GoogleCredentials.get_application_default()
- service = build('compute', 'v1', credentials=credentials)
- PROJECT = 'bamboo-machine-422'
- ZONE = 'us-central1-a'
- request = service.instances().list(project=PROJECT, zone=ZONE)
- response = request.execute()
- print(response)
- """
- NON_SERIALIZED_MEMBERS = (
- frozenset(['_private_key']) |
- OAuth2Credentials.NON_SERIALIZED_MEMBERS)
- """Members that aren't serialized when object is converted to JSON."""
- def __init__(self, access_token, client_id, client_secret, refresh_token,
- token_expiry, token_uri, user_agent,
- revoke_uri=oauth2client.GOOGLE_REVOKE_URI):
- """Create an instance of GoogleCredentials.
- This constructor is not usually called by the user, instead
- GoogleCredentials objects are instantiated by
- GoogleCredentials.from_stream() or
- GoogleCredentials.get_application_default().
- Args:
- access_token: string, access token.
- client_id: string, client identifier.
- client_secret: string, client secret.
- refresh_token: string, refresh token.
- token_expiry: datetime, when the access_token expires.
- token_uri: string, URI of token endpoint.
- user_agent: string, The HTTP User-Agent to provide for this
- application.
- revoke_uri: string, URI for revoke endpoint. Defaults to
- oauth2client.GOOGLE_REVOKE_URI; a token can't be
- revoked if this is None.
- """
- super(GoogleCredentials, self).__init__(
- access_token, client_id, client_secret, refresh_token,
- token_expiry, token_uri, user_agent, revoke_uri=revoke_uri)
- def create_scoped_required(self):
- """Whether this Credentials object is scopeless.
- create_scoped(scopes) method needs to be called in order to create
- a Credentials object for API calls.
- """
- return False
- def create_scoped(self, scopes):
- """Create a Credentials object for the given scopes.
- The Credentials type is preserved.
- """
- return self
- @classmethod
- def from_json(cls, json_data):
- # TODO(issue 388): eliminate the circularity that is the reason for
- # this non-top-level import.
- from oauth2client import service_account
- data = json.loads(_helpers._from_bytes(json_data))
- # We handle service_account.ServiceAccountCredentials since it is a
- # possible return type of GoogleCredentials.get_application_default()
- if (data['_module'] == 'oauth2client.service_account' and
- data['_class'] == 'ServiceAccountCredentials'):
- return service_account.ServiceAccountCredentials.from_json(data)
- elif (data['_module'] == 'oauth2client.service_account' and
- data['_class'] == '_JWTAccessCredentials'):
- return service_account._JWTAccessCredentials.from_json(data)
- token_expiry = _parse_expiry(data.get('token_expiry'))
- google_credentials = cls(
- data['access_token'],
- data['client_id'],
- data['client_secret'],
- data['refresh_token'],
- token_expiry,
- data['token_uri'],
- data['user_agent'],
- revoke_uri=data.get('revoke_uri', None))
- google_credentials.invalid = data['invalid']
- return google_credentials
- @property
- def serialization_data(self):
- """Get the fields and values identifying the current credentials."""
- return {
- 'type': 'authorized_user',
- 'client_id': self.client_id,
- 'client_secret': self.client_secret,
- 'refresh_token': self.refresh_token
- }
- @staticmethod
- def _implicit_credentials_from_gae():
- """Attempts to get implicit credentials in Google App Engine env.
- If the current environment is not detected as App Engine, returns None,
- indicating no Google App Engine credentials can be detected from the
- current environment.
- Returns:
- None, if not in GAE, else an appengine.AppAssertionCredentials
- object.
- """
- if not _in_gae_environment():
- return None
- return _get_application_default_credential_GAE()
- @staticmethod
- def _implicit_credentials_from_gce():
- """Attempts to get implicit credentials in Google Compute Engine env.
- If the current environment is not detected as Compute Engine, returns
- None, indicating no Google Compute Engine credentials can be detected
- from the current environment.
- Returns:
- None, if not in GCE, else a gce.AppAssertionCredentials object.
- """
- if not _in_gce_environment():
- return None
- return _get_application_default_credential_GCE()
- @staticmethod
- def _implicit_credentials_from_files():
- """Attempts to get implicit credentials from local credential files.
- First checks if the environment variable GOOGLE_APPLICATION_CREDENTIALS
- is set with a filename and then falls back to a configuration file (the
- "well known" file) associated with the 'gcloud' command line tool.
- Returns:
- Credentials object associated with the
- GOOGLE_APPLICATION_CREDENTIALS file or the "well known" file if
- either exist. If neither file is define, returns None, indicating
- no credentials from a file can detected from the current
- environment.
- """
- credentials_filename = _get_environment_variable_file()
- if not credentials_filename:
- credentials_filename = _get_well_known_file()
- if os.path.isfile(credentials_filename):
- extra_help = (' (produced automatically when running'
- ' "gcloud auth login" command)')
- else:
- credentials_filename = None
- else:
- extra_help = (' (pointed to by ' + GOOGLE_APPLICATION_CREDENTIALS +
- ' environment variable)')
- if not credentials_filename:
- return
- # If we can read the credentials from a file, we don't need to know
- # what environment we are in.
- SETTINGS.env_name = DEFAULT_ENV_NAME
- try:
- return _get_application_default_credential_from_file(
- credentials_filename)
- except (ApplicationDefaultCredentialsError, ValueError) as error:
- _raise_exception_for_reading_json(credentials_filename,
- extra_help, error)
- @classmethod
- def _get_implicit_credentials(cls):
- """Gets credentials implicitly from the environment.
- Checks environment in order of precedence:
- - Environment variable GOOGLE_APPLICATION_CREDENTIALS pointing to
- a file with stored credentials information.
- - Stored "well known" file associated with `gcloud` command line tool.
- - Google App Engine (production and testing)
- - Google Compute Engine production environment.
- Raises:
- ApplicationDefaultCredentialsError: raised when the credentials
- fail to be retrieved.
- """
- # Environ checks (in order).
- environ_checkers = [
- cls._implicit_credentials_from_files,
- cls._implicit_credentials_from_gae,
- cls._implicit_credentials_from_gce,
- ]
- for checker in environ_checkers:
- credentials = checker()
- if credentials is not None:
- return credentials
- # If no credentials, fail.
- raise ApplicationDefaultCredentialsError(ADC_HELP_MSG)
- @staticmethod
- def get_application_default():
- """Get the Application Default Credentials for the current environment.
- Raises:
- ApplicationDefaultCredentialsError: raised when the credentials
- fail to be retrieved.
- """
- return GoogleCredentials._get_implicit_credentials()
- @staticmethod
- def from_stream(credential_filename):
- """Create a Credentials object by reading information from a file.
- It returns an object of type GoogleCredentials.
- Args:
- credential_filename: the path to the file from where the
- credentials are to be read
- Raises:
- ApplicationDefaultCredentialsError: raised when the credentials
- fail to be retrieved.
- """
- if credential_filename and os.path.isfile(credential_filename):
- try:
- return _get_application_default_credential_from_file(
- credential_filename)
- except (ApplicationDefaultCredentialsError, ValueError) as error:
- extra_help = (' (provided as parameter to the '
- 'from_stream() method)')
- _raise_exception_for_reading_json(credential_filename,
- extra_help,
- error)
- else:
- raise ApplicationDefaultCredentialsError(
- 'The parameter passed to the from_stream() '
- 'method should point to a file.')
- def _save_private_file(filename, json_contents):
- """Saves a file with read-write permissions on for the owner.
- Args:
- filename: String. Absolute path to file.
- json_contents: JSON serializable object to be saved.
- """
- temp_filename = tempfile.mktemp()
- file_desc = os.open(temp_filename, os.O_WRONLY | os.O_CREAT, 0o600)
- with os.fdopen(file_desc, 'w') as file_handle:
- json.dump(json_contents, file_handle, sort_keys=True,
- indent=2, separators=(',', ': '))
- shutil.move(temp_filename, filename)
- def save_to_well_known_file(credentials, well_known_file=None):
- """Save the provided GoogleCredentials to the well known file.
- Args:
- credentials: the credentials to be saved to the well known file;
- it should be an instance of GoogleCredentials
- well_known_file: the name of the file where the credentials are to be
- saved; this parameter is supposed to be used for
- testing only
- """
- # TODO(orestica): move this method to tools.py
- # once the argparse import gets fixed (it is not present in Python 2.6)
- if well_known_file is None:
- well_known_file = _get_well_known_file()
- config_dir = os.path.dirname(well_known_file)
- if not os.path.isdir(config_dir):
- raise OSError(
- 'Config directory does not exist: {0}'.format(config_dir))
- credentials_data = credentials.serialization_data
- _save_private_file(well_known_file, credentials_data)
- def _get_environment_variable_file():
- application_default_credential_filename = (
- os.environ.get(GOOGLE_APPLICATION_CREDENTIALS, None))
- if application_default_credential_filename:
- if os.path.isfile(application_default_credential_filename):
- return application_default_credential_filename
- else:
- raise ApplicationDefaultCredentialsError(
- 'File ' + application_default_credential_filename +
- ' (pointed by ' +
- GOOGLE_APPLICATION_CREDENTIALS +
- ' environment variable) does not exist!')
- def _get_well_known_file():
- """Get the well known file produced by command 'gcloud auth login'."""
- # TODO(orestica): Revisit this method once gcloud provides a better way
- # of pinpointing the exact location of the file.
- default_config_dir = os.getenv(_CLOUDSDK_CONFIG_ENV_VAR)
- if default_config_dir is None:
- if os.name == 'nt':
- try:
- default_config_dir = os.path.join(os.environ['APPDATA'],
- _CLOUDSDK_CONFIG_DIRECTORY)
- except KeyError:
- # This should never happen unless someone is really
- # messing with things.
- drive = os.environ.get('SystemDrive', 'C:')
- default_config_dir = os.path.join(drive, '\\',
- _CLOUDSDK_CONFIG_DIRECTORY)
- else:
- default_config_dir = os.path.join(os.path.expanduser('~'),
- '.config',
- _CLOUDSDK_CONFIG_DIRECTORY)
- return os.path.join(default_config_dir, _WELL_KNOWN_CREDENTIALS_FILE)
- def _get_application_default_credential_from_file(filename):
- """Build the Application Default Credentials from file."""
- # read the credentials from the file
- with open(filename) as file_obj:
- client_credentials = json.load(file_obj)
- credentials_type = client_credentials.get('type')
- if credentials_type == AUTHORIZED_USER:
- required_fields = set(['client_id', 'client_secret', 'refresh_token'])
- elif credentials_type == SERVICE_ACCOUNT:
- required_fields = set(['client_id', 'client_email', 'private_key_id',
- 'private_key'])
- else:
- raise ApplicationDefaultCredentialsError(
- "'type' field should be defined (and have one of the '" +
- AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)")
- missing_fields = required_fields.difference(client_credentials.keys())
- if missing_fields:
- _raise_exception_for_missing_fields(missing_fields)
- if client_credentials['type'] == AUTHORIZED_USER:
- return GoogleCredentials(
- access_token=None,
- client_id=client_credentials['client_id'],
- client_secret=client_credentials['client_secret'],
- refresh_token=client_credentials['refresh_token'],
- token_expiry=None,
- token_uri=oauth2client.GOOGLE_TOKEN_URI,
- user_agent='Python client library')
- else: # client_credentials['type'] == SERVICE_ACCOUNT
- from oauth2client import service_account
- return service_account._JWTAccessCredentials.from_json_keyfile_dict(
- client_credentials)
- def _raise_exception_for_missing_fields(missing_fields):
- raise ApplicationDefaultCredentialsError(
- 'The following field(s) must be defined: ' + ', '.join(missing_fields))
- def _raise_exception_for_reading_json(credential_file,
- extra_help,
- error):
- raise ApplicationDefaultCredentialsError(
- 'An error was encountered while reading json file: ' +
- credential_file + extra_help + ': ' + str(error))
- def _get_application_default_credential_GAE():
- from oauth2client.contrib.appengine import AppAssertionCredentials
- return AppAssertionCredentials([])
- def _get_application_default_credential_GCE():
- from oauth2client.contrib.gce import AppAssertionCredentials
- return AppAssertionCredentials()
- class AssertionCredentials(GoogleCredentials):
- """Abstract Credentials object used for OAuth 2.0 assertion grants.
- This credential does not require a flow to instantiate because it
- represents a two legged flow, and therefore has all of the required
- information to generate and refresh its own access tokens. It must
- be subclassed to generate the appropriate assertion string.
- AssertionCredentials objects may be safely pickled and unpickled.
- """
- @_helpers.positional(2)
- def __init__(self, assertion_type, user_agent=None,
- token_uri=oauth2client.GOOGLE_TOKEN_URI,
- revoke_uri=oauth2client.GOOGLE_REVOKE_URI,
- **unused_kwargs):
- """Constructor for AssertionFlowCredentials.
- Args:
- assertion_type: string, assertion type that will be declared to the
- auth server
- user_agent: string, The HTTP User-Agent to provide for this
- application.
- token_uri: string, URI for token endpoint. For convenience defaults
- to Google's endpoints but any OAuth 2.0 provider can be
- used.
- revoke_uri: string, URI for revoke endpoint.
- """
- super(AssertionCredentials, self).__init__(
- None,
- None,
- None,
- None,
- None,
- token_uri,
- user_agent,
- revoke_uri=revoke_uri)
- self.assertion_type = assertion_type
- def _generate_refresh_request_body(self):
- assertion = self._generate_assertion()
- body = urllib.parse.urlencode({
- 'assertion': assertion,
- 'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
- })
- return body
- def _generate_assertion(self):
- """Generate assertion string to be used in the access token request."""
- raise NotImplementedError
- def _revoke(self, http):
- """Revokes the access_token and deletes the store if available.
- Args:
- http: an object to be used to make HTTP requests.
- """
- self._do_revoke(http, self.access_token)
- def sign_blob(self, blob):
- """Cryptographically sign a blob (of bytes).
- Args:
- blob: bytes, Message to be signed.
- Returns:
- tuple, A pair of the private key ID used to sign the blob and
- the signed contents.
- """
- raise NotImplementedError('This method is abstract.')
- def _require_crypto_or_die():
- """Ensure we have a crypto library, or throw CryptoUnavailableError.
- The oauth2client.crypt module requires either PyCrypto or PyOpenSSL
- to be available in order to function, but these are optional
- dependencies.
- """
- if not HAS_CRYPTO:
- raise CryptoUnavailableError('No crypto library available')
- @_helpers.positional(2)
- def verify_id_token(id_token, audience, http=None,
- cert_uri=ID_TOKEN_VERIFICATION_CERTS):
- """Verifies a signed JWT id_token.
- This function requires PyOpenSSL and because of that it does not work on
- App Engine.
- Args:
- id_token: string, A Signed JWT.
- audience: string, The audience 'aud' that the token should be for.
- http: httplib2.Http, instance to use to make the HTTP request. Callers
- should supply an instance that has caching enabled.
- cert_uri: string, URI of the certificates in JSON format to
- verify the JWT against.
- Returns:
- The deserialized JSON in the JWT.
- Raises:
- oauth2client.crypt.AppIdentityError: if the JWT fails to verify.
- CryptoUnavailableError: if no crypto library is available.
- """
- _require_crypto_or_die()
- if http is None:
- http = transport.get_cached_http()
- resp, content = transport.request(http, cert_uri)
- if resp.status == http_client.OK:
- certs = json.loads(_helpers._from_bytes(content))
- return crypt.verify_signed_jwt_with_certs(id_token, certs, audience)
- else:
- raise VerifyJwtTokenError('Status code: {0}'.format(resp.status))
- def _extract_id_token(id_token):
- """Extract the JSON payload from a JWT.
- Does the extraction w/o checking the signature.
- Args:
- id_token: string or bytestring, OAuth 2.0 id_token.
- Returns:
- object, The deserialized JSON payload.
- """
- if type(id_token) == bytes:
- segments = id_token.split(b'.')
- else:
- segments = id_token.split(u'.')
- if len(segments) != 3:
- raise VerifyJwtTokenError(
- 'Wrong number of segments in token: {0}'.format(id_token))
- return json.loads(
- _helpers._from_bytes(_helpers._urlsafe_b64decode(segments[1])))
- def _parse_exchange_token_response(content):
- """Parses response of an exchange token request.
- Most providers return JSON but some (e.g. Facebook) return a
- url-encoded string.
- Args:
- content: The body of a response
- Returns:
- Content as a dictionary object. Note that the dict could be empty,
- i.e. {}. That basically indicates a failure.
- """
- resp = {}
- content = _helpers._from_bytes(content)
- try:
- resp = json.loads(content)
- except Exception:
- # different JSON libs raise different exceptions,
- # so we just do a catch-all here
- resp = _helpers.parse_unique_urlencoded(content)
- # some providers respond with 'expires', others with 'expires_in'
- if resp and 'expires' in resp:
- resp['expires_in'] = resp.pop('expires')
- return resp
- @_helpers.positional(4)
- def credentials_from_code(client_id, client_secret, scope, code,
- redirect_uri='postmessage', http=None,
- user_agent=None,
- token_uri=oauth2client.GOOGLE_TOKEN_URI,
- auth_uri=oauth2client.GOOGLE_AUTH_URI,
- revoke_uri=oauth2client.GOOGLE_REVOKE_URI,
- device_uri=oauth2client.GOOGLE_DEVICE_URI,
- token_info_uri=oauth2client.GOOGLE_TOKEN_INFO_URI,
- pkce=False,
- code_verifier=None):
- """Exchanges an authorization code for an OAuth2Credentials object.
- Args:
- client_id: string, client identifier.
- client_secret: string, client secret.
- scope: string or iterable of strings, scope(s) to request.
- code: string, An authorization code, most likely passed down from
- the client
- redirect_uri: string, this is generally set to 'postmessage' to match
- the redirect_uri that the client specified
- http: httplib2.Http, optional http instance to use to do the fetch
- token_uri: string, URI for token endpoint. For convenience defaults
- to Google's endpoints but any OAuth 2.0 provider can be
- used.
- auth_uri: string, URI for authorization endpoint. For convenience
- defaults to Google's endpoints but any OAuth 2.0 provider
- can be used.
- revoke_uri: string, URI for revoke endpoint. For convenience
- defaults to Google's endpoints but any OAuth 2.0 provider
- can be used.
- device_uri: string, URI for device authorization endpoint. For
- convenience defaults to Google's endpoints but any OAuth
- 2.0 provider can be used.
- pkce: boolean, default: False, Generate and include a "Proof Key
- for Code Exchange" (PKCE) with your authorization and token
- requests. This adds security for installed applications that
- cannot protect a client_secret. See RFC 7636 for details.
- code_verifier: bytestring or None, default: None, parameter passed
- as part of the code exchange when pkce=True. If
- None, a code_verifier will automatically be
- generated as part of step1_get_authorize_url(). See
- RFC 7636 for details.
- Returns:
- An OAuth2Credentials object.
- Raises:
- FlowExchangeError if the authorization code cannot be exchanged for an
- access token
- """
- flow = OAuth2WebServerFlow(client_id, client_secret, scope,
- redirect_uri=redirect_uri,
- user_agent=user_agent,
- auth_uri=auth_uri,
- token_uri=token_uri,
- revoke_uri=revoke_uri,
- device_uri=device_uri,
- token_info_uri=token_info_uri,
- pkce=pkce,
- code_verifier=code_verifier)
- credentials = flow.step2_exchange(code, http=http)
- return credentials
- @_helpers.positional(3)
- def credentials_from_clientsecrets_and_code(filename, scope, code,
- message=None,
- redirect_uri='postmessage',
- http=None,
- cache=None,
- device_uri=None):
- """Returns OAuth2Credentials from a clientsecrets file and an auth code.
- Will create the right kind of Flow based on the contents of the
- clientsecrets file or will raise InvalidClientSecretsError for unknown
- types of Flows.
- Args:
- filename: string, File name of clientsecrets.
- scope: string or iterable of strings, scope(s) to request.
- code: string, An authorization code, most likely passed down from
- the client
- message: string, A friendly string to display to the user if the
- clientsecrets file is missing or invalid. If message is
- provided then sys.exit will be called in the case of an error.
- If message in not provided then
- clientsecrets.InvalidClientSecretsError will be raised.
- redirect_uri: string, this is generally set to 'postmessage' to match
- the redirect_uri that the client specified
- http: httplib2.Http, optional http instance to use to do the fetch
- cache: An optional cache service client that implements get() and set()
- methods. See clientsecrets.loadfile() for details.
- device_uri: string, OAuth 2.0 device authorization endpoint
- pkce: boolean, default: False, Generate and include a "Proof Key
- for Code Exchange" (PKCE) with your authorization and token
- requests. This adds security for installed applications that
- cannot protect a client_secret. See RFC 7636 for details.
- code_verifier: bytestring or None, default: None, parameter passed
- as part of the code exchange when pkce=True. If
- None, a code_verifier will automatically be
- generated as part of step1_get_authorize_url(). See
- RFC 7636 for details.
- Returns:
- An OAuth2Credentials object.
- Raises:
- FlowExchangeError: if the authorization code cannot be exchanged for an
- access token
- UnknownClientSecretsFlowError: if the file describes an unknown kind
- of Flow.
- clientsecrets.InvalidClientSecretsError: if the clientsecrets file is
- invalid.
- """
- flow = flow_from_clientsecrets(filename, scope, message=message,
- cache=cache, redirect_uri=redirect_uri,
- device_uri=device_uri)
- credentials = flow.step2_exchange(code, http=http)
- return credentials
- class DeviceFlowInfo(collections.namedtuple('DeviceFlowInfo', (
- 'device_code', 'user_code', 'interval', 'verification_url',
- 'user_code_expiry'))):
- """Intermediate information the OAuth2 for devices flow."""
- @classmethod
- def FromResponse(cls, response):
- """Create a DeviceFlowInfo from a server response.
- The response should be a dict containing entries as described here:
- http://tools.ietf.org/html/draft-ietf-oauth-v2-05#section-3.7.1
- """
- # device_code, user_code, and verification_url are required.
- kwargs = {
- 'device_code': response['device_code'],
- 'user_code': response['user_code'],
- }
- # The response may list the verification address as either
- # verification_url or verification_uri, so we check for both.
- verification_url = response.get(
- 'verification_url', response.get('verification_uri'))
- if verification_url is None:
- raise OAuth2DeviceCodeError(
- 'No verification_url provided in server response')
- kwargs['verification_url'] = verification_url
- # expires_in and interval are optional.
- kwargs.update({
- 'interval': response.get('interval'),
- 'user_code_expiry': None,
- })
- if 'expires_in' in response:
- kwargs['user_code_expiry'] = (
- _UTCNOW() +
- datetime.timedelta(seconds=int(response['expires_in'])))
- return cls(**kwargs)
- def _oauth2_web_server_flow_params(kwargs):
- """Configures redirect URI parameters for OAuth2WebServerFlow."""
- params = {
- 'access_type': 'offline',
- 'response_type': 'code',
- }
- params.update(kwargs)
- # Check for the presence of the deprecated approval_prompt param and
- # warn appropriately.
- approval_prompt = params.get('approval_prompt')
- if approval_prompt is not None:
- logger.warning(
- 'The approval_prompt parameter for OAuth2WebServerFlow is '
- 'deprecated. Please use the prompt parameter instead.')
- if approval_prompt == 'force':
- logger.warning(
- 'approval_prompt="force" has been adjusted to '
- 'prompt="consent"')
- params['prompt'] = 'consent'
- del params['approval_prompt']
- return params
- class OAuth2WebServerFlow(Flow):
- """Does the Web Server Flow for OAuth 2.0.
- OAuth2WebServerFlow objects may be safely pickled and unpickled.
- """
- @_helpers.positional(4)
- def __init__(self, client_id,
- client_secret=None,
- scope=None,
- redirect_uri=None,
- user_agent=None,
- auth_uri=oauth2client.GOOGLE_AUTH_URI,
- token_uri=oauth2client.GOOGLE_TOKEN_URI,
- revoke_uri=oauth2client.GOOGLE_REVOKE_URI,
- login_hint=None,
- device_uri=oauth2client.GOOGLE_DEVICE_URI,
- token_info_uri=oauth2client.GOOGLE_TOKEN_INFO_URI,
- authorization_header=None,
- pkce=False,
- code_verifier=None,
- **kwargs):
- """Constructor for OAuth2WebServerFlow.
- The kwargs argument is used to set extra query parameters on the
- auth_uri. For example, the access_type and prompt
- query parameters can be set via kwargs.
- Args:
- client_id: string, client identifier.
- client_secret: string client secret.
- scope: string or iterable of strings, scope(s) of the credentials
- being requested.
- redirect_uri: string, Either the string 'urn:ietf:wg:oauth:2.0:oob'
- for a non-web-based application, or a URI that
- handles the callback from the authorization server.
- user_agent: string, HTTP User-Agent to provide for this
- application.
- auth_uri: string, URI for authorization endpoint. For convenience
- defaults to Google's endpoints but any OAuth 2.0 provider
- can be used.
- token_uri: string, URI for token endpoint. For convenience
- defaults to Google's endpoints but any OAuth 2.0
- provider can be used.
- revoke_uri: string, URI for revoke endpoint. For convenience
- defaults to Google's endpoints but any OAuth 2.0
- provider can be used.
- login_hint: string, Either an email address or domain. Passing this
- hint will either pre-fill the email box on the sign-in
- form or select the proper multi-login session, thereby
- simplifying the login flow.
- device_uri: string, URI for device authorization endpoint. For
- convenience defaults to Google's endpoints but any
- OAuth 2.0 provider can be used.
- authorization_header: string, For use with OAuth 2.0 providers that
- require a client to authenticate using a
- header value instead of passing client_secret
- in the POST body.
- pkce: boolean, default: False, Generate and include a "Proof Key
- for Code Exchange" (PKCE) with your authorization and token
- requests. This adds security for installed applications that
- cannot protect a client_secret. See RFC 7636 for details.
- code_verifier: bytestring or None, default: None, parameter passed
- as part of the code exchange when pkce=True. If
- None, a code_verifier will automatically be
- generated as part of step1_get_authorize_url(). See
- RFC 7636 for details.
- **kwargs: dict, The keyword arguments are all optional and required
- parameters for the OAuth calls.
- """
- # scope is a required argument, but to preserve backwards-compatibility
- # we don't want to rearrange the positional arguments
- if scope is None:
- raise TypeError("The value of scope must not be None")
- self.client_id = client_id
- self.client_secret = client_secret
- self.scope = _helpers.scopes_to_string(scope)
- self.redirect_uri = redirect_uri
- self.login_hint = login_hint
- self.user_agent = user_agent
- self.auth_uri = auth_uri
- self.token_uri = token_uri
- self.revoke_uri = revoke_uri
- self.device_uri = device_uri
- self.token_info_uri = token_info_uri
- self.authorization_header = authorization_header
- self._pkce = pkce
- self.code_verifier = code_verifier
- self.params = _oauth2_web_server_flow_params(kwargs)
- @_helpers.positional(1)
- def step1_get_authorize_url(self, redirect_uri=None, state=None):
- """Returns a URI to redirect to the provider.
- Args:
- redirect_uri: string, Either the string 'urn:ietf:wg:oauth:2.0:oob'
- for a non-web-based application, or a URI that
- handles the callback from the authorization server.
- This parameter is deprecated, please move to passing
- the redirect_uri in via the constructor.
- state: string, Opaque state string which is passed through the
- OAuth2 flow and returned to the client as a query parameter
- in the callback.
- Returns:
- A URI as a string to redirect the user to begin the authorization
- flow.
- """
- if redirect_uri is not None:
- logger.warning((
- 'The redirect_uri parameter for '
- 'OAuth2WebServerFlow.step1_get_authorize_url is deprecated. '
- 'Please move to passing the redirect_uri in via the '
- 'constructor.'))
- self.redirect_uri = redirect_uri
- if self.redirect_uri is None:
- raise ValueError('The value of redirect_uri must not be None.')
- query_params = {
- 'client_id': self.client_id,
- 'redirect_uri': self.redirect_uri,
- 'scope': self.scope,
- }
- if state is not None:
- query_params['state'] = state
- if self.login_hint is not None:
- query_params['login_hint'] = self.login_hint
- if self._pkce:
- if not self.code_verifier:
- self.code_verifier = _pkce.code_verifier()
- challenge = _pkce.code_challenge(self.code_verifier)
- query_params['code_challenge'] = challenge
- query_params['code_challenge_method'] = 'S256'
- query_params.update(self.params)
- return _helpers.update_query_params(self.auth_uri, query_params)
- @_helpers.positional(1)
- def step1_get_device_and_user_codes(self, http=None):
- """Returns a user code and the verification URL where to enter it
- Returns:
- A user code as a string for the user to authorize the application
- An URL as a string where the user has to enter the code
- """
- if self.device_uri is None:
- raise ValueError('The value of device_uri must not be None.')
- body = urllib.parse.urlencode({
- 'client_id': self.client_id,
- 'scope': self.scope,
- })
- headers = {
- 'content-type': 'application/x-www-form-urlencoded',
- }
- if self.user_agent is not None:
- headers['user-agent'] = self.user_agent
- if http is None:
- http = transport.get_http_object()
- resp, content = transport.request(
- http, self.device_uri, method='POST', body=body, headers=headers)
- content = _helpers._from_bytes(content)
- if resp.status == http_client.OK:
- try:
- flow_info = json.loads(content)
- except ValueError as exc:
- raise OAuth2DeviceCodeError(
- 'Could not parse server response as JSON: "{0}", '
- 'error: "{1}"'.format(content, exc))
- return DeviceFlowInfo.FromResponse(flow_info)
- else:
- error_msg = 'Invalid response {0}.'.format(resp.status)
- try:
- error_dict = json.loads(content)
- if 'error' in error_dict:
- error_msg += ' Error: {0}'.format(error_dict['error'])
- except ValueError:
- # Couldn't decode a JSON response, stick with the
- # default message.
- pass
- raise OAuth2DeviceCodeError(error_msg)
- @_helpers.positional(2)
- def step2_exchange(self, code=None, http=None, device_flow_info=None):
- """Exchanges a code for OAuth2Credentials.
- Args:
- code: string, a dict-like object, or None. For a non-device
- flow, this is either the response code as a string, or a
- dictionary of query parameters to the redirect_uri. For a
- device flow, this should be None.
- http: httplib2.Http, optional http instance to use when fetching
- credentials.
- device_flow_info: DeviceFlowInfo, return value from step1 in the
- case of a device flow.
- Returns:
- An OAuth2Credentials object that can be used to authorize requests.
- Raises:
- FlowExchangeError: if a problem occurred exchanging the code for a
- refresh_token.
- ValueError: if code and device_flow_info are both provided or both
- missing.
- """
- if code is None and device_flow_info is None:
- raise ValueError('No code or device_flow_info provided.')
- if code is not None and device_flow_info is not None:
- raise ValueError('Cannot provide both code and device_flow_info.')
- if code is None:
- code = device_flow_info.device_code
- elif not isinstance(code, (six.string_types, six.binary_type)):
- if 'code' not in code:
- raise FlowExchangeError(code.get(
- 'error', 'No code was supplied in the query parameters.'))
- code = code['code']
- post_data = {
- 'client_id': self.client_id,
- 'code': code,
- 'scope': self.scope,
- }
- if self.client_secret is not None:
- post_data['client_secret'] = self.client_secret
- if self._pkce:
- post_data['code_verifier'] = self.code_verifier
- if device_flow_info is not None:
- post_data['grant_type'] = 'http://oauth.net/grant_type/device/1.0'
- else:
- post_data['grant_type'] = 'authorization_code'
- post_data['redirect_uri'] = self.redirect_uri
- body = urllib.parse.urlencode(post_data)
- headers = {
- 'content-type': 'application/x-www-form-urlencoded',
- }
- if self.authorization_header is not None:
- headers['Authorization'] = self.authorization_header
- if self.user_agent is not None:
- headers['user-agent'] = self.user_agent
- if http is None:
- http = transport.get_http_object()
- resp, content = transport.request(
- http, self.token_uri, method='POST', body=body, headers=headers)
- d = _parse_exchange_token_response(content)
- if resp.status == http_client.OK and 'access_token' in d:
- access_token = d['access_token']
- refresh_token = d.get('refresh_token', None)
- if not refresh_token:
- logger.info(
- 'Received token response with no refresh_token. Consider '
- "reauthenticating with prompt='consent'.")
- token_expiry = None
- if 'expires_in' in d:
- delta = datetime.timedelta(seconds=int(d['expires_in']))
- token_expiry = delta + _UTCNOW()
- extracted_id_token = None
- id_token_jwt = None
- if 'id_token' in d:
- extracted_id_token = _extract_id_token(d['id_token'])
- id_token_jwt = d['id_token']
- logger.info('Successfully retrieved access token')
- return OAuth2Credentials(
- access_token, self.client_id, self.client_secret,
- refresh_token, token_expiry, self.token_uri, self.user_agent,
- revoke_uri=self.revoke_uri, id_token=extracted_id_token,
- id_token_jwt=id_token_jwt, token_response=d, scopes=self.scope,
- token_info_uri=self.token_info_uri)
- else:
- logger.info('Failed to retrieve access token: %s', content)
- if 'error' in d:
- # you never know what those providers got to say
- error_msg = (str(d['error']) +
- str(d.get('error_description', '')))
- else:
- error_msg = 'Invalid response: {0}.'.format(str(resp.status))
- raise FlowExchangeError(error_msg)
- @_helpers.positional(2)
- def flow_from_clientsecrets(filename, scope, redirect_uri=None,
- message=None, cache=None, login_hint=None,
- device_uri=None, pkce=None, code_verifier=None,
- prompt=None):
- """Create a Flow from a clientsecrets file.
- Will create the right kind of Flow based on the contents of the
- clientsecrets file or will raise InvalidClientSecretsError for unknown
- types of Flows.
- Args:
- filename: string, File name of client secrets.
- scope: string or iterable of strings, scope(s) to request.
- redirect_uri: string, Either the string 'urn:ietf:wg:oauth:2.0:oob' for
- a non-web-based application, or a URI that handles the
- callback from the authorization server.
- message: string, A friendly string to display to the user if the
- clientsecrets file is missing or invalid. If message is
- provided then sys.exit will be called in the case of an error.
- If message in not provided then
- clientsecrets.InvalidClientSecretsError will be raised.
- cache: An optional cache service client that implements get() and set()
- methods. See clientsecrets.loadfile() for details.
- login_hint: string, Either an email address or domain. Passing this
- hint will either pre-fill the email box on the sign-in form
- or select the proper multi-login session, thereby
- simplifying the login flow.
- device_uri: string, URI for device authorization endpoint. For
- convenience defaults to Google's endpoints but any
- OAuth 2.0 provider can be used.
- Returns:
- A Flow object.
- Raises:
- UnknownClientSecretsFlowError: if the file describes an unknown kind of
- Flow.
- clientsecrets.InvalidClientSecretsError: if the clientsecrets file is
- invalid.
- """
- try:
- client_type, client_info = clientsecrets.loadfile(filename,
- cache=cache)
- if client_type in (clientsecrets.TYPE_WEB,
- clientsecrets.TYPE_INSTALLED):
- constructor_kwargs = {
- 'redirect_uri': redirect_uri,
- 'auth_uri': client_info['auth_uri'],
- 'token_uri': client_info['token_uri'],
- 'login_hint': login_hint,
- }
- revoke_uri = client_info.get('revoke_uri')
- optional = (
- 'revoke_uri',
- 'device_uri',
- 'pkce',
- 'code_verifier',
- 'prompt'
- )
- for param in optional:
- if locals()[param] is not None:
- constructor_kwargs[param] = locals()[param]
- return OAuth2WebServerFlow(
- client_info['client_id'], client_info['client_secret'],
- scope, **constructor_kwargs)
- except clientsecrets.InvalidClientSecretsError as e:
- if message is not None:
- if e.args:
- message = ('The client secrets were invalid: '
- '\n{0}\n{1}'.format(e, message))
- sys.exit(message)
- else:
- raise
- else:
- raise UnknownClientSecretsFlowError(
- 'This OAuth 2.0 flow is unsupported: {0!r}'.format(client_type))
|