Skip to content

Commit f95c07f

Browse files
authored
Merge pull request #122 from britive/main
v1.6.1rc2 develop2
2 parents 0bcddaf + 55762ab commit f95c07f

3 files changed

Lines changed: 50 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
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+
## v1.6.1rc2 [2023-12-08]
6+
#### What's New
7+
* None
8+
9+
#### Enhancements
10+
* Send proper logout type based on the type of user (local or SAML)
11+
12+
#### Bug Fixes
13+
* Fixes issue with `user` command
14+
15+
#### Dependencies
16+
* None
17+
18+
#### Other
19+
* Additional logging when entering a login/logout loop
520

621
## v1.6.1rc1 [2023-12-07]
722
#### What's New

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.1rc1
3+
version = 1.6.1rc2
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: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ 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
4140
self.available_profiles = None
4241
self.config = ConfigManager(tenant_name=tenant_name, cli=self)
4342
self.list_separator = '|'
@@ -102,19 +101,18 @@ def login(self, explicit: bool = False, browser: str = None):
102101
token=self.token,
103102
query_features=False
104103
)
105-
self.user = self.b.my_access.whoami() # this is what may cause UnauthorizedRequest
106104
except exceptions.UnauthorizedRequest as e:
107105
raise click.ClickException('Invalid API token provided.') from e
108106
except exceptions.InvalidRequest as e:
109107
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?
108+
pass
111109
else:
112110
raise e
113111
else:
114-
counter = 0
112+
counter = 1
115113
while True: # will break after we successfully get logged in or 3 attempts have occurred
116114
# protect against infinite loop
117-
if counter > 2:
115+
if counter > 3:
118116
raise Exception('could not login after 3 attempts')
119117

120118
# attempt login and making an api call to ensure the credentials we have are valid
@@ -125,10 +123,10 @@ def login(self, explicit: bool = False, browser: str = None):
125123
token=self.credential_manager.get_token(),
126124
query_features=False
127125
)
128-
self.user = self.b.my_access.whoami() # this is what may cause UnauthorizedRequest
129126
break
130127
except exceptions.UnauthorizedRequest as e:
131128
if '401 - e0000' in str(e).lower():
129+
self.print(f'attempt {counter} of 3 - login failed')
132130
self.logout()
133131
else:
134132
raise e
@@ -159,6 +157,27 @@ def _cleanup_credentials(self):
159157
self.set_credential_manager()
160158
self.credential_manager.delete()
161159

160+
@staticmethod
161+
def _is_saml_user(token):
162+
import jwt # lazy load as this will not happen often
163+
164+
try:
165+
username = jwt.decode(
166+
token,
167+
# validation of the token will occur on the Britive backend
168+
# so not verifying everything here is okay since we are just
169+
# trying to extract the username to determine if they are a
170+
# SAML user or not
171+
options={
172+
'verify_signature': False,
173+
'verify_aud': False
174+
}
175+
).get('username', '')
176+
177+
return username.startswith('SAML')
178+
except:
179+
return False
180+
162181
def logout(self):
163182
# if dealing with a token there is no concept of logout
164183
if self.token:
@@ -172,14 +191,21 @@ def logout(self):
172191
# if we do we need to invalidate them at the tenant and clean them up on the client side
173192
# if we don't have valid credentials for the tenant then there is no need to logout
174193
if self.credential_manager.has_valid_credentials():
194+
token = self.credential_manager.get_token()
195+
175196
# keep it as local variable, so we don't mess up anything that may be happening in login
176197
# if this method is called due to a 401 E0000 error
177198
b = Britive(
178199
tenant=self.tenant_name,
179-
token=self.credential_manager.get_token(),
200+
token=token,
180201
query_features=False
181202
)
182-
b.delete(f'https://{Britive.parse_tenant(self.tenant_name)}/api/auth')
203+
204+
params = {}
205+
if self._is_saml_user(token):
206+
params['type'] = 'sso'
207+
208+
b.delete(f'https://{Britive.parse_tenant(self.tenant_name)}/api/auth', params=params)
183209
self._cleanup_credentials()
184210

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

0 commit comments

Comments
 (0)