Exemplo n.º 1
0
    def _update(self,
                rapt,
                content,
                access_token,
                refresh_token=None,
                expires_in=None,
                id_token=None):
        if rapt:
            self.rapt_token = rapt
        self.token_response = content
        self.access_token = access_token
        self.refresh_token = (refresh_token
                              if refresh_token else self.refresh_token)
        if expires_in:
            delta = datetime.timedelta(seconds=int(expires_in))
            self.token_expiry = delta + client._UTCNOW()
        else:
            self.token_expiry = None
        self.id_token_jwt = id_token
        self.id_token = (client._extract_id_token(id_token)
                         if id_token else None)

        self.invalid = False
        if self.store:
            self.store.locked_put(self)
Exemplo n.º 2
0
    def _refresh(self, http):
        """Refreshes the access token.

        Args:
            http: unused HTTP object
        """
        self.devshell_response = _SendRecv()
        self.access_token = self.devshell_response.access_token
        expires_in = self.devshell_response.expires_in
        if expires_in is not None:
            delta = datetime.timedelta(seconds=expires_in)
            self.token_expiry = client._UTCNOW() + delta
        else:
            self.token_expiry = None
 def _create_token(self, additional_claims=None):
     now = client._UTCNOW()
     lifetime = datetime.timedelta(seconds=self._MAX_TOKEN_LIFETIME_SECS)
     expiry = now + lifetime
     payload = {
         'iat': _datetime_to_secs(now),
         'exp': _datetime_to_secs(expiry),
         'iss': self._service_account_email,
         'sub': self._service_account_email
     }
     payload.update(self._kwargs)
     if additional_claims is not None:
         payload.update(additional_claims)
     jwt = crypt.make_signed_jwt(self._signer, payload,
                                 key_id=self._private_key_id)
     return jwt.decode('ascii'), expiry
Exemplo n.º 4
0
def get_token(http, service_account='default'):
    """Fetch an oauth token for the

    Args:
        http: an object to be used to make HTTP requests.
        service_account: An email specifying the service account this token
            should represent. Default will be a token for the "default" service
            account of the current compute engine instance.

    Returns:
         A tuple of (access token, token expiration), where access token is the
         access token as a string and token expiration is a datetime object
         that indicates when the access token will expire.
    """
    token_json = get(
        http, 'instance/service-accounts/{0}/token'.format(service_account))
    token_expiry = client._UTCNOW() + datetime.timedelta(
        seconds=token_json['expires_in'])
    return token_json['access_token'], token_expiry