2022-05-05 21:08:48 +08:00
|
|
|
|
|
|
|
var mgr;
|
2022-09-01 20:53:42 +08:00
|
|
|
var _management_logger;
|
2022-05-05 21:08:48 +08:00
|
|
|
|
2023-03-31 15:51:22 +08:00
|
|
|
/*
|
|
|
|
function oauth_initialize_if_required_deprecated() {
|
2022-05-05 21:08:48 +08:00
|
|
|
rabbit_port = window.location.port ? ":" + window.location.port : ""
|
2022-10-04 22:43:18 +08:00
|
|
|
rabbit_path_prefix = window.location.pathname.replace(/(\/js\/oidc-oauth\/.*$|\/+$)/, "")
|
2022-10-04 15:13:57 +08:00
|
|
|
rabbit_base_uri = window.location.protocol + "//" + window.location.hostname
|
2022-10-04 22:43:18 +08:00
|
|
|
+ rabbit_port + rabbit_path_prefix
|
2022-05-05 21:08:48 +08:00
|
|
|
|
|
|
|
var request = new XMLHttpRequest();
|
2022-10-04 15:13:57 +08:00
|
|
|
request.open("GET", rabbit_base_uri + "/api/auth", false);
|
2022-05-05 21:08:48 +08:00
|
|
|
request.send(null);
|
|
|
|
if (request.status === 200) {
|
2022-05-10 22:22:04 +08:00
|
|
|
return oauth_initialize(JSON.parse(request.responseText));
|
2022-10-04 15:13:57 +08:00
|
|
|
} else {
|
2022-09-02 05:17:58 +08:00
|
|
|
return { "enabled" : false };
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-03-31 15:51:22 +08:00
|
|
|
*/
|
2022-10-04 22:43:18 +08:00
|
|
|
|
2023-03-31 15:51:22 +08:00
|
|
|
function rabbit_base_uri() {
|
2023-05-18 23:17:11 +08:00
|
|
|
return window.location.protocol + "//" + window.location.hostname + rabbit_port() + rabbit_path_prefix()
|
|
|
|
}
|
|
|
|
function rabbit_path_prefix() {
|
|
|
|
return window.location.pathname.replace(/(\/js\/oidc-oauth\/.*$|\/+$)/, "");
|
|
|
|
}
|
|
|
|
function rabbit_port() {
|
|
|
|
return window.location.port ? ":" + window.location.port : "";
|
2023-03-31 15:51:22 +08:00
|
|
|
}
|
2022-05-10 22:22:04 +08:00
|
|
|
function auth_settings_apply_defaults(authSettings) {
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-04 15:13:57 +08:00
|
|
|
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_initialize(authSettings) {
|
2022-05-05 21:08:48 +08:00
|
|
|
oauth = {
|
|
|
|
"logged_in": false,
|
2022-09-02 05:17:58 +08:00
|
|
|
"enabled" : authSettings.oauth_enabled,
|
2022-05-26 22:49:07 +08:00
|
|
|
"authority" : authSettings.oauth_provider_url
|
2022-11-08 16:41:47 +08:00
|
|
|
};
|
2022-05-05 21:08:48 +08:00
|
|
|
|
2022-09-02 16:33:11 +08:00
|
|
|
if (!oauth.enabled) return oauth;
|
2022-05-05 21:08:48 +08:00
|
|
|
|
2022-11-08 16:41:47 +08:00
|
|
|
oauth.sp_initiated = true;
|
|
|
|
if (authSettings.oauth_initiated_logon_type == "idp_initiated") {
|
|
|
|
oauth.sp_initiated = false;
|
|
|
|
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,
|
2022-05-10 22:22:04 +08:00
|
|
|
response_type: authSettings.oauth_response_type,
|
2023-04-13 17:21:17 +08:00
|
|
|
scope: authSettings.oauth_scopes,
|
2022-05-05 21:08:48 +08:00
|
|
|
resource: authSettings.oauth_resource_id,
|
2023-03-31 15:51:22 +08:00
|
|
|
redirect_uri: rabbit_base_uri() + "/js/oidc-oauth/login-callback.html",
|
|
|
|
post_logout_redirect_uri: rabbit_base_uri() + "/",
|
2022-05-05 21:08:48 +08:00
|
|
|
|
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
|
|
|
};
|
2022-11-08 16:41:47 +08:00
|
|
|
if (authSettings.oauth_client_secret != "") {
|
|
|
|
oidcSettings.client_secret = authSettings.oauth_client_secret;
|
|
|
|
}
|
|
|
|
if (authSettings.oauth_metadata_url != "") {
|
|
|
|
oidcSettings.metadataUrl = authSettings.oauth_metadata_url;
|
|
|
|
}
|
2022-05-05 21:08:48 +08:00
|
|
|
|
2022-09-01 20:53:42 +08:00
|
|
|
oidc.Log.setLevel(oidc.Log.DEBUG);
|
|
|
|
oidc.Log.setLogger(console);
|
2022-05-05 21:08:48 +08:00
|
|
|
|
|
|
|
mgr = new oidc.UserManager(oidcSettings);
|
2022-11-08 16:41:47 +08:00
|
|
|
oauth.readiness_url = mgr.settings.metadataUrl;
|
2022-05-05 21:08:48 +08:00
|
|
|
|
2022-09-01 20:53:42 +08:00
|
|
|
_management_logger = new oidc.Logger("Management");
|
2022-05-05 21:08:48 +08:00
|
|
|
|
2022-05-05 22:22:47 +08:00
|
|
|
mgr.events.addAccessTokenExpiring(function() {
|
2022-11-08 16:41:47 +08:00
|
|
|
_management_logger.info("token expiring...");
|
2022-05-05 22:22:47 +08:00
|
|
|
});
|
2022-07-07 17:10:03 +08:00
|
|
|
mgr.events.addAccessTokenExpired(function() {
|
2022-11-08 16:41:47 +08:00
|
|
|
_management_logger.info("token expired!!");
|
2022-07-07 17:10:03 +08:00
|
|
|
});
|
2022-05-05 22:22:47 +08:00
|
|
|
mgr.events.addSilentRenewError(function(err) {
|
2022-11-08 16:41:47 +08:00
|
|
|
_management_logger.error("token expiring failed due to ", err);
|
2022-05-05 22:22:47 +08:00
|
|
|
});
|
2022-07-07 17:10:03 +08:00
|
|
|
mgr.events.addUserLoaded(function(user) {
|
2022-12-19 22:39:32 +08:00
|
|
|
console.log("addUserLoaded setting oauth.access_token ")
|
|
|
|
oauth.access_token = user.access_token // DEPRECATED
|
|
|
|
set_token_auth(oauth.access_token)
|
2022-11-08 16:41:47 +08:00
|
|
|
});
|
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
|
|
|
|
});
|
2022-09-01 20:53:42 +08:00
|
|
|
_management_logger.info(message)
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|
|
|
|
|
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-11-08 16:41:47 +08:00
|
|
|
if (oauth.sp_initiated) {
|
2022-09-01 20:53:42 +08:00
|
|
|
mgr.signinRedirect({ state: { } }).then(function() {
|
|
|
|
_management_logger.debug("signinRedirect done");
|
2022-05-05 21:08:48 +08:00
|
|
|
}).catch(function(err) {
|
2022-09-01 20:53:42 +08:00
|
|
|
_management_logger.error(err);
|
2022-11-08 16:41:47 +08:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
location.href = oauth.authority;
|
|
|
|
}
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|
2022-10-04 15:13:57 +08:00
|
|
|
|
2022-05-26 22:49:07 +08:00
|
|
|
function oauth_redirectToHome(oauth) {
|
2022-12-13 21:09:18 +08:00
|
|
|
console.log("oauth_redirectToHome set_token_auth")
|
|
|
|
set_token_auth(oauth.access_token)
|
|
|
|
go_to_home()
|
2022-11-08 16:41:47 +08:00
|
|
|
}
|
|
|
|
function go_to_home() {
|
2023-05-18 23:17:11 +08:00
|
|
|
location.href = rabbit_path_prefix() + "/"
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|
2022-11-08 16:41:47 +08:00
|
|
|
function go_to_authority() {
|
2022-12-13 21:09:18 +08:00
|
|
|
location.href = oauth.authority
|
2022-11-08 16:41:47 +08:00
|
|
|
}
|
2022-05-26 22:49:07 +08:00
|
|
|
function oauth_redirectToLogin(error) {
|
2023-05-18 23:17:11 +08:00
|
|
|
if (!error) location.href = rabbit_path_prefix() + "/"
|
2022-05-26 22:49:07 +08:00
|
|
|
else {
|
2023-05-18 23:17:11 +08:00
|
|
|
location.href = rabbit_path_prefix() + "/?error=" + error
|
2022-05-26 22:49:07 +08:00
|
|
|
}
|
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-12-13 21:09:18 +08:00
|
|
|
_management_logger.error(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-11-08 16:41:47 +08:00
|
|
|
if (oauth.sp_initiated) {
|
2022-12-13 21:09:18 +08:00
|
|
|
mgr.signoutRedirect()
|
2022-11-08 16:41:47 +08:00
|
|
|
} else {
|
2022-12-13 21:09:18 +08:00
|
|
|
go_to_authority()
|
2022-11-08 16:41:47 +08:00
|
|
|
}
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|
2022-05-10 22:22:04 +08:00
|
|
|
function oauth_completeLogout() {
|
2022-12-13 21:09:18 +08:00
|
|
|
clear_auth()
|
|
|
|
mgr.signoutRedirectCallback().then(_ => oauth_redirectToLogin())
|
2022-05-05 21:08:48 +08:00
|
|
|
}
|