diff --git a/dot-net-cs/OAuth2Demo/Controllers/AuthWorker.cs b/dot-net-cs/OAuth2Demo/Controllers/AuthWorker.cs index 821b9fa..432ffa0 100644 --- a/dot-net-cs/OAuth2Demo/Controllers/AuthWorker.cs +++ b/dot-net-cs/OAuth2Demo/Controllers/AuthWorker.cs @@ -47,6 +47,7 @@ public AuthInfo RefreshToken(string refreshToken) { var _readToEnd = _sr.ReadToEnd(); var _res = SimpleJson.DeserializeObject(_readToEnd); + _res.life_time = DateTime.Now.AddSeconds(Double.Parse(_res.expires_in)); return new AuthInfo { Success = true, Token = _res }; } } @@ -61,6 +62,14 @@ public AuthInfo RefreshToken(string refreshToken) } } + public TokenInfo GetToken(TokenInfo info) + { + if (info.life_time < DateTime.Now) + info = RefreshToken(info.refresh_token).Token; + return info; + + } + private HttpWebRequest CreateRq() { var _webRequest = (HttpWebRequest) WebRequest.Create(oAuthUrl + "?q=oauth2/token"); @@ -95,6 +104,7 @@ public AuthInfo GetAuthInfo(string code) { var _readToEnd = _sr.ReadToEnd(); var _res = SimpleJson.DeserializeObject(_readToEnd); + _res.life_time = DateTime.Now.AddSeconds(Double.Parse(_res.expires_in)); return new AuthInfo{Success = true, Token = _res}; } } @@ -115,7 +125,7 @@ public AuthInfo GetAuthInfo(string code) /// public string GetAuthUrl() { - return oAuthUrl+ "?q=oauth2/authorize&redirect_uri="+redirectUrl+"&response_type=code&state=123&client_id="+appId+"&scope=test"; + return oAuthUrl+ "?q=oauth2/authorize&redirect_uri="+redirectUrl+"&response_type=code&state=123&client_id="+appId+"&scope=external"; } } } \ No newline at end of file diff --git a/dot-net-cs/OAuth2Demo/Controllers/DefaultController.cs b/dot-net-cs/OAuth2Demo/Controllers/DefaultController.cs index 185ce78..dfbcb35 100644 --- a/dot-net-cs/OAuth2Demo/Controllers/DefaultController.cs +++ b/dot-net-cs/OAuth2Demo/Controllers/DefaultController.cs @@ -17,18 +17,12 @@ public ActionResult AuthCallback(string code) { //Retrieving token var _authInfo = authWorker.GetAuthInfo(code); - if (_authInfo.Success) - { - //Simulating token expiration - calling refresh - //In real application you shall do it only when token is expired - //So this call can be safely removed here - _authInfo = authWorker.RefreshToken(_authInfo.Token.refresh_token); + if (_authInfo.Success) if (_authInfo.Success) { - var _listFiles = new BasicApiWorker(_authInfo.Token, Options.ApiUrl).ListFiles(); + var _listFiles = new BasicApiWorker(authWorker.GetToken(_authInfo.Token), Options.ApiUrl).ListFiles(); return View("FilesList", _listFiles); - } - } + } return new ContentResult { Content = "Error while retrieving access token:" + _authInfo.ErrorMessage }; } return new ContentResult{Content = "User cancelled the auth sequence"}; @@ -38,6 +32,6 @@ public ActionResult AuthCallback(string code) public ActionResult ViewFiles() { return Redirect(authWorker.GetAuthUrl()); - } + } } } \ No newline at end of file diff --git a/dot-net-cs/OAuth2Demo/Controllers/TokenInfo.cs b/dot-net-cs/OAuth2Demo/Controllers/TokenInfo.cs index 65848c7..eadf16e 100644 --- a/dot-net-cs/OAuth2Demo/Controllers/TokenInfo.cs +++ b/dot-net-cs/OAuth2Demo/Controllers/TokenInfo.cs @@ -1,4 +1,6 @@ -namespace OAuth2Demo.Controllers +using System; + +namespace OAuth2Demo.Controllers { /// /// TokenInfo is a data structure holding all authentication related properties @@ -10,5 +12,6 @@ public class TokenInfo public string token_type { get; set; } public string scope { get; set; } public string refresh_token { get; set; } + public DateTime life_time { get; set; } } } \ No newline at end of file diff --git a/java-servlet/pom.xml b/java-servlet/pom.xml index 185519a..8b18f5c 100644 --- a/java-servlet/pom.xml +++ b/java-servlet/pom.xml @@ -48,7 +48,7 @@ com.sequencing oauth2-core - 1.0 + 1.9 diff --git a/java-spring/pom.xml b/java-spring/pom.xml index 2d5f692..6be9cef 100644 --- a/java-spring/pom.xml +++ b/java-spring/pom.xml @@ -62,7 +62,7 @@ com.sequencing oauth2-core - 1.0 + 1.9