Skip to content

Commit

Permalink
Use DeviceCodeErrorResponseType::ExpiredToken for expired Device (#195)
Browse files Browse the repository at this point in the history
It is good to use RequestTokenError::ServerResponse for expired
Device token instead of having a "Device code expired" string
is set to Other(). We see to it that they are have the same Error
with the error came from the endpoint. There was a timing
issue found that expired_token is returned from the endpoint
and sometimes the RequestTokenError::Other() was returned
after the expiration time given by the endpoint.

We make it uniform for easy error handling upon use.
  • Loading branch information
LorenzoLeonardo authored Nov 8, 2022
1 parent a9ff9b3 commit 7261513
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2333,7 +2333,13 @@ where
loop {
let now = (*self.time_fn)();
if now > timeout_dt {
break Err(RequestTokenError::Other("Device code expired".to_string()));
break Err(RequestTokenError::ServerResponse(
DeviceCodeErrorResponse::new(
DeviceCodeErrorResponseType::ExpiredToken,
Some(String::from("This device code has expired.")),
None,
),
));
}

match self.process_response(http_client(self.prepare_request()?), interval) {
Expand Down Expand Up @@ -2372,7 +2378,13 @@ where
loop {
let now = (*self.time_fn)();
if now > timeout_dt {
break Err(RequestTokenError::Other("Device code expired".to_string()));
break Err(RequestTokenError::ServerResponse(
DeviceCodeErrorResponse::new(
DeviceCodeErrorResponseType::ExpiredToken,
Some(String::from("This device code has expired.")),
None,
),
));
}

match self.process_response(http_client(self.prepare_request()?).await, interval) {
Expand Down
9 changes: 8 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,14 @@ fn test_device_token_authorization_timeout() {
.err()
.unwrap();
match token {
RequestTokenError::Other(msg) => assert_eq!(msg, "Device code expired"),
RequestTokenError::ServerResponse(msg) => assert_eq!(
msg,
DeviceCodeErrorResponse::new(
DeviceCodeErrorResponseType::ExpiredToken,
Some(String::from("This device code has expired.")),
None,
)
),
_ => unreachable!("Error should be an expiry"),
}
}
Expand Down

0 comments on commit 7261513

Please sign in to comment.