|
| 1 | +const clientId = 'openeo-public'; |
| 2 | +const redirectUri = window.location.origin; |
| 3 | +console.log("Using Redirect URI: " + redirectUri) |
| 4 | +const scope = 'openid profile email'; |
| 5 | +const oidcConfigUrl = 'https://auth.test.eoepca.org/realms/eoepca/.well-known/openid-configuration'; |
| 6 | +let authEndpoint, tokenEndpoint, userInfoEndpoint; |
| 7 | + |
| 8 | +async function fetchOidcConfig() { |
| 9 | + const response = await fetch(oidcConfigUrl); |
| 10 | + const oidcConfig = await response.json(); |
| 11 | + authEndpoint = oidcConfig.authorization_endpoint; |
| 12 | + tokenEndpoint = oidcConfig.token_endpoint; |
| 13 | + userInfoEndpoint = oidcConfig.userinfo_endpoint; |
| 14 | +} |
| 15 | + |
| 16 | +function generateRandomString(length) { |
| 17 | + const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 18 | + let randomString = ''; |
| 19 | + for (let i = 0; i < length; i++) { |
| 20 | + const randomIndex = Math.floor(Math.random() * charset.length); |
| 21 | + randomString += charset.charAt(randomIndex); |
| 22 | + } |
| 23 | + return randomString; |
| 24 | +} |
| 25 | + |
| 26 | +function base64UrlEncode(str) { |
| 27 | + return btoa(str).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); |
| 28 | +} |
| 29 | + |
| 30 | +function sha256(plain) { |
| 31 | + const shaObj = new jsSHA('SHA-256', 'TEXT'); |
| 32 | + shaObj.update(plain); |
| 33 | + const hash = shaObj.getHash('BYTES'); |
| 34 | + return hash; |
| 35 | +} |
| 36 | + |
| 37 | +async function login() { |
| 38 | + const codeVerifier = generateRandomString(128); |
| 39 | + sessionStorage.setItem('codeVerifier', codeVerifier); |
| 40 | + |
| 41 | + const codeChallenge = base64UrlEncode(sha256(codeVerifier)); |
| 42 | + const state = generateRandomString(16); |
| 43 | + |
| 44 | + await fetchOidcConfig(); |
| 45 | + |
| 46 | + const authUrl = `${authEndpoint}?response_type=code&client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&code_challenge=${codeChallenge}&code_challenge_method=S256&state=${state}`; |
| 47 | + window.location = authUrl; |
| 48 | +} |
| 49 | + |
| 50 | +async function handleCallback(redirectTo) { |
| 51 | + const urlParams = new URLSearchParams(window.location.search); |
| 52 | + const code = urlParams.get('code'); |
| 53 | + const state = urlParams.get('state'); |
| 54 | + const codeVerifier = sessionStorage.getItem('codeVerifier'); |
| 55 | + |
| 56 | + if (code) { |
| 57 | + await fetchOidcConfig(); |
| 58 | + |
| 59 | + const tokenResponse = await fetch(tokenEndpoint, { |
| 60 | + method: 'POST', |
| 61 | + headers: { |
| 62 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 63 | + }, |
| 64 | + body: new URLSearchParams({ |
| 65 | + grant_type: 'authorization_code', |
| 66 | + code: code, |
| 67 | + client_id: clientId, |
| 68 | + redirect_uri: redirectUri, |
| 69 | + code_verifier: codeVerifier |
| 70 | + }) |
| 71 | + }); |
| 72 | + const tokenData = await tokenResponse.json(); |
| 73 | + sessionStorage.setItem('accessToken', tokenData.access_token); |
| 74 | + await fetchUserInfo(tokenData.access_token); |
| 75 | + } |
| 76 | + |
| 77 | + window.location = redirectTo; |
| 78 | +} |
| 79 | + |
| 80 | +async function fetchUserInfo(accessToken) { |
| 81 | + const userInfoResponse = await fetch(userInfoEndpoint, { |
| 82 | + headers: { |
| 83 | + 'Authorization': `Bearer ${accessToken}` |
| 84 | + } |
| 85 | + }); |
| 86 | + const userInfo = await userInfoResponse.json(); |
| 87 | + localStorage.setItem('userInfo', JSON.stringify(userInfo)); |
| 88 | +} |
| 89 | + |
| 90 | +async function home() { |
| 91 | + const urlParams = new URLSearchParams(window.location.search); |
| 92 | + const code = urlParams.get('code'); |
| 93 | + |
| 94 | + if (code) { |
| 95 | + await handleCallback(window.location.origin); |
| 96 | + } |
| 97 | + |
| 98 | + const userInfo = JSON.parse(localStorage.getItem('userInfo')); |
| 99 | + if (userInfo) { |
| 100 | + document.getElementById('userInfo').innerText = `Hello, ${userInfo.name}`; |
| 101 | + } else { |
| 102 | + console.log("Userinfo is not set"); |
| 103 | + document.getElementById('userInfo').innerText = ''; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function logout() { |
| 108 | + sessionStorage.removeItem('accessToken'); |
| 109 | + localStorage.removeItem('userInfo'); |
| 110 | + document.getElementById('userInfo').innerText = ''; |
| 111 | +} |
| 112 | + |
| 113 | +document.getElementById('loginButton').addEventListener('click', login); |
| 114 | +document.getElementById('logoutButton').addEventListener('click', logout); |
| 115 | + |
| 116 | +window.onload = home; |
0 commit comments