Skip to content
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

Oauth keycloak plugin #18

Merged
merged 7 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: url and request header fix
- now after getting a vaild token, auth code and session state in url will be removed.
- token now can be set to put in the request header.
- endpoint url fix.
  • Loading branch information
ppop123456 committed Jan 17, 2023
commit 8bdc77b1847e872b3b72e9f922ced9ce961f0004
9 changes: 5 additions & 4 deletions bluelight/data/configOAuth.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"client_id":"account",
"endpoints":
{
"auth":"/TestRealm/protocol/openid-connect/auth",
"validation":"/TestRealm/protocol/openid-connect/userinfo",
"token":"/TestRealm/protocol/openid-connect/token"
}
"auth":"realms/TestRealm/protocol/openid-connect/auth",
"validation":"realms/TestRealm/protocol/openid-connect/userinfo",
"token":"realms/TestRealm/protocol/openid-connect/token"
},
"tokenInRequest":false
}
30 changes: 29 additions & 1 deletion bluelight/scripts/plugin/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function auth() {
let authCode = searchParams.get("code");
let session_state = searchParams.get("session_state");
let thePort = OAuthConfig.port != "" ? `:${OAuthConfig.port}` : "";
keycloakAPI = `${OAuthConfig.http}://${OAuthConfig.hostname}${thePort}/realms`;
keycloakAPI = `${OAuthConfig.http}://${OAuthConfig.hostname}${thePort}/`;

// No token but have auth code (usually when it's login complete and is redirecting back).
if (theToken == "" && authCode != null) {
Expand All @@ -25,10 +25,24 @@ async function auth() {
// Have token so let's see if it's vaild or not.
let tokenVaild = await isTokenVaild(theToken);
if (tokenVaild) {
if(window.location.href.indexOf(`code=`) != -1)
{
let originalUrl = removeURLParameter(window.location.href, "code");
originalUrl = removeURLParameter(originalUrl, "session_state");
window.location.href = originalUrl;
}
if(OAuthConfig.tokenInRequest == true)
{
ConfigLog.QIDO.token = "Bearer " + theToken;
ConfigLog.WADO.token = "Bearer " + theToken;
ConfigLog.STOW.token = "Bearer " + theToken;
}
console.log(ConfigLog);
return true;
}
// No token or token is not vaild, redirect to keycloak login page and put current url in the Callback URL parameter.
else {
setCookie("access_token","",7);
let redirectUri = removeURLParameter(window.location.href, "code");
redirectUri = removeURLParameter(redirectUri, "session_state");
let loginPage = `${keycloakAPI}${OAuthConfig.endpoints.auth}?client_id=${OAuthConfig.client_id}&grant_type=authorization_code&response_type=code&redirect_uri=${redirectUri}`;
Expand All @@ -55,6 +69,19 @@ function getCookie(name) {
}
}

/**
* Set a cookie.
*/
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}

/**
* send token to the keycloak server to see if the token is vaild or not.
*/
Expand Down Expand Up @@ -103,6 +130,7 @@ function requestToken(code, session_state) {
request.onload = function () {
let result = request.response;
responseToken = result.access_token;
setCookie("access_token",responseToken,7);
resolve(responseToken);
}
});
Expand Down
9 changes: 5 additions & 4 deletions search/data/configOAuth.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"client_id":"account",
"endpoints":
{
"auth":"/TestRealm/protocol/openid-connect/auth",
"validation":"/TestRealm/protocol/openid-connect/userinfo",
"token":"/TestRealm/protocol/openid-connect/token"
}
"auth":"realms/TestRealm/protocol/openid-connect/auth",
"validation":"realms/TestRealm/protocol/openid-connect/userinfo",
"token":"realms/TestRealm/protocol/openid-connect/token"
},
"tokenInRequest":false
}
2 changes: 1 addition & 1 deletion search/html/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

<script src="../scripts/toolfunction.js"></script>
<script src="../scripts/readsome.js"></script>
<script src="../scripts/oauth.js"></script>
<script src="../scripts/onload.js"></script>
<script src="../scripts/oauth.js"></script>

<link rel="stylesheet" href="../css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
Expand Down
32 changes: 30 additions & 2 deletions search/scripts/oauth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var OAuthConfig = {};
var keycloakAPI = "";

//auth();
window.addEventListener("load", function(event) {
auth();
});

/**
* Login Auth Check
*/
Expand All @@ -16,7 +16,7 @@ async function auth() {
let authCode = searchParams.get("code");
let session_state = searchParams.get("session_state");
let thePort = OAuthConfig.port != "" ? `:${OAuthConfig.port}` : "";
keycloakAPI = `${OAuthConfig.http}://${OAuthConfig.hostname}${thePort}/realms`;
keycloakAPI = `${OAuthConfig.http}://${OAuthConfig.hostname}${thePort}/`;

// No token but have auth code (usually when it's login complete and is redirecting back).
if (theToken == "" && authCode != null) {
Expand All @@ -25,10 +25,24 @@ async function auth() {
// Have token so let's see if it's vaild or not.
let tokenVaild = await isTokenVaild(theToken);
if (tokenVaild) {
if(window.location.href.indexOf(`code=`) != -1)
{
let originalUrl = removeURLParameter(window.location.href, "code");
originalUrl = removeURLParameter(originalUrl, "session_state");
window.location.href = originalUrl;
}
if(OAuthConfig.tokenInRequest == true)
{
ConfigLog.QIDO.token = "Bearer " + theToken;
ConfigLog.WADO.token = "Bearer " + theToken;
ConfigLog.STOW.token = "Bearer " + theToken;
}
console.log(ConfigLog);
return true;
}
// No token or token is not vaild, redirect to keycloak login page and put current url in the Callback URL parameter.
else {
setCookie("access_token","",7);
let redirectUri = removeURLParameter(window.location.href, "code");
redirectUri = removeURLParameter(redirectUri, "session_state");
let loginPage = `${keycloakAPI}${OAuthConfig.endpoints.auth}?client_id=${OAuthConfig.client_id}&grant_type=authorization_code&response_type=code&redirect_uri=${redirectUri}`;
Expand All @@ -55,6 +69,19 @@ function getCookie(name) {
}
}

/**
* Set a cookie.
*/
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}

/**
* send token to the keycloak server to see if the token is vaild or not.
*/
Expand Down Expand Up @@ -103,6 +130,7 @@ function requestToken(code, session_state) {
request.onload = function () {
let result = request.response;
responseToken = result.access_token;
setCookie("access_token",responseToken,7);
resolve(responseToken);
}
});
Expand Down
1 change: 1 addition & 0 deletions search/scripts/onload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ window.onload = function () {
//setInterval(function () { createTable() }, 1000);
function onLosdSerch() {
getByid("searchButton").onclick();
auth();
}
loadLdcmview(onLosdSerch);
}
Expand Down