|
13 | 13 | //! ...and follow the instructions. |
14 | 14 | //! |
15 | 15 |
|
16 | | -use oauth2::basic::BasicClient; |
| 16 | +use oauth2::{basic::BasicClient, revocation::StandardRevocableToken, TokenResponse}; |
17 | 17 | // Alternatively, this can be oauth2::curl::http_client or a custom. |
18 | 18 | use oauth2::reqwest::http_client; |
19 | 19 | use oauth2::{ |
20 | 20 | AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, PkceCodeChallenge, RedirectUrl, |
21 | | - Scope, TokenUrl, |
| 21 | + RevocationUrl, Scope, TokenUrl, |
22 | 22 | }; |
23 | 23 | use std::env; |
24 | 24 | use std::io::{BufRead, BufReader, Write}; |
@@ -49,6 +49,11 @@ fn main() { |
49 | 49 | // See below for the server implementation. |
50 | 50 | .set_redirect_url( |
51 | 51 | 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"), |
52 | 57 | ); |
53 | 58 |
|
54 | 59 | // Google supports Proof Key for Code Exchange (PKCE - https://oauth.net/2/pkce/). |
@@ -127,14 +132,29 @@ fn main() { |
127 | 132 | ); |
128 | 133 |
|
129 | 134 | // Exchange the code with a token. |
130 | | - let token = client |
| 135 | + let token_response = client |
131 | 136 | .exchange_code(code) |
132 | 137 | .set_pkce_verifier(pkce_code_verifier) |
133 | 138 | .request(http_client); |
134 | 139 |
|
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"); |
136 | 156 |
|
137 | | - // The server will terminate itself after collecting the first code. |
| 157 | + // The server will terminate itself after revoking the token. |
138 | 158 | break; |
139 | 159 | } |
140 | 160 | } |
|
0 commit comments