Skip to content

Commit

Permalink
Add sso flow
Browse files Browse the repository at this point in the history
  • Loading branch information
aneelac22 committed Aug 13, 2024
1 parent a47e095 commit 9dba31f
Showing 1 changed file with 67 additions and 8 deletions.
75 changes: 67 additions & 8 deletions pkg/assets/static/redirectpage.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// this is where we load our config from
const configURL = "/api/v1/authconfig";
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const link = urlParams.get("link");
const keyword = urlParams.get("keyword");
const selectedId = urlParams.get("selectedId");
const consoleUrl = "https://console.redhat.com/openshift/sandbox";
const baseUrl = `https://${data.consoleURL}/`;
const appendedUrl = `${link}/ns/${data.defaultUserNamespace}`;

const params = {
keyword,
selectedId,
};
const redirectUrl = new URL(baseUrl + appendedUrl);

Object.keys(params).forEach((key) => {
if (params[key]) {
Expand All @@ -19,8 +19,11 @@ Object.keys(params).forEach((key) => {
});

function handleSuccess(data) {
const baseUrl = `https://${data.consoleURL}/`;
const appendedUrl = `${link}/ns/${data.defaultUserNamespace}`;
const redirectUrl = new URL(baseUrl + appendedUrl);
if (data.status != "ready") {
window.location.href = "https://console.redhat.com/openshift/sandbox";
window.location.href = consoleUrl;
} else {
window.location.href =
link === "notebookController"
Expand All @@ -32,22 +35,78 @@ function handleSuccess(data) {
}

function handleError() {
window.location.href = "https://console.redhat.com/openshift/sandbox";
window.location.href = consoleUrl;
}

if (keycloak) {
fetch(configURL)
.then(response => response.json())
.then(data => {
loadAuthLibrary(
data["auth-client-library-url"],
function () {
console.log("client library load success!");
var clientConfig = JSON.parse(data["auth-client-config"]);
console.log(
"using client configuration: " + JSON.stringify(clientConfig)
);
keycloak = new Keycloak(clientConfig);
keycloak
.init({
onLoad: "check-sso",
silentCheckSsoRedirectUri:
window.location.origin + "/silent-check-sso.html",
})
.then(function (authenticated) {
if (authenticated == true) {
console.log("user is authenticated");
// start 15s interval token refresh.
intervalRefRefresh = setInterval(refreshToken, 15000);
keycloak
.loadUserInfo()
.then(function (data) {
console.log("retrieved user info..");
idToken = keycloak.idToken;
showUser(
data.preferred_username,
data.sub,
data.original_sub
);
})
.catch(function () {
console.log("Failed to pull in user data");
showError("Failed to pull in user data.");
handleError();
});
} else {
console.log("user not authenticated");
hideUser();
hideAll();
idToken = null;
show("state-getstarted");
handleError();
}
})
.catch(function () {
console.log("Failed to initialize authorization");
showError("Failed to initialize authorization.");
handleError();
});
})
});

if (idToken) {
fetch(
"registration-service-toolchain-host-operator.apps.sandbox.x8i5.p1.openshiftapps.com/api/v1/signup",
{
method: "GET",
headers: {
Authorization: "Bearer" + keycloak.token,
Authorization: "Bearer" + idToken,
},
}
)
.then((response) => response.json())
.then((data) => handleSuccess(data))
.catch((error) => handleError(error));
} else {
login();
getJSON();
}

0 comments on commit 9dba31f

Please sign in to comment.