Skip to content

Commit 0282bb6

Browse files
authored
Merge pull request #30 from Noskcaj19/master
Add refresh token exchanging
2 parents 837ff93 + a0c1a72 commit 0282bb6

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "oauth2"
33
authors = ["Alex Crichton <[email protected]>", "Florin Lipan <[email protected]>"]
4-
version = "1.2.1"
4+
version = "1.3.0"
55
license = "MIT/Apache-2.0"
66
description = "Bindings for exchanging OAuth 2 tokens"
77
repository = "https://github.com/alexcrichton/oauth2-rs"

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ impl Config {
300300
self.request_token(params)
301301
}
302302

303+
///
304+
/// Exchanges a refresh token for an access token
305+
///
306+
/// See https://tools.ietf.org/html/rfc6749#section-6
307+
///
308+
pub fn exchange_refresh_token<T>(&self, token: T) -> Result<Token, TokenError>
309+
where T: Into<String> {
310+
let params = vec![
311+
("grant_type", "refresh_token".to_string()),
312+
("refresh_token", token.into()),
313+
];
314+
315+
self.request_token(params)
316+
}
317+
303318
fn request_token(&self, mut params: Vec<(&str, String)>) -> Result<Token, TokenError> {
304319
let mut easy = Easy::new();
305320

tests/lib.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,75 @@ fn test_exchange_client_credentials_with_json_response() {
190190
assert_eq!(None, token.refresh_token);
191191
}
192192

193+
#[test]
194+
fn test_exchange_refresh_token_with_form_response() {
195+
let mock = mock("POST", "/token")
196+
.match_body("grant_type=refresh_token&refresh_token=ccc&client_id=aaa&client_secret=bbb")
197+
.with_body("access_token=12%2F34&token_type=bearer&scope=read,write")
198+
.create();
199+
200+
let config = Config::new("aaa", "bbb", "http://example.com/auth", &(SERVER_URL.to_string() + "/token"));
201+
let token = config.exchange_refresh_token("ccc");
202+
203+
mock.assert();
204+
205+
assert!(token.is_ok());
206+
207+
let token = token.unwrap();
208+
assert_eq!("12/34", token.access_token);
209+
assert_eq!("bearer", token.token_type);
210+
assert_eq!(vec!["read".to_string(), "write".to_string()], token.scopes);
211+
assert_eq!(None, token.expires_in);
212+
assert_eq!(None, token.refresh_token);
213+
}
214+
215+
#[test]
216+
fn test_exchange_refresh_token_with_basic_auth() {
217+
let mock = mock("POST", "/token")
218+
.match_header("Authorization", "Basic YWFhOmJiYg==") // base64("aaa:bbb")
219+
.match_body("grant_type=refresh_token&refresh_token=ccc")
220+
.with_body("access_token=12%2F34&token_type=bearer&scope=read,write")
221+
.create();
222+
223+
let mut config = Config::new("aaa", "bbb", "http://example.com/auth", &(SERVER_URL.to_string() + "/token"));
224+
config = config.set_auth_type(oauth2::AuthType::BasicAuth);
225+
let token = config.exchange_refresh_token("ccc");
226+
227+
mock.assert();
228+
229+
assert!(token.is_ok());
230+
231+
let token = token.unwrap();
232+
assert_eq!("12/34", token.access_token);
233+
assert_eq!("bearer", token.token_type);
234+
assert_eq!(vec!["read".to_string(), "write".to_string()], token.scopes);
235+
assert_eq!(None, token.expires_in);
236+
assert_eq!(None, token.refresh_token);
237+
}
238+
239+
#[test]
240+
fn test_exchange_refresh_token_with_json_response() {
241+
let mock = mock("POST", "/token")
242+
.match_body("grant_type=refresh_token&refresh_token=ccc&client_id=aaa&client_secret=bbb")
243+
.with_header("content-type", "application/json")
244+
.with_body("{\"access_token\": \"12/34\", \"token_type\": \"bearer\", \"scopes\": [\"read\", \"write\"]}")
245+
.create();
246+
247+
let config = Config::new("aaa", "bbb", "http://example.com/auth", &(SERVER_URL.to_string() + "/token"));
248+
let token = config.exchange_refresh_token("ccc");
249+
250+
mock.assert();
251+
252+
assert!(token.is_ok());
253+
254+
let token = token.unwrap();
255+
assert_eq!("12/34", token.access_token);
256+
assert_eq!("bearer", token.token_type);
257+
assert_eq!(vec!["read".to_string(), "write".to_string()], token.scopes);
258+
assert_eq!(None, token.expires_in);
259+
assert_eq!(None, token.refresh_token);
260+
}
261+
193262
#[test]
194263
fn test_exchange_password_with_form_response() {
195264
let mock = mock("POST", "/token")

0 commit comments

Comments
 (0)