Cleanup
This commit is contained in:
parent
889c62f51e
commit
14c9091e77
4 changed files with 83 additions and 62 deletions
|
@ -11,7 +11,7 @@
|
||||||
"type-check": "vue-tsc --build"
|
"type-check": "vue-tsc --build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@dsb-norge/vue-keycloak-js": "3.0.1",
|
"@dsb-norge/vue-keycloak-js": "^3.0.1",
|
||||||
"axios": "1.7.9",
|
"axios": "1.7.9",
|
||||||
"keycloak-js": "^26.1.0",
|
"keycloak-js": "^26.1.0",
|
||||||
"vue": "3.5.13"
|
"vue": "3.5.13"
|
||||||
|
|
2
pnpm-lock.yaml
generated
2
pnpm-lock.yaml
generated
|
@ -9,7 +9,7 @@ importers:
|
||||||
.:
|
.:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@dsb-norge/vue-keycloak-js':
|
'@dsb-norge/vue-keycloak-js':
|
||||||
specifier: 3.0.1
|
specifier: ^3.0.1
|
||||||
version: 3.0.1(vue@3.5.13(typescript@5.6.3))
|
version: 3.0.1(vue@3.5.13(typescript@5.6.3))
|
||||||
axios:
|
axios:
|
||||||
specifier: 1.7.9
|
specifier: 1.7.9
|
||||||
|
|
11
src/App.vue
11
src/App.vue
|
@ -1,7 +1,8 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { HTTP } from '@/main'
|
import { authServer, resourceServer } from '@/main'
|
||||||
import type { AxiosResponseHeaders } from 'axios'
|
import type { AxiosResponseHeaders } from 'axios'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
|
import Keycloak from "keycloak-js"
|
||||||
import { useKeycloak } from '@dsb-norge/vue-keycloak-js'
|
import { useKeycloak } from '@dsb-norge/vue-keycloak-js'
|
||||||
import { settings } from '../settings'
|
import { settings } from '../settings'
|
||||||
|
|
||||||
|
@ -17,7 +18,7 @@ function manuallyRefreshAccessToken() {
|
||||||
async function doAuthenticatedRequest() {
|
async function doAuthenticatedRequest() {
|
||||||
// Doesn't really go anywhere, but as you see from the headers in the request
|
// Doesn't really go anywhere, but as you see from the headers in the request
|
||||||
// it contains the latest access token at all times
|
// it contains the latest access token at all times
|
||||||
const response = await HTTP.get('/oidc-test-web')
|
const response = await authServer.get('/oidc-test-web')
|
||||||
requestHeaders.value = response.config.headers
|
requestHeaders.value = response.config.headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,13 +31,16 @@ function accountManagemnt() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function get_resource(id: string) {
|
async function get_resource(id: string) {
|
||||||
await HTTP.get(`${settings.url}/${id}`).then(
|
if (!keycloak.keycloak) { return }
|
||||||
|
await keycloak.keycloak.updateToken(5000)
|
||||||
|
await resourceServer.get(id).then(
|
||||||
resp => resource.value = resp['data']
|
resp => resource.value = resp['data']
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
<div id="app">
|
||||||
<h1>OIDC-test - web client</h1>
|
<h1>OIDC-test - web client</h1>
|
||||||
<p class="center">
|
<p class="center">
|
||||||
Test the authentication and authorization, with OpenID Connect and OAuth2 with a Keycloak provider.
|
Test the authentication and authorization, with OpenID Connect and OAuth2 with a Keycloak provider.
|
||||||
|
@ -77,4 +81,5 @@ async function get_resource(id: string) {
|
||||||
<div class="token" :innerText="requestHeaders?.toString()">
|
<div class="token" :innerText="requestHeaders?.toString()">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
48
src/main.ts
48
src/main.ts
|
@ -1,38 +1,54 @@
|
||||||
import { createApp } from 'vue'
|
import { createApp } from 'vue'
|
||||||
import Keycloak from "keycloak-js"
|
import Keycloak from "keycloak-js"
|
||||||
import VueKeycloakJs, { useKeycloak } from '@dsb-norge/vue-keycloak-js'
|
import VueKeycloakJs from '@dsb-norge/vue-keycloak-js'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import { settings as app_settings } from "../settings"
|
import { settings } from "../settings"
|
||||||
|
|
||||||
export const HTTP = axios.create({
|
export const authServer = axios.create({
|
||||||
baseURL: '/',
|
baseURL: '/',
|
||||||
timeout: 10000
|
timeout: 10000
|
||||||
})
|
})
|
||||||
|
|
||||||
function initializeTokenInterceptor() {
|
export const resourceServer = axios.create({
|
||||||
HTTP.interceptors.request.use(settings => {
|
baseURL: settings.url,
|
||||||
const keycloak = useKeycloak()
|
timeout: 10000
|
||||||
|
})
|
||||||
|
|
||||||
|
function initializeTokenInterceptor(keycloak: Keycloak) {
|
||||||
|
authServer.interceptors.request.use(axiosSettings => {
|
||||||
if (keycloak.authenticated) {
|
if (keycloak.authenticated) {
|
||||||
settings.headers.Authorization = `Bearer ${keycloak.token}`
|
axiosSettings.headers.Authorization = `Bearer ${keycloak.token}`
|
||||||
settings.headers.auth_provider = app_settings.auth_provider
|
axiosSettings.headers.auth_provider = settings.auth_provider
|
||||||
}
|
}
|
||||||
return settings
|
return axiosSettings
|
||||||
|
}, error => {
|
||||||
|
return Promise.reject(error)
|
||||||
|
})
|
||||||
|
resourceServer.interceptors.request.use(axiosSettings => {
|
||||||
|
if (keycloak.authenticated) {
|
||||||
|
axiosSettings.headers.Authorization = `Bearer ${keycloak.token}`
|
||||||
|
axiosSettings.headers.auth_provider = settings.auth_provider
|
||||||
|
}
|
||||||
|
return axiosSettings
|
||||||
}, error => {
|
}, error => {
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
createApp(App)
|
const app = createApp(App)
|
||||||
.use(VueKeycloakJs, {
|
app.mount('#app')
|
||||||
|
app.use(VueKeycloakJs, {
|
||||||
config: {
|
config: {
|
||||||
url: 'https://philo.ydns.eu/auth/',
|
url: 'https://philo.ydns.eu/auth/',
|
||||||
realm: 'test',
|
realm: 'test',
|
||||||
clientId: 'oidc-test-web',
|
clientId: 'oidc-test-web',
|
||||||
},
|
},
|
||||||
init: { onLoad: app_settings.sso ? 'check-sso' : 'login-required' },
|
init: {
|
||||||
onReady(keycloak: Keycloak, vkk) {
|
onLoad: settings.sso ? 'check-sso' : 'login-required',
|
||||||
initializeTokenInterceptor()
|
scope: "openid email profile get:time get:bs"
|
||||||
},
|
},
|
||||||
})
|
onReady(keycloak: Keycloak) {
|
||||||
.mount('#app')
|
initializeTokenInterceptor(keycloak)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue