Skip to content

Commit d270e08

Browse files
authored
Abstract out permissions to scuba constantspy (#812)
* move scopes to scuba_constants.py
1 parent 5a9b498 commit d270e08

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

scubagoggles/auth.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from google.oauth2.credentials import Credentials
1313
from google.oauth2.service_account import Credentials as SvcCredentials
1414
from google_auth_oauthlib.flow import InstalledAppFlow
15+
from scubagoggles.scuba_constants import API_SCOPES
1516

1617
# The class is worth it just for the encapsulation. It allows the potential
1718
# of credential refresh multiple times, which may be beneficial during a
@@ -24,17 +25,6 @@ class GwsAuth:
2425
"""Generates an Oauth token for accessing Google's APIs
2526
"""
2627

27-
_base_auth_url = 'https://www.googleapis.com/auth'
28-
29-
_scopes = (f'{_base_auth_url}/admin.reports.audit.readonly',
30-
f'{_base_auth_url}/admin.directory.domain.readonly',
31-
f'{_base_auth_url}/admin.directory.orgunit.readonly',
32-
f'{_base_auth_url}/admin.directory.user.readonly',
33-
f'{_base_auth_url}/admin.directory.group.readonly',
34-
f'{_base_auth_url}/admin.directory.customer.readonly',
35-
f'{_base_auth_url}/apps.groups.settings',
36-
f'{_base_auth_url}/cloud-identity.policies.readonly')
37-
3828
def __init__(self, credentials_path: Path, svc_account_email: str = None):
3929
"""GwsAuth class initialization.
4030
@@ -61,7 +51,7 @@ def __init__(self, credentials_path: Path, svc_account_email: str = None):
6151
if svc_account_email:
6252
get_credentials = SvcCredentials.from_service_account_file
6353
self._token = get_credentials(str(credentials_path),
64-
scopes=self._scopes,
54+
scopes=API_SCOPES,
6555
subject=svc_account_email)
6656
return
6757

@@ -79,7 +69,7 @@ def __init__(self, credentials_path: Path, svc_account_email: str = None):
7969
# have worked when no browser was available).
8070
credentials_file = str(self._credentials_path)
8171
flow = InstalledAppFlow.from_client_secrets_file(credentials_file,
82-
self._scopes)
72+
API_SCOPES)
8373

8474
try:
8575
self._token = flow.run_local_server(
@@ -116,7 +106,7 @@ def _check_scopes(self):
116106
token = json.load(in_stream)
117107

118108
token_scopes = frozenset(token['scopes'])
119-
valid_scopes = frozenset(self._scopes)
109+
valid_scopes = frozenset(API_SCOPES)
120110

121111
# Delete the token file if its scopes don't match those defined in
122112
# this class. The token file will be recreated in the constructor
@@ -145,7 +135,7 @@ def _load_token(self):
145135
# refresh the token if it has expired.
146136
token_file = str(self._token_path)
147137
self._token = Credentials.from_authorized_user_file(token_file,
148-
self._scopes)
138+
API_SCOPES)
149139

150140
self._refresh_token()
151141

scubagoggles/scuba_constants.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ class ApiUrl(Enum):
4545
NUMBER_OF_UUID_CHARACTERS_TO_TRUNCATE_CHOICES = (
4646
0, 13, 18, 36
4747
)
48+
49+
BASE_AUTH_URL = 'https://www.googleapis.com/auth'
50+
API_SCOPES = (f'{BASE_AUTH_URL}/admin.reports.audit.readonly',
51+
f'{BASE_AUTH_URL}/admin.directory.domain.readonly',
52+
f'{BASE_AUTH_URL}/admin.directory.orgunit.readonly',
53+
f'{BASE_AUTH_URL}/admin.directory.user.readonly',
54+
f'{BASE_AUTH_URL}/admin.directory.group.readonly',
55+
f'{BASE_AUTH_URL}/admin.directory.customer.readonly',
56+
f'{BASE_AUTH_URL}/apps.groups.settings',
57+
f'{BASE_AUTH_URL}/cloud-identity.policies.readonly')

0 commit comments

Comments
 (0)