Skip to content

Commit eb907af

Browse files
Docs 113 (#2322)
* Use pnpm * Add v112 docs * Add v113 docs --------- Co-authored-by: qodo-merge-for-open-source[bot] <189517486+qodo-merge-for-open-source[bot]@users.noreply.github.com>
1 parent 57284b9 commit eb907af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3587
-9278
lines changed

.github/workflows/pull-request-docs.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

docs/docs/advanced/interceptors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Methods that you can override are:
1414
- `BeforeRequest(RestRequest request, CancellationToken cancellationToken)`
1515
- `AfterRequest(RestResponse response, CancellationToken cancellationToken)`
1616
- `BeforeHttpRequest(HttpRequestMessage requestMessage, CancellationToken cancellationToken)`
17-
- `AfterHttpResponse(HttpResponseMessage responseMessage, CancellationToken cancellationToken)`
17+
- `AfterHttpRequest(HttpResponseMessage responseMessage, CancellationToken cancellationToken)`
1818
- `BeforeDeserialization(RestResponse response, CancellationToken cancellationToken)`
1919

2020
All those functions must return a `ValueTask` instance.

docs/docs/changelog.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ sidebar_position: 1
88

99
For release notes of previous versions, please check the [Releases page](https://github.com/restsharp/RestSharp/releases) in RestSharp GitHub repository.
1010

11-
Changes between major versions are documented in the documentation for each version on this website.
11+
Changes between major versions are documented in the documentation for each version on this website.
12+
13+
# v112.0
14+
15+
* Security fix for [CVE-2024-45302](https://github.com/restsharp/RestSharp/security/advisories/GHSA-4rr6-2v9v-wcpc). Header values cannot contain `CRLF`.
16+
17+
## v112.1
18+
19+
* Follow up on v112.0 security fix: remove `\t` from the list of forbidden characters in headers.

docs/docusaurus.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ const config: Config = {
99
url: "https://restsharp.dev",
1010
baseUrl: "/",
1111
onBrokenLinks: "throw",
12-
onBrokenMarkdownLinks: "warn",
1312
i18n: {
1413
defaultLocale: "en",
1514
locales: ["en"],
1615
},
16+
markdown: {
17+
hooks: {
18+
onBrokenMarkdownLinks: "warn",
19+
}
20+
},
1721
plugins: [
1822
[
1923
'@docusaurus/plugin-client-redirects',

docs/package.json

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
"typecheck": "tsc"
1616
},
1717
"dependencies": {
18-
"@docusaurus/core": "^3.4.0",
19-
"@docusaurus/plugin-client-redirects": "^3.4.0",
20-
"@docusaurus/preset-classic": "^3.4.0",
21-
"@mdx-js/react": "^3.0.0",
22-
"clsx": "^2.0.0",
23-
"prism-react-renderer": "^2.3.0",
24-
"react": "^18.0.0",
25-
"react-dom": "^18.0.0"
18+
"@docusaurus/core": "^3.9.2",
19+
"@docusaurus/plugin-client-redirects": "^3.9.2",
20+
"@docusaurus/preset-classic": "^3.9.2",
21+
"@mdx-js/react": "^3.1.1",
22+
"clsx": "^2.1.1",
23+
"prism-react-renderer": "^2.4.1",
24+
"react": "^19.2.0",
25+
"react-dom": "^19.2.0"
2626
},
2727
"devDependencies": {
28-
"@docusaurus/module-type-aliases": "^3.4.0",
29-
"@docusaurus/tsconfig": "^3.4.0",
30-
"@docusaurus/types": "^3.4.0",
31-
"typescript": "~5.2.2"
28+
"@docusaurus/module-type-aliases": "^3.9.2",
29+
"@docusaurus/tsconfig": "^3.9.2",
30+
"@docusaurus/types": "^3.9.2",
31+
"typescript": "~5.9.3"
3232
},
3333
"browserslist": {
3434
"production": [
@@ -42,7 +42,5 @@
4242
"last 5 safari version"
4343
]
4444
},
45-
"engines": {
46-
"node": ">=18.0"
47-
}
45+
"packageManager": "[email protected]"
4846
}

docs/versioned_docs/version-v110/advanced/interceptors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Methods that you can override are:
1414
- `BeforeRequest(RestRequest request, CancellationToken cancellationToken)`
1515
- `AfterRequest(RestResponse response, CancellationToken cancellationToken)`
1616
- `BeforeHttpRequest(HttpRequestMessage requestMessage, CancellationToken cancellationToken)`
17-
- `AfterHttpResponse(HttpResponseMessage responseMessage, CancellationToken cancellationToken)`
17+
- `AfterHttpRequest(HttpResponseMessage responseMessage, CancellationToken cancellationToken)`
1818
- `BeforeDeserialization(RestResponse response, CancellationToken cancellationToken)`
1919

2020
All those functions must return a `ValueTask` instance.

docs/versioned_docs/version-v111/advanced/interceptors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Methods that you can override are:
1414
- `BeforeRequest(RestRequest request, CancellationToken cancellationToken)`
1515
- `AfterRequest(RestResponse response, CancellationToken cancellationToken)`
1616
- `BeforeHttpRequest(HttpRequestMessage requestMessage, CancellationToken cancellationToken)`
17-
- `AfterHttpResponse(HttpResponseMessage responseMessage, CancellationToken cancellationToken)`
17+
- `AfterHttpRequest(HttpResponseMessage responseMessage, CancellationToken cancellationToken)`
1818
- `BeforeDeserialization(RestResponse response, CancellationToken cancellationToken)`
1919

2020
All those functions must return a `ValueTask` instance.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "Advanced topics",
3+
"position": 4,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Authenticators
2+
3+
RestSharp includes authenticators for basic HTTP, OAuth1 and token-based (JWT and OAuth2).
4+
5+
There are two ways to set the authenticator: client-wide or per-request.
6+
7+
Set the client-wide authenticator by assigning the `Authenticator` property of `RestClientOptions`:
8+
9+
```csharp
10+
var options = new RestClientOptions("https://example.com") {
11+
Authenticator = new HttpBasicAuthenticator("username", "password")
12+
};
13+
var client = new RestClient(options);
14+
```
15+
16+
To set the authenticator per-request, assign the `Authenticator` property of `RestRequest`:
17+
18+
```csharp
19+
var request = new RestRequest("/api/users/me") {
20+
Authenticator = new HttpBasicAuthenticator("username", "password")
21+
};
22+
var response = await client.ExecuteAsync(request, cancellationToken);
23+
```
24+
25+
## Basic authentication
26+
27+
The `HttpBasicAuthenticator` allows you pass a username and password as a basic `Authorization` header using a base64 encoded string.
28+
29+
```csharp
30+
var options = new RestClientOptions("https://example.com") {
31+
Authenticator = new HttpBasicAuthenticator("username", "password")
32+
};
33+
var client = new RestClient(options);
34+
```
35+
36+
## OAuth1
37+
38+
For OAuth1 authentication the `OAuth1Authenticator` class provides static methods to help generate an OAuth authenticator.
39+
OAuth1 authenticator will add the necessary OAuth parameters to the request, including signature.
40+
41+
The authenticator will use `HMAC SHA1` to create a signature by default.
42+
Each static function to create the authenticator allows you to override the default and use another method to generate the signature.
43+
44+
### Request token
45+
46+
Getting a temporary request token is the usual first step in the 3-legged OAuth1 flow.
47+
Use `OAuth1Authenticator.ForRequestToken` function to get the request token authenticator.
48+
This method requires a `consumerKey` and `consumerSecret` to authenticate.
49+
50+
```csharp
51+
var options = new RestClientOptions("https://api.twitter.com") {
52+
Authenticator = OAuth1Authenticator.ForRequestToken(consumerKey, consumerSecret)
53+
};
54+
var client = new RestClient(options);
55+
var request = new RestRequest("oauth/request_token");
56+
```
57+
58+
The response should contain the token and the token secret, which can then be used to complete the authorization process.
59+
If you need to provide the callback URL, assign the `CallbackUrl` property of the authenticator to the callback destination.
60+
61+
### Access token
62+
63+
Getting an access token is the usual third step in the 3-legged OAuth1 flow.
64+
This method retrieves an access token when provided `consumerKey`, `consumerSecret`, `oauthToken`, and `oauthTokenSecret`.
65+
If you don't have a token for this call, you need to make a call to get the request token as described above.
66+
67+
```csharp
68+
var authenticator = OAuth1Authenticator.ForAccessToken(
69+
consumerKey, consumerSecret, oauthToken, oauthTokenSecret
70+
);
71+
var options = new RestClientOptions("https://api.twitter.com") {
72+
Authenticator = authenticator
73+
};
74+
var client = new RestClient(options);
75+
var request = new RestRequest("oauth/access_token");
76+
```
77+
78+
If the second step in 3-leg OAuth1 flow returned a verifier value, you can use another overload of `ForAccessToken`:
79+
80+
```csharp
81+
var authenticator = OAuth1Authenticator.ForAccessToken(
82+
consumerKey, consumerSecret, oauthToken, oauthTokenSecret, verifier
83+
);
84+
```
85+
86+
The response should contain the access token that can be used to make calls to protected resources.
87+
88+
For refreshing access tokens, use one of the two overloads of `ForAccessToken` that accept `sessionHandle`.
89+
90+
### Protected resource
91+
92+
When the access token is available, use `ForProtectedResource` function to get the authenticator for accessing protected resources.
93+
94+
```csharp
95+
var authenticator = OAuth1Authenticator.ForAccessToken(
96+
consumerKey, consumerSecret, accessToken, accessTokenSecret
97+
);
98+
var options = new RestClientOptions("https://api.twitter.com/1.1") {
99+
Authenticator = authenticator
100+
};
101+
var client = new RestClient(options);
102+
var request = new RestRequest("statuses/update.json", Method.Post)
103+
.AddParameter("status", "Hello Ladies + Gentlemen, a signed OAuth request!")
104+
.AddParameter("include_entities", "true");
105+
```
106+
107+
### xAuth
108+
109+
xAuth is a simplified version of OAuth1. It allows sending the username and password as `x_auth_username` and `x_auth_password` request parameters and directly get the access token. xAuth is not widely supported, but RestSharp still allows using it.
110+
111+
Create an xAuth authenticator using `OAuth1Authenticator.ForClientAuthentication` function:
112+
113+
```csharp
114+
var authenticator = OAuth1Authenticator.ForClientAuthentication(
115+
consumerKey, consumerSecret, username, password
116+
);
117+
```
118+
119+
### 0-legged OAuth
120+
121+
The access token authenticator can be used in 0-legged OAuth scenarios by providing `null` for the `consumerSecret`.
122+
123+
```csharp
124+
var authenticator = OAuth1Authenticator.ForAccessToken(
125+
consumerKey, null, oauthToken, oauthTokenSecret
126+
);
127+
```
128+
129+
## OAuth2
130+
131+
RestSharp has two very simple authenticators to send the access token as part of the request.
132+
133+
`OAuth2UriQueryParameterAuthenticator` accepts the access token as the only constructor argument, and it will send the provided token as a query parameter `oauth_token`.
134+
135+
`OAuth2AuthorizationRequestHeaderAuthenticator` has two constructors. One only accepts a single argument, which is the access token. The other constructor also allows you to specify the token type. The authenticator will then add an `Authorization` header using the specified token type or `OAuth` as the default token type, and the token itself.
136+
137+
For example:
138+
139+
```csharp
140+
var authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(
141+
token, "Bearer"
142+
);
143+
var options = new RestClientOptions("https://example.com") {
144+
Authenticator = authenticator
145+
};
146+
var client = new RestClient(options);
147+
```
148+
149+
The code above will tell RestSharp to send the bearer token with each request as a header. Essentially, the code above does the same as the sample for `JwtAuthenticator` below.
150+
151+
As those authenticators don't do much to get the token itself, you might be interested in looking at our [sample OAuth2 authenticator](../usage/example.md#authenticator), which requests the token on its own.
152+
153+
## JWT
154+
155+
The JWT authentication can be supported by using `JwtAuthenticator`. It is a very simple class that can be constructed like this:
156+
157+
```csharp
158+
var authenticator = new JwtAuthenticator(myToken);
159+
var options = new RestClientOptions("https://example.com") {
160+
Authenticator = authenticator
161+
};
162+
var client = new RestClient(options);
163+
```
164+
165+
For each request, it will add an `Authorization` header with the value `Bearer <your token>`.
166+
167+
As you might need to refresh the token from, you can use the `SetBearerToken` method to update the token.
168+
169+
## Custom authenticator
170+
171+
You can write your own implementation by implementing `IAuthenticator` and
172+
registering it with your RestClient:
173+
174+
```csharp
175+
var authenticator = new SuperAuthenticator(); // implements IAuthenticator
176+
var options = new RestClientOptions("https://example.com") {
177+
Authenticator = authenticator
178+
};
179+
var client = new RestClient(options);
180+
```
181+
182+
The `Authenticate` method is the very first thing called upon calling `RestClient.Execute` or `RestClient.Execute<T>`.
183+
It gets the `RestRequest` currently being executed giving you access to every part of the request data (headers, parameters, etc.)
184+
185+
You can find an example of a custom authenticator that fetches and uses an OAuth2 bearer token [here](../usage/example.md#authenticator).

0 commit comments

Comments
 (0)