Skip to content

Commit 21130ce

Browse files
authored
Support RFC 7009 OAuth 2.0 Token Revocation (#122)
1 parent e02b68d commit 21130ce

File tree

7 files changed

+735
-28
lines changed

7 files changed

+735
-28
lines changed

examples/google.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
//! ...and follow the instructions.
1414
//!
1515
16-
use oauth2::basic::BasicClient;
16+
use oauth2::{basic::BasicClient, revocation::StandardRevocableToken, TokenResponse};
1717
// Alternatively, this can be oauth2::curl::http_client or a custom.
1818
use oauth2::reqwest::http_client;
1919
use oauth2::{
2020
AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, PkceCodeChallenge, RedirectUrl,
21-
Scope, TokenUrl,
21+
RevocationUrl, Scope, TokenUrl,
2222
};
2323
use std::env;
2424
use std::io::{BufRead, BufReader, Write};
@@ -49,6 +49,11 @@ fn main() {
4949
// See below for the server implementation.
5050
.set_redirect_url(
5151
RedirectUrl::new("http://localhost:8080".to_string()).expect("Invalid redirect URL"),
52+
)
53+
// Google supports OAuth 2.0 Token Revocation (RFC-7009)
54+
.set_revocation_url(
55+
RevocationUrl::new("https://oauth2.googleapis.com/revoke".to_string())
56+
.expect("Invalid revocation endpoint URL"),
5257
);
5358

5459
// Google supports Proof Key for Code Exchange (PKCE - https://oauth.net/2/pkce/).
@@ -127,14 +132,29 @@ fn main() {
127132
);
128133

129134
// Exchange the code with a token.
130-
let token = client
135+
let token_response = client
131136
.exchange_code(code)
132137
.set_pkce_verifier(pkce_code_verifier)
133138
.request(http_client);
134139

135-
println!("Google returned the following token:\n{:?}\n", token);
140+
println!(
141+
"Google returned the following token:\n{:?}\n",
142+
token_response
143+
);
144+
145+
// Revoke the obtained token
146+
let token_response = token_response.unwrap();
147+
let token_to_revoke: StandardRevocableToken = match token_response.refresh_token() {
148+
Some(token) => token.into(),
149+
None => token_response.access_token().into(),
150+
};
151+
152+
client
153+
.revoke_token(token_to_revoke)
154+
.request(http_client)
155+
.expect("Failed to revoke token");
136156

137-
// The server will terminate itself after collecting the first code.
157+
// The server will terminate itself after revoking the token.
138158
break;
139159
}
140160
}

examples/wunderlist.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
//! ...and follow the instructions.
1515
//!
1616
17-
use oauth2::basic::{BasicErrorResponse, BasicTokenIntrospectionResponse, BasicTokenType};
1817
use oauth2::TokenType;
18+
use oauth2::{
19+
basic::{
20+
BasicErrorResponse, BasicRevocationErrorResponse, BasicTokenIntrospectionResponse,
21+
BasicTokenType,
22+
},
23+
revocation::StandardRevocableToken,
24+
};
1925
// Alternatively, this can be `oauth2::curl::http_client` or a custom client.
2026
use oauth2::helpers;
2127
use oauth2::reqwest::http_client;
@@ -39,6 +45,8 @@ type SpecialClient = Client<
3945
SpecialTokenResponse,
4046
BasicTokenType,
4147
BasicTokenIntrospectionResponse,
48+
StandardRevocableToken,
49+
BasicRevocationErrorResponse,
4250
>;
4351

4452
fn default_token_type() -> Option<BasicTokenType> {

src/basic.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ use super::{
55
Client, EmptyExtraTokenFields, ErrorResponseType, RequestTokenError, StandardErrorResponse,
66
StandardTokenResponse, TokenType,
77
};
8-
use crate::StandardTokenIntrospectionResponse;
8+
use crate::{
9+
revocation::{RevocationErrorResponseType, StandardRevocableToken},
10+
StandardTokenIntrospectionResponse,
11+
};
912

1013
///
1114
/// Basic OAuth2 client specialization, suitable for most applications.
1215
///
13-
pub type BasicClient =
14-
Client<BasicErrorResponse, BasicTokenResponse, BasicTokenType, BasicTokenIntrospectionResponse>;
16+
pub type BasicClient = Client<
17+
BasicErrorResponse,
18+
BasicTokenResponse,
19+
BasicTokenType,
20+
BasicTokenIntrospectionResponse,
21+
StandardRevocableToken,
22+
BasicRevocationErrorResponse,
23+
>;
1524

1625
///
1726
/// Basic OAuth2 authorization token types.
@@ -189,3 +198,8 @@ pub type BasicErrorResponse = StandardErrorResponse<BasicErrorResponseType>;
189198
/// Token error specialization for basic OAuth2 implementation.
190199
///
191200
pub type BasicRequestTokenError<RE> = RequestTokenError<RE, BasicErrorResponse>;
201+
202+
///
203+
/// Revocation error response specialization for basic OAuth2 implementation.
204+
///
205+
pub type BasicRevocationErrorResponse = StandardErrorResponse<RevocationErrorResponseType>;

0 commit comments

Comments
 (0)