oauth_client_req_auth() authenticates a request using the authentication
strategy defined by the auth and auth_params arguments to
oauth_client(). This is used to authenticate the client as part of the
OAuth flow, not to authenticate a request on behalf of a user.
There are three built-in strategies:
oauth_client_req_auth_body()adds the client id and (optionally) the secret to the request body, as described in Section 2.3.1 of RFC 6749.oauth_client_req_auth_header()adds the client id and secret using HTTP basic authentication with theAuthorizationheader, as described in Section 2.3.1 of RFC 6749.oauth_client_req_auth_jwt_sig()adds a client assertion to the body using a JWT signed withjwt_encode_sig()using a private key, as described in Section 2.2 of RFC 7523.
You will generally not call these functions directly but will instead
specify them through the auth argument to oauth_client(). The req and
client parameters are automatically filled in; other parameters come from
the auth_params argument.
Usage
oauth_client_req_auth(req, client)
oauth_client_req_auth_header(req, client)
oauth_client_req_auth_body(req, client)
oauth_client_req_auth_jwt_sig(
req,
client,
claim = NULL,
size = 256,
header = list()
)Arguments
- req
A httr2 request object.
- client
An oauth_client.
- claim
Claim set produced by
jwt_claim().- size
Size, in bits, of sha2 signature, i.e. 256, 384 or 512. Only for HMAC/RSA, not applicable for ECDSA keys.
- header
A named list giving additional fields to include in the JWT header.
Value
A modified HTTP request.
Examples
# Show what the various forms of client authentication look like
req <- request("https://example.com/whoami")
client1 <- oauth_client(
id = "12345",
secret = "56789",
token_url = "https://example.com/oauth/access_token",
name = "oauth-example",
auth = "body" # the default
)
# calls oauth_client_req_auth_body()
req_dry_run(oauth_client_req_auth(req, client1))
#> POST /whoami HTTP/1.1
#> accept: */*
#> accept-encoding: deflate, gzip, br, zstd
#> content-length: 35
#> content-type: application/x-www-form-urlencoded
#> host: example.com
#> user-agent: httr2/1.3.0.9000 r-curl/8.1.0 libcurl/8.18.0
#>
#> client_id=12345&client_secret=56789
client2 <- oauth_client(
id = "12345",
secret = "56789",
token_url = "https://example.com/oauth/access_token",
name = "oauth-example",
auth = "header"
)
# calls oauth_client_req_auth_header()
req_dry_run(oauth_client_req_auth(req, client2))
#> GET /whoami HTTP/1.1
#> accept: */*
#> accept-encoding: deflate, gzip, br, zstd
#> authorization: <REDACTED>
#> host: example.com
#> user-agent: httr2/1.3.0.9000 r-curl/8.1.0 libcurl/8.18.0
#>
client3 <- oauth_client(
id = "12345",
key = openssl::rsa_keygen(),
token_url = "https://example.com/oauth/access_token",
name = "oauth-example",
auth = "jwt_sig",
auth_params = list(claim = jwt_claim())
)
# calls oauth_client_req_auth_jwt_sig()
req_dry_run(oauth_client_req_auth(req, client3))
#> POST /whoami HTTP/1.1
#> accept: */*
#> accept-encoding: deflate, gzip, br, zstd
#> content-length: 623
#> content-type: application/x-www-form-urlencoded
#> host: example.com
#> user-agent: httr2/1.3.0.9000 r-curl/8.1.0 libcurl/8.18.0
#>
#> client_assertion=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE3OTQ5MjI5NDksIm5iZiI6MTc5NDkyMjY0OSwiaWF0IjoxNzk0OTIyNjQ5LCJqdGkiOiJlV21sM0M5QjFyTEc1SW8wUUVxUXozeXNsUnV6TTVBMVVrUnRBdTRQTkpJIn0.FY-NxlBeHlCmqaB914E6Qm3-SB0BpRSLaPmAHczEecMrxQyG-9RTKcRCPXBOsD16eaB_IjEGzMQ9i6MxHD7cj6045vT2dQCS-GYBP7UgJHWymAz7eFqy4HeF_87QSirBcy4cqzTuPEuUd7Gp1wrpR9FDnD9mxjLGVUwiqf-1axHU7aI0G17KFKzg7pyfMZ-fPAOoW9phU-ghhpwhiXbN7V8KkDR4lSTl5sSdPlgxJH2grvgB_JYIGlDI2qjoygwl2QtamMpgMx94FZzLi0NabNaSDuOBqp1YT0rULyR45stBKWP6BScLeF7KeJ7Dzgf6EaDu5rkj3cT39zXmJgvz4Q&client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer
