From cce6471a938c95160f89f149a8326b544147353a Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Thu, 24 Dec 2015 15:49:48 -0500 Subject: [PATCH] Implement Client::ensure_token --- src/client/mod.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index c3487b4..84c2b06 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -9,7 +9,7 @@ use url::{form_urlencoded, Url}; use error::OAuth2Error; use provider::Provider; -use token::{Token, Expiring}; +use token::{Token, Lifetime, Expiring}; use self::response::FromResponse; pub mod response; @@ -181,6 +181,15 @@ impl Client

where P::Token: Token { let token = try!(P::Token::from_response_inherit(&json, &token)); Ok(token) } + + /// Ensures an access token is valid by refreshing it if necessary. + pub fn ensure_token(&self, token: P::Token) -> Result { + if token.lifetime().expired() { + self.refresh_token(token, None) + } else { + Ok(token) + } + } } #[cfg(test)]