Skip to content

Commit a6ffc1a

Browse files
authored
GenesisCoreAuth enhanced to support custom login logic (#32)
1 parent f14f733 commit a6ffc1a

1 file changed

Lines changed: 63 additions & 5 deletions

File tree

gcl_iam/tests/functional/clients.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import datetime
1818
import os
1919
import uuid as sys_uuid
20+
from random import randint
2021

2122
import bazooka
2223
from bazooka import common
@@ -34,18 +35,53 @@ def __init__(
3435
self,
3536
username: str,
3637
password: str,
38+
grant_type: str = "password",
3739
client_uuid: str = "00000000-0000-0000-0000-000000000000",
3840
client_id: str = "GenesisCoreClientId",
3941
client_secret: str = "GenesisCoreClientSecret",
4042
uuid: str = "00000000-0000-0000-0000-000000000000",
41-
email: str = "admin@genesis.com",
43+
email: str = None,
44+
phone: str = None,
45+
login: str = None,
4246
project_id: str = None,
4347
):
48+
"""
49+
Handles authentication for the Genesis Core API.
50+
51+
:param username: The username for authentication.
52+
:param password: The password for authentication.
53+
:param grant_type: grant type, can be one of:
54+
- "password": default type, auth by username + password
55+
- "username+password": same as "password"
56+
- "email+password": auth by email + password
57+
- "phone+password": auth by phone + password
58+
- "login+password": "smart" dynamic auth by username/email/phone + password,
59+
depending on what's in the `self.login` string
60+
:param client_uuid: Unique identifier for the client - UUID.
61+
:param client_id: Client ID.
62+
:param client_secret: Client secret.
63+
:param uuid: Unique user identifier - UUID.
64+
:param email: User email address. Can be used for auth with `grant_type="email+password"`.
65+
:param phone: User phone number. Can be used for auth with `grant_type="phone+password"`.
66+
:param login: Generic login identifier, can be username/email/phone. Can be used for auth with `grant_type="login+password"`.
67+
:param project_id (str, optional): Project identifier for scoped authentication.
68+
69+
Example:
70+
>>> auth = GenesisCoreAuth(
71+
... username="user123",
72+
... password="securepassword",
73+
... )
74+
>>> auth.client_id
75+
'GenesisCoreClientId'
76+
"""
4477
super().__init__()
4578
self._uuid = uuid
46-
self._email = email
4779
self._username = username
4880
self._password = password
81+
self._email = email
82+
self._phone = phone
83+
self._login = login
84+
self._grant_type = grant_type
4985
self._client_uuid = client_uuid
5086
self._client_id = client_id
5187
self._client_secret = client_secret
@@ -75,10 +111,22 @@ def email(self):
75111
def username(self):
76112
return self._username
77113

114+
@property
115+
def phone(self):
116+
return self._phone
117+
118+
@property
119+
def login(self):
120+
return self._login
121+
78122
@property
79123
def password(self):
80124
return self._password
81125

126+
@property
127+
def grant_type(self):
128+
return self._grant_type
129+
82130
@property
83131
def client_uuid(self):
84132
return self._client_uuid
@@ -96,16 +144,26 @@ def project_id(self):
96144
return self._project_id
97145

98146
def get_password_auth_params(self):
99-
return {
100-
"grant_type": "password",
147+
params = {
148+
"grant_type": self._grant_type,
101149
"client_id": self._client_id,
102150
"client_secret": self._client_secret,
103-
"username": self._username,
104151
"password": self._password,
105152
"scope": (
106153
f"project:{self._project_id}" if self._project_id else ""
107154
),
108155
}
156+
if self.grant_type in ("password", "username+password"):
157+
params["username"] = self._username
158+
elif self.grant_type == "email+password":
159+
params["email"] = self._email
160+
elif self.grant_type == "phone+password":
161+
params["phone"] = self._phone
162+
elif self.grant_type == "login+password":
163+
params["login"] = self._login
164+
else:
165+
raise ValueError(f"Unexpected grant_type: {self.grant_type}")
166+
return params
109167

110168
def get_refresh_token_auth_params(self, refresh_token):
111169
return {

0 commit comments

Comments
 (0)