2022-05-05 21:08:48 +08:00
|
|
|
|
|
|
|
//import {UserManager} from "./scripts/oidc-client-ts/oidc-client-ts.js"
|
|
|
|
// import {axios} from "./scripts/axios/axios.min.js"
|
|
|
|
|
|
|
|
|
|
|
|
var mgr;
|
|
|
|
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_initialize_if_required() {
|
2022-05-05 21:08:48 +08:00
|
|
|
rabbit_port = window.location.port ? ":" + window.location.port : ""
|
|
|
|
rabbit_base_uri = window.location.protocol + "//" + window.location.hostname + rabbit_port
|
|
|
|
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open('GET', rabbit_base_uri + '/api/auth', false);
|
|
|
|
request.send(null);
|
|
|
|
if (request.status === 200) {
|
2022-05-10 22:22:04 +08:00
|
|
|
return oauth_initialize(JSON.parse(request.responseText));
|
2022-05-05 21:08:48 +08:00
|
|
|
}else {
|
|
|
|
return { "enable" : false };
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-05-10 22:22:04 +08:00
|
|
|
function auth_settings_apply_defaults(authSettings) {
|
2022-05-24 23:07:39 +08:00
|
|
|
if (authSettings.enable_uaa == "true") {
|
2022-05-10 22:22:04 +08:00
|
|
|
|
|
|
|
if (!authSettings.oauth_provider_url) {
|
|
|
|
authSettings.oauth_provider_url = authSettings.uaa_location
|
|
|
|
}
|
|
|
|
if (!authSettings.oauth_client_id) {
|
|
|
|
authSettings.oauth_client_id = authSettings.uaa_client_id
|
|
|
|
}
|
|
|
|
if (!authSettings.oauth_client_secret) {
|
|
|
|
authSettings.oauth_client_secret = authSettings.uaa_client_secret
|
|
|
|
}
|
2022-05-24 23:07:39 +08:00
|
|
|
if (!authSettings.oauth_scopes) {
|
|
|
|
authSettings.oauth_scopes = "openid profile " + authSettings.oauth_resource_id + ".*";
|
|
|
|
}
|
2022-05-10 22:22:04 +08:00
|
|
|
}
|
|
|
|
if (!authSettings.oauth_response_type) {
|
|
|
|
authSettings.oauth_response_type = "code"; // although the default value in oidc client
|
|
|
|
}
|
|
|
|
|
2022-05-24 23:07:39 +08:00
|
|
|
if (!authSettings.oauth_scopes) {
|
|
|
|
authSettings.oauth_scopes = "openid profile";
|
2022-05-10 22:22:04 +08:00
|
|
|
}
|
2022-05-05 21:08:48 +08:00
|
|
|
|
2022-05-10 22:22:04 +08:00
|
|
|
return authSettings;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function oauth_initialize(authSettings) {
|
2022-05-05 21:08:48 +08:00
|
|
|
oauth = {
|
|
|
|
"logged_in": false,
|
2022-05-26 22:49:07 +08:00
|
|
|
"enable" : authSettings.oauth_enable,
|
|
|
|
"authority" : authSettings.oauth_provider_url
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!oauth.enable) return oauth;
|
|
|
|
|
2022-05-10 22:22:04 +08:00
|
|
|
authSettings = auth_settings_apply_defaults(authSettings);
|
|
|
|
|
2022-05-05 21:08:48 +08:00
|
|
|
oidcSettings = {
|
|
|
|
//userStore: new WebStorageStateStore({ store: window.localStorage }),
|
|
|
|
authority: authSettings.oauth_provider_url,
|
|
|
|
client_id: authSettings.oauth_client_id,
|
|
|
|
client_secret: authSettings.oauth_client_secret,
|
2022-05-10 22:22:04 +08:00
|
|
|
response_type: authSettings.oauth_response_type,
|
2022-05-24 23:07:39 +08:00
|
|
|
scope: authSettings.oauth_scopes, // for uaa we may need to include <resource-server-id>.*
|
2022-05-05 21:08:48 +08:00
|
|
|
resource: authSettings.oauth_resource_id,
|
|
|
|
redirect_uri: rabbit_base_uri + "/js/oidc-oauth/login-callback.html",
|
|
|
|
post_logout_redirect_uri: rabbit_base_uri + "/js/oidc-oauth/logout-callback.html",
|
|
|
|
|
|
|
|
filterProtocolClaims: true,
|
2022-05-05 22:22:47 +08:00
|
|
|
automaticSilentRenew: true,
|
2022-05-05 21:08:48 +08:00
|
|
|
revokeAccessTokenOnSignout: true,
|
2022-05-31 21:08:32 +08:00
|
|
|
extraQueryParams: {
|
|
|
|
audience: authSettings.oauth_resource_id, // required by oauth0
|
|
|
|
},
|
2022-05-05 21:08:48 +08:00
|
|
|
};
|
|
|
|
if (authSettings.oauth_metadata_url != "") oidcSettings.metadataUrl = authSettings.oauth_metadata_url
|
|
|
|
|
2022-05-10 22:22:04 +08:00
|
|
|
if (authSettings.enable_uaa == true) {
|
2022-05-05 21:08:48 +08:00
|
|
|
// This is required for old versions of UAA because the newer ones do expose
|
|
|
|
// the end_session_endpoint on the oidc discovery endpoint, .a.k.a. metadataUrl
|
|
|
|
oidcSettings.metadataSeed = {
|
|
|
|
end_session_endpoint: authSettings.oauth_provider_url + '/logout.do'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mgr = new oidc.UserManager(oidcSettings);
|
|
|
|
oauth.readiness_url = mgr.settings.metadataUrl
|
|
|
|
|
|
|
|
oidc.Log.setLogger(console);
|
|
|
|
oidc.Log.setLevel(oidc.Log.INFO);
|
|
|
|
|
2022-05-05 22:22:47 +08:00
|
|
|
mgr.events.addAccessTokenExpiring(function() {
|
|
|
|
console.log("token expiring...");
|
|
|
|
});
|
2022-07-07 17:10:03 +08:00
|
|
|
mgr.events.addAccessTokenExpired(function() {
|
|
|
|
console.log("token expired !!");
|
|
|
|
});
|
2022-05-05 22:22:47 +08:00
|
|
|
mgr.events.addSilentRenewError(function(err) {
|
|
|
|
console.log("token expiring failed due to " + err);
|
|
|
|
});
|
2022-07-07 17:10:03 +08:00
|
|
|
mgr.events.addUserLoaded(function(user) {
|
|
|
|
let expiryDate = new Date(user.expires_at * 1000) // it is epoch in seconds
|
|
|
|
console.log("user loaded with token which expires at " + expiryDate);
|
|
|
|
oauth.access_token = user.access_token;
|
|
|
|
});
|
2022-05-05 22:22:47 +08:00
|
|
|
|
2022-05-05 21:08:48 +08:00
|
|
|
return oauth;
|
|
|
|
}
|
|
|
|
|
|
|
|
function log() {
|
|
|
|
message = ""
|
|
|
|
Array.prototype.forEach.call(arguments, function(msg) {
|
|
|
|
if (msg instanceof Error) {
|
|
|
|
msg = "Error: " + msg.message;
|
|
|
|
}
|
|
|
|
else if (typeof msg !== "string") {
|
|
|
|
msg = JSON.stringify(msg, null, 2);
|
|
|
|
}
|
|
|
|
message += msg
|
|
|
|
});
|
|
|
|
console.log(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_registerCallbacks() {
|
2022-05-05 21:08:48 +08:00
|
|
|
mgr.events.addUserLoaded(function (user) {
|
|
|
|
console.log("addUserLoaded=> ", user);
|
|
|
|
mgr.getUser().then(function() {
|
|
|
|
console.log("getUser loaded user after userLoaded event fired");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
mgr.events.addUserUnloaded(function (e) {
|
|
|
|
console.log("addUserUnloaded=> ", e);
|
|
|
|
});
|
|
|
|
|
|
|
|
mgr.events.addUserSignedIn(function (e) {
|
|
|
|
log("addUserSignedIn=> " , e);
|
|
|
|
});
|
|
|
|
mgr.events.addUserSignedOut(function (e) {
|
|
|
|
log("addUserSignedOut=> ", e);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_is_logged_in() {
|
2022-05-05 21:08:48 +08:00
|
|
|
return mgr.getUser().then(user => {
|
|
|
|
if (!user) {
|
|
|
|
return { "loggedIn": false };
|
|
|
|
}
|
|
|
|
return { "user": user, "loggedIn": !user.expired };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_initiateLogin() {
|
2022-06-23 22:25:15 +08:00
|
|
|
mgr.signinRedirect({ state: { } /*, useReplaceToNavigate: true*/ }).then(function() {
|
2022-05-05 21:08:48 +08:00
|
|
|
log("signinRedirect done");
|
|
|
|
}).catch(function(err) {
|
|
|
|
console.error(err);
|
|
|
|
log(err);
|
|
|
|
});
|
|
|
|
}
|
2022-05-26 22:49:07 +08:00
|
|
|
function oauth_redirectToHome(oauth) {
|
|
|
|
set_auth_pref(oauth.user_name + ':' + oauth.access_token);
|
2022-05-05 21:08:48 +08:00
|
|
|
location.href = "/"
|
|
|
|
}
|
2022-05-26 22:49:07 +08:00
|
|
|
function oauth_redirectToLogin(error) {
|
|
|
|
if (!error) location.href = "/"
|
|
|
|
else {
|
|
|
|
location.href = "/?error=" + error
|
|
|
|
}
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_completeLogin() {
|
2022-05-26 22:49:07 +08:00
|
|
|
mgr.signinRedirectCallback().then(user => oauth_redirectToHome(user)).catch(function(err) {
|
2022-05-05 21:08:48 +08:00
|
|
|
console.error(err);
|
|
|
|
log(err);
|
2022-05-26 22:49:07 +08:00
|
|
|
oauth_redirectToLogin(err)
|
2022-05-05 21:08:48 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_initiateLogout() {
|
2022-05-05 21:08:48 +08:00
|
|
|
mgr.signoutRedirect();
|
|
|
|
}
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_completeLogout() {
|
|
|
|
mgr.signoutRedirectCallback().then(_ => oauth_redirectToLogin());
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|