CIAM API Client
Bases: BaseAPIClient
Source code in reportconnectors/api_client/ciam/__init__.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
is_logged
property
Checks if the client is logged in to the API.
__init__(url, **kwargs)
Initialize the CIAM client with the URL of the API.
It is based on the BaseAPIClient class and extends it with the CIAM specific methods. Therefore, it supports all keyword arguments of the BaseAPIClient class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL of the CIAM OAuth2 API. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
jwks |
Dict
|
JSON Web Keys store. Default: |
openid_config |
OpenIdConfiguration
|
OpenID configuration data model. If not provided, it will be obtained from the local store file. |
token_refresh_function |
TokenRefreshFunction
|
External function to refresh the token. If not provided then the token refreshment will be done locally. If there are several clients using the same access_token the token refreshment can be done externally to avoid multiple refreshes and race conditions. |
Source code in reportconnectors/api_client/ciam/__init__.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
authenticate(client_id, client_secret, password, username, **kwargs)
Authenticate to the CIAM APIs using grant type password.
To authenticate we need to provide client_id, client_secret, username and password.
Additionally, we can provide the token_url, which is the URL of the authentication provider. If not provided, the URL is obtained from the OpenID configuration.
As a result it sets the access token, id token, refresh token and expiration date in the auth_data, and returns True if the authentication is successful and the client is logged in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
Client ID |
required |
client_secret
|
str
|
Client secret |
required |
password
|
str
|
User password |
required |
username
|
str
|
Username |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
token_url |
str
|
Authentication token URL. If not provided, it's obtained from the OpenID configuration. |
timeout |
float
|
Timeout for the authentication process. Default: |
Returns:
| Type | Description |
|---|---|
bool
|
True if client is successfully authenticated and access token in obtained and set. Otherwise, False. |
Source code in reportconnectors/api_client/ciam/__init__.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
decode_jwt_payload(token, validate=False, **kwargs)
Decodes the JWT token and returns the payload.
If the validate flag is set to True, the token will be validated. Otherwise, it will be just decoded. If the audience is provided, the token will be validated with the audience check. Otherwise, it will be validated without the audience check.
If the JWK (JSON Web Key) is provided, it will be used for the token validation. Otherwise, the JWK will be obtained from the JWKS endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
Authentication token to decode |
required |
validate
|
bool
|
If set to True, the token will be validated. Default: False |
False
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
audience |
str
|
Expected audience value of the token. In CIAM it's equal to Client ID. If not provided, the token will be validated without the audience check. |
jwk |
dict
|
JSON Web Key to be used for the token validation. If not provided, it will be obtained from the JWKS endpoint. |
Returns:
| Type | Description |
|---|---|
Optional[JwtPayload]
|
JWT payload if the token is successfully decoded and validated. Otherwise, None. |
Source code in reportconnectors/api_client/ciam/__init__.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
get_jwk(kid)
Gets the JSON Web Key identified by the kid key.
If the key is not found in the local store, it will be updated from the JWKS endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kid
|
str
|
JWK private key identifier |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict]
|
Private key as a dictionary. |
Source code in reportconnectors/api_client/ciam/__init__.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
get_openid_config(endpoint='/polaris/oidc/.well-known/openid-configuration', force=False)
Gets OpenID config from the ".well-known/openid-configuration" endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
URL to the Well-Known endpoint. Default: |
'/polaris/oidc/.well-known/openid-configuration'
|
force
|
bool
|
If True then the config read from the ".well-known" endpoint is forced. Default: |
False
|
Returns:
| Type | Description |
|---|---|
OpenIdConfiguration
|
OpenID configuration data model. |
Source code in reportconnectors/api_client/ciam/__init__.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
get_token(token_url, client_id=None, client_secret=None, username=None, password=None, refresh_token=None, **kwargs)
classmethod
Gets OpenID token from the auth provider using the password authentication flow.
It uses the client_id and client_secret with username and password to authenticate communicate
with authentication provided at token_url.
By default, it is the CIAM portal, but in theory any OAuth2 provided will do.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_url
|
str
|
Token URL that identifies the authentication provider. |
required |
client_id
|
Optional[str]
|
Client ID |
None
|
client_secret
|
Optional[str]
|
Client Secret |
None
|
username
|
Optional[str]
|
Username |
None
|
password
|
Optional[str]
|
User password |
None
|
refresh_token
|
Optional[str]
|
Refresh token to be used for the token refreshment. |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
timeout |
float
|
Timeout for the authentication process. Default: |
content_type |
str
|
Content type of the request. Default: |
proxies |
Optional[Dict]
|
Proxy settings. Default: |
scope |
List[str]
|
List of required scopes. Default: |
Returns:
| Type | Description |
|---|---|
OAuth2TokenResponse
|
OAuth2TokenResponse object with the response from the authentication provider. |
Raises:
| Type | Description |
|---|---|
OAuth2Error
|
If the authentication fails. |
ValidationError
|
If the response is not valid. |
Notes
When authorizing at CIAM the User-Agent header is expected. That is the reason why we add the custom headers to the fetch_token method call.
Source code in reportconnectors/api_client/ciam/__init__.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
get_user_info()
Gets the user information from the CIAM's OIDC userinfo endpoint.
Returns:
| Type | Description |
|---|---|
UserInfo
|
User info data model. |
Source code in reportconnectors/api_client/ciam/__init__.py
452 453 454 455 456 457 458 459 460 461 462 463 | |
logout(end_session_url=None)
Ends session with CIAM client and fully logs out from the API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
end_session_url
|
Optional[str]
|
End session URL. If not provided then the one from OpenID configuration used |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True, if fully logged out. Otherwise, False. |
Source code in reportconnectors/api_client/ciam/__init__.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
refresh_authentication(force=False, refresh_token=None, client_id=None, token_url=None, auto_refresh_delta=datetime.timedelta(minutes=5))
Refreshes the tokens using the refresh token. If no refresh token is provided, the one from the
refresh_token property is used.
If token's remaining time is more than auto_refresh_delta (5 min by default) then the token is not refreshed. However, it can be forced by setting force to True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force
|
bool
|
If true then the token refresh process is executed regardless of the above conditions. Default: False |
False
|
refresh_token
|
Optional[str]
|
Custom refresh token to be used. If not provided, then the |
None
|
client_id
|
Optional[str]
|
Client ID to be used for the token refreshment. If not provided, it is taken from client auth data. |
None
|
token_url
|
Optional[str]
|
Token URL that identifies the authentication provider. If not provided, it is taken from the OpenID configuration. |
None
|
auto_refresh_delta
|
timedelta
|
Time delta before the token expiration date when the token should be refreshed. Default: 5 minutes. |
timedelta(minutes=5)
|
Returns:
| Type | Description |
|---|---|
bool
|
True, if the token refresh process is successful. False, otherwise. |
Source code in reportconnectors/api_client/ciam/__init__.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
set_token(access_token, validate=False, **kwargs)
Sets the authorization tokens without the authentication process. It might be used when the tokens are obtained from external source.
We expect the access token to be JWT Bearer token. We also support the refresh token and the ID token as input parameters. If the validate flag is set to True, we will validate the access token's signature using the available public keys. Otherwise, we will just decode the access token to check its payload.
If token expiration date is not provided, it will be extracted from the access token payload. Only if the token is valid and not expired, it will be set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token
|
str
|
Access token to be set. |
required |
validate
|
bool
|
If set to True, the token will be validated. Default: False |
False
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
refresh_token |
str
|
Refresh token to be set. |
id_token |
str
|
Refresh token to be set. |
client_id |
str
|
Client ID to be set. If not provided, it will be extracted from the token's payload. |
expiration_date |
datetime
|
Expiration date of the access token. If not provided, it will be extracted from the token's payload. |
Returns:
| Type | Description |
|---|---|
bool
|
True if provided token is set and valid. Otherwise, False. |
Source code in reportconnectors/api_client/ciam/__init__.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |