Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/google/auth/api_key.py: 55%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright 2022 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.
15"""Google API key support.
16This module provides authentication using the `API key`_.
17.. _API key:
18 https://cloud.google.com/docs/authentication/api-keys/
19"""
21from google.auth import _helpers, credentials, exceptions
24class Credentials(credentials.Credentials):
25 """API key credentials.
26 These credentials use API key to provide authorization to applications.
27 """
29 def __init__(self, token):
30 """
31 Args:
32 token (str): API key string
33 Raises:
34 ValueError: If the provided API key is not a non-empty string.
35 """
36 super(Credentials, self).__init__()
37 if not token:
38 raise exceptions.InvalidValue("Token must be a non-empty API key string")
39 self.token = token
41 @property
42 def expired(self):
43 return False
45 @property
46 def valid(self):
47 return True
49 @_helpers.copy_docstring(credentials.Credentials)
50 def refresh(self, request):
51 return
53 def apply(self, headers, token=None):
54 """Apply the API key token to the x-goog-api-key header.
55 Args:
56 headers (Mapping): The HTTP request headers.
57 token (Optional[str]): If specified, overrides the current access
58 token.
59 """
60 headers["x-goog-api-key"] = token or self.token
62 def before_request(self, request, method, url, headers):
63 """Performs credential-specific before request logic.
64 Refreshes the credentials if necessary, then calls :meth:`apply` to
65 apply the token to the x-goog-api-key header.
66 Args:
67 request (google.auth.transport.Request): The object used to make
68 HTTP requests.
69 method (str): The request's HTTP method or the RPC method being
70 invoked.
71 url (str): The request's URI or the RPC service's URI.
72 headers (Mapping): The request's headers.
73 """
74 self.apply(headers)