Skip to content

Added updated oauth2-core dependency for java projects #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion dot-net-cs/OAuth2Demo/Controllers/AuthWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public AuthInfo RefreshToken(string refreshToken)
{
var _readToEnd = _sr.ReadToEnd();
var _res = SimpleJson.DeserializeObject<TokenInfo>(_readToEnd);
_res.life_time = DateTime.Now.AddSeconds(Double.Parse(_res.expires_in));
return new AuthInfo { Success = true, Token = _res };
}
}
Expand All @@ -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");
Expand Down Expand Up @@ -95,6 +104,7 @@ public AuthInfo GetAuthInfo(string code)
{
var _readToEnd = _sr.ReadToEnd();
var _res = SimpleJson.DeserializeObject<TokenInfo>(_readToEnd);
_res.life_time = DateTime.Now.AddSeconds(Double.Parse(_res.expires_in));
return new AuthInfo{Success = true, Token = _res};
}
}
Expand All @@ -115,7 +125,7 @@ public AuthInfo GetAuthInfo(string code)
/// <returns></returns>
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";
}
}
}
14 changes: 4 additions & 10 deletions dot-net-cs/OAuth2Demo/Controllers/DefaultController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand All @@ -38,6 +32,6 @@ public ActionResult AuthCallback(string code)
public ActionResult ViewFiles()
{
return Redirect(authWorker.GetAuthUrl());
}
}
}
}
5 changes: 4 additions & 1 deletion dot-net-cs/OAuth2Demo/Controllers/TokenInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OAuth2Demo.Controllers
using System;

namespace OAuth2Demo.Controllers
{
/// <summary>
/// TokenInfo is a data structure holding all authentication related properties
Expand All @@ -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; }
}
}
2 changes: 1 addition & 1 deletion java-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>com.sequencing</groupId>
<artifactId>oauth2-core</artifactId>
<version>1.0</version>
<version>1.9</version>
</dependency>

</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion java-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<dependency>
<groupId>com.sequencing</groupId>
<artifactId>oauth2-core</artifactId>
<version>1.0</version>
<version>1.9</version>
</dependency>
</dependencies>

Expand Down