1# Copyright 2020 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Service Accounts: JSON Web Token (JWT) Profile for OAuth 2.0
16
17NOTE: This file adds asynchronous refresh methods to both credentials
18classes, and therefore async/await syntax is required when calling this
19method when using service account credentials with asynchronous functionality.
20Otherwise, all other methods are inherited from the regular service account
21credentials file google.oauth2.service_account
22
23"""
24
25from google.auth import _credentials_async as credentials_async
26from google.auth import _helpers
27from google.oauth2 import _client_async
28from google.oauth2 import service_account
29
30
31class Credentials(
32    service_account.Credentials, credentials_async.Scoped, credentials_async.Credentials
33):
34    """Service account credentials
35
36    Usually, you'll create these credentials with one of the helper
37    constructors. To create credentials using a Google service account
38    private key JSON file::
39
40        credentials = _service_account_async.Credentials.from_service_account_file(
41            'service-account.json')
42
43    Or if you already have the service account file loaded::
44
45        service_account_info = json.load(open('service_account.json'))
46        credentials = _service_account_async.Credentials.from_service_account_info(
47            service_account_info)
48
49    Both helper methods pass on arguments to the constructor, so you can
50    specify additional scopes and a subject if necessary::
51
52        credentials = _service_account_async.Credentials.from_service_account_file(
53            'service-account.json',
54            scopes=['email'],
55            subject='[email protected]')
56
57    The credentials are considered immutable. If you want to modify the scopes
58    or the subject used for delegation, use :meth:`with_scopes` or
59    :meth:`with_subject`::
60
61        scoped_credentials = credentials.with_scopes(['email'])
62        delegated_credentials = credentials.with_subject(subject)
63
64    To add a quota project, use :meth:`with_quota_project`::
65
66        credentials = credentials.with_quota_project('myproject-123')
67    """
68
69    @_helpers.copy_docstring(credentials_async.Credentials)
70    async def refresh(self, request):
71        assertion = self._make_authorization_grant_assertion()
72        access_token, expiry, _ = await _client_async.jwt_grant(
73            request, self._token_uri, assertion
74        )
75        self.token = access_token
76        self.expiry = expiry
77
78
79class IDTokenCredentials(
80    service_account.IDTokenCredentials,
81    credentials_async.Signing,
82    credentials_async.Credentials,
83):
84    """Open ID Connect ID Token-based service account credentials.
85
86    These credentials are largely similar to :class:`.Credentials`, but instead
87    of using an OAuth 2.0 Access Token as the bearer token, they use an Open
88    ID Connect ID Token as the bearer token. These credentials are useful when
89    communicating to services that require ID Tokens and can not accept access
90    tokens.
91
92    Usually, you'll create these credentials with one of the helper
93    constructors. To create credentials using a Google service account
94    private key JSON file::
95
96        credentials = (
97            _service_account_async.IDTokenCredentials.from_service_account_file(
98                'service-account.json'))
99
100    Or if you already have the service account file loaded::
101
102        service_account_info = json.load(open('service_account.json'))
103        credentials = (
104            _service_account_async.IDTokenCredentials.from_service_account_info(
105                service_account_info))
106
107    Both helper methods pass on arguments to the constructor, so you can
108    specify additional scopes and a subject if necessary::
109
110        credentials = (
111            _service_account_async.IDTokenCredentials.from_service_account_file(
112                'service-account.json',
113                scopes=['email'],
114                subject='[email protected]'))
115
116    The credentials are considered immutable. If you want to modify the scopes
117    or the subject used for delegation, use :meth:`with_scopes` or
118    :meth:`with_subject`::
119
120        scoped_credentials = credentials.with_scopes(['email'])
121        delegated_credentials = credentials.with_subject(subject)
122
123    """
124
125    @_helpers.copy_docstring(credentials_async.Credentials)
126    async def refresh(self, request):
127        assertion = self._make_authorization_grant_assertion()
128        access_token, expiry, _ = await _client_async.id_token_jwt_grant(
129            request, self._token_uri, assertion
130        )
131        self.token = access_token
132        self.expiry = expiry
133