
    g!U                         d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ dZdZ	d	Z
d
Zd	Z G d de      Z G d de      Z G d de      Z G d dej"                        Zy)a)  Downscoping with Credential Access Boundaries

This module provides the ability to downscope credentials using
`Downscoping with Credential Access Boundaries`_. This is useful to restrict the
Identity and Access Management (IAM) permissions that a short-lived credential
can use.

To downscope permissions of a source credential, a Credential Access Boundary
that specifies which resources the new credential can access, as well as
an upper bound on the permissions that are available on each resource, has to
be defined. A downscoped credential can then be instantiated using the source
credential and the Credential Access Boundary.

The common pattern of usage is to have a token broker with elevated access
generate these downscoped credentials from higher access source credentials and
pass the downscoped short-lived access tokens to a token consumer via some
secure authenticated channel for limited access to Google Cloud Storage
resources.

For example, a token broker can be set up on a server in a private network.
Various workloads (token consumers) in the same network will send authenticated
requests to that broker for downscoped tokens to access or modify specific google
cloud storage buckets.

The broker will instantiate downscoped credentials instances that can be used to
generate short lived downscoped access tokens that can be passed to the token
consumer. These downscoped access tokens can be injected by the consumer into
google.oauth2.Credentials and used to initialize a storage client instance to
access Google Cloud Storage resources with restricted access.

Note: Only Cloud Storage supports Credential Access Boundaries. Other Google
Cloud services do not support this feature.

.. _Downscoping with Credential Access Boundaries: https://cloud.google.com/iam/docs/downscoping-short-lived-credentials
    N)_helpers)credentials)
exceptions)sts
   z/urn:ietf:params:oauth:grant-type:token-exchangez-urn:ietf:params:oauth:token-type:access_tokenzhttps://sts.{}/v1/tokenc                   Z    e Zd ZdZg fdZed        Zej                  d        Zd Zd Z	y)CredentialAccessBoundarya0  Defines a Credential Access Boundary which contains a list of access boundary
    rules. Each rule contains information on the resource that the rule applies to,
    the upper bound of the permissions that are available on that resource and an
    optional condition to further restrict permissions.
    c                     || _         y)a  Instantiates a Credential Access Boundary. A Credential Access Boundary
        can contain up to 10 access boundary rules.

        Args:
            rules (Sequence[google.auth.downscoped.AccessBoundaryRule]): The list of
                access boundary rules limiting the access that a downscoped credential
                will have.
        Raises:
            InvalidType: If any of the rules are not a valid type.
            InvalidValue: If the provided rules exceed the maximum allowed.
        N)rules)selfr   s     s/var/www/html/FastMealFinder_FlaskServer-InitialRelease/venv/lib/python3.12/site-packages/google/auth/downscoped.py__init__z!CredentialAccessBoundary.__init__O   s     
    c                 ,    t        | j                        S )ai  Returns the list of access boundary rules defined on the Credential
        Access Boundary.

        Returns:
            Tuple[google.auth.downscoped.AccessBoundaryRule, ...]: The list of access
                boundary rules defined on the Credential Access Boundary. These are returned
                as an immutable tuple to prevent modification.
        )tuple_rulesr   s    r   r   zCredentialAccessBoundary.rules]   s     T[[!!r   c                     t        |      t        kD  r(t        j                  dj	                  t                    |D ]'  }t        |t              rt        j                  d       t        |      | _	        y)a  Updates the current rules on the Credential Access Boundary. This will overwrite
        the existing set of rules.

        Args:
            value (Sequence[google.auth.downscoped.AccessBoundaryRule]): The list of
                access boundary rules limiting the access that a downscoped credential
                will have.
        Raises:
            InvalidType: If any of the rules are not a valid type.
            InvalidValue: If the provided rules exceed the maximum allowed.
        @Credential access boundary rules can have a maximum of {} rules.zZList of rules provided do not contain a valid 'google.auth.downscoped.AccessBoundaryRule'.N)
len _MAX_ACCESS_BOUNDARY_RULES_COUNTr   InvalidValueformat
isinstanceAccessBoundaryRuleInvalidTypelistr   )r   valueaccess_boundary_rules      r   r   zCredentialAccessBoundary.rulesi   sv     u:88))RYY4 
 %* 	 24FG ,,p 	 5kr   c                    t        | j                        t        k(  r(t        j                  dj                  t                    t        |t              st        j                  d      | j                  j                  |       y)a  Adds a single access boundary rule to the existing rules.

        Args:
            rule (google.auth.downscoped.AccessBoundaryRule): The access boundary rule,
                limiting the access that a downscoped credential will have, to be added to
                the existing rules.
        Raises:
            InvalidType: If any of the rules are not a valid type.
            InvalidValue: If the provided rules exceed the maximum allowed.
        r   zWThe provided rule does not contain a valid 'google.auth.downscoped.AccessBoundaryRule'.N)r   r   r   r   r   r   r   r   r   r   append)r   rules     r   add_rulez!CredentialAccessBoundary.add_rule   sp     tzz?>>))RYY4 
 $ 23((i  	4 r   c                 r    g }| j                   D ]!  }|j                  |j                                # dd|iiS )a  Generates the dictionary representation of the Credential Access Boundary.
        This uses the format expected by the Security Token Service API as documented in
        `Defining a Credential Access Boundary`_.

        .. _Defining a Credential Access Boundary:
            https://cloud.google.com/iam/docs/downscoping-short-lived-credentials#define-boundary

        Returns:
            Mapping: Credential Access Boundary Rule represented in a dictionary object.
        accessBoundaryaccessBoundaryRules)r   r!   to_json)r   r   r   s      r   r'   z CredentialAccessBoundary.to_json   sF     $(JJ 	9 LL-5578	9 !#8%"@AAr   N)
__name__
__module____qualname____doc__r   propertyr   setterr#   r'    r   r   r	   r	   H   sH        	" 	" \\" "4!.Br   r	   c                       e Zd ZdZ	 ddZed        Zej                  d        Zed        Zej                  d        Zed        Z	e	j                  d	        Z	d
 Z
y)r   zDefines an access boundary rule which contains information on the resource that
    the rule applies to, the upper bound of the permissions that are available on that
    resource and an optional condition to further restrict permissions.
    Nc                 .    || _         || _        || _        y)ay  Instantiates a single access boundary rule.

        Args:
            available_resource (str): The full resource name of the Cloud Storage bucket
                that the rule applies to. Use the format
                "//storage.googleapis.com/projects/_/buckets/bucket-name".
            available_permissions (Sequence[str]): A list defining the upper bound that
                the downscoped token will have on the available permissions for the
                resource. Each value is the identifier for an IAM predefined role or
                custom role, with the prefix "inRole:". For example:
                "inRole:roles/storage.objectViewer".
                Only the permissions in these roles will be available.
            availability_condition (Optional[google.auth.downscoped.AvailabilityCondition]):
                Optional condition that restricts the availability of permissions to
                specific Cloud Storage objects.

        Raises:
            InvalidType: If any of the parameters are not of the expected types.
            InvalidValue: If any of the parameters are not of the expected values.
        N)available_resourceavailable_permissionsavailability_condition)r   r1   r2   r3   s       r   r   zAccessBoundaryRule.__init__   s    . #5%:"&<#r   c                     | j                   S )zrReturns the current available resource.

        Returns:
           str: The current available resource.
        )_available_resourcer   s    r   r1   z%AccessBoundaryRule.available_resource   s     '''r   c                 \    t        |t              st        j                  d      || _        y)zUpdates the current available resource.

        Args:
            value (str): The updated value of the available resource.

        Raises:
            google.auth.exceptions.InvalidType: If the value is not a string.
        z0The provided available_resource is not a string.N)r   strr   r   r5   r   r   s     r   r1   z%AccessBoundaryRule.available_resource   s.     %%((B  $) r   c                 ,    t        | j                        S )zReturns the current available permissions.

        Returns:
           Tuple[str, ...]: The current available permissions. These are returned
               as an immutable tuple to prevent modification.
        )r   _available_permissionsr   s    r   r2   z(AccessBoundaryRule.available_permissions   s     T0011r   c                     |D ]P  }t        |t              st        j                  d      |j	                  d      dk7  s=t        j
                  d       t        |      | _        y)a  Updates the current available permissions.

        Args:
            value (Sequence[str]): The updated value of the available permissions.

        Raises:
            InvalidType: If the value is not a list of strings.
            InvalidValue: If the value is not valid.
        z9Provided available_permissions are not a list of strings.zinRole:r   z6available_permissions must be prefixed with 'inRole:'.N)r   r7   r   r   findr   r   r:   )r   r   available_permissions      r   r2   z(AccessBoundaryRule.available_permissions   sn     %* 	 2C8 ,,O  $((3q8 --L 	 '+5k#r   c                     | j                   S )zReturns the current availability condition.

        Returns:
           Optional[google.auth.downscoped.AvailabilityCondition]: The current
               availability condition.
        )_availability_conditionr   s    r   r3   z)AccessBoundaryRule.availability_condition  s     +++r   c                 `    t        |t              s|t        j                  d      || _        y)as  Updates the current availability condition.

        Args:
            value (Optional[google.auth.downscoped.AvailabilityCondition]): The updated
                value of the availability condition.

        Raises:
            google.auth.exceptions.InvalidType: If the value is not of type google.auth.downscoped.AvailabilityCondition
                or None.
        NzdThe provided availability_condition is not a 'google.auth.downscoped.AvailabilityCondition' or None.)r   AvailabilityConditionr   r   r?   r8   s     r   r3   z)AccessBoundaryRule.availability_condition  s4     %!67E<M((v  (-$r   c                     t        | j                        | j                  d}| j                  r| j                  j	                         |d<   |S )a  Generates the dictionary representation of the access boundary rule.
        This uses the format expected by the Security Token Service API as documented in
        `Defining a Credential Access Boundary`_.

        .. _Defining a Credential Access Boundary:
            https://cloud.google.com/iam/docs/downscoping-short-lived-credentials#define-boundary

        Returns:
            Mapping: The access boundary rule represented in a dictionary object.
        )availablePermissionsavailableResourceavailabilityCondition)r   r2   r1   r3   r'   r   jsons     r   r'   zAccessBoundaryRule.to_json$  sL     %))C)C$D!%!8!8
 &&,0,G,G,O,O,QD()r   )N)r(   r)   r*   r+   r   r,   r1   r-   r2   r3   r'   r.   r   r   r   r      s     QU=6 ( ( ) ) 2 2 !!2 "2, , , ""- #-"r   r   c                       e Zd ZdZddZed        Zej                  d        Zed        Zej                  d        Zed        Z	e	j                  d	        Z	d
 Z
y)rA   zsAn optional condition that can be used as part of a Credential Access Boundary
    to further restrict permissions.Nc                 .    || _         || _        || _        y)a  Instantiates an availability condition using the provided expression and
        optional title or description.

        Args:
            expression (str): A condition expression that specifies the Cloud Storage
                objects where permissions are available. For example, this expression
                makes permissions available for objects whose name starts with "customer-a":
                "resource.name.startsWith('projects/_/buckets/example-bucket/objects/customer-a')"
            title (Optional[str]): An optional short string that identifies the purpose of
                the condition.
            description (Optional[str]): Optional details about the purpose of the condition.

        Raises:
            InvalidType: If any of the parameters are not of the expected types.
            InvalidValue: If any of the parameters are not of the expected values.
        N
expressiontitledescription)r   rK   rL   rM   s       r   r   zAvailabilityCondition.__init__<  s    " %
&r   c                     | j                   S )zuReturns the current condition expression.

        Returns:
           str: The current conditon expression.
        )_expressionr   s    r   rK   z AvailabilityCondition.expressionQ  s     r   c                 \    t        |t              st        j                  d      || _        y)zUpdates the current condition expression.

        Args:
            value (str): The updated value of the condition expression.

        Raises:
            google.auth.exceptions.InvalidType: If the value is not of type string.
        z(The provided expression is not a string.N)r   r7   r   r   rO   r8   s     r   rK   z AvailabilityCondition.expressionZ  s(     %%(()STT r   c                     | j                   S )zbReturns the current title.

        Returns:
           Optional[str]: The current title.
        )_titler   s    r   rL   zAvailabilityCondition.titleh  s     {{r   c                 `    t        |t              s|t        j                  d      || _        y)zUpdates the current title.

        Args:
            value (Optional[str]): The updated value of the title.

        Raises:
            google.auth.exceptions.InvalidType: If the value is not of type string or None.
        Nz+The provided title is not a string or None.)r   r7   r   r   rR   r8   s     r   rL   zAvailabilityCondition.titleq  s,     %%%*;(()VWWr   c                     | j                   S )znReturns the current description.

        Returns:
           Optional[str]: The current description.
        )_descriptionr   s    r   rM   z!AvailabilityCondition.description  s        r   c                 `    t        |t              s|t        j                  d      || _        y)zUpdates the current description.

        Args:
            value (Optional[str]): The updated value of the description.

        Raises:
            google.auth.exceptions.InvalidType: If the value is not of type string or None.
        Nz1The provided description is not a string or None.)r   r7   r   r   rU   r8   s     r   rM   z!AvailabilityCondition.description  s3     %%%*;((C  "r   c                     d| j                   i}| j                  r| j                  |d<   | j                  r| j                  |d<   |S )a  Generates the dictionary representation of the availability condition.
        This uses the format expected by the Security Token Service API as documented in
        `Defining a Credential Access Boundary`_.

        .. _Defining a Credential Access Boundary:
            https://cloud.google.com/iam/docs/downscoping-short-lived-credentials#define-boundary

        Returns:
            Mapping[str, str]: The availability condition represented in a dictionary
                object.
        rK   rL   rM   rJ   rF   s     r   r'   zAvailabilityCondition.to_json  sD     doo.:: JJDM"&"2"2Dr   )NN)r(   r)   r*   r+   r   r,   rK   r-   rL   rM   r'   r.   r   r   rA   rA   8  s    ('*     ! !   \\  ! ! " "r   rA   c                        e Zd ZdZdej
                  f fd	Z ej                  ej                        d        Z
 ej                  ej                        d        Z xZS )Credentialsa;  Defines a set of Google credentials that are downscoped from an existing set
    of Google OAuth2 credentials. This is useful to restrict the Identity and Access
    Management (IAM) permissions that a short-lived credential can use.
    The common pattern of usage is to have a token broker with elevated access
    generate these downscoped credentials from higher access source credentials and
    pass the downscoped short-lived access tokens to a token consumer via some
    secure authenticated channel for limited access to Google Cloud Storage
    resources.
    Nc                     t         t        |           || _        || _        || _        |xs t        j                  | _        t        j                  t        j                  | j                              | _        y)a`  Instantiates a downscoped credentials object using the provided source
        credentials and credential access boundary rules.
        To downscope permissions of a source credential, a Credential Access Boundary
        that specifies which resources the new credential can access, as well as an
        upper bound on the permissions that are available on each resource, has to be
        defined. A downscoped credential can then be instantiated using the source
        credential and the Credential Access Boundary.

        Args:
            source_credentials (google.auth.credentials.Credentials): The source credentials
                to be downscoped based on the provided Credential Access Boundary rules.
            credential_access_boundary (google.auth.downscoped.CredentialAccessBoundary):
                The Credential Access Boundary which contains a list of access boundary
                rules. Each rule contains information on the resource that the rule applies to,
                the upper bound of the permissions that are available on that resource and an
                optional condition to further restrict permissions.
            quota_project_id (Optional[str]): The optional quota project ID.
            universe_domain (Optional[str]): The universe domain value, default is googleapis.com
        Raises:
            google.auth.exceptions.RefreshError: If the source credentials
                return an error on token refresh.
            google.auth.exceptions.OAuthError: If the STS token exchange
                endpoint returned an error during downscoped token generation.
        N)superrY   r   _source_credentials_credential_access_boundary_quota_project_idr   DEFAULT_UNIVERSE_DOMAIN_universe_domainr   Client_STS_TOKEN_URL_PATTERNr   universe_domain_sts_client)r   source_credentialscredential_access_boundaryquota_project_idrc   	__class__s        r   r   zCredentials.__init__  sc    @ 	k4)+#5 +E(!1 / V;3V3V::"))$*>*>?
r   c           	         | j                   j                  |       t        j                         }| j                  j                  |t        | j                   j                  t        t        | j                  j                               }|j                  d      | _        |j                  d      r0t        j                  |j                  d            }||z   | _        y | j                   j                  | _        y )N)request
grant_typesubject_tokensubject_token_typerequested_token_typeadditional_optionsaccess_token
expires_in)seconds)r\   refreshr   utcnowrd   exchange_token_STS_GRANT_TYPEtoken_STS_SUBJECT_TOKEN_TYPE_STS_REQUESTED_TOKEN_TYPEr]   r'   getdatetime	timedeltaexpiry)r   rj   nowresponse_datalifetimes        r   rs   zCredentials.refresh  s     	  ((1oo((77&22886!:#??GGI 8 
 #&&~6
 \*))-2C2CL2QRH.DK2299DKr   c                 R    | j                  | j                  | j                  |      S )N)rg   )rh   r\   r]   )r   rg   s     r   with_quota_projectzCredentials.with_quota_project  s-    ~~$$,,-  
 	
r   )r(   r)   r*   r+   r   r_   r   r   copy_docstringrY   rs   CredentialsWithQuotaProjectr   __classcell__)rh   s   @r   rY   rY     sk     #;;'
R X[445: 6:2 X[DDE
 F
r   rY   )r+   r{   google.authr   r   r   google.oauth2r   r   rv   ry   rb   rx   objectr	   r   rA   r   rY   r.   r   r   <module>r      s   "H    # "  $&  CK 2  J bBv bBJH HVqF qhT
+99 T
r   