2025-02-13 12:21:25 +01:00
|
|
|
import { createApp, ref } from 'vue'
|
2025-01-25 02:23:43 +01:00
|
|
|
import Keycloak from "keycloak-js"
|
2025-01-30 20:34:22 +01:00
|
|
|
import VueKeycloakJs from '@dsb-norge/vue-keycloak-js'
|
2025-01-31 03:12:36 +01:00
|
|
|
import axios, { type AxiosInstance } from 'axios'
|
2025-01-25 02:23:43 +01:00
|
|
|
import App from './App.vue'
|
|
|
|
|
2025-01-31 03:12:36 +01:00
|
|
|
interface Settings {
|
|
|
|
keycloakUri: string
|
|
|
|
realm: string
|
|
|
|
clientId: string
|
|
|
|
sso: boolean
|
|
|
|
resourceServerUrl: string
|
|
|
|
resourceScopes: string[]
|
|
|
|
authProvider: string
|
|
|
|
tokenSandbox: boolean
|
|
|
|
}
|
|
|
|
|
2025-02-13 12:21:25 +01:00
|
|
|
export interface Resource {
|
|
|
|
name: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Resources {
|
|
|
|
[name: string]: Resource
|
|
|
|
}
|
|
|
|
|
2025-01-31 03:12:36 +01:00
|
|
|
export let settings: Settings
|
|
|
|
export let authServer: AxiosInstance
|
|
|
|
export let resourceServer: AxiosInstance
|
|
|
|
|
2025-01-31 04:36:38 +01:00
|
|
|
// The settings.json file is expected at the server's base url
|
2025-01-31 03:12:36 +01:00
|
|
|
axios.get("settings.json").then().then(
|
|
|
|
resp => {
|
|
|
|
settings = resp.data
|
|
|
|
resourceServer = axios.create({
|
|
|
|
baseURL: settings.resourceServerUrl,
|
|
|
|
timeout: 10000
|
|
|
|
})
|
|
|
|
authServer = axios.create({
|
|
|
|
baseURL: '/',
|
|
|
|
timeout: 10000
|
|
|
|
})
|
|
|
|
app.use(VueKeycloakJs, {
|
|
|
|
config: {
|
|
|
|
url: settings.keycloakUri,
|
|
|
|
realm: settings.realm,
|
|
|
|
clientId: settings.clientId
|
|
|
|
},
|
|
|
|
init: {
|
|
|
|
onLoad: settings.sso ? "check-sso" : "login-required",
|
|
|
|
scope: "openid email profile " + settings.resourceScopes.join(" ")
|
|
|
|
},
|
|
|
|
onReady(keycloak: Keycloak) {
|
|
|
|
initializeTokenInterceptor(keycloak)
|
2025-02-07 05:00:27 +01:00
|
|
|
checkPerms('links-to-check')
|
2025-01-31 03:12:36 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
app.mount("#app")
|
|
|
|
}
|
|
|
|
)
|
2025-01-25 02:23:43 +01:00
|
|
|
|
2025-01-30 20:34:22 +01:00
|
|
|
|
|
|
|
function initializeTokenInterceptor(keycloak: Keycloak) {
|
|
|
|
authServer.interceptors.request.use(axiosSettings => {
|
|
|
|
if (keycloak.authenticated) {
|
|
|
|
axiosSettings.headers.Authorization = `Bearer ${keycloak.token}`
|
2025-01-31 03:12:36 +01:00
|
|
|
axiosSettings.headers.auth_provider = settings.authProvider
|
2025-01-30 20:34:22 +01:00
|
|
|
}
|
|
|
|
return axiosSettings
|
|
|
|
}, error => {
|
|
|
|
return Promise.reject(error)
|
|
|
|
})
|
|
|
|
resourceServer.interceptors.request.use(axiosSettings => {
|
2025-01-25 02:23:43 +01:00
|
|
|
if (keycloak.authenticated) {
|
2025-01-30 20:34:22 +01:00
|
|
|
axiosSettings.headers.Authorization = `Bearer ${keycloak.token}`
|
2025-01-31 03:12:36 +01:00
|
|
|
axiosSettings.headers.auth_provider = settings.authProvider
|
2025-01-25 02:23:43 +01:00
|
|
|
}
|
2025-01-30 20:34:22 +01:00
|
|
|
return axiosSettings
|
2025-01-25 02:23:43 +01:00
|
|
|
}, error => {
|
|
|
|
return Promise.reject(error)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-02-13 12:21:25 +01:00
|
|
|
export async function checkResource(elem: HTMLLinkElement) {
|
|
|
|
const url = elem.getAttribute("resource-name")
|
2025-02-07 16:45:27 +01:00
|
|
|
if (!url) return
|
|
|
|
await resourceServer.get(url).then(
|
2025-02-07 05:00:27 +01:00
|
|
|
resp => {
|
|
|
|
elem.classList.add("hasResponseStatus")
|
|
|
|
elem.classList.add("status-" + resp.status)
|
|
|
|
elem.title = "Response code: " + resp.status + " - " + resp.statusText
|
|
|
|
}).catch(err => {
|
|
|
|
elem.classList.add("hasResponseStatus")
|
|
|
|
elem.classList.add("status-" + err.response.status)
|
|
|
|
elem.title = "Response code: " + err.response.status + " - " + err.response.statusText
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkPerms(className: string) {
|
2025-02-13 12:21:25 +01:00
|
|
|
// Scan elements with className and check the respose
|
2025-02-07 05:00:27 +01:00
|
|
|
var rootElems = document.getElementsByClassName(className)
|
|
|
|
Array.from(rootElems).forEach(elem =>
|
2025-02-13 12:21:25 +01:00
|
|
|
Array.from(elem.children).forEach(elem => checkResource(<HTMLLinkElement>elem))
|
2025-02-07 05:00:27 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2025-01-30 20:34:22 +01:00
|
|
|
const app = createApp(App)
|