Skip to content

Commit cb53c9a

Browse files
authored
Merge pull request #120 from britive/develop
1.6.1rc1
2 parents 2d38e07 + 0bcddaf commit cb53c9a

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
* As of v1.4.0 release candidates will be published in an effort to get new features out faster while still allowing time for full QA testing before moving the release candidate to a full release.
44

5+
6+
## v1.6.1rc1 [2023-12-07]
7+
#### What's New
8+
* None
9+
10+
#### Enhancements
11+
* None
12+
13+
#### Bug Fixes
14+
* Fixes issue when and authentication token has been invalidated on the server side by auto-logging out the user from the CLI and re-authenticating
15+
16+
#### Dependencies
17+
* None
18+
19+
#### Other
20+
* None
21+
22+
523
## v1.6.0 [2023-11-08]
624
#### What's New
725
* Initial support for Kubernetes - this functionality is not yet available publicly on the Britive Platform - this is a beta feature for internal use only

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pybritive
3-
version = 1.6.0
3+
version = 1.6.1rc1
44
author = Britive Inc.
55
author_email = support@britive.com
66
description = A pure Python CLI for Britive

src/pybritive/britive_cli.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(self, tenant_name: str = None, token: str = None, silent: bool = Fa
3737
self.tenant_alias = None
3838
self.token = token
3939
self.b = None
40+
self.user = None
4041
self.available_profiles = None
4142
self.config = ConfigManager(tenant_name=tenant_name, cli=self)
4243
self.list_separator = '|'
@@ -85,33 +86,54 @@ def set_credential_manager(self):
8586
raise click.ClickException(f'invalid credential backend {backend}.')
8687

8788
def login(self, explicit: bool = False, browser: str = None):
89+
# explicit means the user called pybritive login, otherwise it is being implicitly called by something else
90+
8891
self.browser = browser
8992
self.tenant_name = self.config.get_tenant()['name']
9093
self.tenant_alias = self.config.alias
9194
if explicit and self.token:
9295
raise click.ClickException('Interactive login unavailable when an API token is provided.')
9396

97+
# taking a very straightforward approach here...if user provided a token and it doesn't work just exit
9498
if self.token:
9599
try:
96100
self.b = Britive(
97101
tenant=self.tenant_name,
98102
token=self.token,
99103
query_features=False
100104
)
105+
self.user = self.b.my_access.whoami() # this is what may cause UnauthorizedRequest
101106
except exceptions.UnauthorizedRequest as e:
102107
raise click.ClickException('Invalid API token provided.') from e
108+
except exceptions.InvalidRequest as e:
109+
if '400 - e1000 - bad request' in str(e).lower(): # this is for SCIM token
110+
self.user = {} # not sure what else to set this to?
111+
else:
112+
raise e
103113
else:
104-
while True: # will break after we successfully get logged in
114+
counter = 0
115+
while True: # will break after we successfully get logged in or 3 attempts have occurred
116+
# protect against infinite loop
117+
if counter > 2:
118+
raise Exception('could not login after 3 attempts')
119+
120+
# attempt login and making an api call to ensure the credentials we have are valid
105121
try:
106122
self.set_credential_manager()
107123
self.b = Britive(
108124
tenant=self.tenant_name,
109125
token=self.credential_manager.get_token(),
110126
query_features=False
111127
)
128+
self.user = self.b.my_access.whoami() # this is what may cause UnauthorizedRequest
112129
break
113-
except exceptions.UnauthorizedRequest:
114-
self._cleanup_credentials()
130+
except exceptions.UnauthorizedRequest as e:
131+
if '401 - e0000' in str(e).lower():
132+
self.logout()
133+
else:
134+
raise e
135+
finally:
136+
counter += 1
115137

116138
self._update_sdk_user_agent()
117139

@@ -150,13 +172,14 @@ def logout(self):
150172
# if we do we need to invalidate them at the tenant and clean them up on the client side
151173
# if we don't have valid credentials for the tenant then there is no need to logout
152174
if self.credential_manager.has_valid_credentials():
153-
154-
self.b = Britive(
175+
# keep it as local variable, so we don't mess up anything that may be happening in login
176+
# if this method is called due to a 401 E0000 error
177+
b = Britive(
155178
tenant=self.tenant_name,
156179
token=self.credential_manager.get_token(),
157180
query_features=False
158181
)
159-
self.b.delete(f'https://{Britive.parse_tenant(self.tenant_name)}/api/auth')
182+
b.delete(f'https://{Britive.parse_tenant(self.tenant_name)}/api/auth')
160183
self._cleanup_credentials()
161184

162185
def debug(self, data: object, ignore_silent: bool = False):

src/pybritive/cli_interface.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ def safe_cli():
2525
sys.tracebacklimit = 0
2626
cli()
2727
except Exception as e:
28-
if '401 - e0000' in str(e).lower():
29-
click.echo('You have logged out of Britive via the browser. Please run '
30-
'`pybritive logout [-t/--tenant <tenant>]` to clear your '
31-
'token and then re-run your command.')
32-
return
3328
if debug:
3429
raise e
3530
raise e from None

0 commit comments

Comments
 (0)