Skip to content

Commit 92f8775

Browse files
authored
feat: add possibility to use an existing bearer token (w/o client+secret) (#27)
* feat: add possibility to use an existing bearer token (w/o client+secret) * linter
1 parent 8ee7bb9 commit 92f8775

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/bssclient/client/bssclient.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def __init__(self, config: OAuthBssConfig):
199199
oauth_token_url=str(config.token_url),
200200
)
201201
self._oauth_config = config
202-
self._bearer_token: str | None = None
202+
self._bearer_token: str | None = config.bearer_token if config.bearer_token else None
203203

204204
async def _get_session(self) -> ClientSession:
205205
"""

src/bssclient/client/config.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
contains a class with which the BSS client is instantiated/configured
33
"""
44

5-
from pydantic import BaseModel, ConfigDict, HttpUrl, field_validator
5+
from pydantic import BaseModel, ConfigDict, HttpUrl, field_validator, model_validator
66
from yarl import URL
77

88

@@ -78,13 +78,27 @@ class OAuthBssConfig(BssConfig):
7878
Url of the token endpoint; e.g. 'https://lynqtech-dev-auth-server.auth.eu-central-1.amazoncognito.com/oauth2/token'
7979
"""
8080

81-
# pylint:disable=no-self-argument
82-
@field_validator("client_id", "client_secret")
83-
def validate_string_is_not_empty(cls, value):
81+
bearer_token: str | None = None
82+
"""
83+
You may optionally provide a 'hardcoded' bearer token here. As long as its valid, it's used for requests and the
84+
client id and secret are not used.
85+
This is useful when you have ways to get a token but not a client id and secret.
86+
"""
87+
88+
@model_validator(mode="after")
89+
def check_secret_or_token_is_present(cls, values): # pylint:disable=no-self-argument
8490
"""
85-
Check that no one tries to bypass validation with empty strings.
86-
If we had wanted that you can omit values, we had used Optional[str] instead of str.
91+
Ensures that either (id+secret) or a bare token are present
8792
"""
88-
if not value.strip():
89-
raise ValueError("my_string cannot be empty")
90-
return value
93+
token_is_present = values.bearer_token is not None and values.bearer_token.strip()
94+
client_id_and_secret_are_present = (
95+
values.client_id is not None
96+
and values.client_id.strip()
97+
and values.client_secret is not None
98+
and values.client_secret.strip()
99+
)
100+
if not token_is_present and not client_id_and_secret_are_present:
101+
raise ValueError(
102+
# pylint:disable=line-too-long
103+
"You must provide either client id and secret or a bearer token, but not None of both"
104+
)

0 commit comments

Comments
 (0)