API
requests_oauth2client
¶
Main module for requests_oauth2client
.
You can import any class from any submodule directly from this main module.
ApiClient
¶
A Wrapper around requests.Session with extra features for REST API calls.
Additional features compared to using a requests.Session directly:
- You must set a root url at creation time, which then allows passing relative urls at request time.
- It may also raise exceptions instead of returning error responses.
- You can also pass additional kwargs at init time, which will be used to configure the Session, instead of setting them later.
- for parameters passed as
json
,params
ordata
, values that areNone
can be automatically discarded from the request - boolean values in
data
orparams
fields can be serialized to values that are suitable for the target API, like"true"
or"false"
, or"1"
/"0"
, instead of the default values"True"
or"False"
, - you may pass
cookies
andheaders
, which will be added to the session cookie handler or request headers respectively. - you may use the
user_agent
parameter to change theUser-Agent
header easily. Set it toNone
to remove that header.
base_url
will serve as root for relative urls passed to
ApiClient.request(),
ApiClient.get(), etc.
A requests.HTTPError will be raised everytime an API call returns an error code (>= 400), unless
you set raise_for_status
to False
. Additional parameters passed at init time, including
auth
will be used to configure the Session.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_url
|
str
|
the base api url, that is the root for all the target API endpoints. |
required |
auth
|
AuthBase | None
|
the requests.auth.AuthBase to use as authentication handler. |
None
|
timeout
|
int | None
|
the default timeout, in seconds, to use for each request from this |
60
|
raise_for_status
|
bool
|
if |
True
|
none_fields
|
Literal['include', 'exclude', 'empty']
|
defines what to do with parameters with value
|
'exclude'
|
bool_fields
|
tuple[Any, Any] | None
|
a tuple of |
('true', 'false')
|
cookies
|
Mapping[str, Any] | None
|
a mapping of cookies to set in the underlying |
None
|
headers
|
Mapping[str, Any] | None
|
a mapping of headers to set in the underlying |
None
|
session
|
Session | None
|
a preconfigured |
None
|
**session_kwargs
|
Any
|
additional kwargs to configure the underlying |
{}
|
Raises:
Type | Description |
---|---|
InvalidBoolFieldsParam
|
if the provided |
Source code in requests_oauth2client/api_client.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
|
request(method, path=None, *, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=False, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None, raise_for_status=None, none_fields=None, bool_fields=None)
¶
A wrapper around requests.Session.request method with extra features.
Additional features are described in ApiClient documentation.
All parameters will be passed as-is to requests.Session.request, expected those described below which have a special behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
None | str | bytes | Iterable[str | bytes | int]
|
the url where the request will be sent to. Can be:
|
None
|
raise_for_status
|
bool | None
|
like the parameter of the same name from ApiClient, but this will be applied for this request only. |
None
|
none_fields
|
Literal['include', 'exclude', 'empty'] | None
|
like the parameter of the same name from ApiClient, but this will be applied for this request only. |
None
|
bool_fields
|
tuple[Any, Any] | None
|
like the parameter of the same name from ApiClient, but this will be applied for this request only. |
None
|
Returns:
Type | Description |
---|---|
Response
|
a Response as returned by requests |
Raises:
Type | Description |
---|---|
InvalidBoolFieldsParam
|
if the provided |
Source code in requests_oauth2client/api_client.py
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 |
|
to_absolute_url(path=None)
¶
Convert a relative url to an absolute url.
Given a path
, return the matching absolute url, based on the base_url
that is
configured for this API.
The result of this method is different from a standard urljoin()
, because a relative_url
that starts with a "/" will not override the path from the base url. You can also pass an
iterable of path parts as relative url, which will be properly joined with "/". Those parts
may be str
(which will be urlencoded) or bytes
(which will be decoded as UTF-8 first) or
any other type (which will be converted to str
first, using the str() function
). See the
table below for example results which would exhibit most cases:
base_url | relative_url | result_url |
---|---|---|
"https://myhost.com/root" |
"/path" |
"https://myhost.com/root/path" |
"https://myhost.com/root" |
"/path" |
"https://myhost.com/root/path" |
"https://myhost.com/root" |
b"/path" |
"https://myhost.com/root/path" |
"https://myhost.com/root" |
"path" |
"https://myhost.com/root/path" |
"https://myhost.com/root" |
None |
"https://myhost.com/root" |
"https://myhost.com/root" |
("user", 1, "resource") |
"https://myhost.com/root/user/1/resource" |
"https://myhost.com/root" |
"https://otherhost.org/foo" |
ValueError |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
None | str | bytes | Iterable[str | bytes | int]
|
a relative url |
None
|
Returns:
Type | Description |
---|---|
str
|
the resulting absolute url |
Raises:
Type | Description |
---|---|
InvalidPathParam
|
if the provided path does not allow constructing a valid url |
Source code in requests_oauth2client/api_client.py
get(path=None, raise_for_status=None, **kwargs)
¶
Send a GET request and return a Response object.
The passed url
is relative to the base_url
passed at initialization time.
It takes the same parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
None | str | bytes | Iterable[str | bytes | int]
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
post(path=None, raise_for_status=None, **kwargs)
¶
Send a POST request and return a Response object.
The passed url
is relative to the base_url
passed at initialization time.
It takes the same parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | bytes | Iterable[str | bytes] | None
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
patch(path=None, raise_for_status=None, **kwargs)
¶
Send a PATCH request. Return a Response object.
The passed url
is relative to the base_url
passed at initialization time.
It takes the same parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | bytes | Iterable[str | bytes] | None
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a Response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
put(path=None, raise_for_status=None, **kwargs)
¶
Send a PUT request. Return a Response object.
The passed url
is relative to the base_url
passed at initialization time.
It takes the same parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | bytes | Iterable[str | bytes] | None
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a Response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
delete(path=None, raise_for_status=None, **kwargs)
¶
Send a DELETE request. Return a Response object.
The passed url
may be relative to the url passed at initialization time. It takes the same
parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | bytes | Iterable[str | bytes] | None
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
InvalidBoolFieldsParam
¶
Bases: ValueError
Raised when an invalid value is passed as 'bool_fields' parameter.
Source code in requests_oauth2client/api_client.py
InvalidPathParam
¶
Bases: TypeError
, ValueError
Raised when an unexpected path is passed as 'url' parameter.
Source code in requests_oauth2client/api_client.py
NonRenewableTokenError
¶
OAuth2AccessTokenAuth
¶
Bases: AuthBase
Authentication Handler for OAuth 2.0 Access Tokens and (optional) Refresh Tokens.
This Requests Auth handler implementation uses an access token as Bearer or DPoP token, and can automatically refresh it when expired, if a refresh token is available.
Token can be a simple str
containing a raw access token value, or a
BearerToken that can contain a refresh_token
.
In addition to adding a properly formatted Authorization
header, this will obtain a new token
once the current token is expired. Expiration is detected based on the expires_in
hint
returned by the AS. A configurable leeway
, in number of seconds, will make sure that a new
token is obtained some seconds before the actual expiration is reached. This may help in
situations where the client, AS and RS have slightly offset clocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the client to use to refresh tokens. |
required |
token
|
str | BearerToken
|
an initial Access Token, if you have one already. In most cases, leave |
required |
leeway
|
int
|
expiration leeway, in number of seconds. |
20
|
**token_kwargs
|
Any
|
additional kwargs to pass to the token endpoint. |
{}
|
Example
Source code in requests_oauth2client/auth.py
renew_token()
¶
Obtain a new Bearer Token.
This will try to use the refresh_token
, if there is one.
Source code in requests_oauth2client/auth.py
OAuth2AuthorizationCodeAuth
¶
Bases: OAuth2AccessTokenAuth
Authentication handler for the Authorization Code grant.
This Requests Auth handler implementation exchanges an Authorization Code for an access token, then automatically refreshes it once it is expired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the client to use to obtain Access Tokens. |
required |
code
|
str | AuthorizationResponse | None
|
an Authorization Code that has been obtained from the AS. |
required |
token
|
str | BearerToken | None
|
an initial Access Token, if you have one already. In most cases, leave |
None
|
leeway
|
int
|
expiration leeway, in number of seconds. |
20
|
**token_kwargs
|
Any
|
additional kwargs to pass to the token endpoint. |
{}
|
Example
Source code in requests_oauth2client/auth.py
exchange_code_for_token()
¶
Exchange the authorization code for an access token.
OAuth2ClientCredentialsAuth
¶
Bases: OAuth2AccessTokenAuth
An Auth Handler for the Client Credentials grant.
This requests AuthBase automatically gets Access Tokens from an OAuth 2.0 Token Endpoint with the Client Credentials grant, and will get a new one once the current one is expired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the OAuth2Client to use to obtain Access Tokens. |
required |
token
|
str | BearerToken | None
|
an initial Access Token, if you have one already. In most cases, leave |
None
|
leeway
|
int
|
expiration leeway, in number of seconds |
20
|
**token_kwargs
|
Any
|
extra kw parameters to pass to the Token Endpoint. May include |
{}
|
Example
Source code in requests_oauth2client/auth.py
OAuth2DeviceCodeAuth
¶
Bases: OAuth2AccessTokenAuth
Authentication Handler for the Device Code Flow.
This Requests Auth handler implementation exchanges a Device Code for an Access Token, then automatically refreshes it once it is expired.
It needs a Device Code and an OAuth2Client to be able to get a token from the AS Token Endpoint just before the first request using this Auth Handler is being sent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the OAuth2Client to use to obtain Access Tokens. |
required |
device_code
|
str | DeviceAuthorizationResponse
|
a Device Code obtained from the AS. |
required |
interval
|
int
|
the interval to use to pool the Token Endpoint, in seconds. |
5
|
expires_in
|
int
|
the lifetime of the token, in seconds. |
360
|
token
|
str | BearerToken | None
|
an initial Access Token, if you have one already. In most cases, leave |
None
|
leeway
|
int
|
expiration leeway, in number of seconds. |
20
|
**token_kwargs
|
Any
|
additional kwargs to pass to the token endpoint. |
{}
|
Example
Source code in requests_oauth2client/auth.py
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 |
|
exchange_device_code_for_token()
¶
Exchange the Device Code for an access token.
This will poll the Token Endpoint until the user finishes the authorization process.
Source code in requests_oauth2client/auth.py
OAuth2ResourceOwnerPasswordAuth
¶
Bases: OAuth2AccessTokenAuth
Authentication Handler for the Resource Owner Password Credentials Flow.
This Requests Auth handler implementation exchanges the user credentials for an Access Token, then automatically repeats the process to get a new one once the current one is expired.
Note that this flow is considered deprecated, and the Authorization Code flow should be used whenever possible. Among other bad things, ROPC:
- does not support SSO between multiple apps,
- does not support MFA or risk-based adaptative authentication,
- depends on the user typing its credentials directly inside the application, instead of on a dedicated, centralized login page managed by the AS, which makes it totally insecure for 3rd party apps.
It needs the username and password and an OAuth2Client to be able to get a token from the AS Token Endpoint just before the first request using this Auth Handler is being sent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the client to use to obtain Access Tokens |
required |
username
|
str
|
the username |
required |
password
|
str
|
the user password |
required |
leeway
|
int
|
an amount of time, in seconds |
20
|
token
|
str | BearerToken | None
|
an initial Access Token, if you have one already. In most cases, leave |
None
|
**token_kwargs
|
Any
|
additional kwargs to pass to the token endpoint |
{}
|
Example
Source code in requests_oauth2client/auth.py
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 |
|
renew_token()
¶
Exchange the user credentials for an Access Token.
AuthorizationRequest
¶
Represent an Authorization Request.
This class makes it easy to generate valid Authorization Request URI (possibly including a state, nonce, PKCE, and custom args), to store all parameters, and to validate an Authorization Response.
All parameters passed at init time will be included in the request query parameters as-is, excepted for a few parameters which have a special behaviour:
state
: if...
(default), a randomstate
parameter will be generated for you. You may pass your ownstate
asstr
, or set it toNone
so that thestate
parameter will not be included in the request. You may access that state in thestate
attribute from this request.nonce
: if...
(default) andscope
includes 'openid', a randomnonce
will be generated and included in the request. You may access thatnonce
in thenonce
attribute from this request.code_verifier
: ifNone
, andcode_challenge_method
is'S256'
or'plain'
, a validcode_challenge
andcode_verifier
for PKCE will be automatically generated, and thecode_challenge
will be included in the request. You may pass your owncode_verifier
as astr
parameter, in which case the appropriatecode_challenge
will be included in the request, according to thecode_challenge_method
.-
authorization_response_iss_parameter_supported
andissuer
: those are used for Server Issuer Identification. By default:- If
ìssuer
is set and an issuer is included in the Authorization Response, then the consistency between those 2 values will be checked when usingvalidate_callback()
. - If issuer is not included in the response, then no issuer check is performed.
Set
authorization_response_iss_parameter_supported
toTrue
to enforce server identification:- an
issuer
must also be provided as parameter, and the AS must return that same value for the response to be considered valid byvalidate_callback()
. - if no issuer is included in the Authorization Response, then an error will be raised.
- If
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_endpoint
|
str
|
the uri for the authorization endpoint. |
required |
client_id
|
str
|
the client_id to include in the request. |
required |
redirect_uri
|
str | None
|
the redirect_uri to include in the request. This is required in OAuth 2.0 and optional
in OAuth 2.1. Pass |
None
|
scope
|
None | str | Iterable[str]
|
the scope to include in the request, as an iterable of |
'openid'
|
response_type
|
str
|
the response type to include in the request. |
CODE
|
state
|
str | ellipsis | None
|
the state to include in the request, or |
...
|
nonce
|
str | ellipsis | None
|
the nonce to include in the request, or |
...
|
code_verifier
|
str | None
|
the code verifier to include in the request.
If left as |
None
|
code_challenge_method
|
str | None
|
the method to use to derive the |
S256
|
acr_values
|
str | Iterable[str] | None
|
requested Authentication Context Class Reference values. |
None
|
issuer
|
str | None
|
Issuer Identifier value from the OAuth/OIDC Server, if using Server Issuer Identification. |
None
|
**kwargs
|
Any
|
extra parameters to include in the request, as-is. |
{}
|
Example
Raises:
Type | Description |
---|---|
InvalidMaxAgeParam
|
if the |
MissingIssuerParam
|
if |
UnsupportedResponseTypeParam
|
if |
Source code in requests_oauth2client/authorization_request.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 |
|
code_challenge
cached
property
¶
The code_challenge
that matches code_verifier
and code_challenge_method
.
dpop_jkt
cached
property
¶
The DPoP JWK thumbprint that matches `dpop_key
.
args
property
¶
Return a dict with all the query parameters from this AuthorizationRequest.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
a dict of parameters |
furl
property
¶
Return the Authorization Request URI, as a furl
.
uri
property
¶
Return the Authorization Request URI, as a str
.
generate_state()
classmethod
¶
generate_nonce()
classmethod
¶
as_dict()
¶
Return the full argument dict.
This can be used to serialize this request and/or to initialize a similar request.
Source code in requests_oauth2client/authorization_request.py
validate_callback(response)
¶
Validate an Authorization Response against this Request.
Validate a given Authorization Response URI against this Authorization Request, and return an AuthorizationResponse.
This includes matching the state
parameter, checking for returned errors, and extracting
the returned code
and other parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
str
|
the Authorization Response URI. This can be the full URL, or just the query parameters (still encoded as x-www-form-urlencoded). |
required |
Returns:
Type | Description |
---|---|
AuthorizationResponse
|
the extracted code, if all checks are successful |
Raises:
Type | Description |
---|---|
MissingAuthCode
|
if the |
MissingIssuer
|
if Server Issuer verification is active and the response does
not contain an |
MismatchingIssuer
|
if the 'iss' received from the response does not match the expected value. |
MismatchingState
|
if the response |
OAuth2Error
|
if the response includes an error. |
MissingAuthCode
|
if the response does not contain a |
UnsupportedResponseTypeParam
|
if response_type anything else than 'code'. |
Source code in requests_oauth2client/authorization_request.py
sign_request_jwt(jwk, alg=None, lifetime=None)
¶
Sign the request
object that matches this Authorization Request parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwk
|
Jwk | dict[str, Any]
|
the JWK to use to sign the request |
required |
alg
|
str | None
|
the alg to use to sign the request, if the provided |
None
|
lifetime
|
int | None
|
an optional number of seconds of validity for the signed request.
If present, |
None
|
Returns:
Type | Description |
---|---|
SignedJwt
|
a |
Source code in requests_oauth2client/authorization_request.py
sign(jwk, alg=None, lifetime=None, **kwargs)
¶
Sign this Authorization Request and return a new one.
This replaces all parameters with a signed request
JWT.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwk
|
Jwk | dict[str, Any]
|
the JWK to use to sign the request |
required |
alg
|
str | None
|
the alg to use to sign the request, if the provided |
None
|
lifetime
|
int | None
|
lifetime of the resulting Jwt (used to calculate the 'exp' claim). By default, don't use an 'exp' claim. |
None
|
kwargs
|
Any
|
additional query parameters to include in the signed authorization request |
{}
|
Returns:
Type | Description |
---|---|
RequestParameterAuthorizationRequest
|
the signed Authorization Request |
Source code in requests_oauth2client/authorization_request.py
sign_and_encrypt_request_jwt(sign_jwk, enc_jwk, sign_alg=None, enc_alg=None, enc='A128CBC-HS256', lifetime=None)
¶
Sign and encrypt a request
object for this Authorization Request.
The signed request
will contain the same parameters as this AuthorizationRequest.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sign_jwk
|
Jwk | dict[str, Any]
|
the JWK to use to sign the request |
required |
enc_jwk
|
Jwk | dict[str, Any]
|
the JWK to use to encrypt the request |
required |
sign_alg
|
str | None
|
the alg to use to sign the request, if |
None
|
enc_alg
|
str | None
|
the alg to use to encrypt the request, if |
None
|
enc
|
str
|
the encoding to use to encrypt the request. |
'A128CBC-HS256'
|
lifetime
|
int | None
|
lifetime of the resulting Jwt (used to calculate the 'exp' claim). By default, do not include an 'exp' claim. |
None
|
Returns:
Type | Description |
---|---|
JweCompact
|
the signed and encrypted request object, as a |
Source code in requests_oauth2client/authorization_request.py
sign_and_encrypt(sign_jwk, enc_jwk, sign_alg=None, enc_alg=None, enc='A128CBC-HS256', lifetime=None)
¶
Sign and encrypt the current Authorization Request.
This replaces all parameters with a matching request
object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sign_jwk
|
Jwk | dict[str, Any]
|
the JWK to use to sign the request |
required |
enc_jwk
|
Jwk | dict[str, Any]
|
the JWK to use to encrypt the request |
required |
sign_alg
|
str | None
|
the alg to use to sign the request, if |
None
|
enc_alg
|
str | None
|
the alg to use to encrypt the request, if |
None
|
enc
|
str
|
the encoding to use to encrypt the request. |
'A128CBC-HS256'
|
lifetime
|
int | None
|
lifetime of the resulting Jwt (used to calculate the 'exp' claim). By default, do not include an 'exp' claim. |
None
|
Returns:
Type | Description |
---|---|
RequestParameterAuthorizationRequest
|
a |
Source code in requests_oauth2client/authorization_request.py
on_response_error(response)
¶
Error handler for Authorization Response errors.
Triggered by validate_callback() if the response uri contains an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
str
|
the Authorization Response URI. This can be the full URL, or just the query parameters. |
required |
Returns:
Type | Description |
---|---|
AuthorizationResponse
|
may return a default code that will be returned by |
AuthorizationResponse
|
will most likely raise exceptions instead. |
Raises:
Type | Description |
---|---|
AuthorizationResponseError
|
if the response contains an |
Source code in requests_oauth2client/authorization_request.py
AuthorizationRequestSerializer
¶
(De)Serializer for AuthorizationRequest
instances.
You might need to store pending authorization requests in session, either server-side or client- side. This class is here to help you do that.
Source code in requests_oauth2client/authorization_request.py
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 |
|
default_dumper(azr)
staticmethod
¶
Provide a default dumper implementation.
Serialize an AuthorizationRequest as JSON, then compress with deflate, then encodes as base64url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
azr
|
AuthorizationRequest
|
the |
required |
Returns:
Type | Description |
---|---|
str
|
the serialized value |
Source code in requests_oauth2client/authorization_request.py
default_loader(serialized, azr_class=AuthorizationRequest)
staticmethod
¶
Provide a default deserializer implementation.
This does the opposite operations than default_dumper
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized
|
str
|
the serialized AuthorizationRequest |
required |
azr_class
|
type[AuthorizationRequest]
|
the class to deserialize the Authorization Request to |
AuthorizationRequest
|
Returns:
Type | Description |
---|---|
AuthorizationRequest
|
an AuthorizationRequest |
Source code in requests_oauth2client/authorization_request.py
dumps(azr)
¶
Serialize and compress a given AuthorizationRequest for easier storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
azr
|
AuthorizationRequest
|
an AuthorizationRequest to serialize |
required |
Returns:
Type | Description |
---|---|
str
|
the serialized AuthorizationRequest, as a str |
Source code in requests_oauth2client/authorization_request.py
loads(serialized)
¶
Deserialize a serialized AuthorizationRequest.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized
|
str
|
the serialized AuthorizationRequest |
required |
Returns:
Type | Description |
---|---|
AuthorizationRequest
|
the deserialized AuthorizationRequest |
Source code in requests_oauth2client/authorization_request.py
AuthorizationResponse
¶
Represent a successful Authorization Response.
An Authorization Response is the redirection initiated by the AS to the client's redirection
endpoint (redirect_uri), after an Authorization Request.
This Response is typically created with a call to AuthorizationRequest.validate_callback()
once the call to the client Redirection Endpoint is made.
AuthorizationResponse
contains the following attributes:
- all the parameters that have been returned by the AS, most notably the
code
, and optional parameters such asstate
. - the
redirect_uri
that was used for the Authorization Request - the
code_verifier
matching thecode_challenge
that was used for the Authorization Request
Parameters redirect_uri
and code_verifier
must be those from the matching
AuthorizationRequest
. All other parameters including code
and state
must be those
extracted from the Authorization Response parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
The authorization |
required |
redirect_uri
|
str | None
|
The |
None
|
code_verifier
|
str | None
|
the |
None
|
state
|
str | None
|
the |
None
|
nonce
|
str | None
|
the |
None
|
acr_values
|
str | Sequence[str] | None
|
the |
None
|
max_age
|
int | None
|
the |
None
|
issuer
|
str | None
|
the expected |
None
|
dpop_key
|
DPoPKey | None
|
the |
None
|
**kwargs
|
str
|
other parameters as returned by the AS. |
{}
|
Source code in requests_oauth2client/authorization_request.py
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 |
|
CodeChallengeMethods
¶
Bases: str
, Enum
All standardised code_challenge_method
values.
You should always use S256
.
Source code in requests_oauth2client/authorization_request.py
InvalidCodeVerifierParam
¶
Bases: ValueError
Raised when an invalid code_verifier is supplied.
Source code in requests_oauth2client/authorization_request.py
InvalidMaxAgeParam
¶
Bases: ValueError
Raised when an invalid 'max_age' parameter is provided.
Source code in requests_oauth2client/authorization_request.py
MissingIssuerParam
¶
Bases: ValueError
Raised when the 'issuer' parameter is required but not provided.
Source code in requests_oauth2client/authorization_request.py
PkceUtils
¶
Contains helper methods for PKCE, as described in RFC7636.
See RFC7636.
Source code in requests_oauth2client/authorization_request.py
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 |
|
code_verifier_pattern = re.compile('^[a-zA-Z0-9_\\-~.]{43,128}$')
class-attribute
instance-attribute
¶
A regex that matches valid code verifiers.
generate_code_verifier()
classmethod
¶
Generate a valid code_verifier
.
Returns:
Type | Description |
---|---|
str
|
a |
derive_challenge(verifier, method=CodeChallengeMethods.S256)
classmethod
¶
Derive the code_challenge
from a given code_verifier
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verifier
|
str | bytes
|
a code verifier |
required |
method
|
str
|
the method to use for deriving the challenge. Accepts 'S256' or 'plain'. |
S256
|
Returns:
Type | Description |
---|---|
str
|
a |
Raises:
Type | Description |
---|---|
InvalidCodeVerifierParam
|
if the |
UnsupportedCodeChallengeMethod
|
if the method is not supported |
Source code in requests_oauth2client/authorization_request.py
generate_code_verifier_and_challenge(method=CodeChallengeMethods.S256)
classmethod
¶
Generate a valid code_verifier
and its matching code_challenge
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
the method to use for deriving the challenge. Accepts 'S256' or 'plain'. |
S256
|
Returns:
Type | Description |
---|---|
tuple[str, str]
|
a |
Source code in requests_oauth2client/authorization_request.py
validate_code_verifier(verifier, challenge, method=CodeChallengeMethods.S256)
classmethod
¶
Validate a code_verifier
against a code_challenge
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verifier
|
str
|
the |
required |
challenge
|
str
|
the |
required |
method
|
str
|
the method to use for deriving the challenge. Accepts 'S256' or 'plain'. |
S256
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in requests_oauth2client/authorization_request.py
RequestParameterAuthorizationRequest
¶
Represent an Authorization Request that includes a request
JWT.
To construct such a request yourself, the easiest way is to initialize
an AuthorizationRequest
then sign it with
AuthorizationRequest.sign()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_endpoint
|
str
|
the Authorization Endpoint uri |
required |
client_id
|
str
|
the client_id |
required |
request
|
Jwt | str
|
the request JWT |
required |
expires_at
|
datetime | None
|
the expiration date for this request |
None
|
kwargs
|
Any
|
extra parameters to include in the request |
{}
|
Source code in requests_oauth2client/authorization_request.py
RequestUriParameterAuthorizationRequest
¶
Represent an Authorization Request that includes a request_uri
parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_endpoint
|
str
|
The Authorization Endpoint uri. |
required |
client_id
|
str
|
The Client ID. |
required |
request_uri
|
str
|
The |
required |
expires_at
|
datetime | None
|
The expiration date for this request. |
None
|
kwargs
|
Any
|
Extra query parameters to include in the request. |
{}
|
Source code in requests_oauth2client/authorization_request.py
ResponseTypes
¶
Bases: str
, Enum
All standardised response_type
values.
Note that you should always use code
. All other values are deprecated.
Source code in requests_oauth2client/authorization_request.py
UnsupportedCodeChallengeMethod
¶
UnsupportedResponseTypeParam
¶
Bases: ValueError
Raised when an unsupported response_type is passed as parameter.
Source code in requests_oauth2client/authorization_request.py
BackChannelAuthenticationPoolingJob
¶
Bases: BaseTokenEndpointPoolingJob
A pooling job for the BackChannel Authentication flow.
This will poll the Token Endpoint until the user finishes with its authentication.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
an OAuth2Client that will be used to pool the token endpoint. |
required |
auth_req_id
|
str | BackChannelAuthenticationResponse
|
an |
required |
interval
|
int | None
|
The pooling interval, in seconds, to use. This overrides
the one in |
None
|
slow_down_interval
|
int
|
Number of seconds to add to the pooling interval when the AS returns a slow down request. |
5
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the underlying calls to requests.request. |
None
|
**token_kwargs
|
Any
|
Additional parameters for the token request. |
{}
|
Example
Source code in requests_oauth2client/backchannel_authentication.py
token_request()
¶
Implement the CIBA token request.
This actually calls [OAuth2Client.ciba(auth_req_id)] on client
.
Returns:
Type | Description |
---|---|
BearerToken
|
Source code in requests_oauth2client/backchannel_authentication.py
BackChannelAuthenticationResponse
¶
Represent a BackChannel Authentication Response.
This contains all the parameters that are returned by the AS as a result of a BackChannel
Authentication Request, such as auth_req_id
(required), and the optional expires_at
,
interval
, and/or any custom parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth_req_id
|
str
|
the |
required |
expires_at
|
datetime | None
|
the date when the |
None
|
interval
|
int | None
|
the Token Endpoint pooling interval, in seconds, as returned by the AS. |
20
|
**kwargs
|
Any
|
any additional custom parameters as returned by the AS. |
{}
|
Source code in requests_oauth2client/backchannel_authentication.py
expires_in
property
¶
Number of seconds until expiration.
is_expired(leeway=0)
¶
Return True
if the auth_req_id
within this response is expired.
Expiration is evaluated at the time of the call. If there is no "expires_at" hint (which is
derived from the expires_in
hint returned by the AS BackChannel Authentication endpoint),
this will return None
.
Returns:
Type | Description |
---|---|
bool | None
|
|
bool | None
|
no |
Source code in requests_oauth2client/backchannel_authentication.py
Endpoints
¶
Bases: str
, Enum
All standardised OAuth 2.0 and extensions endpoints.
If an endpoint is not mentioned here, then its usage is not supported by OAuth2Client.
Source code in requests_oauth2client/client.py
GrantTypes
¶
Bases: str
, Enum
An enum of standardized grant_type
values.
Source code in requests_oauth2client/client.py
InvalidAcrValuesParam
¶
Bases: InvalidParam
Raised when an invalid 'acr_values' parameter is provided.
Source code in requests_oauth2client/client.py
InvalidBackchannelAuthenticationRequestHintParam
¶
Bases: InvalidParam
Raised when an invalid hint is provided in a backchannel authentication request.
InvalidDiscoveryDocument
¶
Bases: ValueError
Raised when handling an invalid Discovery Document.
Source code in requests_oauth2client/client.py
InvalidEndpointUri
¶
Bases: InvalidParam
Raised when an invalid endpoint uri is provided.
Source code in requests_oauth2client/client.py
InvalidIssuer
¶
Bases: InvalidEndpointUri
Raised when an invalid issuer parameter is provided.
Source code in requests_oauth2client/client.py
InvalidParam
¶
InvalidScopeParam
¶
Bases: InvalidParam
Raised when an invalid scope parameter is provided.
Source code in requests_oauth2client/client.py
MissingAuthRequestId
¶
Bases: ValueError
Raised when an 'auth_req_id' is missing in a BackChannelAuthenticationResponse.
Source code in requests_oauth2client/client.py
MissingDeviceCode
¶
Bases: ValueError
Raised when a device_code is required but not provided.
Source code in requests_oauth2client/client.py
MissingEndpointUri
¶
Bases: AttributeError
Raised when a required endpoint uri is not known.
Source code in requests_oauth2client/client.py
MissingIdTokenEncryptedResponseAlgParam
¶
Bases: InvalidParam
Raised when an ID Token encryption is required but not provided.
Source code in requests_oauth2client/client.py
MissingRefreshToken
¶
Bases: ValueError
Raised when a refresh token is required but not present.
Source code in requests_oauth2client/client.py
OAuth2Client
¶
An OAuth 2.x Client that can send requests to an OAuth 2.x Authorization Server.
OAuth2Client
is able to obtain tokens from the Token Endpoint using any of the standardised
Grant Types, and to communicate with the various backend endpoints like the Revocation,
Introspection, and UserInfo Endpoint.
To init an OAuth2Client, you only need the url to the Token Endpoint and the Credentials (a client_id and one of a secret or private_key) that will be used to authenticate to that endpoint. Other endpoint urls, such as the Authorization Endpoint, Revocation Endpoint, etc. can be passed as parameter as well if you intend to use them.
This class is not intended to help with the end-user authentication or any request that goes in
a browser. For authentication requests, see
AuthorizationRequest. You
may use the method authorization_request()
to generate AuthorizationRequest
s with the
preconfigured authorization_endpoint
, client_id
and `redirect_uri' from this client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_endpoint
|
str
|
the Token Endpoint URI where this client will get access tokens |
required |
auth
|
AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
|
the authentication handler to use for client authentication on the token endpoint. Can be:
|
None
|
client_id
|
str | None
|
client ID (use either this or |
None
|
client_secret
|
str | None
|
client secret (use either this or |
None
|
private_key
|
Jwk | dict[str, Any] | None
|
private_key to use for client authentication (use either this or |
None
|
revocation_endpoint
|
str | None
|
the Revocation Endpoint URI to use for revoking tokens |
None
|
introspection_endpoint
|
str | None
|
the Introspection Endpoint URI to use to get info about tokens |
None
|
userinfo_endpoint
|
str | None
|
the Userinfo Endpoint URI to use to get information about the user |
None
|
authorization_endpoint
|
str | None
|
the Authorization Endpoint URI, used for initializing Authorization Requests |
None
|
redirect_uri
|
str | None
|
the redirect_uri for this client |
None
|
backchannel_authentication_endpoint
|
str | None
|
the BackChannel Authentication URI |
None
|
device_authorization_endpoint
|
str | None
|
the Device Authorization Endpoint URI to use to authorize devices |
None
|
jwks_uri
|
str | None
|
the JWKS URI to use to obtain the AS public keys |
None
|
code_challenge_method
|
str
|
challenge method to use for PKCE (should always be 'S256') |
S256
|
session
|
Session | None
|
a requests Session to use when sending HTTP requests. Useful if some extra parameters such as proxy or client certificate must be used to connect to the AS. |
None
|
token_class
|
type[BearerToken]
|
a custom BearerToken class, if required |
BearerToken
|
dpop_bound_access_tokens
|
bool
|
if |
False
|
dpop_key_generator
|
Callable[[str], DPoPKey]
|
a callable that generates a DPoPKey, for whill be called when doing a token request with DPoP enabled. |
generate
|
testing
|
bool
|
if |
False
|
**extra_metadata
|
Any
|
additional metadata for this client, unused by this class, but may be
used by subclasses. Those will be accessible with the |
{}
|
Example
Raises:
Type | Description |
---|---|
MissingIDTokenEncryptedResponseAlgParam
|
if an
|
MissingIssuerParam
|
if |
InvalidEndpointUri
|
if a provided endpoint uri is not considered valid. For the rare cases
where those checks must be disabled, you can use |
InvalidIssuer
|
if the |
Source code in requests_oauth2client/client.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 |
|
client_id
property
¶
Client ID.
client_secret
property
¶
Client Secret.
client_jwks
property
¶
A JwkSet
containing the public keys for this client.
Keys are:
- the public key for client assertion signature verification (if using private_key_jwt)
- the ID Token encryption key
validate_endpoint_uri(attribute, uri)
¶
Validate that an endpoint URI is suitable for use.
If you need to disable some checks (for AS testing purposes only!), provide a different method here.
Source code in requests_oauth2client/client.py
validate_issuer_uri(attribute, uri)
¶
Validate that an Issuer identifier is suitable for use.
This is the same check as an endpoint URI, but the path may be (and usually is) empty.
Source code in requests_oauth2client/client.py
token_request(data, *, timeout=10, dpop=None, dpop_key=None, **requests_kwargs)
¶
Send a request to the token endpoint.
Authentication will be added automatically based on the defined auth
for this client.
Parameters:
Name | Type | Description | Default | ||
---|---|---|---|---|---|
data
|
dict[str, Any]
|
parameters to send to the token endpoint. Items with a |
required | ||
timeout
|
int
|
a timeout value for the call |
10
|
||
dpop
|
bool | None
|
toggles DPoP-proofing for the token request:
|
None
|
||
dpop_key
|
DPoPKey | None
|
a chosen |
None
|
||
**requests_kwargs
|
Any
|
additional parameters for requests.post() |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
the token endpoint response |
Source code in requests_oauth2client/client.py
parse_token_response(response, *, dpop_key=None)
¶
Parse a Response returned by the Token Endpoint.
Invoked by token_request to parse
responses returned by the Token Endpoint. Those responses contain an access_token
and
additional attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
BearerToken
|
a |
Source code in requests_oauth2client/client.py
on_token_error(response, *, dpop_key=None)
¶
Error handler for token_request()
.
Invoked by token_request when the Token Endpoint returns an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the |
required |
dpop_key
|
DPoPKey | None
|
the DPoPKey that was used to proof the token request, if any. |
None
|
Returns:
Type | Description |
---|---|
BearerToken
|
nothing, and raises an exception instead. But a subclass may return a |
BearerToken
|
|
Raises:
Type | Description |
---|---|
InvalidTokenResponse
|
if the error response does not contain an OAuth 2.0 standard error response. |
Source code in requests_oauth2client/client.py
client_credentials(scope=None, *, dpop=None, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request to the token endpoint using the client_credentials
grant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
str | Iterable[str] | None
|
the scope to send with the request. Can be a str, or an iterable of str.
to pass that way include |
None
|
dpop
|
bool | None
|
toggles DPoP-proofing for the token request:
|
None
|
dpop_key
|
DPoPKey | None
|
a chosen |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for the call to requests |
None
|
**token_kwargs
|
Any
|
additional parameters that will be added in the form data for the token endpoint,
alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
a |
Raises:
Type | Description |
---|---|
InvalidScopeParam
|
if the |
Source code in requests_oauth2client/client.py
authorization_code(code, *, validate=True, dpop=False, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request to the token endpoint with the authorization_code
grant.
You can either pass an authorization code, as a str
, or pass an AuthorizationResponse
instance as
returned by AuthorizationRequest.validate_callback()
(recommended). If you do the latter, this will
automatically:
- add the appropriate
redirect_uri
value that was initially passed in the Authorization Request parameters. This is no longer mandatory in OAuth 2.1, but a lot of Authorization Servers are still expecting it since it was part of the OAuth 2.0 specifications. - add the appropriate
code_verifier
for PKCE that was generated before sending the AuthorizationRequest. - handle DPoP binding based on the same
DPoPKey
that was used to initialize theAuthenticationRequest
and whose JWK thumbprint was passed asdpop_jkt
parameter in the Auth Request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str | AuthorizationResponse
|
An authorization code or an |
required |
validate
|
bool
|
If |
True
|
dpop
|
bool
|
Toggles DPoP binding for the Access Token, even if Authorization Code DPoP binding was not initially done. |
False
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the call to the underlying HTTP |
None
|
**token_kwargs
|
Any
|
Additional parameters that will be added in the form data for the token endpoint,
alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint Response. |
Source code in requests_oauth2client/client.py
refresh_token(refresh_token, requests_kwargs=None, **token_kwargs)
¶
Send a request to the token endpoint with the refresh_token
grant.
If refresh_token
is a DPoPToken
instance, (which means that DPoP was used to obtain the initial
Access/Refresh Tokens), then the same DPoP key will be used to DPoP proof the refresh token request,
as defined in RFC9449.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
refresh_token
|
str | BearerToken
|
A refresh_token, as a string, or as a |
required |
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the call to |
None
|
**token_kwargs
|
Any
|
Additional parameters for the token endpoint,
alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The token endpoint response. |
Raises:
Type | Description |
---|---|
MissingRefreshToken
|
If |
Source code in requests_oauth2client/client.py
device_code(device_code, *, dpop=False, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request to the token endpoint using the Device Code grant.
The grant_type is urn:ietf:params:oauth:grant-type:device_code
. This needs a Device Code,
or a DeviceAuthorizationResponse
as parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device_code
|
str | DeviceAuthorizationResponse
|
A device code, or a |
required |
dpop
|
bool
|
Toggles DPoP Binding. If |
False
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the call to requests. |
None
|
**token_kwargs
|
Any
|
Additional parameters for the token endpoint, alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Raises:
Type | Description |
---|---|
MissingDeviceCode
|
if |
Source code in requests_oauth2client/client.py
ciba(auth_req_id, requests_kwargs=None, **token_kwargs)
¶
Send a CIBA request to the Token Endpoint.
A CIBA request is a Token Request using the urn:openid:params:grant-type:ciba
grant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth_req_id
|
str | BackChannelAuthenticationResponse
|
an authentication request ID, as returned by the AS |
required |
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for the call to requests |
None
|
**token_kwargs
|
Any
|
additional parameters for the token endpoint, alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Raises:
Type | Description |
---|---|
MissingAuthRequestId
|
if |
Source code in requests_oauth2client/client.py
token_exchange(*, subject_token, subject_token_type=None, actor_token=None, actor_token_type=None, requested_token_type=None, dpop=False, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a Token Exchange request.
A Token Exchange request is actually a request to the Token Endpoint with a grant_type
urn:ietf:params:oauth:grant-type:token-exchange
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subject_token
|
str | BearerToken | IdToken
|
The subject token to exchange for a new token. |
required |
subject_token_type
|
str | None
|
A token type identifier for the subject_token, mandatory if it cannot be guessed based
on |
None
|
actor_token
|
None | str | BearerToken | IdToken
|
The actor token to include in the request, if any. |
None
|
actor_token_type
|
str | None
|
A token type identifier for the actor_token, mandatory if it cannot be guessed based
on |
None
|
requested_token_type
|
str | None
|
A token type identifier for the requested token. |
None
|
dpop
|
bool
|
Toggles DPoP Binding. If |
False
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters to pass to the underlying |
None
|
**token_kwargs
|
Any
|
Additional parameters to include in the request body. |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Raises:
Type | Description |
---|---|
UnknownSubjectTokenType
|
If the type of |
UnknownActorTokenType
|
If the type of |
Source code in requests_oauth2client/client.py
jwt_bearer(assertion, *, dpop=False, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request using a JWT as authorization grant.
This is defined in (RFC7523 $2.1)[https://www.rfc-editor.org/rfc/rfc7523.html#section-2.1).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
assertion
|
Jwt | str
|
A JWT (as an instance of |
required |
dpop
|
bool
|
Toggles DPoP Binding. If |
False
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters to pass to the underlying |
None
|
**token_kwargs
|
Any
|
Additional parameters to include in the request body. |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Source code in requests_oauth2client/client.py
resource_owner_password(username, password, *, dpop=None, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request using the Resource Owner Password Grant.
This Grant Type is deprecated and should only be used when there is no other choice.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
the resource owner user name |
required |
password
|
str
|
the resource owner password |
required |
dpop
|
bool | None
|
Toggles DPoP Binding. If |
None
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters to pass to the underlying |
None
|
**token_kwargs
|
Any
|
additional parameters to include in the request body. |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Source code in requests_oauth2client/client.py
authorization_request(*, scope='openid', response_type=ResponseTypes.CODE, redirect_uri=None, state=..., nonce=..., code_verifier=None, dpop=None, dpop_key=None, dpop_alg=None, **kwargs)
¶
Generate an Authorization Request for this client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
None | str | Iterable[str]
|
The |
'openid'
|
response_type
|
str
|
The |
CODE
|
redirect_uri
|
str | None
|
The |
None
|
state
|
str | ellipsis | None
|
The |
...
|
nonce
|
str | ellipsis | None
|
A |
...
|
dpop
|
bool | None
|
Toggles DPoP binding.
- if |
None
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
dpop_alg
|
str | None
|
A signature alg to sign the DPoP proof. If |
None
|
code_verifier
|
str | None
|
The PKCE |
None
|
**kwargs
|
Any
|
Additional query parameters to include in the auth request. |
{}
|
Returns:
Type | Description |
---|---|
AuthorizationRequest
|
The Token Endpoint response. |
Source code in requests_oauth2client/client.py
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 |
|
pushed_authorization_request(authorization_request, requests_kwargs=None)
¶
Send a Pushed Authorization Request.
This sends a request to the Pushed Authorization Request Endpoint, and returns a
RequestUriParameterAuthorizationRequest
initialized with the AS response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_request
|
AuthorizationRequest
|
The authorization request to send. |
required |
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for |
None
|
Returns:
Type | Description |
---|---|
RequestUriParameterAuthorizationRequest
|
The |
Source code in requests_oauth2client/client.py
parse_pushed_authorization_response(response, *, dpop_key=None)
¶
Parse the response obtained by pushed_authorization_request()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
The |
required |
dpop_key
|
DPoPKey | None
|
The |
None
|
Returns:
Type | Description |
---|---|
RequestUriParameterAuthorizationRequest
|
A |
Source code in requests_oauth2client/client.py
on_pushed_authorization_request_error(response, *, dpop_key=None)
¶
Error Handler for Pushed Authorization Endpoint errors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
The HTTP response as returned by the AS PAR endpoint. |
required |
dpop_key
|
DPoPKey | None
|
The |
None
|
Returns:
Type | Description |
---|---|
RequestUriParameterAuthorizationRequest
|
Should not return anything, but raise an Exception instead. A |
RequestUriParameterAuthorizationRequest
|
may be returned by subclasses for testing purposes. |
Raises:
Type | Description |
---|---|
EndpointError
|
A subclass of this error depending on the error returned by the AS. |
InvalidPushedAuthorizationResponse
|
If the returned response is not following the specifications. |
UnknownTokenEndpointError
|
For unknown/unhandled errors. |
Source code in requests_oauth2client/client.py
userinfo(access_token)
¶
Call the UserInfo endpoint.
This sends a request to the UserInfo endpoint, with the specified access_token, and returns the parsed result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token
|
BearerToken | str
|
the access token to use |
required |
Returns:
Type | Description |
---|---|
Any
|
the Response returned by the userinfo endpoint. |
Source code in requests_oauth2client/client.py
parse_userinfo_response(resp, *, dpop_key=None)
¶
Parse the response obtained by userinfo()
.
Invoked by userinfo() to parse the response from the UserInfo endpoint, this will extract and return its JSON content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resp
|
Response
|
a Response returned from the UserInfo endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
Any
|
the parsed JSON content from this response. |
Source code in requests_oauth2client/client.py
on_userinfo_error(resp, *, dpop_key=None)
¶
Parse UserInfo error response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resp
|
Response
|
a Response returned from the UserInfo endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
Any
|
nothing, raises exception instead. |
Source code in requests_oauth2client/client.py
get_token_type(token_type=None, token=None)
classmethod
¶
Get standardized token type identifiers.
Return a standardized token type identifier, based on a short token_type
hint and/or a
token value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_type
|
str | None
|
a token_type hint, as |
None
|
token
|
None | str | BearerToken | IdToken
|
a token value, as an instance of |
None
|
Returns:
Type | Description |
---|---|
str
|
the token_type as defined in the Token Exchange RFC8693. |
Raises:
Type | Description |
---|---|
UnknownTokenType
|
if the type of token cannot be determined |
Source code in requests_oauth2client/client.py
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 |
|
revoke_access_token(access_token, requests_kwargs=None, **revoke_kwargs)
¶
Send a request to the Revocation Endpoint to revoke an access token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token
|
BearerToken | str
|
the access token to revoke |
required |
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for the underlying requests.post() call |
None
|
**revoke_kwargs
|
Any
|
additional parameters to pass to the revocation endpoint |
{}
|
Source code in requests_oauth2client/client.py
revoke_refresh_token(refresh_token, requests_kwargs=None, **revoke_kwargs)
¶
Send a request to the Revocation Endpoint to revoke a refresh token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
refresh_token
|
str | BearerToken
|
the refresh token to revoke. |
required |
requests_kwargs
|
dict[str, Any] | None
|
additional parameters to pass to the revocation endpoint. |
None
|
**revoke_kwargs
|
Any
|
additional parameters to pass to the revocation endpoint. |
{}
|
Returns:
Type | Description |
---|---|
bool
|
|
bool
|
revocation endpoint. |
Raises:
Type | Description |
---|---|
MissingRefreshToken
|
when |
Source code in requests_oauth2client/client.py
revoke_token(token, token_type_hint=None, requests_kwargs=None, **revoke_kwargs)
¶
Send a Token Revocation request.
By default, authentication will be the same than the one used for the Token Endpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str | BearerToken
|
the token to revoke. |
required |
token_type_hint
|
str | None
|
a token_type_hint to send to the revocation endpoint. |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters to the underling call to requests.post() |
None
|
**revoke_kwargs
|
Any
|
additional parameters to send to the revocation endpoint. |
{}
|
Returns:
Type | Description |
---|---|
bool
|
the result from |
Raises:
Type | Description |
---|---|
MissingEndpointUri
|
if the Revocation Endpoint URI is not configured. |
MissingRefreshToken
|
if |
Source code in requests_oauth2client/client.py
parse_revocation_response(response, *, dpop_key=None)
¶
Parse reponses from the Revocation Endpoint.
Since those do not return any meaningful information in a standardised fashion, this just returns True
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
bool
|
non-standardised error is returned. |
Source code in requests_oauth2client/client.py
on_revocation_error(response, *, dpop_key=None)
¶
Error handler for revoke_token()
.
Invoked by revoke_token() when the revocation endpoint returns an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
bool
|
revocation response. |
Raises:
Type | Description |
---|---|
EndpointError
|
if the response contains a standardised OAuth 2.0 error. |
Source code in requests_oauth2client/client.py
introspect_token(token, token_type_hint=None, requests_kwargs=None, **introspect_kwargs)
¶
Send a request to the Introspection Endpoint.
Parameter token
can be:
- a
str
- a
BearerToken
instance
You may pass any arbitrary token
and token_type_hint
values as str
. Those will
be included in the request, as-is.
If token
is a BearerToken
, then token_type_hint
must be either:
None
: the access_token will be instrospected and no token_type_hint will be included in the requestaccess_token
: same asNone
, but the token_type_hint will be included- or
refresh_token
: only available if a Refresh Token is present in the BearerToken.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str | BearerToken
|
the token to instrospect |
required |
token_type_hint
|
str | None
|
the |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters to the underling call to requests.post() |
None
|
**introspect_kwargs
|
Any
|
additional parameters to send to the introspection endpoint. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
the response as returned by the Introspection Endpoint. |
Raises:
Type | Description |
---|---|
MissingRefreshToken
|
if |
UnknownTokenType
|
if |
Source code in requests_oauth2client/client.py
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 |
|
parse_introspection_response(response, *, dpop_key=None)
¶
Parse Token Introspection Responses received by introspect_token()
.
Invoked by introspect_token() to parse the returned response. This decodes the JSON content if possible, otherwise it returns the response as a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the Response as returned by the Introspection Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
Any
|
the decoded JSON content, or a |
Source code in requests_oauth2client/client.py
on_introspection_error(response, *, dpop_key=None)
¶
Error handler for introspect_token()
.
Invoked by introspect_token() to parse the returned response in the case an error is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response as returned by the Introspection Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
Any
|
usually raises exceptions. A subclass can return a default response instead. |
Raises:
Type | Description |
---|---|
EndpointError
|
(or one of its subclasses) if the response contains a standard OAuth 2.0 error. |
UnknownIntrospectionError
|
if the response is not a standard error response. |
Source code in requests_oauth2client/client.py
backchannel_authentication_request(scope='openid', *, client_notification_token=None, acr_values=None, login_hint_token=None, id_token_hint=None, login_hint=None, binding_message=None, user_code=None, requested_expiry=None, private_jwk=None, alg=None, requests_kwargs=None, **ciba_kwargs)
¶
Send a CIBA Authentication Request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
None | str | Iterable[str]
|
the scope to include in the request. |
'openid'
|
client_notification_token
|
str | None
|
the Client Notification Token to include in the request. |
None
|
acr_values
|
None | str | Iterable[str]
|
the acr values to include in the request. |
None
|
login_hint_token
|
str | None
|
the Login Hint Token to include in the request. |
None
|
id_token_hint
|
str | None
|
the ID Token Hint to include in the request. |
None
|
login_hint
|
str | None
|
the Login Hint to include in the request. |
None
|
binding_message
|
str | None
|
the Binding Message to include in the request. |
None
|
user_code
|
str | None
|
the User Code to include in the request |
None
|
requested_expiry
|
int | None
|
the Requested Expiry, in seconds, to include in the request. |
None
|
private_jwk
|
Jwk | dict[str, Any] | None
|
the JWK to use to sign the request (optional) |
None
|
alg
|
str | None
|
the alg to use to sign the request, if the provided JWK does not include an "alg" parameter. |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for |
None
|
**ciba_kwargs
|
Any
|
additional parameters to include in the request. |
{}
|
Returns:
Type | Description |
---|---|
BackChannelAuthenticationResponse
|
a BackChannelAuthenticationResponse as returned by AS |
Raises:
Type | Description |
---|---|
InvalidBackchannelAuthenticationRequestHintParam
|
if none of |
InvalidScopeParam
|
if the |
InvalidAcrValuesParam
|
if the |
Source code in requests_oauth2client/client.py
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 |
|
parse_backchannel_authentication_response(response, *, dpop_key=None)
¶
Parse a response received by backchannel_authentication_request()
.
Invoked by backchannel_authentication_request() to parse the response returned by the BackChannel Authentication Endpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response returned by the BackChannel Authentication Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
BackChannelAuthenticationResponse
|
a |
Raises:
Type | Description |
---|---|
InvalidBackChannelAuthenticationResponse
|
if the response does not contain a standard BackChannel Authentication response. |
Source code in requests_oauth2client/client.py
on_backchannel_authentication_error(response, *, dpop_key=None)
¶
Error handler for backchannel_authentication_request()
.
Invoked by backchannel_authentication_request() to parse the response returned by the BackChannel Authentication Endpoint, when it is an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response returned by the BackChannel Authentication Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
BackChannelAuthenticationResponse
|
usually raises an exception. But a subclass can return a default response instead. |
Raises:
Type | Description |
---|---|
EndpointError
|
(or one of its subclasses) if the response contains a standard OAuth 2.0 error. |
InvalidBackChannelAuthenticationResponse
|
for non-standard error responses. |
Source code in requests_oauth2client/client.py
authorize_device(requests_kwargs=None, **data)
¶
Send a Device Authorization Request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**data
|
Any
|
additional data to send to the Device Authorization Endpoint |
{}
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for |
None
|
Returns:
Type | Description |
---|---|
DeviceAuthorizationResponse
|
a Device Authorization Response |
Raises:
Type | Description |
---|---|
MissingEndpointUri
|
if the Device Authorization URI is not configured |
Source code in requests_oauth2client/client.py
parse_device_authorization_response(response, *, dpop_key=None)
¶
Parse a Device Authorization Response received by authorize_device()
.
Invoked by authorize_device() to parse the response returned by the Device Authorization Endpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response returned by the Device Authorization Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
DeviceAuthorizationResponse
|
a |
Source code in requests_oauth2client/client.py
on_device_authorization_error(response, *, dpop_key=None)
¶
Error handler for authorize_device()
.
Invoked by authorize_device() to parse the response returned by the Device Authorization Endpoint, when that response is an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response returned by the Device Authorization Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
DeviceAuthorizationResponse
|
usually raises an Exception. But a subclass may return a default response instead. |
Raises:
Type | Description |
---|---|
EndpointError
|
for standard OAuth 2.0 errors |
InvalidDeviceAuthorizationResponse
|
for non-standard error responses. |
Source code in requests_oauth2client/client.py
update_authorization_server_public_keys(requests_kwargs=None)
¶
Update the cached AS public keys by retrieving them from its jwks_uri
.
Public keys are returned by this method, as a jwskate.JwkSet
. They are also
available in attribute authorization_server_jwks
.
Returns:
Type | Description |
---|---|
JwkSet
|
the retrieved public keys |
Raises:
Type | Description |
---|---|
ValueError
|
if no |
Source code in requests_oauth2client/client.py
from_discovery_endpoint(url=None, issuer=None, *, auth=None, client_id=None, client_secret=None, private_key=None, session=None, testing=False, **kwargs)
classmethod
¶
Initialize an OAuth2Client
using an AS Discovery Document endpoint.
If an url
is provided, an HTTPS request will be done to that URL to obtain the Authorization Server Metadata.
If an issuer
is provided, the OpenID Connect Discovery document url will be automatically
derived from it, as specified in OpenID Connect Discovery.
Once the standardized metadata document is obtained, this will extract
all Endpoint Uris from that document, will fetch the current public keys from its
jwks_uri
, then will initialize an OAuth2Client based on those endpoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str | None
|
The url where the server metadata will be retrieved. |
None
|
issuer
|
str | None
|
The issuer value that is expected in the discovery document.
If not |
None
|
auth
|
AuthBase | tuple[str, str] | str | None
|
The authentication handler to use for client authentication. |
None
|
client_id
|
str | None
|
Client ID. |
None
|
client_secret
|
str | None
|
Client secret to use to authenticate the client. |
None
|
private_key
|
Jwk | dict[str, Any] | None
|
Private key to sign client assertions. |
None
|
session
|
Session | None
|
A |
None
|
testing
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional keyword parameters to pass to |
{}
|
Returns:
Type | Description |
---|---|
OAuth2Client
|
An |
Raises:
Type | Description |
---|---|
InvalidIssuer
|
If |
InvalidParam
|
If neither |
HTTPError
|
If an error happens while fetching the documents. |
Example
Source code in requests_oauth2client/client.py
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 |
|
from_discovery_document(discovery, issuer=None, *, auth=None, client_id=None, client_secret=None, private_key=None, authorization_server_jwks=None, https=True, testing=False, **kwargs)
classmethod
¶
Initialize an OAuth2Client
, based on an AS Discovery Document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
discovery
|
dict[str, Any]
|
A |
required |
issuer
|
str | None
|
If an issuer is given, check that it matches the one mentioned in the document. |
None
|
auth
|
AuthBase | tuple[str, str] | str | None
|
The authentication handler to use for client authentication. |
None
|
client_id
|
str | None
|
Client ID. |
None
|
client_secret
|
str | None
|
Client secret to use to authenticate the client. |
None
|
private_key
|
Jwk | dict[str, Any] | None
|
Private key to sign client assertions. |
None
|
authorization_server_jwks
|
JwkSet | dict[str, Any] | None
|
The current authorization server JWKS keys. |
None
|
https
|
bool
|
(deprecated) If |
True
|
testing
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional args that will be passed to |
{}
|
Returns:
Type | Description |
---|---|
OAuth2Client
|
An |
Raises:
Type | Description |
---|---|
InvalidDiscoveryDocument
|
If the document does not contain at least a |
Examples:
Source code in requests_oauth2client/client.py
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 |
|
UnknownActorTokenType
¶
Bases: UnknownTokenType
Raised when the type of actor_token cannot be determined automatically.
Source code in requests_oauth2client/client.py
UnknownSubjectTokenType
¶
Bases: UnknownTokenType
Raised when the type of subject_token cannot be determined automatically.
Source code in requests_oauth2client/client.py
UnknownTokenType
¶
Bases: InvalidParam
, TypeError
Raised when the type of a token cannot be determined automatically.
Source code in requests_oauth2client/client.py
BaseClientAssertionAuthenticationMethod
¶
Bases: BaseClientAuthenticationMethod
Base class for assertion-based client authentication methods.
Source code in requests_oauth2client/client_authentication.py
client_assertion(audience)
¶
Generate a Client Assertion for a specific audience.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audience
|
str
|
the audience to use for the |
required |
Returns:
Type | Description |
---|---|
str
|
a Client Assertion, as |
Source code in requests_oauth2client/client_authentication.py
BaseClientAuthenticationMethod
¶
Bases: AuthBase
Base class for all Client Authentication methods. This extends requests.auth.AuthBase.
This base class checks that requests are suitable to add Client Authentication parameters to, and does not modify the request.
Source code in requests_oauth2client/client_authentication.py
ClientSecretBasic
¶
Bases: BaseClientAuthenticationMethod
Implement client_secret_basic
authentication.
With this method, the client sends its Client ID and Secret, in the HTTP Authorization
header, with
the Basic
scheme, in each authenticated request to the Authorization Server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
Client ID |
required |
client_secret
|
str
|
Client Secret |
required |
Example
Source code in requests_oauth2client/client_authentication.py
ClientSecretJwt
¶
Bases: BaseClientAssertionAuthenticationMethod
Implement client_secret_jwt
client authentication method.
With this method, the client generates a client assertion, then symmetrically signs it with its Client Secret.
The assertion is then sent to the AS in a client_assertion
field with each authenticated request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
the |
required |
client_secret
|
str
|
the |
required |
alg
|
str
|
the alg to use to sign generated Client Assertions. |
HS256
|
lifetime
|
int
|
the lifetime to use for generated Client Assertions. |
60
|
jti_gen
|
Callable[[], str]
|
a function to generate JWT Token Ids ( |
lambda: str(uuid4())
|
aud
|
str | None
|
the audience value to use. If |
None
|
Example
Source code in requests_oauth2client/client_authentication.py
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 |
|
client_assertion(audience)
¶
Generate a symmetrically signed Client Assertion.
Assertion is signed with the client_secret
as key and the alg
passed at init time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audience
|
str
|
the audience to use for the generated Client Assertion. |
required |
Returns:
Type | Description |
---|---|
str
|
a Client Assertion, as |
Source code in requests_oauth2client/client_authentication.py
ClientSecretPost
¶
Bases: BaseClientAuthenticationMethod
Implement client_secret_post
client authentication method.
With this method, the client inserts its client_id and client_secret in each authenticated request to the AS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
Client ID |
required |
client_secret
|
str
|
Client Secret |
required |
Example
Source code in requests_oauth2client/client_authentication.py
InvalidClientAssertionSigningKeyOrAlg
¶
Bases: ValueError
Raised when the client assertion signing alg is not specified or invalid.
Source code in requests_oauth2client/client_authentication.py
InvalidRequestForClientAuthentication
¶
Bases: RuntimeError
Raised when a request is not suitable for OAuth 2.0 client authentication.
Source code in requests_oauth2client/client_authentication.py
PrivateKeyJwt
¶
Bases: BaseClientAssertionAuthenticationMethod
Implement private_key_jwt
client authentication method.
With this method, the client generates and sends a client_assertion, that is asymmetrically signed with a private key, on each direct request to the Authorization Server.
The private key must be supplied as a jwskate.Jwk
instance,
or any key material that can be used to initialize one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
the |
required |
private_jwk
|
Jwk | dict[str, Any] | Any
|
the private key to use to sign generated Client Assertions. |
required |
alg
|
str | None
|
the alg to use to sign generated Client Assertions. |
None
|
lifetime
|
int
|
the lifetime to use for generated Client Assertions. |
60
|
jti_gen
|
Callable[[], str]
|
a function to generate JWT Token Ids ( |
lambda: str(uuid4())
|
aud
|
str | None
|
the audience value to use. If |
None
|
Example
Source code in requests_oauth2client/client_authentication.py
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 |
|
client_assertion(audience)
¶
Generate a Client Assertion, asymmetrically signed with private_jwk
as key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audience
|
str
|
the audience to use for the generated Client Assertion. |
required |
Returns:
Type | Description |
---|---|
str
|
a Client Assertion. |
Source code in requests_oauth2client/client_authentication.py
PublicApp
¶
Bases: BaseClientAuthenticationMethod
Implement the none
authentication method for public apps.
This scheme is used for Public Clients, which do not have any secret credentials. Those only send their client_id to the Authorization Server.
Example
Source code in requests_oauth2client/client_authentication.py
UnsupportedClientCredentials
¶
DeviceAuthorizationPoolingJob
¶
Bases: BaseTokenEndpointPoolingJob
A Token Endpoint pooling job for the Device Authorization Flow.
This periodically checks if the user has finished with his authorization in a Device Authorization flow.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
an OAuth2Client that will be used to pool the token endpoint. |
required |
device_code
|
str | DeviceAuthorizationResponse
|
a |
required |
interval
|
int | None
|
The pooling interval to use. This overrides the one in |
None
|
slow_down_interval
|
int
|
Number of seconds to add to the pooling interval when the AS returns a slow-down request. |
5
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the underlying calls to requests.request. |
None
|
**token_kwargs
|
Any
|
Additional parameters for the token request. |
{}
|
Example
Source code in requests_oauth2client/device_authorization.py
token_request()
¶
Implement the Device Code token request.
This actually calls OAuth2Client.device_code(device_code)
on self.client
.
Returns:
Type | Description |
---|---|
BearerToken
|
Source code in requests_oauth2client/device_authorization.py
DeviceAuthorizationResponse
¶
Represent a response returned by the device Authorization Endpoint.
All parameters are those returned by the AS as response to a Device Authorization Request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device_code
|
str
|
the |
required |
user_code
|
str
|
the |
required |
verification_uri
|
str
|
the |
required |
verification_uri_complete
|
str | None
|
the |
None
|
expires_at
|
datetime | None
|
the expiration date for the device_code.
Also accepts an |
None
|
interval
|
int | None
|
the pooling |
None
|
**kwargs
|
Any
|
additional parameters as returned by the AS. |
{}
|
Source code in requests_oauth2client/device_authorization.py
is_expired(leeway=0)
¶
Check if the device_code
within this response is expired.
Returns:
Type | Description |
---|---|
bool | None
|
|
bool | None
|
no |
Source code in requests_oauth2client/device_authorization.py
DPoPKey
¶
Wrapper around a DPoP proof signature key.
This handles DPoP proof generation. It also keeps track of a nonce, if provided by the Resource Server. Its behavior follows the standard DPoP specifications. You may subclass or otherwise customize this class to implement custom behavior, like adding or modifying claims to the proofs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
private_key
|
Any
|
the private key to use for DPoP proof signatures. |
required |
alg
|
str | None
|
the alg to use for signatures, if not specified of the |
None
|
jti_generator
|
Callable[[], str]
|
a callable that generates unique JWT Token ID (jti) values to include in proofs. |
lambda: str(uuid4())
|
iat_generator
|
Callable[[], int]
|
a callable that generates the Issuer Date (iat) to include in proofs. |
lambda: timestamp()
|
jwt_typ
|
str
|
the token type ( |
'dpop+jwt'
|
dpop_token_class
|
type[DPoPToken]
|
the class to use to represent DPoP tokens. |
DPoPToken
|
rs_nonce
|
str | None
|
an initial DPoP |
None
|
Source code in requests_oauth2client/dpop.py
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 |
|
public_jwk
cached
property
¶
The public JWK key that matches the private key.
dpop_jkt
cached
property
¶
The key thumbprint, used for Authorization Code DPoP binding.
generate(alg=jwskate.SignatureAlgs.ES256, jwt_typ='dpop+jwt', jti_generator=lambda: str(uuid4()), iat_generator=lambda: jwskate.Jwt.timestamp(), dpop_token_class=DPoPToken, as_nonce=None, rs_nonce=None)
classmethod
¶
Generate a new DPoPKey with a new private key that is suitable for the given alg
.
Source code in requests_oauth2client/dpop.py
proof(htm, htu, ath=None, nonce=None)
¶
Generate a DPoP proof.
Proof will contain the following claims:
1 2 3 4 5 6 |
|
The proof will be signed with the private key of this DPoPKey, using the configured alg
signature algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
htm
|
str
|
The HTTP method value of the request to which the proof is attached. |
required |
htu
|
str
|
The HTTP target URI of the request to which the proof is attached. Query and Fragment parts will
be automatically removed before being used as |
required |
ath
|
str | None
|
The Access Token hash value. |
None
|
nonce
|
str | None
|
A recent nonce provided via the DPoP-Nonce HTTP header, from either the AS or RS. If |
None
|
Returns:
Type | Description |
---|---|
SignedJwt
|
the proof value (as a signed JWT) |
Source code in requests_oauth2client/dpop.py
handle_as_provided_dpop_nonce(response)
¶
Handle an Authorization Server response containing a use_dpop_nonce
error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response from the AS. |
required |
Source code in requests_oauth2client/dpop.py
handle_rs_provided_dpop_nonce(response)
¶
Handle a Resource Server response containing a use_dpop_nonce
error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response from the AS. |
required |
Source code in requests_oauth2client/dpop.py
DPoPToken
¶
Bases: BearerToken
Represent a DPoP token (RFC9449).
A DPoP is very much like a BearerToken, with an additional private key bound to it.
Source code in requests_oauth2client/dpop.py
InvalidDPoPAccessToken
¶
Bases: ValueError
Raised when an access token contains invalid characters.
Source code in requests_oauth2client/dpop.py
InvalidDPoPAlg
¶
Bases: ValueError
Raised when an invalid or unsupported DPoP alg is given.
Source code in requests_oauth2client/dpop.py
InvalidDPoPKey
¶
Bases: ValueError
Raised when a DPoPToken is initialized with a non-suitable key.
Source code in requests_oauth2client/dpop.py
InvalidDPoPProof
¶
Bases: ValueError
Raised when a DPoP proof does not verify.
Source code in requests_oauth2client/dpop.py
InvalidUseDPoPNonceResponse
¶
Bases: Exception
Base class for invalid Responses with a use_dpop_nonce
error.
Source code in requests_oauth2client/dpop.py
MissingDPoPNonce
¶
Bases: InvalidUseDPoPNonceResponse
Raised when a server requests a DPoP nonce but none is provided in its response.
Source code in requests_oauth2client/dpop.py
RepeatedDPoPNonce
¶
Bases: InvalidUseDPoPNonceResponse
Raised when the server requests a DPoP nonce value that is the same as already included in the request.
Source code in requests_oauth2client/dpop.py
AccessDenied
¶
Bases: EndpointError
Raised when the Authorization Server returns error = access_denied
.
Source code in requests_oauth2client/exceptions.py
AccountSelectionRequired
¶
Bases: InteractionRequired
Raised when the Authorization Endpoint returns error = account_selection_required
.
Source code in requests_oauth2client/exceptions.py
AuthorizationPending
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = authorization_pending
.
Source code in requests_oauth2client/exceptions.py
AuthorizationResponseError
¶
Bases: Exception
Base class for error responses returned by the Authorization endpoint.
An AuthorizationResponseError
contains the error message, description and uri that are
returned by the AS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error
|
str
|
the |
required |
description
|
str | None
|
the |
None
|
uri
|
str | None
|
the |
None
|
Source code in requests_oauth2client/exceptions.py
BackChannelAuthenticationError
¶
Bases: EndpointError
Base class for errors returned by the BackChannel Authentication endpoint.
Source code in requests_oauth2client/exceptions.py
ConsentRequired
¶
Bases: InteractionRequired
Raised when the Authorization Endpoint returns error = consent_required
.
Source code in requests_oauth2client/exceptions.py
DeviceAuthorizationError
¶
Bases: EndpointError
Base class for Device Authorization Endpoint errors.
Source code in requests_oauth2client/exceptions.py
EndpointError
¶
Bases: OAuth2Error
Base class for exceptions raised from backend endpoint errors.
This contains the error message, description and uri that are returned by the AS in the OAuth 2.0 standardised way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the raw response containing the error. |
required |
error
|
str
|
the |
required |
description
|
str | None
|
the |
None
|
uri
|
str | None
|
the |
None
|
Source code in requests_oauth2client/exceptions.py
ExpiredToken
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = expired_token
.
Source code in requests_oauth2client/exceptions.py
InteractionRequired
¶
Bases: AuthorizationResponseError
Raised when the Authorization Endpoint returns error = interaction_required
.
Source code in requests_oauth2client/exceptions.py
IntrospectionError
¶
Bases: EndpointError
Base class for Introspection Endpoint errors.
Source code in requests_oauth2client/exceptions.py
InvalidAuthResponse
¶
Bases: ValueError
Raised when the Authorization Endpoint returns an invalid response.
Source code in requests_oauth2client/exceptions.py
InvalidBackChannelAuthenticationResponse
¶
Bases: OAuth2Error
Raised when the BackChannel Authentication endpoint returns a non-standard response.
Source code in requests_oauth2client/exceptions.py
InvalidClient
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_client
.
Source code in requests_oauth2client/exceptions.py
InvalidDeviceAuthorizationResponse
¶
Bases: OAuth2Error
Raised when the Device Authorization Endpoint returns a non-standard error response.
Source code in requests_oauth2client/exceptions.py
InvalidGrant
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_grant
.
Source code in requests_oauth2client/exceptions.py
InvalidPushedAuthorizationResponse
¶
Bases: OAuth2Error
Raised when the Pushed Authorization Endpoint returns an error.
Source code in requests_oauth2client/exceptions.py
InvalidRequest
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_request
.
Source code in requests_oauth2client/exceptions.py
InvalidScope
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_scope
.
Source code in requests_oauth2client/exceptions.py
InvalidTarget
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_target
.
Source code in requests_oauth2client/exceptions.py
InvalidTokenResponse
¶
Bases: OAuth2Error
Raised when the Token Endpoint returns a non-standard response.
Source code in requests_oauth2client/exceptions.py
LoginRequired
¶
Bases: InteractionRequired
Raised when the Authorization Endpoint returns error = login_required
.
Source code in requests_oauth2client/exceptions.py
MismatchingIssuer
¶
Bases: InvalidAuthResponse
Raised on mismatching iss
value.
This happens when the Authorization Endpoints returns an 'iss' that doesn't match the expected value.
Source code in requests_oauth2client/exceptions.py
MismatchingState
¶
Bases: InvalidAuthResponse
Raised on mismatching state
value.
This happens when the Authorization Endpoints returns a 'state' parameter that doesn't match the value passed in the Authorization Request.
Source code in requests_oauth2client/exceptions.py
MissingAuthCode
¶
Bases: InvalidAuthResponse
Raised when the Authorization Endpoint does not return the mandatory code
.
This happens when the Authorization Endpoint does not return an error, but does not return an
authorization code
either.
Source code in requests_oauth2client/exceptions.py
MissingIssuer
¶
Bases: InvalidAuthResponse
Raised when the Authorization Endpoint does not return an iss
parameter as expected.
The Authorization Server advertises its support with a flag
authorization_response_iss_parameter_supported
in its discovery document. If it is set to
true
, it must include an iss
parameter in its authorization responses, containing its issuer
identifier.
Source code in requests_oauth2client/exceptions.py
OAuth2Error
¶
Bases: Exception
Base class for Exceptions raised when a backend endpoint returns an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the HTTP response containing the error |
required |
client
|
the OAuth2Client used to send the request |
required | |
description
|
str | None
|
description of the error |
None
|
Source code in requests_oauth2client/exceptions.py
request
property
¶
The request leading to the error.
RevocationError
¶
Bases: EndpointError
Base class for Revocation Endpoint errors.
Source code in requests_oauth2client/exceptions.py
ServerError
¶
Bases: EndpointError
Raised when the token endpoint returns error = server_error
.
Source code in requests_oauth2client/exceptions.py
SessionSelectionRequired
¶
Bases: InteractionRequired
Raised when the Authorization Endpoint returns error = session_selection_required
.
Source code in requests_oauth2client/exceptions.py
SlowDown
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = slow_down
.
Source code in requests_oauth2client/exceptions.py
TokenEndpointError
¶
Bases: EndpointError
Base class for errors that are specific to the token endpoint.
Source code in requests_oauth2client/exceptions.py
UnauthorizedClient
¶
Bases: EndpointError
Raised when the Authorization Server returns error = unauthorized_client
.
Source code in requests_oauth2client/exceptions.py
UnknownIntrospectionError
¶
Bases: OAuth2Error
Raised when the Introspection Endpoint returns a non-standard error.
Source code in requests_oauth2client/exceptions.py
UnknownTokenEndpointError
¶
Bases: EndpointError
Raised when the token endpoint returns an otherwise unknown error.
Source code in requests_oauth2client/exceptions.py
UnsupportedTokenType
¶
Bases: RevocationError
Raised when the Revocation endpoint returns error = unsupported_token_type
.
Source code in requests_oauth2client/exceptions.py
UseDPoPNonce
¶
Bases: TokenEndpointError
Raised when the Token Endpoint raises error = use_dpop_nonce`.
Source code in requests_oauth2client/exceptions.py
BaseTokenEndpointPoolingJob
¶
Base class for Token Endpoint pooling jobs.
This is used for decoupled flows like CIBA or Device Authorization.
This class must be subclassed to implement actual BackChannel flows. This needs an
OAuth2Client that will be used to pool the token
endpoint. The initial pooling interval
is configurable.
Source code in requests_oauth2client/pooling.py
sleep()
¶
Implement the wait between two requests of the token endpoint.
By default, relies on time.sleep().
slow_down()
¶
Implement the behavior when receiving a 'slow_down' response from the AS.
By default, it increases the pooling interval by the slow down interval.
authorization_pending()
¶
Implement the behavior when receiving an 'authorization_pending' response from the AS.
By default, it does nothing.
token_request()
¶
Abstract method for the token endpoint call.
Subclasses must implement this. This method must raise
AuthorizationPending to retry after
the pooling interval, or SlowDown to increase
the pooling interval by slow_down_interval
seconds.
Returns:
Type | Description |
---|---|
BearerToken
|
Source code in requests_oauth2client/pooling.py
BearerToken
¶
Bases: TokenResponse
, AuthBase
Represents a Bearer Token as returned by a Token Endpoint.
This is a wrapper around a Bearer Token and associated parameters, such as expiration date and refresh token, as returned by an OAuth 2.x or OIDC 1.0 Token Endpoint.
All parameters are as returned by a Token Endpoint. The token expiration date can be passed as
datetime in the expires_at
parameter, or an expires_in
parameter, as number of seconds in
the future, can be passed instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token
|
str
|
an |
required |
expires_at
|
datetime | None
|
an expiration date. This method also accepts an |
None
|
scope
|
str | None
|
a |
None
|
refresh_token
|
str | None
|
a |
None
|
token_type
|
str
|
a |
TOKEN_TYPE
|
id_token
|
str | bytes | IdToken | JweCompact | None
|
an |
None
|
**kwargs
|
Any
|
additional parameters as returned by the AS, if any. |
{}
|
Source code in requests_oauth2client/tokens.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
expires_in
property
¶
Number of seconds until expiration.
access_token_jwt
cached
property
¶
If the access token is a JWT, return it as an instance of jwskate.SignedJwt
.
This method is just a helper for AS testing purposes. Note that, as an OAuth 2.0 Client, you should never have to decode or analyze an access token, since it is simply an abstract string value. It is not even mandatory that Access Tokens are JWTs, just an implementation choice. Only Resource Servers (APIs) should check for the contents of Access Tokens they receive.
is_expired(leeway=0)
¶
Check if the access token is expired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
leeway
|
int
|
If the token expires in the next given number of seconds, then consider it expired already. |
0
|
Returns:
Type | Description |
---|---|
bool | None
|
One of: |
bool | None
|
|
bool | None
|
|
bool | None
|
|
Source code in requests_oauth2client/tokens.py
authorization_header()
¶
Return the appropriate Authorization Header value for this token.
The value is formatted correctly according to RFC6750.
Returns:
Type | Description |
---|---|
str
|
the value to use in an HTTP Authorization Header |
Source code in requests_oauth2client/tokens.py
validate_id_token(client, azr, exp_leeway=0, auth_time_leeway=10)
¶
Validate the ID Token, and return a new instance with the decrypted ID Token.
If the ID Token was not encrypted, the returned instance will contain the same ID Token.
This will validate the id_token as described in OIDC 1.0 $3.1.3.7.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the |
required |
azr
|
AuthorizationResponse
|
the |
required |
exp_leeway
|
int
|
a leeway, in seconds, applied to the ID Token expiration date |
0
|
auth_time_leeway
|
int
|
a leeway, in seconds, applied to the |
10
|
Raises:
Type | Description |
---|---|
MissingIdToken
|
if the ID Token is missing |
InvalidIdToken
|
this is a base exception class, which is raised:
|
MismatchingIdTokenAlg
|
if the |
MismatchingIdTokenIssuer
|
if the |
MismatchingIdTokenAcr
|
if the |
MismatchingIdTokenAudience
|
if the |
MismatchingIdTokenAzp
|
if the |
MismatchingIdTokenNonce
|
if the |
ExpiredIdToken
|
if the ID Token is expired at the time of the check. |
UnsupportedIdTokenAlg
|
if the signature alg for the ID Token is not supported. |
Source code in requests_oauth2client/tokens.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
as_dict()
¶
Return a dict of parameters.
That is suitable for serialization or to init another BearerToken.
Source code in requests_oauth2client/tokens.py
BearerTokenSerializer
¶
A helper class to serialize Token Response returned by an AS.
This may be used to store BearerTokens in session or cookies.
It needs a dumper
and a loader
functions that will respectively serialize and deserialize
BearerTokens. Default implementations are provided with use gzip and base64url on the serialized
JSON representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dumper
|
Callable[[BearerToken], str] | None
|
a function to serialize a token into a |
None
|
loader
|
Callable[[str], BearerToken] | None
|
a function to deserialize a serialized token representation. |
None
|
Source code in requests_oauth2client/tokens.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
|
default_dumper(token)
staticmethod
¶
Serialize a token as JSON, then compress with deflate, then encodes as base64url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
BearerToken
|
the |
required |
Returns:
Type | Description |
---|---|
str
|
the serialized value |
Source code in requests_oauth2client/tokens.py
default_loader(serialized, token_class=BearerToken)
¶
Deserialize a BearerToken.
This does the opposite operations than default_dumper
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized
|
str
|
the serialized token |
required |
token_class
|
type[BearerToken]
|
class to use to deserialize the Token |
BearerToken
|
Returns:
Type | Description |
---|---|
BearerToken
|
a BearerToken |
Source code in requests_oauth2client/tokens.py
dumps(token)
¶
Serialize and compress a given token for easier storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
BearerToken
|
a BearerToken to serialize |
required |
Returns:
Type | Description |
---|---|
str
|
the serialized token, as a str |
Source code in requests_oauth2client/tokens.py
loads(serialized)
¶
Deserialize a serialized token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized
|
str
|
the serialized token |
required |
Returns:
Type | Description |
---|---|
BearerToken
|
the deserialized token |
ExpiredAccessToken
¶
ExpiredIdToken
¶
Bases: InvalidIdToken
Raised when the returned ID Token is expired.
Source code in requests_oauth2client/tokens.py
IdToken
¶
Bases: SignedJwt
Represent an ID Token.
An ID Token is actually a Signed JWT. If the ID Token is encrypted, it must be decoded beforehand.
Source code in requests_oauth2client/tokens.py
authorized_party
property
¶
The Authorized Party (azp).
auth_datetime
property
¶
The last user authentication time (auth_time).
hash_method(key, alg=None)
classmethod
¶
Returns a callable that generates valid OIDC hashes, such as at_hash
, c_hash
, etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
Jwk
|
the ID token signature verification public key |
required |
alg
|
str | None
|
the ID token signature algorithm |
None
|
Returns:
Type | Description |
---|---|
Callable[[str], str]
|
a callable that takes a string as input and produces a valid hash as a str output |
Source code in requests_oauth2client/tokens.py
InvalidIdToken
¶
Bases: ValueError
Raised when trying to validate an invalid ID Token value.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenAcr
¶
Bases: InvalidIdToken
Raised when the returned ID Token doesn't contain one of the requested ACR Values.
This happens when the authorization request includes an acr_values
parameter but the returned
ID Token includes a different value.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenAlg
¶
Bases: InvalidIdToken
Raised when the returned ID Token is signed with an unexpected alg.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenAudience
¶
Bases: InvalidIdToken
Raised when the ID Token audience does not include the requesting Client ID.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenAzp
¶
Bases: InvalidIdToken
Raised when the ID Token Authorized Presenter (azp) claim is not the Client ID.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenIssuer
¶
Bases: InvalidIdToken
Raised on mismatching iss
value in an ID Token.
This happens when the expected issuer
value is different from the iss
value in an obtained ID Token.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenNonce
¶
Bases: InvalidIdToken
Raised on mismatching nonce
value in an ID Token.
This happens when the authorization request includes a nonce
but the returned ID Token include
a different value.
Source code in requests_oauth2client/tokens.py
MissingIdToken
¶
Bases: InvalidIdToken
Raised when the Authorization Endpoint does not return a mandatory ID Token.
This happens when the Authorization Endpoint does not return an error, but does not return an ID Token either.
Source code in requests_oauth2client/tokens.py
InvalidUri
¶
Bases: ValueError
Raised when a URI does not pass validation by validate_endpoint_uri()
.
Source code in requests_oauth2client/utils.py
errors()
¶
Iterate over all error descriptions, as str.
Source code in requests_oauth2client/utils.py
oauth2_discovery_document_url(issuer)
¶
Construct the standardised OAuth 2.0 discovery document url for a given issuer
.
Based an issuer
identifier, returns the standardised URL where the OAuth20 server metadata can
be retrieved.
The returned URL is built as specified in RFC8414.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
issuer
|
str
|
an OAuth20 Authentication Server |
required |
Returns:
Type | Description |
---|---|
str
|
the standardised discovery document URL. Note that no attempt to fetch this document is |
str
|
made. |
Source code in requests_oauth2client/discovery.py
oidc_discovery_document_url(issuer)
¶
Construct the OIDC discovery document url for a given issuer
.
Given an issuer
identifier, return the standardised URL where the OIDC discovery document can
be retrieved.
The returned URL is biuilt as specified in OpenID Connect Discovery 1.0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
issuer
|
str
|
an OIDC Authentication Server |
required |
Returns:
Type | Description |
---|---|
str
|
the standardised discovery document URL. Note that no attempt to fetch this document is |
str
|
made. |
Source code in requests_oauth2client/discovery.py
well_known_uri(origin, name, *, at_root=True)
¶
Return the location of a well-known document on an origin url.
See RFC8615 and OIDC Discovery.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
origin
|
str
|
origin to use to build the well-known uri. |
required |
name
|
str
|
document name to use to build the well-known uri. |
required |
at_root
|
bool
|
if |
True
|
Returns:
Type | Description |
---|---|
str
|
the well-know uri, relative to origin, where the well-known document named |
str
|
found. |
Source code in requests_oauth2client/discovery.py
validate_dpop_proof(proof, *, htm, htu, ath=None, nonce=None, leeway=60, alg=None, algs=())
¶
Validate a DPoP proof.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proof
|
str | bytes
|
The serialized DPoP proof. |
required |
htm
|
str
|
The value of the HTTP method of the request to which the JWT is attached. |
required |
htu
|
str
|
The HTTP target URI of the request to which the JWT is attached, without query and fragment parts. |
required |
ath
|
str | None
|
The Hash of the access token. |
None
|
nonce
|
str | None
|
A recent nonce provided via the DPoP-Nonce HTTP header, from either the AS or RS. |
None
|
leeway
|
int
|
A leeway, in number of seconds, to validate the proof |
60
|
alg
|
str | None
|
Allowed signature alg, if there is only one. Use this or |
None
|
algs
|
Sequence[str]
|
Allowed signature algs, if there is several. Use this or |
()
|
Returns:
Type | Description |
---|---|
SignedJwt
|
The validated DPoP proof, as a |
Source code in requests_oauth2client/dpop.py
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 |
|
validate_endpoint_uri(uri, *, https=True, no_credentials=True, no_port=False, no_fragment=True, path=True)
¶
Validate that a URI is suitable as an endpoint URI.
It checks:
- that the scheme is
https
- that no custom port number is being used
- that no username or password are included
- that no fragment is included
- that a path is present
Those checks can be individually disabled by using the parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
str
|
the uri |
required |
https
|
bool
|
if |
True
|
no_port
|
bool
|
if |
False
|
no_credentials
|
bool
|
if |
True
|
no_fragment
|
bool
|
if |
True
|
path
|
bool
|
if |
True
|
Raises:
Type | Description |
---|---|
ValueError
|
if the supplied url is not suitable |
Returns:
Type | Description |
---|---|
str
|
the endpoint URI, if all checks passed |
Source code in requests_oauth2client/utils.py
validate_issuer_uri(uri)
¶
Validate that an Issuer Identifier URI is valid.
This is almost the same as a valid endpoint URI, but a path is not mandatory.
api_client
¶
ApiClient
main module.
InvalidBoolFieldsParam
¶
Bases: ValueError
Raised when an invalid value is passed as 'bool_fields' parameter.
Source code in requests_oauth2client/api_client.py
InvalidPathParam
¶
Bases: TypeError
, ValueError
Raised when an unexpected path is passed as 'url' parameter.
Source code in requests_oauth2client/api_client.py
ApiClient
¶
A Wrapper around requests.Session with extra features for REST API calls.
Additional features compared to using a requests.Session directly:
- You must set a root url at creation time, which then allows passing relative urls at request time.
- It may also raise exceptions instead of returning error responses.
- You can also pass additional kwargs at init time, which will be used to configure the Session, instead of setting them later.
- for parameters passed as
json
,params
ordata
, values that areNone
can be automatically discarded from the request - boolean values in
data
orparams
fields can be serialized to values that are suitable for the target API, like"true"
or"false"
, or"1"
/"0"
, instead of the default values"True"
or"False"
, - you may pass
cookies
andheaders
, which will be added to the session cookie handler or request headers respectively. - you may use the
user_agent
parameter to change theUser-Agent
header easily. Set it toNone
to remove that header.
base_url
will serve as root for relative urls passed to
ApiClient.request(),
ApiClient.get(), etc.
A requests.HTTPError will be raised everytime an API call returns an error code (>= 400), unless
you set raise_for_status
to False
. Additional parameters passed at init time, including
auth
will be used to configure the Session.
Example
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_url
|
str
|
the base api url, that is the root for all the target API endpoints. |
required |
auth
|
AuthBase | None
|
the requests.auth.AuthBase to use as authentication handler. |
None
|
timeout
|
int | None
|
the default timeout, in seconds, to use for each request from this |
60
|
raise_for_status
|
bool
|
if |
True
|
none_fields
|
Literal['include', 'exclude', 'empty']
|
defines what to do with parameters with value
|
'exclude'
|
bool_fields
|
tuple[Any, Any] | None
|
a tuple of |
('true', 'false')
|
cookies
|
Mapping[str, Any] | None
|
a mapping of cookies to set in the underlying |
None
|
headers
|
Mapping[str, Any] | None
|
a mapping of headers to set in the underlying |
None
|
session
|
Session | None
|
a preconfigured |
None
|
**session_kwargs
|
Any
|
additional kwargs to configure the underlying |
{}
|
Raises:
Type | Description |
---|---|
InvalidBoolFieldsParam
|
if the provided |
Source code in requests_oauth2client/api_client.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
|
request(method, path=None, *, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=False, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None, raise_for_status=None, none_fields=None, bool_fields=None)
¶
A wrapper around requests.Session.request method with extra features.
Additional features are described in ApiClient documentation.
All parameters will be passed as-is to requests.Session.request, expected those described below which have a special behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
None | str | bytes | Iterable[str | bytes | int]
|
the url where the request will be sent to. Can be:
|
None
|
raise_for_status
|
bool | None
|
like the parameter of the same name from ApiClient, but this will be applied for this request only. |
None
|
none_fields
|
Literal['include', 'exclude', 'empty'] | None
|
like the parameter of the same name from ApiClient, but this will be applied for this request only. |
None
|
bool_fields
|
tuple[Any, Any] | None
|
like the parameter of the same name from ApiClient, but this will be applied for this request only. |
None
|
Returns:
Type | Description |
---|---|
Response
|
a Response as returned by requests |
Raises:
Type | Description |
---|---|
InvalidBoolFieldsParam
|
if the provided |
Source code in requests_oauth2client/api_client.py
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 |
|
to_absolute_url(path=None)
¶
Convert a relative url to an absolute url.
Given a path
, return the matching absolute url, based on the base_url
that is
configured for this API.
The result of this method is different from a standard urljoin()
, because a relative_url
that starts with a "/" will not override the path from the base url. You can also pass an
iterable of path parts as relative url, which will be properly joined with "/". Those parts
may be str
(which will be urlencoded) or bytes
(which will be decoded as UTF-8 first) or
any other type (which will be converted to str
first, using the str() function
). See the
table below for example results which would exhibit most cases:
base_url | relative_url | result_url |
---|---|---|
"https://myhost.com/root" |
"/path" |
"https://myhost.com/root/path" |
"https://myhost.com/root" |
"/path" |
"https://myhost.com/root/path" |
"https://myhost.com/root" |
b"/path" |
"https://myhost.com/root/path" |
"https://myhost.com/root" |
"path" |
"https://myhost.com/root/path" |
"https://myhost.com/root" |
None |
"https://myhost.com/root" |
"https://myhost.com/root" |
("user", 1, "resource") |
"https://myhost.com/root/user/1/resource" |
"https://myhost.com/root" |
"https://otherhost.org/foo" |
ValueError |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
None | str | bytes | Iterable[str | bytes | int]
|
a relative url |
None
|
Returns:
Type | Description |
---|---|
str
|
the resulting absolute url |
Raises:
Type | Description |
---|---|
InvalidPathParam
|
if the provided path does not allow constructing a valid url |
Source code in requests_oauth2client/api_client.py
get(path=None, raise_for_status=None, **kwargs)
¶
Send a GET request and return a Response object.
The passed url
is relative to the base_url
passed at initialization time.
It takes the same parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
None | str | bytes | Iterable[str | bytes | int]
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
post(path=None, raise_for_status=None, **kwargs)
¶
Send a POST request and return a Response object.
The passed url
is relative to the base_url
passed at initialization time.
It takes the same parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | bytes | Iterable[str | bytes] | None
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
patch(path=None, raise_for_status=None, **kwargs)
¶
Send a PATCH request. Return a Response object.
The passed url
is relative to the base_url
passed at initialization time.
It takes the same parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | bytes | Iterable[str | bytes] | None
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a Response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
put(path=None, raise_for_status=None, **kwargs)
¶
Send a PUT request. Return a Response object.
The passed url
is relative to the base_url
passed at initialization time.
It takes the same parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | bytes | Iterable[str | bytes] | None
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a Response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
delete(path=None, raise_for_status=None, **kwargs)
¶
Send a DELETE request. Return a Response object.
The passed url
may be relative to the url passed at initialization time. It takes the same
parameters as ApiClient.request().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | bytes | Iterable[str | bytes] | None
|
the path where the request will be sent. |
None
|
raise_for_status
|
bool | None
|
overrides the |
None
|
**kwargs
|
Any
|
additional kwargs for |
{}
|
Returns:
Type | Description |
---|---|
Response
|
a response object. |
Raises:
Type | Description |
---|---|
HTTPError
|
if |
Source code in requests_oauth2client/api_client.py
validate_bool_fields(bool_fields)
¶
Validate the bool_fields
parameter.
It must be a sequence of 2 values. First one is the True
value, second one is the False
value.
Both must be str
or string-able values.
Source code in requests_oauth2client/api_client.py
auth
¶
This module contains requests
-compatible Auth Handlers that implement OAuth 2.0.
NonRenewableTokenError
¶
OAuth2AccessTokenAuth
¶
Bases: AuthBase
Authentication Handler for OAuth 2.0 Access Tokens and (optional) Refresh Tokens.
This Requests Auth handler implementation uses an access token as Bearer or DPoP token, and can automatically refresh it when expired, if a refresh token is available.
Token can be a simple str
containing a raw access token value, or a
BearerToken that can contain a refresh_token
.
In addition to adding a properly formatted Authorization
header, this will obtain a new token
once the current token is expired. Expiration is detected based on the expires_in
hint
returned by the AS. A configurable leeway
, in number of seconds, will make sure that a new
token is obtained some seconds before the actual expiration is reached. This may help in
situations where the client, AS and RS have slightly offset clocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the client to use to refresh tokens. |
required |
token
|
str | BearerToken
|
an initial Access Token, if you have one already. In most cases, leave |
required |
leeway
|
int
|
expiration leeway, in number of seconds. |
20
|
**token_kwargs
|
Any
|
additional kwargs to pass to the token endpoint. |
{}
|
Example
Source code in requests_oauth2client/auth.py
renew_token()
¶
Obtain a new Bearer Token.
This will try to use the refresh_token
, if there is one.
Source code in requests_oauth2client/auth.py
OAuth2ClientCredentialsAuth
¶
Bases: OAuth2AccessTokenAuth
An Auth Handler for the Client Credentials grant.
This requests AuthBase automatically gets Access Tokens from an OAuth 2.0 Token Endpoint with the Client Credentials grant, and will get a new one once the current one is expired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the OAuth2Client to use to obtain Access Tokens. |
required |
token
|
str | BearerToken | None
|
an initial Access Token, if you have one already. In most cases, leave |
None
|
leeway
|
int
|
expiration leeway, in number of seconds |
20
|
**token_kwargs
|
Any
|
extra kw parameters to pass to the Token Endpoint. May include |
{}
|
Example
Source code in requests_oauth2client/auth.py
OAuth2AuthorizationCodeAuth
¶
Bases: OAuth2AccessTokenAuth
Authentication handler for the Authorization Code grant.
This Requests Auth handler implementation exchanges an Authorization Code for an access token, then automatically refreshes it once it is expired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the client to use to obtain Access Tokens. |
required |
code
|
str | AuthorizationResponse | None
|
an Authorization Code that has been obtained from the AS. |
required |
token
|
str | BearerToken | None
|
an initial Access Token, if you have one already. In most cases, leave |
None
|
leeway
|
int
|
expiration leeway, in number of seconds. |
20
|
**token_kwargs
|
Any
|
additional kwargs to pass to the token endpoint. |
{}
|
Example
Source code in requests_oauth2client/auth.py
exchange_code_for_token()
¶
Exchange the authorization code for an access token.
OAuth2ResourceOwnerPasswordAuth
¶
Bases: OAuth2AccessTokenAuth
Authentication Handler for the Resource Owner Password Credentials Flow.
This Requests Auth handler implementation exchanges the user credentials for an Access Token, then automatically repeats the process to get a new one once the current one is expired.
Note that this flow is considered deprecated, and the Authorization Code flow should be used whenever possible. Among other bad things, ROPC:
- does not support SSO between multiple apps,
- does not support MFA or risk-based adaptative authentication,
- depends on the user typing its credentials directly inside the application, instead of on a dedicated, centralized login page managed by the AS, which makes it totally insecure for 3rd party apps.
It needs the username and password and an OAuth2Client to be able to get a token from the AS Token Endpoint just before the first request using this Auth Handler is being sent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the client to use to obtain Access Tokens |
required |
username
|
str
|
the username |
required |
password
|
str
|
the user password |
required |
leeway
|
int
|
an amount of time, in seconds |
20
|
token
|
str | BearerToken | None
|
an initial Access Token, if you have one already. In most cases, leave |
None
|
**token_kwargs
|
Any
|
additional kwargs to pass to the token endpoint |
{}
|
Example
Source code in requests_oauth2client/auth.py
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 |
|
renew_token()
¶
Exchange the user credentials for an Access Token.
OAuth2DeviceCodeAuth
¶
Bases: OAuth2AccessTokenAuth
Authentication Handler for the Device Code Flow.
This Requests Auth handler implementation exchanges a Device Code for an Access Token, then automatically refreshes it once it is expired.
It needs a Device Code and an OAuth2Client to be able to get a token from the AS Token Endpoint just before the first request using this Auth Handler is being sent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the OAuth2Client to use to obtain Access Tokens. |
required |
device_code
|
str | DeviceAuthorizationResponse
|
a Device Code obtained from the AS. |
required |
interval
|
int
|
the interval to use to pool the Token Endpoint, in seconds. |
5
|
expires_in
|
int
|
the lifetime of the token, in seconds. |
360
|
token
|
str | BearerToken | None
|
an initial Access Token, if you have one already. In most cases, leave |
None
|
leeway
|
int
|
expiration leeway, in number of seconds. |
20
|
**token_kwargs
|
Any
|
additional kwargs to pass to the token endpoint. |
{}
|
Example
Source code in requests_oauth2client/auth.py
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 |
|
exchange_device_code_for_token()
¶
Exchange the Device Code for an access token.
This will poll the Token Endpoint until the user finishes the authorization process.
Source code in requests_oauth2client/auth.py
authorization_request
¶
Classes and utilities related to Authorization Requests and Responses.
ResponseTypes
¶
Bases: str
, Enum
All standardised response_type
values.
Note that you should always use code
. All other values are deprecated.
Source code in requests_oauth2client/authorization_request.py
CodeChallengeMethods
¶
Bases: str
, Enum
All standardised code_challenge_method
values.
You should always use S256
.
Source code in requests_oauth2client/authorization_request.py
UnsupportedCodeChallengeMethod
¶
InvalidCodeVerifierParam
¶
Bases: ValueError
Raised when an invalid code_verifier is supplied.
Source code in requests_oauth2client/authorization_request.py
PkceUtils
¶
Contains helper methods for PKCE, as described in RFC7636.
See RFC7636.
Source code in requests_oauth2client/authorization_request.py
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 |
|
code_verifier_pattern = re.compile('^[a-zA-Z0-9_\\-~.]{43,128}$')
class-attribute
instance-attribute
¶
A regex that matches valid code verifiers.
generate_code_verifier()
classmethod
¶
Generate a valid code_verifier
.
Returns:
Type | Description |
---|---|
str
|
a |
derive_challenge(verifier, method=CodeChallengeMethods.S256)
classmethod
¶
Derive the code_challenge
from a given code_verifier
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verifier
|
str | bytes
|
a code verifier |
required |
method
|
str
|
the method to use for deriving the challenge. Accepts 'S256' or 'plain'. |
S256
|
Returns:
Type | Description |
---|---|
str
|
a |
Raises:
Type | Description |
---|---|
InvalidCodeVerifierParam
|
if the |
UnsupportedCodeChallengeMethod
|
if the method is not supported |
Source code in requests_oauth2client/authorization_request.py
generate_code_verifier_and_challenge(method=CodeChallengeMethods.S256)
classmethod
¶
Generate a valid code_verifier
and its matching code_challenge
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
the method to use for deriving the challenge. Accepts 'S256' or 'plain'. |
S256
|
Returns:
Type | Description |
---|---|
tuple[str, str]
|
a |
Source code in requests_oauth2client/authorization_request.py
validate_code_verifier(verifier, challenge, method=CodeChallengeMethods.S256)
classmethod
¶
Validate a code_verifier
against a code_challenge
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verifier
|
str
|
the |
required |
challenge
|
str
|
the |
required |
method
|
str
|
the method to use for deriving the challenge. Accepts 'S256' or 'plain'. |
S256
|
Returns:
Type | Description |
---|---|
bool
|
|
Source code in requests_oauth2client/authorization_request.py
UnsupportedResponseTypeParam
¶
Bases: ValueError
Raised when an unsupported response_type is passed as parameter.
Source code in requests_oauth2client/authorization_request.py
MissingIssuerParam
¶
Bases: ValueError
Raised when the 'issuer' parameter is required but not provided.
Source code in requests_oauth2client/authorization_request.py
InvalidMaxAgeParam
¶
Bases: ValueError
Raised when an invalid 'max_age' parameter is provided.
Source code in requests_oauth2client/authorization_request.py
AuthorizationResponse
¶
Represent a successful Authorization Response.
An Authorization Response is the redirection initiated by the AS to the client's redirection
endpoint (redirect_uri), after an Authorization Request.
This Response is typically created with a call to AuthorizationRequest.validate_callback()
once the call to the client Redirection Endpoint is made.
AuthorizationResponse
contains the following attributes:
- all the parameters that have been returned by the AS, most notably the
code
, and optional parameters such asstate
. - the
redirect_uri
that was used for the Authorization Request - the
code_verifier
matching thecode_challenge
that was used for the Authorization Request
Parameters redirect_uri
and code_verifier
must be those from the matching
AuthorizationRequest
. All other parameters including code
and state
must be those
extracted from the Authorization Response parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
The authorization |
required |
redirect_uri
|
str | None
|
The |
None
|
code_verifier
|
str | None
|
the |
None
|
state
|
str | None
|
the |
None
|
nonce
|
str | None
|
the |
None
|
acr_values
|
str | Sequence[str] | None
|
the |
None
|
max_age
|
int | None
|
the |
None
|
issuer
|
str | None
|
the expected |
None
|
dpop_key
|
DPoPKey | None
|
the |
None
|
**kwargs
|
str
|
other parameters as returned by the AS. |
{}
|
Source code in requests_oauth2client/authorization_request.py
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 |
|
AuthorizationRequest
¶
Represent an Authorization Request.
This class makes it easy to generate valid Authorization Request URI (possibly including a state, nonce, PKCE, and custom args), to store all parameters, and to validate an Authorization Response.
All parameters passed at init time will be included in the request query parameters as-is, excepted for a few parameters which have a special behaviour:
state
: if...
(default), a randomstate
parameter will be generated for you. You may pass your ownstate
asstr
, or set it toNone
so that thestate
parameter will not be included in the request. You may access that state in thestate
attribute from this request.nonce
: if...
(default) andscope
includes 'openid', a randomnonce
will be generated and included in the request. You may access thatnonce
in thenonce
attribute from this request.code_verifier
: ifNone
, andcode_challenge_method
is'S256'
or'plain'
, a validcode_challenge
andcode_verifier
for PKCE will be automatically generated, and thecode_challenge
will be included in the request. You may pass your owncode_verifier
as astr
parameter, in which case the appropriatecode_challenge
will be included in the request, according to thecode_challenge_method
.-
authorization_response_iss_parameter_supported
andissuer
: those are used for Server Issuer Identification. By default:- If
ìssuer
is set and an issuer is included in the Authorization Response, then the consistency between those 2 values will be checked when usingvalidate_callback()
. - If issuer is not included in the response, then no issuer check is performed.
Set
authorization_response_iss_parameter_supported
toTrue
to enforce server identification:- an
issuer
must also be provided as parameter, and the AS must return that same value for the response to be considered valid byvalidate_callback()
. - if no issuer is included in the Authorization Response, then an error will be raised.
- If
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_endpoint
|
str
|
the uri for the authorization endpoint. |
required |
client_id
|
str
|
the client_id to include in the request. |
required |
redirect_uri
|
str | None
|
the redirect_uri to include in the request. This is required in OAuth 2.0 and optional
in OAuth 2.1. Pass |
None
|
scope
|
None | str | Iterable[str]
|
the scope to include in the request, as an iterable of |
'openid'
|
response_type
|
str
|
the response type to include in the request. |
CODE
|
state
|
str | ellipsis | None
|
the state to include in the request, or |
...
|
nonce
|
str | ellipsis | None
|
the nonce to include in the request, or |
...
|
code_verifier
|
str | None
|
the code verifier to include in the request.
If left as |
None
|
code_challenge_method
|
str | None
|
the method to use to derive the |
S256
|
acr_values
|
str | Iterable[str] | None
|
requested Authentication Context Class Reference values. |
None
|
issuer
|
str | None
|
Issuer Identifier value from the OAuth/OIDC Server, if using Server Issuer Identification. |
None
|
**kwargs
|
Any
|
extra parameters to include in the request, as-is. |
{}
|
Example
Raises:
Type | Description |
---|---|
InvalidMaxAgeParam
|
if the |
MissingIssuerParam
|
if |
UnsupportedResponseTypeParam
|
if |
Source code in requests_oauth2client/authorization_request.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 |
|
code_challenge
cached
property
¶
The code_challenge
that matches code_verifier
and code_challenge_method
.
dpop_jkt
cached
property
¶
The DPoP JWK thumbprint that matches `dpop_key
.
args
property
¶
Return a dict with all the query parameters from this AuthorizationRequest.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
a dict of parameters |
furl
property
¶
Return the Authorization Request URI, as a furl
.
uri
property
¶
Return the Authorization Request URI, as a str
.
generate_state()
classmethod
¶
generate_nonce()
classmethod
¶
as_dict()
¶
Return the full argument dict.
This can be used to serialize this request and/or to initialize a similar request.
Source code in requests_oauth2client/authorization_request.py
validate_callback(response)
¶
Validate an Authorization Response against this Request.
Validate a given Authorization Response URI against this Authorization Request, and return an AuthorizationResponse.
This includes matching the state
parameter, checking for returned errors, and extracting
the returned code
and other parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
str
|
the Authorization Response URI. This can be the full URL, or just the query parameters (still encoded as x-www-form-urlencoded). |
required |
Returns:
Type | Description |
---|---|
AuthorizationResponse
|
the extracted code, if all checks are successful |
Raises:
Type | Description |
---|---|
MissingAuthCode
|
if the |
MissingIssuer
|
if Server Issuer verification is active and the response does
not contain an |
MismatchingIssuer
|
if the 'iss' received from the response does not match the expected value. |
MismatchingState
|
if the response |
OAuth2Error
|
if the response includes an error. |
MissingAuthCode
|
if the response does not contain a |
UnsupportedResponseTypeParam
|
if response_type anything else than 'code'. |
Source code in requests_oauth2client/authorization_request.py
sign_request_jwt(jwk, alg=None, lifetime=None)
¶
Sign the request
object that matches this Authorization Request parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwk
|
Jwk | dict[str, Any]
|
the JWK to use to sign the request |
required |
alg
|
str | None
|
the alg to use to sign the request, if the provided |
None
|
lifetime
|
int | None
|
an optional number of seconds of validity for the signed request.
If present, |
None
|
Returns:
Type | Description |
---|---|
SignedJwt
|
a |
Source code in requests_oauth2client/authorization_request.py
sign(jwk, alg=None, lifetime=None, **kwargs)
¶
Sign this Authorization Request and return a new one.
This replaces all parameters with a signed request
JWT.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jwk
|
Jwk | dict[str, Any]
|
the JWK to use to sign the request |
required |
alg
|
str | None
|
the alg to use to sign the request, if the provided |
None
|
lifetime
|
int | None
|
lifetime of the resulting Jwt (used to calculate the 'exp' claim). By default, don't use an 'exp' claim. |
None
|
kwargs
|
Any
|
additional query parameters to include in the signed authorization request |
{}
|
Returns:
Type | Description |
---|---|
RequestParameterAuthorizationRequest
|
the signed Authorization Request |
Source code in requests_oauth2client/authorization_request.py
sign_and_encrypt_request_jwt(sign_jwk, enc_jwk, sign_alg=None, enc_alg=None, enc='A128CBC-HS256', lifetime=None)
¶
Sign and encrypt a request
object for this Authorization Request.
The signed request
will contain the same parameters as this AuthorizationRequest.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sign_jwk
|
Jwk | dict[str, Any]
|
the JWK to use to sign the request |
required |
enc_jwk
|
Jwk | dict[str, Any]
|
the JWK to use to encrypt the request |
required |
sign_alg
|
str | None
|
the alg to use to sign the request, if |
None
|
enc_alg
|
str | None
|
the alg to use to encrypt the request, if |
None
|
enc
|
str
|
the encoding to use to encrypt the request. |
'A128CBC-HS256'
|
lifetime
|
int | None
|
lifetime of the resulting Jwt (used to calculate the 'exp' claim). By default, do not include an 'exp' claim. |
None
|
Returns:
Type | Description |
---|---|
JweCompact
|
the signed and encrypted request object, as a |
Source code in requests_oauth2client/authorization_request.py
sign_and_encrypt(sign_jwk, enc_jwk, sign_alg=None, enc_alg=None, enc='A128CBC-HS256', lifetime=None)
¶
Sign and encrypt the current Authorization Request.
This replaces all parameters with a matching request
object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sign_jwk
|
Jwk | dict[str, Any]
|
the JWK to use to sign the request |
required |
enc_jwk
|
Jwk | dict[str, Any]
|
the JWK to use to encrypt the request |
required |
sign_alg
|
str | None
|
the alg to use to sign the request, if |
None
|
enc_alg
|
str | None
|
the alg to use to encrypt the request, if |
None
|
enc
|
str
|
the encoding to use to encrypt the request. |
'A128CBC-HS256'
|
lifetime
|
int | None
|
lifetime of the resulting Jwt (used to calculate the 'exp' claim). By default, do not include an 'exp' claim. |
None
|
Returns:
Type | Description |
---|---|
RequestParameterAuthorizationRequest
|
a |
Source code in requests_oauth2client/authorization_request.py
on_response_error(response)
¶
Error handler for Authorization Response errors.
Triggered by validate_callback() if the response uri contains an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
str
|
the Authorization Response URI. This can be the full URL, or just the query parameters. |
required |
Returns:
Type | Description |
---|---|
AuthorizationResponse
|
may return a default code that will be returned by |
AuthorizationResponse
|
will most likely raise exceptions instead. |
Raises:
Type | Description |
---|---|
AuthorizationResponseError
|
if the response contains an |
Source code in requests_oauth2client/authorization_request.py
RequestParameterAuthorizationRequest
¶
Represent an Authorization Request that includes a request
JWT.
To construct such a request yourself, the easiest way is to initialize
an AuthorizationRequest
then sign it with
AuthorizationRequest.sign()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_endpoint
|
str
|
the Authorization Endpoint uri |
required |
client_id
|
str
|
the client_id |
required |
request
|
Jwt | str
|
the request JWT |
required |
expires_at
|
datetime | None
|
the expiration date for this request |
None
|
kwargs
|
Any
|
extra parameters to include in the request |
{}
|
Source code in requests_oauth2client/authorization_request.py
RequestUriParameterAuthorizationRequest
¶
Represent an Authorization Request that includes a request_uri
parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_endpoint
|
str
|
The Authorization Endpoint uri. |
required |
client_id
|
str
|
The Client ID. |
required |
request_uri
|
str
|
The |
required |
expires_at
|
datetime | None
|
The expiration date for this request. |
None
|
kwargs
|
Any
|
Extra query parameters to include in the request. |
{}
|
Source code in requests_oauth2client/authorization_request.py
AuthorizationRequestSerializer
¶
(De)Serializer for AuthorizationRequest
instances.
You might need to store pending authorization requests in session, either server-side or client- side. This class is here to help you do that.
Source code in requests_oauth2client/authorization_request.py
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 |
|
default_dumper(azr)
staticmethod
¶
Provide a default dumper implementation.
Serialize an AuthorizationRequest as JSON, then compress with deflate, then encodes as base64url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
azr
|
AuthorizationRequest
|
the |
required |
Returns:
Type | Description |
---|---|
str
|
the serialized value |
Source code in requests_oauth2client/authorization_request.py
default_loader(serialized, azr_class=AuthorizationRequest)
staticmethod
¶
Provide a default deserializer implementation.
This does the opposite operations than default_dumper
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized
|
str
|
the serialized AuthorizationRequest |
required |
azr_class
|
type[AuthorizationRequest]
|
the class to deserialize the Authorization Request to |
AuthorizationRequest
|
Returns:
Type | Description |
---|---|
AuthorizationRequest
|
an AuthorizationRequest |
Source code in requests_oauth2client/authorization_request.py
dumps(azr)
¶
Serialize and compress a given AuthorizationRequest for easier storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
azr
|
AuthorizationRequest
|
an AuthorizationRequest to serialize |
required |
Returns:
Type | Description |
---|---|
str
|
the serialized AuthorizationRequest, as a str |
Source code in requests_oauth2client/authorization_request.py
loads(serialized)
¶
Deserialize a serialized AuthorizationRequest.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized
|
str
|
the serialized AuthorizationRequest |
required |
Returns:
Type | Description |
---|---|
AuthorizationRequest
|
the deserialized AuthorizationRequest |
Source code in requests_oauth2client/authorization_request.py
backchannel_authentication
¶
Implementation of CIBA.
CIBA stands for Client Initiated BackChannel Authentication and is standardised by the OpenID Fundation. https://openid.net/specs/openid-client-initiated-backchannel- authentication-core-1_0.html.
BackChannelAuthenticationResponse
¶
Represent a BackChannel Authentication Response.
This contains all the parameters that are returned by the AS as a result of a BackChannel
Authentication Request, such as auth_req_id
(required), and the optional expires_at
,
interval
, and/or any custom parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth_req_id
|
str
|
the |
required |
expires_at
|
datetime | None
|
the date when the |
None
|
interval
|
int | None
|
the Token Endpoint pooling interval, in seconds, as returned by the AS. |
20
|
**kwargs
|
Any
|
any additional custom parameters as returned by the AS. |
{}
|
Source code in requests_oauth2client/backchannel_authentication.py
expires_in
property
¶
Number of seconds until expiration.
is_expired(leeway=0)
¶
Return True
if the auth_req_id
within this response is expired.
Expiration is evaluated at the time of the call. If there is no "expires_at" hint (which is
derived from the expires_in
hint returned by the AS BackChannel Authentication endpoint),
this will return None
.
Returns:
Type | Description |
---|---|
bool | None
|
|
bool | None
|
no |
Source code in requests_oauth2client/backchannel_authentication.py
BackChannelAuthenticationPoolingJob
¶
Bases: BaseTokenEndpointPoolingJob
A pooling job for the BackChannel Authentication flow.
This will poll the Token Endpoint until the user finishes with its authentication.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
an OAuth2Client that will be used to pool the token endpoint. |
required |
auth_req_id
|
str | BackChannelAuthenticationResponse
|
an |
required |
interval
|
int | None
|
The pooling interval, in seconds, to use. This overrides
the one in |
None
|
slow_down_interval
|
int
|
Number of seconds to add to the pooling interval when the AS returns a slow down request. |
5
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the underlying calls to requests.request. |
None
|
**token_kwargs
|
Any
|
Additional parameters for the token request. |
{}
|
Example
Source code in requests_oauth2client/backchannel_authentication.py
token_request()
¶
Implement the CIBA token request.
This actually calls [OAuth2Client.ciba(auth_req_id)] on client
.
Returns:
Type | Description |
---|---|
BearerToken
|
Source code in requests_oauth2client/backchannel_authentication.py
client
¶
This module contains the OAuth2Client
class.
InvalidParam
¶
MissingIdTokenEncryptedResponseAlgParam
¶
Bases: InvalidParam
Raised when an ID Token encryption is required but not provided.
Source code in requests_oauth2client/client.py
InvalidEndpointUri
¶
Bases: InvalidParam
Raised when an invalid endpoint uri is provided.
Source code in requests_oauth2client/client.py
InvalidIssuer
¶
Bases: InvalidEndpointUri
Raised when an invalid issuer parameter is provided.
Source code in requests_oauth2client/client.py
InvalidScopeParam
¶
Bases: InvalidParam
Raised when an invalid scope parameter is provided.
Source code in requests_oauth2client/client.py
MissingRefreshToken
¶
Bases: ValueError
Raised when a refresh token is required but not present.
Source code in requests_oauth2client/client.py
MissingDeviceCode
¶
Bases: ValueError
Raised when a device_code is required but not provided.
Source code in requests_oauth2client/client.py
MissingAuthRequestId
¶
Bases: ValueError
Raised when an 'auth_req_id' is missing in a BackChannelAuthenticationResponse.
Source code in requests_oauth2client/client.py
UnknownTokenType
¶
Bases: InvalidParam
, TypeError
Raised when the type of a token cannot be determined automatically.
Source code in requests_oauth2client/client.py
UnknownSubjectTokenType
¶
Bases: UnknownTokenType
Raised when the type of subject_token cannot be determined automatically.
Source code in requests_oauth2client/client.py
UnknownActorTokenType
¶
Bases: UnknownTokenType
Raised when the type of actor_token cannot be determined automatically.
Source code in requests_oauth2client/client.py
InvalidBackchannelAuthenticationRequestHintParam
¶
Bases: InvalidParam
Raised when an invalid hint is provided in a backchannel authentication request.
InvalidAcrValuesParam
¶
Bases: InvalidParam
Raised when an invalid 'acr_values' parameter is provided.
Source code in requests_oauth2client/client.py
InvalidDiscoveryDocument
¶
Bases: ValueError
Raised when handling an invalid Discovery Document.
Source code in requests_oauth2client/client.py
Endpoints
¶
Bases: str
, Enum
All standardised OAuth 2.0 and extensions endpoints.
If an endpoint is not mentioned here, then its usage is not supported by OAuth2Client.
Source code in requests_oauth2client/client.py
MissingEndpointUri
¶
Bases: AttributeError
Raised when a required endpoint uri is not known.
Source code in requests_oauth2client/client.py
GrantTypes
¶
Bases: str
, Enum
An enum of standardized grant_type
values.
Source code in requests_oauth2client/client.py
OAuth2Client
¶
An OAuth 2.x Client that can send requests to an OAuth 2.x Authorization Server.
OAuth2Client
is able to obtain tokens from the Token Endpoint using any of the standardised
Grant Types, and to communicate with the various backend endpoints like the Revocation,
Introspection, and UserInfo Endpoint.
To init an OAuth2Client, you only need the url to the Token Endpoint and the Credentials (a client_id and one of a secret or private_key) that will be used to authenticate to that endpoint. Other endpoint urls, such as the Authorization Endpoint, Revocation Endpoint, etc. can be passed as parameter as well if you intend to use them.
This class is not intended to help with the end-user authentication or any request that goes in
a browser. For authentication requests, see
AuthorizationRequest. You
may use the method authorization_request()
to generate AuthorizationRequest
s with the
preconfigured authorization_endpoint
, client_id
and `redirect_uri' from this client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_endpoint
|
str
|
the Token Endpoint URI where this client will get access tokens |
required |
auth
|
AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
|
the authentication handler to use for client authentication on the token endpoint. Can be:
|
None
|
client_id
|
str | None
|
client ID (use either this or |
None
|
client_secret
|
str | None
|
client secret (use either this or |
None
|
private_key
|
Jwk | dict[str, Any] | None
|
private_key to use for client authentication (use either this or |
None
|
revocation_endpoint
|
str | None
|
the Revocation Endpoint URI to use for revoking tokens |
None
|
introspection_endpoint
|
str | None
|
the Introspection Endpoint URI to use to get info about tokens |
None
|
userinfo_endpoint
|
str | None
|
the Userinfo Endpoint URI to use to get information about the user |
None
|
authorization_endpoint
|
str | None
|
the Authorization Endpoint URI, used for initializing Authorization Requests |
None
|
redirect_uri
|
str | None
|
the redirect_uri for this client |
None
|
backchannel_authentication_endpoint
|
str | None
|
the BackChannel Authentication URI |
None
|
device_authorization_endpoint
|
str | None
|
the Device Authorization Endpoint URI to use to authorize devices |
None
|
jwks_uri
|
str | None
|
the JWKS URI to use to obtain the AS public keys |
None
|
code_challenge_method
|
str
|
challenge method to use for PKCE (should always be 'S256') |
S256
|
session
|
Session | None
|
a requests Session to use when sending HTTP requests. Useful if some extra parameters such as proxy or client certificate must be used to connect to the AS. |
None
|
token_class
|
type[BearerToken]
|
a custom BearerToken class, if required |
BearerToken
|
dpop_bound_access_tokens
|
bool
|
if |
False
|
dpop_key_generator
|
Callable[[str], DPoPKey]
|
a callable that generates a DPoPKey, for whill be called when doing a token request with DPoP enabled. |
generate
|
testing
|
bool
|
if |
False
|
**extra_metadata
|
Any
|
additional metadata for this client, unused by this class, but may be
used by subclasses. Those will be accessible with the |
{}
|
Example
Raises:
Type | Description |
---|---|
MissingIDTokenEncryptedResponseAlgParam
|
if an
|
MissingIssuerParam
|
if |
InvalidEndpointUri
|
if a provided endpoint uri is not considered valid. For the rare cases
where those checks must be disabled, you can use |
InvalidIssuer
|
if the |
Source code in requests_oauth2client/client.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 |
|
client_id
property
¶
Client ID.
client_secret
property
¶
Client Secret.
client_jwks
property
¶
A JwkSet
containing the public keys for this client.
Keys are:
- the public key for client assertion signature verification (if using private_key_jwt)
- the ID Token encryption key
validate_endpoint_uri(attribute, uri)
¶
Validate that an endpoint URI is suitable for use.
If you need to disable some checks (for AS testing purposes only!), provide a different method here.
Source code in requests_oauth2client/client.py
validate_issuer_uri(attribute, uri)
¶
Validate that an Issuer identifier is suitable for use.
This is the same check as an endpoint URI, but the path may be (and usually is) empty.
Source code in requests_oauth2client/client.py
token_request(data, *, timeout=10, dpop=None, dpop_key=None, **requests_kwargs)
¶
Send a request to the token endpoint.
Authentication will be added automatically based on the defined auth
for this client.
Parameters:
Name | Type | Description | Default | ||
---|---|---|---|---|---|
data
|
dict[str, Any]
|
parameters to send to the token endpoint. Items with a |
required | ||
timeout
|
int
|
a timeout value for the call |
10
|
||
dpop
|
bool | None
|
toggles DPoP-proofing for the token request:
|
None
|
||
dpop_key
|
DPoPKey | None
|
a chosen |
None
|
||
**requests_kwargs
|
Any
|
additional parameters for requests.post() |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
the token endpoint response |
Source code in requests_oauth2client/client.py
parse_token_response(response, *, dpop_key=None)
¶
Parse a Response returned by the Token Endpoint.
Invoked by token_request to parse
responses returned by the Token Endpoint. Those responses contain an access_token
and
additional attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
BearerToken
|
a |
Source code in requests_oauth2client/client.py
on_token_error(response, *, dpop_key=None)
¶
Error handler for token_request()
.
Invoked by token_request when the Token Endpoint returns an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the |
required |
dpop_key
|
DPoPKey | None
|
the DPoPKey that was used to proof the token request, if any. |
None
|
Returns:
Type | Description |
---|---|
BearerToken
|
nothing, and raises an exception instead. But a subclass may return a |
BearerToken
|
|
Raises:
Type | Description |
---|---|
InvalidTokenResponse
|
if the error response does not contain an OAuth 2.0 standard error response. |
Source code in requests_oauth2client/client.py
client_credentials(scope=None, *, dpop=None, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request to the token endpoint using the client_credentials
grant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
str | Iterable[str] | None
|
the scope to send with the request. Can be a str, or an iterable of str.
to pass that way include |
None
|
dpop
|
bool | None
|
toggles DPoP-proofing for the token request:
|
None
|
dpop_key
|
DPoPKey | None
|
a chosen |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for the call to requests |
None
|
**token_kwargs
|
Any
|
additional parameters that will be added in the form data for the token endpoint,
alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
a |
Raises:
Type | Description |
---|---|
InvalidScopeParam
|
if the |
Source code in requests_oauth2client/client.py
authorization_code(code, *, validate=True, dpop=False, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request to the token endpoint with the authorization_code
grant.
You can either pass an authorization code, as a str
, or pass an AuthorizationResponse
instance as
returned by AuthorizationRequest.validate_callback()
(recommended). If you do the latter, this will
automatically:
- add the appropriate
redirect_uri
value that was initially passed in the Authorization Request parameters. This is no longer mandatory in OAuth 2.1, but a lot of Authorization Servers are still expecting it since it was part of the OAuth 2.0 specifications. - add the appropriate
code_verifier
for PKCE that was generated before sending the AuthorizationRequest. - handle DPoP binding based on the same
DPoPKey
that was used to initialize theAuthenticationRequest
and whose JWK thumbprint was passed asdpop_jkt
parameter in the Auth Request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str | AuthorizationResponse
|
An authorization code or an |
required |
validate
|
bool
|
If |
True
|
dpop
|
bool
|
Toggles DPoP binding for the Access Token, even if Authorization Code DPoP binding was not initially done. |
False
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the call to the underlying HTTP |
None
|
**token_kwargs
|
Any
|
Additional parameters that will be added in the form data for the token endpoint,
alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint Response. |
Source code in requests_oauth2client/client.py
refresh_token(refresh_token, requests_kwargs=None, **token_kwargs)
¶
Send a request to the token endpoint with the refresh_token
grant.
If refresh_token
is a DPoPToken
instance, (which means that DPoP was used to obtain the initial
Access/Refresh Tokens), then the same DPoP key will be used to DPoP proof the refresh token request,
as defined in RFC9449.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
refresh_token
|
str | BearerToken
|
A refresh_token, as a string, or as a |
required |
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the call to |
None
|
**token_kwargs
|
Any
|
Additional parameters for the token endpoint,
alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The token endpoint response. |
Raises:
Type | Description |
---|---|
MissingRefreshToken
|
If |
Source code in requests_oauth2client/client.py
device_code(device_code, *, dpop=False, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request to the token endpoint using the Device Code grant.
The grant_type is urn:ietf:params:oauth:grant-type:device_code
. This needs a Device Code,
or a DeviceAuthorizationResponse
as parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device_code
|
str | DeviceAuthorizationResponse
|
A device code, or a |
required |
dpop
|
bool
|
Toggles DPoP Binding. If |
False
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the call to requests. |
None
|
**token_kwargs
|
Any
|
Additional parameters for the token endpoint, alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Raises:
Type | Description |
---|---|
MissingDeviceCode
|
if |
Source code in requests_oauth2client/client.py
ciba(auth_req_id, requests_kwargs=None, **token_kwargs)
¶
Send a CIBA request to the Token Endpoint.
A CIBA request is a Token Request using the urn:openid:params:grant-type:ciba
grant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth_req_id
|
str | BackChannelAuthenticationResponse
|
an authentication request ID, as returned by the AS |
required |
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for the call to requests |
None
|
**token_kwargs
|
Any
|
additional parameters for the token endpoint, alongside |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Raises:
Type | Description |
---|---|
MissingAuthRequestId
|
if |
Source code in requests_oauth2client/client.py
token_exchange(*, subject_token, subject_token_type=None, actor_token=None, actor_token_type=None, requested_token_type=None, dpop=False, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a Token Exchange request.
A Token Exchange request is actually a request to the Token Endpoint with a grant_type
urn:ietf:params:oauth:grant-type:token-exchange
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
subject_token
|
str | BearerToken | IdToken
|
The subject token to exchange for a new token. |
required |
subject_token_type
|
str | None
|
A token type identifier for the subject_token, mandatory if it cannot be guessed based
on |
None
|
actor_token
|
None | str | BearerToken | IdToken
|
The actor token to include in the request, if any. |
None
|
actor_token_type
|
str | None
|
A token type identifier for the actor_token, mandatory if it cannot be guessed based
on |
None
|
requested_token_type
|
str | None
|
A token type identifier for the requested token. |
None
|
dpop
|
bool
|
Toggles DPoP Binding. If |
False
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters to pass to the underlying |
None
|
**token_kwargs
|
Any
|
Additional parameters to include in the request body. |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Raises:
Type | Description |
---|---|
UnknownSubjectTokenType
|
If the type of |
UnknownActorTokenType
|
If the type of |
Source code in requests_oauth2client/client.py
jwt_bearer(assertion, *, dpop=False, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request using a JWT as authorization grant.
This is defined in (RFC7523 $2.1)[https://www.rfc-editor.org/rfc/rfc7523.html#section-2.1).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
assertion
|
Jwt | str
|
A JWT (as an instance of |
required |
dpop
|
bool
|
Toggles DPoP Binding. If |
False
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters to pass to the underlying |
None
|
**token_kwargs
|
Any
|
Additional parameters to include in the request body. |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Source code in requests_oauth2client/client.py
resource_owner_password(username, password, *, dpop=None, dpop_key=None, requests_kwargs=None, **token_kwargs)
¶
Send a request using the Resource Owner Password Grant.
This Grant Type is deprecated and should only be used when there is no other choice.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
the resource owner user name |
required |
password
|
str
|
the resource owner password |
required |
dpop
|
bool | None
|
Toggles DPoP Binding. If |
None
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters to pass to the underlying |
None
|
**token_kwargs
|
Any
|
additional parameters to include in the request body. |
{}
|
Returns:
Type | Description |
---|---|
BearerToken
|
The Token Endpoint response. |
Source code in requests_oauth2client/client.py
authorization_request(*, scope='openid', response_type=ResponseTypes.CODE, redirect_uri=None, state=..., nonce=..., code_verifier=None, dpop=None, dpop_key=None, dpop_alg=None, **kwargs)
¶
Generate an Authorization Request for this client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
None | str | Iterable[str]
|
The |
'openid'
|
response_type
|
str
|
The |
CODE
|
redirect_uri
|
str | None
|
The |
None
|
state
|
str | ellipsis | None
|
The |
...
|
nonce
|
str | ellipsis | None
|
A |
...
|
dpop
|
bool | None
|
Toggles DPoP binding.
- if |
None
|
dpop_key
|
DPoPKey | None
|
A chosen DPoP key. Leave |
None
|
dpop_alg
|
str | None
|
A signature alg to sign the DPoP proof. If |
None
|
code_verifier
|
str | None
|
The PKCE |
None
|
**kwargs
|
Any
|
Additional query parameters to include in the auth request. |
{}
|
Returns:
Type | Description |
---|---|
AuthorizationRequest
|
The Token Endpoint response. |
Source code in requests_oauth2client/client.py
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 |
|
pushed_authorization_request(authorization_request, requests_kwargs=None)
¶
Send a Pushed Authorization Request.
This sends a request to the Pushed Authorization Request Endpoint, and returns a
RequestUriParameterAuthorizationRequest
initialized with the AS response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
authorization_request
|
AuthorizationRequest
|
The authorization request to send. |
required |
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for |
None
|
Returns:
Type | Description |
---|---|
RequestUriParameterAuthorizationRequest
|
The |
Source code in requests_oauth2client/client.py
parse_pushed_authorization_response(response, *, dpop_key=None)
¶
Parse the response obtained by pushed_authorization_request()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
The |
required |
dpop_key
|
DPoPKey | None
|
The |
None
|
Returns:
Type | Description |
---|---|
RequestUriParameterAuthorizationRequest
|
A |
Source code in requests_oauth2client/client.py
on_pushed_authorization_request_error(response, *, dpop_key=None)
¶
Error Handler for Pushed Authorization Endpoint errors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
The HTTP response as returned by the AS PAR endpoint. |
required |
dpop_key
|
DPoPKey | None
|
The |
None
|
Returns:
Type | Description |
---|---|
RequestUriParameterAuthorizationRequest
|
Should not return anything, but raise an Exception instead. A |
RequestUriParameterAuthorizationRequest
|
may be returned by subclasses for testing purposes. |
Raises:
Type | Description |
---|---|
EndpointError
|
A subclass of this error depending on the error returned by the AS. |
InvalidPushedAuthorizationResponse
|
If the returned response is not following the specifications. |
UnknownTokenEndpointError
|
For unknown/unhandled errors. |
Source code in requests_oauth2client/client.py
userinfo(access_token)
¶
Call the UserInfo endpoint.
This sends a request to the UserInfo endpoint, with the specified access_token, and returns the parsed result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token
|
BearerToken | str
|
the access token to use |
required |
Returns:
Type | Description |
---|---|
Any
|
the Response returned by the userinfo endpoint. |
Source code in requests_oauth2client/client.py
parse_userinfo_response(resp, *, dpop_key=None)
¶
Parse the response obtained by userinfo()
.
Invoked by userinfo() to parse the response from the UserInfo endpoint, this will extract and return its JSON content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resp
|
Response
|
a Response returned from the UserInfo endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
Any
|
the parsed JSON content from this response. |
Source code in requests_oauth2client/client.py
on_userinfo_error(resp, *, dpop_key=None)
¶
Parse UserInfo error response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
resp
|
Response
|
a Response returned from the UserInfo endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
Any
|
nothing, raises exception instead. |
Source code in requests_oauth2client/client.py
get_token_type(token_type=None, token=None)
classmethod
¶
Get standardized token type identifiers.
Return a standardized token type identifier, based on a short token_type
hint and/or a
token value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_type
|
str | None
|
a token_type hint, as |
None
|
token
|
None | str | BearerToken | IdToken
|
a token value, as an instance of |
None
|
Returns:
Type | Description |
---|---|
str
|
the token_type as defined in the Token Exchange RFC8693. |
Raises:
Type | Description |
---|---|
UnknownTokenType
|
if the type of token cannot be determined |
Source code in requests_oauth2client/client.py
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 |
|
revoke_access_token(access_token, requests_kwargs=None, **revoke_kwargs)
¶
Send a request to the Revocation Endpoint to revoke an access token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token
|
BearerToken | str
|
the access token to revoke |
required |
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for the underlying requests.post() call |
None
|
**revoke_kwargs
|
Any
|
additional parameters to pass to the revocation endpoint |
{}
|
Source code in requests_oauth2client/client.py
revoke_refresh_token(refresh_token, requests_kwargs=None, **revoke_kwargs)
¶
Send a request to the Revocation Endpoint to revoke a refresh token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
refresh_token
|
str | BearerToken
|
the refresh token to revoke. |
required |
requests_kwargs
|
dict[str, Any] | None
|
additional parameters to pass to the revocation endpoint. |
None
|
**revoke_kwargs
|
Any
|
additional parameters to pass to the revocation endpoint. |
{}
|
Returns:
Type | Description |
---|---|
bool
|
|
bool
|
revocation endpoint. |
Raises:
Type | Description |
---|---|
MissingRefreshToken
|
when |
Source code in requests_oauth2client/client.py
revoke_token(token, token_type_hint=None, requests_kwargs=None, **revoke_kwargs)
¶
Send a Token Revocation request.
By default, authentication will be the same than the one used for the Token Endpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str | BearerToken
|
the token to revoke. |
required |
token_type_hint
|
str | None
|
a token_type_hint to send to the revocation endpoint. |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters to the underling call to requests.post() |
None
|
**revoke_kwargs
|
Any
|
additional parameters to send to the revocation endpoint. |
{}
|
Returns:
Type | Description |
---|---|
bool
|
the result from |
Raises:
Type | Description |
---|---|
MissingEndpointUri
|
if the Revocation Endpoint URI is not configured. |
MissingRefreshToken
|
if |
Source code in requests_oauth2client/client.py
parse_revocation_response(response, *, dpop_key=None)
¶
Parse reponses from the Revocation Endpoint.
Since those do not return any meaningful information in a standardised fashion, this just returns True
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
bool
|
non-standardised error is returned. |
Source code in requests_oauth2client/client.py
on_revocation_error(response, *, dpop_key=None)
¶
Error handler for revoke_token()
.
Invoked by revoke_token() when the revocation endpoint returns an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
bool
|
|
bool
|
revocation response. |
Raises:
Type | Description |
---|---|
EndpointError
|
if the response contains a standardised OAuth 2.0 error. |
Source code in requests_oauth2client/client.py
introspect_token(token, token_type_hint=None, requests_kwargs=None, **introspect_kwargs)
¶
Send a request to the Introspection Endpoint.
Parameter token
can be:
- a
str
- a
BearerToken
instance
You may pass any arbitrary token
and token_type_hint
values as str
. Those will
be included in the request, as-is.
If token
is a BearerToken
, then token_type_hint
must be either:
None
: the access_token will be instrospected and no token_type_hint will be included in the requestaccess_token
: same asNone
, but the token_type_hint will be included- or
refresh_token
: only available if a Refresh Token is present in the BearerToken.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str | BearerToken
|
the token to instrospect |
required |
token_type_hint
|
str | None
|
the |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters to the underling call to requests.post() |
None
|
**introspect_kwargs
|
Any
|
additional parameters to send to the introspection endpoint. |
{}
|
Returns:
Type | Description |
---|---|
Any
|
the response as returned by the Introspection Endpoint. |
Raises:
Type | Description |
---|---|
MissingRefreshToken
|
if |
UnknownTokenType
|
if |
Source code in requests_oauth2client/client.py
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 |
|
parse_introspection_response(response, *, dpop_key=None)
¶
Parse Token Introspection Responses received by introspect_token()
.
Invoked by introspect_token() to parse the returned response. This decodes the JSON content if possible, otherwise it returns the response as a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the Response as returned by the Introspection Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
Any
|
the decoded JSON content, or a |
Source code in requests_oauth2client/client.py
on_introspection_error(response, *, dpop_key=None)
¶
Error handler for introspect_token()
.
Invoked by introspect_token() to parse the returned response in the case an error is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response as returned by the Introspection Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
Any
|
usually raises exceptions. A subclass can return a default response instead. |
Raises:
Type | Description |
---|---|
EndpointError
|
(or one of its subclasses) if the response contains a standard OAuth 2.0 error. |
UnknownIntrospectionError
|
if the response is not a standard error response. |
Source code in requests_oauth2client/client.py
backchannel_authentication_request(scope='openid', *, client_notification_token=None, acr_values=None, login_hint_token=None, id_token_hint=None, login_hint=None, binding_message=None, user_code=None, requested_expiry=None, private_jwk=None, alg=None, requests_kwargs=None, **ciba_kwargs)
¶
Send a CIBA Authentication Request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
None | str | Iterable[str]
|
the scope to include in the request. |
'openid'
|
client_notification_token
|
str | None
|
the Client Notification Token to include in the request. |
None
|
acr_values
|
None | str | Iterable[str]
|
the acr values to include in the request. |
None
|
login_hint_token
|
str | None
|
the Login Hint Token to include in the request. |
None
|
id_token_hint
|
str | None
|
the ID Token Hint to include in the request. |
None
|
login_hint
|
str | None
|
the Login Hint to include in the request. |
None
|
binding_message
|
str | None
|
the Binding Message to include in the request. |
None
|
user_code
|
str | None
|
the User Code to include in the request |
None
|
requested_expiry
|
int | None
|
the Requested Expiry, in seconds, to include in the request. |
None
|
private_jwk
|
Jwk | dict[str, Any] | None
|
the JWK to use to sign the request (optional) |
None
|
alg
|
str | None
|
the alg to use to sign the request, if the provided JWK does not include an "alg" parameter. |
None
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for |
None
|
**ciba_kwargs
|
Any
|
additional parameters to include in the request. |
{}
|
Returns:
Type | Description |
---|---|
BackChannelAuthenticationResponse
|
a BackChannelAuthenticationResponse as returned by AS |
Raises:
Type | Description |
---|---|
InvalidBackchannelAuthenticationRequestHintParam
|
if none of |
InvalidScopeParam
|
if the |
InvalidAcrValuesParam
|
if the |
Source code in requests_oauth2client/client.py
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 |
|
parse_backchannel_authentication_response(response, *, dpop_key=None)
¶
Parse a response received by backchannel_authentication_request()
.
Invoked by backchannel_authentication_request() to parse the response returned by the BackChannel Authentication Endpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response returned by the BackChannel Authentication Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
BackChannelAuthenticationResponse
|
a |
Raises:
Type | Description |
---|---|
InvalidBackChannelAuthenticationResponse
|
if the response does not contain a standard BackChannel Authentication response. |
Source code in requests_oauth2client/client.py
on_backchannel_authentication_error(response, *, dpop_key=None)
¶
Error handler for backchannel_authentication_request()
.
Invoked by backchannel_authentication_request() to parse the response returned by the BackChannel Authentication Endpoint, when it is an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response returned by the BackChannel Authentication Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
BackChannelAuthenticationResponse
|
usually raises an exception. But a subclass can return a default response instead. |
Raises:
Type | Description |
---|---|
EndpointError
|
(or one of its subclasses) if the response contains a standard OAuth 2.0 error. |
InvalidBackChannelAuthenticationResponse
|
for non-standard error responses. |
Source code in requests_oauth2client/client.py
authorize_device(requests_kwargs=None, **data)
¶
Send a Device Authorization Request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**data
|
Any
|
additional data to send to the Device Authorization Endpoint |
{}
|
requests_kwargs
|
dict[str, Any] | None
|
additional parameters for |
None
|
Returns:
Type | Description |
---|---|
DeviceAuthorizationResponse
|
a Device Authorization Response |
Raises:
Type | Description |
---|---|
MissingEndpointUri
|
if the Device Authorization URI is not configured |
Source code in requests_oauth2client/client.py
parse_device_authorization_response(response, *, dpop_key=None)
¶
Parse a Device Authorization Response received by authorize_device()
.
Invoked by authorize_device() to parse the response returned by the Device Authorization Endpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response returned by the Device Authorization Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
DeviceAuthorizationResponse
|
a |
Source code in requests_oauth2client/client.py
on_device_authorization_error(response, *, dpop_key=None)
¶
Error handler for authorize_device()
.
Invoked by authorize_device() to parse the response returned by the Device Authorization Endpoint, when that response is an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response returned by the Device Authorization Endpoint. |
required |
dpop_key
|
DPoPKey | None
|
the |
None
|
Returns:
Type | Description |
---|---|
DeviceAuthorizationResponse
|
usually raises an Exception. But a subclass may return a default response instead. |
Raises:
Type | Description |
---|---|
EndpointError
|
for standard OAuth 2.0 errors |
InvalidDeviceAuthorizationResponse
|
for non-standard error responses. |
Source code in requests_oauth2client/client.py
update_authorization_server_public_keys(requests_kwargs=None)
¶
Update the cached AS public keys by retrieving them from its jwks_uri
.
Public keys are returned by this method, as a jwskate.JwkSet
. They are also
available in attribute authorization_server_jwks
.
Returns:
Type | Description |
---|---|
JwkSet
|
the retrieved public keys |
Raises:
Type | Description |
---|---|
ValueError
|
if no |
Source code in requests_oauth2client/client.py
from_discovery_endpoint(url=None, issuer=None, *, auth=None, client_id=None, client_secret=None, private_key=None, session=None, testing=False, **kwargs)
classmethod
¶
Initialize an OAuth2Client
using an AS Discovery Document endpoint.
If an url
is provided, an HTTPS request will be done to that URL to obtain the Authorization Server Metadata.
If an issuer
is provided, the OpenID Connect Discovery document url will be automatically
derived from it, as specified in OpenID Connect Discovery.
Once the standardized metadata document is obtained, this will extract
all Endpoint Uris from that document, will fetch the current public keys from its
jwks_uri
, then will initialize an OAuth2Client based on those endpoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str | None
|
The url where the server metadata will be retrieved. |
None
|
issuer
|
str | None
|
The issuer value that is expected in the discovery document.
If not |
None
|
auth
|
AuthBase | tuple[str, str] | str | None
|
The authentication handler to use for client authentication. |
None
|
client_id
|
str | None
|
Client ID. |
None
|
client_secret
|
str | None
|
Client secret to use to authenticate the client. |
None
|
private_key
|
Jwk | dict[str, Any] | None
|
Private key to sign client assertions. |
None
|
session
|
Session | None
|
A |
None
|
testing
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional keyword parameters to pass to |
{}
|
Returns:
Type | Description |
---|---|
OAuth2Client
|
An |
Raises:
Type | Description |
---|---|
InvalidIssuer
|
If |
InvalidParam
|
If neither |
HTTPError
|
If an error happens while fetching the documents. |
Example
Source code in requests_oauth2client/client.py
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 |
|
from_discovery_document(discovery, issuer=None, *, auth=None, client_id=None, client_secret=None, private_key=None, authorization_server_jwks=None, https=True, testing=False, **kwargs)
classmethod
¶
Initialize an OAuth2Client
, based on an AS Discovery Document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
discovery
|
dict[str, Any]
|
A |
required |
issuer
|
str | None
|
If an issuer is given, check that it matches the one mentioned in the document. |
None
|
auth
|
AuthBase | tuple[str, str] | str | None
|
The authentication handler to use for client authentication. |
None
|
client_id
|
str | None
|
Client ID. |
None
|
client_secret
|
str | None
|
Client secret to use to authenticate the client. |
None
|
private_key
|
Jwk | dict[str, Any] | None
|
Private key to sign client assertions. |
None
|
authorization_server_jwks
|
JwkSet | dict[str, Any] | None
|
The current authorization server JWKS keys. |
None
|
https
|
bool
|
(deprecated) If |
True
|
testing
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional args that will be passed to |
{}
|
Returns:
Type | Description |
---|---|
OAuth2Client
|
An |
Raises:
Type | Description |
---|---|
InvalidDiscoveryDocument
|
If the document does not contain at least a |
Examples:
Source code in requests_oauth2client/client.py
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 |
|
client_authentication
¶
This module implements OAuth 2.0 Client Authentication Methods.
An OAuth 2.0 Client must authenticate to the AS whenever it sends a request to the Token Endpoint, by including appropriate credentials. This module contains helper classes and methods that implement the standardized and commonly used Client Authentication Methods.
InvalidRequestForClientAuthentication
¶
Bases: RuntimeError
Raised when a request is not suitable for OAuth 2.0 client authentication.
Source code in requests_oauth2client/client_authentication.py
BaseClientAuthenticationMethod
¶
Bases: AuthBase
Base class for all Client Authentication methods. This extends requests.auth.AuthBase.
This base class checks that requests are suitable to add Client Authentication parameters to, and does not modify the request.
Source code in requests_oauth2client/client_authentication.py
ClientSecretBasic
¶
Bases: BaseClientAuthenticationMethod
Implement client_secret_basic
authentication.
With this method, the client sends its Client ID and Secret, in the HTTP Authorization
header, with
the Basic
scheme, in each authenticated request to the Authorization Server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
Client ID |
required |
client_secret
|
str
|
Client Secret |
required |
Example
Source code in requests_oauth2client/client_authentication.py
ClientSecretPost
¶
Bases: BaseClientAuthenticationMethod
Implement client_secret_post
client authentication method.
With this method, the client inserts its client_id and client_secret in each authenticated request to the AS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
Client ID |
required |
client_secret
|
str
|
Client Secret |
required |
Example
Source code in requests_oauth2client/client_authentication.py
BaseClientAssertionAuthenticationMethod
¶
Bases: BaseClientAuthenticationMethod
Base class for assertion-based client authentication methods.
Source code in requests_oauth2client/client_authentication.py
client_assertion(audience)
¶
Generate a Client Assertion for a specific audience.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audience
|
str
|
the audience to use for the |
required |
Returns:
Type | Description |
---|---|
str
|
a Client Assertion, as |
Source code in requests_oauth2client/client_authentication.py
ClientSecretJwt
¶
Bases: BaseClientAssertionAuthenticationMethod
Implement client_secret_jwt
client authentication method.
With this method, the client generates a client assertion, then symmetrically signs it with its Client Secret.
The assertion is then sent to the AS in a client_assertion
field with each authenticated request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
the |
required |
client_secret
|
str
|
the |
required |
alg
|
str
|
the alg to use to sign generated Client Assertions. |
HS256
|
lifetime
|
int
|
the lifetime to use for generated Client Assertions. |
60
|
jti_gen
|
Callable[[], str]
|
a function to generate JWT Token Ids ( |
lambda: str(uuid4())
|
aud
|
str | None
|
the audience value to use. If |
None
|
Example
Source code in requests_oauth2client/client_authentication.py
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 |
|
client_assertion(audience)
¶
Generate a symmetrically signed Client Assertion.
Assertion is signed with the client_secret
as key and the alg
passed at init time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audience
|
str
|
the audience to use for the generated Client Assertion. |
required |
Returns:
Type | Description |
---|---|
str
|
a Client Assertion, as |
Source code in requests_oauth2client/client_authentication.py
InvalidClientAssertionSigningKeyOrAlg
¶
Bases: ValueError
Raised when the client assertion signing alg is not specified or invalid.
Source code in requests_oauth2client/client_authentication.py
PrivateKeyJwt
¶
Bases: BaseClientAssertionAuthenticationMethod
Implement private_key_jwt
client authentication method.
With this method, the client generates and sends a client_assertion, that is asymmetrically signed with a private key, on each direct request to the Authorization Server.
The private key must be supplied as a jwskate.Jwk
instance,
or any key material that can be used to initialize one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
the |
required |
private_jwk
|
Jwk | dict[str, Any] | Any
|
the private key to use to sign generated Client Assertions. |
required |
alg
|
str | None
|
the alg to use to sign generated Client Assertions. |
None
|
lifetime
|
int
|
the lifetime to use for generated Client Assertions. |
60
|
jti_gen
|
Callable[[], str]
|
a function to generate JWT Token Ids ( |
lambda: str(uuid4())
|
aud
|
str | None
|
the audience value to use. If |
None
|
Example
Source code in requests_oauth2client/client_authentication.py
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 |
|
client_assertion(audience)
¶
Generate a Client Assertion, asymmetrically signed with private_jwk
as key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audience
|
str
|
the audience to use for the generated Client Assertion. |
required |
Returns:
Type | Description |
---|---|
str
|
a Client Assertion. |
Source code in requests_oauth2client/client_authentication.py
PublicApp
¶
Bases: BaseClientAuthenticationMethod
Implement the none
authentication method for public apps.
This scheme is used for Public Clients, which do not have any secret credentials. Those only send their client_id to the Authorization Server.
Example
Source code in requests_oauth2client/client_authentication.py
UnsupportedClientCredentials
¶
client_auth_factory(auth, *, client_id=None, client_secret=None, private_key=None, default_auth_handler=ClientSecretPost)
¶
Initialize the appropriate Auth Handler based on the provided parameters.
This initializes a ClientAuthenticationMethod
subclass based on the provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth
|
AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
|
can be:
|
required |
client_id
|
str | None
|
the Client ID to use for this client |
None
|
client_secret
|
str | None
|
the Client Secret to use for this client, if any (for clients using an authentication method based on a secret) |
None
|
private_key
|
Jwk | dict[str, Any] | None
|
the private key to use for private_key_jwt authentication method |
None
|
default_auth_handler
|
type[ClientSecretPost | ClientSecretBasic | ClientSecretJwt]
|
if a client_id and client_secret are provided, initialize an
instance of this class with those 2 parameters.
You can choose between |
ClientSecretPost
|
Returns:
Type | Description |
---|---|
AuthBase
|
an Auth Handler that will manage client authentication to the AS Token Endpoint or other |
AuthBase
|
backend endpoints. |
Raises:
Type | Description |
---|---|
UnsupportedClientCredentials
|
if the provided parameters are not suitable to guess the desired authentication method. |
Source code in requests_oauth2client/client_authentication.py
device_authorization
¶
Implements the Device Authorization Flow as defined in RFC8628.
See RFC8628.
DeviceAuthorizationResponse
¶
Represent a response returned by the device Authorization Endpoint.
All parameters are those returned by the AS as response to a Device Authorization Request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device_code
|
str
|
the |
required |
user_code
|
str
|
the |
required |
verification_uri
|
str
|
the |
required |
verification_uri_complete
|
str | None
|
the |
None
|
expires_at
|
datetime | None
|
the expiration date for the device_code.
Also accepts an |
None
|
interval
|
int | None
|
the pooling |
None
|
**kwargs
|
Any
|
additional parameters as returned by the AS. |
{}
|
Source code in requests_oauth2client/device_authorization.py
is_expired(leeway=0)
¶
Check if the device_code
within this response is expired.
Returns:
Type | Description |
---|---|
bool | None
|
|
bool | None
|
no |
Source code in requests_oauth2client/device_authorization.py
DeviceAuthorizationPoolingJob
¶
Bases: BaseTokenEndpointPoolingJob
A Token Endpoint pooling job for the Device Authorization Flow.
This periodically checks if the user has finished with his authorization in a Device Authorization flow.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
an OAuth2Client that will be used to pool the token endpoint. |
required |
device_code
|
str | DeviceAuthorizationResponse
|
a |
required |
interval
|
int | None
|
The pooling interval to use. This overrides the one in |
None
|
slow_down_interval
|
int
|
Number of seconds to add to the pooling interval when the AS returns a slow-down request. |
5
|
requests_kwargs
|
dict[str, Any] | None
|
Additional parameters for the underlying calls to requests.request. |
None
|
**token_kwargs
|
Any
|
Additional parameters for the token request. |
{}
|
Example
Source code in requests_oauth2client/device_authorization.py
token_request()
¶
Implement the Device Code token request.
This actually calls OAuth2Client.device_code(device_code)
on self.client
.
Returns:
Type | Description |
---|---|
BearerToken
|
Source code in requests_oauth2client/device_authorization.py
discovery
¶
Implements Metadata discovery documents URLS.
This is as defined in RFC8615 and OpenID Connect Discovery 1.0.
well_known_uri(origin, name, *, at_root=True)
¶
Return the location of a well-known document on an origin url.
See RFC8615 and OIDC Discovery.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
origin
|
str
|
origin to use to build the well-known uri. |
required |
name
|
str
|
document name to use to build the well-known uri. |
required |
at_root
|
bool
|
if |
True
|
Returns:
Type | Description |
---|---|
str
|
the well-know uri, relative to origin, where the well-known document named |
str
|
found. |
Source code in requests_oauth2client/discovery.py
oidc_discovery_document_url(issuer)
¶
Construct the OIDC discovery document url for a given issuer
.
Given an issuer
identifier, return the standardised URL where the OIDC discovery document can
be retrieved.
The returned URL is biuilt as specified in OpenID Connect Discovery 1.0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
issuer
|
str
|
an OIDC Authentication Server |
required |
Returns:
Type | Description |
---|---|
str
|
the standardised discovery document URL. Note that no attempt to fetch this document is |
str
|
made. |
Source code in requests_oauth2client/discovery.py
oauth2_discovery_document_url(issuer)
¶
Construct the standardised OAuth 2.0 discovery document url for a given issuer
.
Based an issuer
identifier, returns the standardised URL where the OAuth20 server metadata can
be retrieved.
The returned URL is built as specified in RFC8414.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
issuer
|
str
|
an OAuth20 Authentication Server |
required |
Returns:
Type | Description |
---|---|
str
|
the standardised discovery document URL. Note that no attempt to fetch this document is |
str
|
made. |
Source code in requests_oauth2client/discovery.py
dpop
¶
Implementation of OAuth 2.0 Demonstrating Proof of Possession (DPoP) (RFC9449).
InvalidDPoPAccessToken
¶
Bases: ValueError
Raised when an access token contains invalid characters.
Source code in requests_oauth2client/dpop.py
InvalidDPoPKey
¶
Bases: ValueError
Raised when a DPoPToken is initialized with a non-suitable key.
Source code in requests_oauth2client/dpop.py
InvalidDPoPAlg
¶
Bases: ValueError
Raised when an invalid or unsupported DPoP alg is given.
Source code in requests_oauth2client/dpop.py
InvalidDPoPProof
¶
Bases: ValueError
Raised when a DPoP proof does not verify.
Source code in requests_oauth2client/dpop.py
InvalidUseDPoPNonceResponse
¶
Bases: Exception
Base class for invalid Responses with a use_dpop_nonce
error.
Source code in requests_oauth2client/dpop.py
MissingDPoPNonce
¶
Bases: InvalidUseDPoPNonceResponse
Raised when a server requests a DPoP nonce but none is provided in its response.
Source code in requests_oauth2client/dpop.py
RepeatedDPoPNonce
¶
Bases: InvalidUseDPoPNonceResponse
Raised when the server requests a DPoP nonce value that is the same as already included in the request.
Source code in requests_oauth2client/dpop.py
DPoPToken
¶
Bases: BearerToken
Represent a DPoP token (RFC9449).
A DPoP is very much like a BearerToken, with an additional private key bound to it.
Source code in requests_oauth2client/dpop.py
DPoPKey
¶
Wrapper around a DPoP proof signature key.
This handles DPoP proof generation. It also keeps track of a nonce, if provided by the Resource Server. Its behavior follows the standard DPoP specifications. You may subclass or otherwise customize this class to implement custom behavior, like adding or modifying claims to the proofs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
private_key
|
Any
|
the private key to use for DPoP proof signatures. |
required |
alg
|
str | None
|
the alg to use for signatures, if not specified of the |
None
|
jti_generator
|
Callable[[], str]
|
a callable that generates unique JWT Token ID (jti) values to include in proofs. |
lambda: str(uuid4())
|
iat_generator
|
Callable[[], int]
|
a callable that generates the Issuer Date (iat) to include in proofs. |
lambda: timestamp()
|
jwt_typ
|
str
|
the token type ( |
'dpop+jwt'
|
dpop_token_class
|
type[DPoPToken]
|
the class to use to represent DPoP tokens. |
DPoPToken
|
rs_nonce
|
str | None
|
an initial DPoP |
None
|
Source code in requests_oauth2client/dpop.py
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 |
|
public_jwk
cached
property
¶
The public JWK key that matches the private key.
dpop_jkt
cached
property
¶
The key thumbprint, used for Authorization Code DPoP binding.
generate(alg=jwskate.SignatureAlgs.ES256, jwt_typ='dpop+jwt', jti_generator=lambda: str(uuid4()), iat_generator=lambda: jwskate.Jwt.timestamp(), dpop_token_class=DPoPToken, as_nonce=None, rs_nonce=None)
classmethod
¶
Generate a new DPoPKey with a new private key that is suitable for the given alg
.
Source code in requests_oauth2client/dpop.py
proof(htm, htu, ath=None, nonce=None)
¶
Generate a DPoP proof.
Proof will contain the following claims:
1 2 3 4 5 6 |
|
The proof will be signed with the private key of this DPoPKey, using the configured alg
signature algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
htm
|
str
|
The HTTP method value of the request to which the proof is attached. |
required |
htu
|
str
|
The HTTP target URI of the request to which the proof is attached. Query and Fragment parts will
be automatically removed before being used as |
required |
ath
|
str | None
|
The Access Token hash value. |
None
|
nonce
|
str | None
|
A recent nonce provided via the DPoP-Nonce HTTP header, from either the AS or RS. If |
None
|
Returns:
Type | Description |
---|---|
SignedJwt
|
the proof value (as a signed JWT) |
Source code in requests_oauth2client/dpop.py
handle_as_provided_dpop_nonce(response)
¶
Handle an Authorization Server response containing a use_dpop_nonce
error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response from the AS. |
required |
Source code in requests_oauth2client/dpop.py
handle_rs_provided_dpop_nonce(response)
¶
Handle a Resource Server response containing a use_dpop_nonce
error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the response from the AS. |
required |
Source code in requests_oauth2client/dpop.py
add_dpop_proof(request, dpop_key, access_token, header_name='DPoP')
¶
Add a valid DPoP proof to a request, in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
PreparedRequest
|
the request to add the proof to. |
required |
dpop_key
|
DPoPKey
|
the DPoP key to use for the proof. |
required |
access_token
|
str
|
the access token to hash in the proof. |
required |
header_name
|
str
|
the name of the header to add the proof to. |
'DPoP'
|
Source code in requests_oauth2client/dpop.py
validate_dpop_proof(proof, *, htm, htu, ath=None, nonce=None, leeway=60, alg=None, algs=())
¶
Validate a DPoP proof.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proof
|
str | bytes
|
The serialized DPoP proof. |
required |
htm
|
str
|
The value of the HTTP method of the request to which the JWT is attached. |
required |
htu
|
str
|
The HTTP target URI of the request to which the JWT is attached, without query and fragment parts. |
required |
ath
|
str | None
|
The Hash of the access token. |
None
|
nonce
|
str | None
|
A recent nonce provided via the DPoP-Nonce HTTP header, from either the AS or RS. |
None
|
leeway
|
int
|
A leeway, in number of seconds, to validate the proof |
60
|
alg
|
str | None
|
Allowed signature alg, if there is only one. Use this or |
None
|
algs
|
Sequence[str]
|
Allowed signature algs, if there is several. Use this or |
()
|
Returns:
Type | Description |
---|---|
SignedJwt
|
The validated DPoP proof, as a |
Source code in requests_oauth2client/dpop.py
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 |
|
exceptions
¶
This module contains all exception classes from requests_oauth2client
.
OAuth2Error
¶
Bases: Exception
Base class for Exceptions raised when a backend endpoint returns an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the HTTP response containing the error |
required |
client
|
the OAuth2Client used to send the request |
required | |
description
|
str | None
|
description of the error |
None
|
Source code in requests_oauth2client/exceptions.py
request
property
¶
The request leading to the error.
EndpointError
¶
Bases: OAuth2Error
Base class for exceptions raised from backend endpoint errors.
This contains the error message, description and uri that are returned by the AS in the OAuth 2.0 standardised way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
the raw response containing the error. |
required |
error
|
str
|
the |
required |
description
|
str | None
|
the |
None
|
uri
|
str | None
|
the |
None
|
Source code in requests_oauth2client/exceptions.py
InvalidTokenResponse
¶
Bases: OAuth2Error
Raised when the Token Endpoint returns a non-standard response.
Source code in requests_oauth2client/exceptions.py
UnknownTokenEndpointError
¶
Bases: EndpointError
Raised when the token endpoint returns an otherwise unknown error.
Source code in requests_oauth2client/exceptions.py
ServerError
¶
Bases: EndpointError
Raised when the token endpoint returns error = server_error
.
Source code in requests_oauth2client/exceptions.py
TokenEndpointError
¶
Bases: EndpointError
Base class for errors that are specific to the token endpoint.
Source code in requests_oauth2client/exceptions.py
InvalidRequest
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_request
.
Source code in requests_oauth2client/exceptions.py
InvalidClient
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_client
.
Source code in requests_oauth2client/exceptions.py
InvalidScope
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_scope
.
Source code in requests_oauth2client/exceptions.py
InvalidTarget
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_target
.
Source code in requests_oauth2client/exceptions.py
InvalidGrant
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = invalid_grant
.
Source code in requests_oauth2client/exceptions.py
UseDPoPNonce
¶
Bases: TokenEndpointError
Raised when the Token Endpoint raises error = use_dpop_nonce`.
Source code in requests_oauth2client/exceptions.py
AccessDenied
¶
Bases: EndpointError
Raised when the Authorization Server returns error = access_denied
.
Source code in requests_oauth2client/exceptions.py
UnauthorizedClient
¶
Bases: EndpointError
Raised when the Authorization Server returns error = unauthorized_client
.
Source code in requests_oauth2client/exceptions.py
RevocationError
¶
Bases: EndpointError
Base class for Revocation Endpoint errors.
Source code in requests_oauth2client/exceptions.py
UnsupportedTokenType
¶
Bases: RevocationError
Raised when the Revocation endpoint returns error = unsupported_token_type
.
Source code in requests_oauth2client/exceptions.py
IntrospectionError
¶
Bases: EndpointError
Base class for Introspection Endpoint errors.
Source code in requests_oauth2client/exceptions.py
UnknownIntrospectionError
¶
Bases: OAuth2Error
Raised when the Introspection Endpoint returns a non-standard error.
Source code in requests_oauth2client/exceptions.py
DeviceAuthorizationError
¶
Bases: EndpointError
Base class for Device Authorization Endpoint errors.
Source code in requests_oauth2client/exceptions.py
AuthorizationPending
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = authorization_pending
.
Source code in requests_oauth2client/exceptions.py
SlowDown
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = slow_down
.
Source code in requests_oauth2client/exceptions.py
ExpiredToken
¶
Bases: TokenEndpointError
Raised when the Token Endpoint returns error = expired_token
.
Source code in requests_oauth2client/exceptions.py
InvalidDeviceAuthorizationResponse
¶
Bases: OAuth2Error
Raised when the Device Authorization Endpoint returns a non-standard error response.
Source code in requests_oauth2client/exceptions.py
AuthorizationResponseError
¶
Bases: Exception
Base class for error responses returned by the Authorization endpoint.
An AuthorizationResponseError
contains the error message, description and uri that are
returned by the AS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error
|
str
|
the |
required |
description
|
str | None
|
the |
None
|
uri
|
str | None
|
the |
None
|
Source code in requests_oauth2client/exceptions.py
InteractionRequired
¶
Bases: AuthorizationResponseError
Raised when the Authorization Endpoint returns error = interaction_required
.
Source code in requests_oauth2client/exceptions.py
LoginRequired
¶
Bases: InteractionRequired
Raised when the Authorization Endpoint returns error = login_required
.
Source code in requests_oauth2client/exceptions.py
AccountSelectionRequired
¶
Bases: InteractionRequired
Raised when the Authorization Endpoint returns error = account_selection_required
.
Source code in requests_oauth2client/exceptions.py
SessionSelectionRequired
¶
Bases: InteractionRequired
Raised when the Authorization Endpoint returns error = session_selection_required
.
Source code in requests_oauth2client/exceptions.py
ConsentRequired
¶
Bases: InteractionRequired
Raised when the Authorization Endpoint returns error = consent_required
.
Source code in requests_oauth2client/exceptions.py
InvalidAuthResponse
¶
Bases: ValueError
Raised when the Authorization Endpoint returns an invalid response.
Source code in requests_oauth2client/exceptions.py
MissingAuthCode
¶
Bases: InvalidAuthResponse
Raised when the Authorization Endpoint does not return the mandatory code
.
This happens when the Authorization Endpoint does not return an error, but does not return an
authorization code
either.
Source code in requests_oauth2client/exceptions.py
MissingIssuer
¶
Bases: InvalidAuthResponse
Raised when the Authorization Endpoint does not return an iss
parameter as expected.
The Authorization Server advertises its support with a flag
authorization_response_iss_parameter_supported
in its discovery document. If it is set to
true
, it must include an iss
parameter in its authorization responses, containing its issuer
identifier.
Source code in requests_oauth2client/exceptions.py
MismatchingState
¶
Bases: InvalidAuthResponse
Raised on mismatching state
value.
This happens when the Authorization Endpoints returns a 'state' parameter that doesn't match the value passed in the Authorization Request.
Source code in requests_oauth2client/exceptions.py
MismatchingIssuer
¶
Bases: InvalidAuthResponse
Raised on mismatching iss
value.
This happens when the Authorization Endpoints returns an 'iss' that doesn't match the expected value.
Source code in requests_oauth2client/exceptions.py
BackChannelAuthenticationError
¶
Bases: EndpointError
Base class for errors returned by the BackChannel Authentication endpoint.
Source code in requests_oauth2client/exceptions.py
InvalidBackChannelAuthenticationResponse
¶
Bases: OAuth2Error
Raised when the BackChannel Authentication endpoint returns a non-standard response.
Source code in requests_oauth2client/exceptions.py
InvalidPushedAuthorizationResponse
¶
Bases: OAuth2Error
Raised when the Pushed Authorization Endpoint returns an error.
Source code in requests_oauth2client/exceptions.py
flask
¶
This module contains helper classes for the Flask Framework.
See Flask framework.
FlaskOAuth2ClientCredentialsAuth
¶
Bases: FlaskSessionAuthMixin
, OAuth2ClientCredentialsAuth
A requests
Auth handler for CC grant that stores its token in Flask session.
It will automatically get Access Tokens from an OAuth 2.x AS with the Client Credentials grant
(and can get a new one once the first one is expired), and stores the retrieved token,
serialized in Flask session
, so that each user has a different access token.
Source code in requests_oauth2client/flask/auth.py
auth
¶
Helper classes for the Flask framework.
FlaskSessionAuthMixin
¶
A Mixin for auth handlers to store their tokens in Flask session.
Storing tokens in Flask session does ensure that each user of a Flask application has a different access token, and that tokens used for backend API access will be persisted between multiple requests to the front-end Flask app.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key
|
str
|
the key that will be used to store the access token in session. |
required |
serializer
|
BearerTokenSerializer | None
|
the serializer that will be used to store the access token in session. |
None
|
Source code in requests_oauth2client/flask/auth.py
token
property
writable
¶
Return the Access Token stored in session.
Returns:
Type | Description |
---|---|
BearerToken | None
|
The current |
FlaskOAuth2ClientCredentialsAuth
¶
Bases: FlaskSessionAuthMixin
, OAuth2ClientCredentialsAuth
A requests
Auth handler for CC grant that stores its token in Flask session.
It will automatically get Access Tokens from an OAuth 2.x AS with the Client Credentials grant
(and can get a new one once the first one is expired), and stores the retrieved token,
serialized in Flask session
, so that each user has a different access token.
Source code in requests_oauth2client/flask/auth.py
pooling
¶
Contains base classes for pooling jobs.
BaseTokenEndpointPoolingJob
¶
Base class for Token Endpoint pooling jobs.
This is used for decoupled flows like CIBA or Device Authorization.
This class must be subclassed to implement actual BackChannel flows. This needs an
OAuth2Client that will be used to pool the token
endpoint. The initial pooling interval
is configurable.
Source code in requests_oauth2client/pooling.py
sleep()
¶
Implement the wait between two requests of the token endpoint.
By default, relies on time.sleep().
slow_down()
¶
Implement the behavior when receiving a 'slow_down' response from the AS.
By default, it increases the pooling interval by the slow down interval.
authorization_pending()
¶
Implement the behavior when receiving an 'authorization_pending' response from the AS.
By default, it does nothing.
token_request()
¶
Abstract method for the token endpoint call.
Subclasses must implement this. This method must raise
AuthorizationPending to retry after
the pooling interval, or SlowDown to increase
the pooling interval by slow_down_interval
seconds.
Returns:
Type | Description |
---|---|
BearerToken
|
Source code in requests_oauth2client/pooling.py
tokens
¶
This module contains classes that represent Tokens used in OAuth2.0 / OIDC.
TokenType
¶
AccessTokenTypes
¶
UnsupportedTokenType
¶
Bases: ValueError
Raised when an unsupported token_type is provided.
Source code in requests_oauth2client/tokens.py
IdToken
¶
Bases: SignedJwt
Represent an ID Token.
An ID Token is actually a Signed JWT. If the ID Token is encrypted, it must be decoded beforehand.
Source code in requests_oauth2client/tokens.py
authorized_party
property
¶
The Authorized Party (azp).
auth_datetime
property
¶
The last user authentication time (auth_time).
hash_method(key, alg=None)
classmethod
¶
Returns a callable that generates valid OIDC hashes, such as at_hash
, c_hash
, etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
Jwk
|
the ID token signature verification public key |
required |
alg
|
str | None
|
the ID token signature algorithm |
None
|
Returns:
Type | Description |
---|---|
Callable[[str], str]
|
a callable that takes a string as input and produces a valid hash as a str output |
Source code in requests_oauth2client/tokens.py
InvalidIdToken
¶
Bases: ValueError
Raised when trying to validate an invalid ID Token value.
Source code in requests_oauth2client/tokens.py
MissingIdToken
¶
Bases: InvalidIdToken
Raised when the Authorization Endpoint does not return a mandatory ID Token.
This happens when the Authorization Endpoint does not return an error, but does not return an ID Token either.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenIssuer
¶
Bases: InvalidIdToken
Raised on mismatching iss
value in an ID Token.
This happens when the expected issuer
value is different from the iss
value in an obtained ID Token.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenNonce
¶
Bases: InvalidIdToken
Raised on mismatching nonce
value in an ID Token.
This happens when the authorization request includes a nonce
but the returned ID Token include
a different value.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenAcr
¶
Bases: InvalidIdToken
Raised when the returned ID Token doesn't contain one of the requested ACR Values.
This happens when the authorization request includes an acr_values
parameter but the returned
ID Token includes a different value.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenAudience
¶
Bases: InvalidIdToken
Raised when the ID Token audience does not include the requesting Client ID.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenAzp
¶
Bases: InvalidIdToken
Raised when the ID Token Authorized Presenter (azp) claim is not the Client ID.
Source code in requests_oauth2client/tokens.py
MismatchingIdTokenAlg
¶
Bases: InvalidIdToken
Raised when the returned ID Token is signed with an unexpected alg.
Source code in requests_oauth2client/tokens.py
ExpiredIdToken
¶
Bases: InvalidIdToken
Raised when the returned ID Token is expired.
Source code in requests_oauth2client/tokens.py
UnsupportedIdTokenAlg
¶
Bases: InvalidIdToken
Raised when the return ID Token is signed with an unsupported alg.
Source code in requests_oauth2client/tokens.py
TokenResponse
¶
ExpiredAccessToken
¶
BearerToken
¶
Bases: TokenResponse
, AuthBase
Represents a Bearer Token as returned by a Token Endpoint.
This is a wrapper around a Bearer Token and associated parameters, such as expiration date and refresh token, as returned by an OAuth 2.x or OIDC 1.0 Token Endpoint.
All parameters are as returned by a Token Endpoint. The token expiration date can be passed as
datetime in the expires_at
parameter, or an expires_in
parameter, as number of seconds in
the future, can be passed instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token
|
str
|
an |
required |
expires_at
|
datetime | None
|
an expiration date. This method also accepts an |
None
|
scope
|
str | None
|
a |
None
|
refresh_token
|
str | None
|
a |
None
|
token_type
|
str
|
a |
TOKEN_TYPE
|
id_token
|
str | bytes | IdToken | JweCompact | None
|
an |
None
|
**kwargs
|
Any
|
additional parameters as returned by the AS, if any. |
{}
|
Source code in requests_oauth2client/tokens.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
expires_in
property
¶
Number of seconds until expiration.
access_token_jwt
cached
property
¶
If the access token is a JWT, return it as an instance of jwskate.SignedJwt
.
This method is just a helper for AS testing purposes. Note that, as an OAuth 2.0 Client, you should never have to decode or analyze an access token, since it is simply an abstract string value. It is not even mandatory that Access Tokens are JWTs, just an implementation choice. Only Resource Servers (APIs) should check for the contents of Access Tokens they receive.
is_expired(leeway=0)
¶
Check if the access token is expired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
leeway
|
int
|
If the token expires in the next given number of seconds, then consider it expired already. |
0
|
Returns:
Type | Description |
---|---|
bool | None
|
One of: |
bool | None
|
|
bool | None
|
|
bool | None
|
|
Source code in requests_oauth2client/tokens.py
authorization_header()
¶
Return the appropriate Authorization Header value for this token.
The value is formatted correctly according to RFC6750.
Returns:
Type | Description |
---|---|
str
|
the value to use in an HTTP Authorization Header |
Source code in requests_oauth2client/tokens.py
validate_id_token(client, azr, exp_leeway=0, auth_time_leeway=10)
¶
Validate the ID Token, and return a new instance with the decrypted ID Token.
If the ID Token was not encrypted, the returned instance will contain the same ID Token.
This will validate the id_token as described in OIDC 1.0 $3.1.3.7.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OAuth2Client
|
the |
required |
azr
|
AuthorizationResponse
|
the |
required |
exp_leeway
|
int
|
a leeway, in seconds, applied to the ID Token expiration date |
0
|
auth_time_leeway
|
int
|
a leeway, in seconds, applied to the |
10
|
Raises:
Type | Description |
---|---|
MissingIdToken
|
if the ID Token is missing |
InvalidIdToken
|
this is a base exception class, which is raised:
|
MismatchingIdTokenAlg
|
if the |
MismatchingIdTokenIssuer
|
if the |
MismatchingIdTokenAcr
|
if the |
MismatchingIdTokenAudience
|
if the |
MismatchingIdTokenAzp
|
if the |
MismatchingIdTokenNonce
|
if the |
ExpiredIdToken
|
if the ID Token is expired at the time of the check. |
UnsupportedIdTokenAlg
|
if the signature alg for the ID Token is not supported. |
Source code in requests_oauth2client/tokens.py
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 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
as_dict()
¶
Return a dict of parameters.
That is suitable for serialization or to init another BearerToken.
Source code in requests_oauth2client/tokens.py
BearerTokenSerializer
¶
A helper class to serialize Token Response returned by an AS.
This may be used to store BearerTokens in session or cookies.
It needs a dumper
and a loader
functions that will respectively serialize and deserialize
BearerTokens. Default implementations are provided with use gzip and base64url on the serialized
JSON representation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dumper
|
Callable[[BearerToken], str] | None
|
a function to serialize a token into a |
None
|
loader
|
Callable[[str], BearerToken] | None
|
a function to deserialize a serialized token representation. |
None
|
Source code in requests_oauth2client/tokens.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
|
default_dumper(token)
staticmethod
¶
Serialize a token as JSON, then compress with deflate, then encodes as base64url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
BearerToken
|
the |
required |
Returns:
Type | Description |
---|---|
str
|
the serialized value |
Source code in requests_oauth2client/tokens.py
default_loader(serialized, token_class=BearerToken)
¶
Deserialize a BearerToken.
This does the opposite operations than default_dumper
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized
|
str
|
the serialized token |
required |
token_class
|
type[BearerToken]
|
class to use to deserialize the Token |
BearerToken
|
Returns:
Type | Description |
---|---|
BearerToken
|
a BearerToken |
Source code in requests_oauth2client/tokens.py
dumps(token)
¶
Serialize and compress a given token for easier storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
BearerToken
|
a BearerToken to serialize |
required |
Returns:
Type | Description |
---|---|
str
|
the serialized token, as a str |
Source code in requests_oauth2client/tokens.py
loads(serialized)
¶
Deserialize a serialized token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serialized
|
str
|
the serialized token |
required |
Returns:
Type | Description |
---|---|
BearerToken
|
the deserialized token |
id_token_converter(id_token)
¶
Utility method that converts an ID Token, as str | bytes
, to an IdToken
or JweCompact
.
Source code in requests_oauth2client/tokens.py
vendor_specific
¶
Vendor-specific utilities.
This module contains vendor-specific subclasses of [requests_oauth2client] classes, that make it easier to work with specific OAuth 2.x providers and/or fix compatibility issues.
Auth0
¶
Auth0-related utilities.
Source code in requests_oauth2client/vendor_specific/auth0.py
14 15 16 17 18 19 20 21 22 23 24 25 26 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 |
|
tenant(tenant)
classmethod
¶
Given a short tenant name, returns the full tenant FQDN.
Source code in requests_oauth2client/vendor_specific/auth0.py
client(tenant, auth=None, *, client_id=None, client_secret=None, private_jwk=None, session=None, **kwargs)
classmethod
¶
Initialise an OAuth2Client for an Auth0 tenant.
Source code in requests_oauth2client/vendor_specific/auth0.py
management_api_client(tenant, auth=None, *, client_id=None, client_secret=None, private_jwk=None, session=None, **kwargs)
classmethod
¶
Initialize a client for the Auth0 Management API.
See Auth0 Management API v2. You must provide the target tenant name and the credentials for a client that is allowed access to the Management API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
str
|
the tenant name. Same definition as for Auth0.client |
required |
auth
|
AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
|
client credentials. Same definition as for OAuth2Client |
None
|
client_id
|
str | None
|
the Client ID. Same definition as for OAuth2Client |
None
|
client_secret
|
str | None
|
the Client Secret. Same definition as for OAuth2Client |
None
|
private_jwk
|
Any | None
|
the private key to use for client authentication. Same definition as for OAuth2Client |
None
|
session
|
Session | None
|
requests session. Same definition as for OAuth2Client |
None
|
**kwargs
|
Any
|
additional kwargs to pass to the ApiClient base class |
{}
|
Example
Source code in requests_oauth2client/vendor_specific/auth0.py
Ping
¶
Ping Identity related utilities.
Source code in requests_oauth2client/vendor_specific/ping.py
client(issuer, auth=None, client_id=None, client_secret=None, private_jwk=None, session=None)
classmethod
¶
Initialize an OAuth2Client for PingFederate.
This will configure all endpoints with PingID specific urls, without using the metadata.
Excepted for avoiding a round-trip to get the metadata url, this does not provide any advantage
over using OAuth2Client.from_discovery_endpoint(issuer="https://myissuer.domain.tld")
.
Source code in requests_oauth2client/vendor_specific/ping.py
auth0
¶
Implements subclasses for Auth0.
Auth0
¶
Auth0-related utilities.
Source code in requests_oauth2client/vendor_specific/auth0.py
14 15 16 17 18 19 20 21 22 23 24 25 26 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 |
|
tenant(tenant)
classmethod
¶
Given a short tenant name, returns the full tenant FQDN.
Source code in requests_oauth2client/vendor_specific/auth0.py
client(tenant, auth=None, *, client_id=None, client_secret=None, private_jwk=None, session=None, **kwargs)
classmethod
¶
Initialise an OAuth2Client for an Auth0 tenant.
Source code in requests_oauth2client/vendor_specific/auth0.py
management_api_client(tenant, auth=None, *, client_id=None, client_secret=None, private_jwk=None, session=None, **kwargs)
classmethod
¶
Initialize a client for the Auth0 Management API.
See Auth0 Management API v2. You must provide the target tenant name and the credentials for a client that is allowed access to the Management API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
str
|
the tenant name. Same definition as for Auth0.client |
required |
auth
|
AuthBase | tuple[str, str] | tuple[str, Jwk] | tuple[str, dict[str, Any]] | str | None
|
client credentials. Same definition as for OAuth2Client |
None
|
client_id
|
str | None
|
the Client ID. Same definition as for OAuth2Client |
None
|
client_secret
|
str | None
|
the Client Secret. Same definition as for OAuth2Client |
None
|
private_jwk
|
Any | None
|
the private key to use for client authentication. Same definition as for OAuth2Client |
None
|
session
|
Session | None
|
requests session. Same definition as for OAuth2Client |
None
|
**kwargs
|
Any
|
additional kwargs to pass to the ApiClient base class |
{}
|
Example
Source code in requests_oauth2client/vendor_specific/auth0.py
ping
¶
PingID specific client.
Ping
¶
Ping Identity related utilities.
Source code in requests_oauth2client/vendor_specific/ping.py
client(issuer, auth=None, client_id=None, client_secret=None, private_jwk=None, session=None)
classmethod
¶
Initialize an OAuth2Client for PingFederate.
This will configure all endpoints with PingID specific urls, without using the metadata.
Excepted for avoiding a round-trip to get the metadata url, this does not provide any advantage
over using OAuth2Client.from_discovery_endpoint(issuer="https://myissuer.domain.tld")
.