|
2 | 2 | contains a class with which the BSS client is instantiated/configured
|
3 | 3 | """
|
4 | 4 |
|
5 |
| -from pydantic import BaseModel, ConfigDict, HttpUrl, field_validator |
| 5 | +from pydantic import BaseModel, ConfigDict, HttpUrl, field_validator, model_validator |
6 | 6 | from yarl import URL
|
7 | 7 |
|
8 | 8 |
|
@@ -78,13 +78,27 @@ class OAuthBssConfig(BssConfig):
|
78 | 78 | Url of the token endpoint; e.g. 'https://lynqtech-dev-auth-server.auth.eu-central-1.amazoncognito.com/oauth2/token'
|
79 | 79 | """
|
80 | 80 |
|
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 |
84 | 90 | """
|
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 |
87 | 92 | """
|
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