|
| 1 | +{% extends "oauth2_provider/base.html" %} |
| 2 | + |
| 3 | +{% block title %}Check Session IFrame{% endblock %} |
| 4 | + |
| 5 | +{% block js %} |
| 6 | +<script language="JavaScript" type="text/javascript"> |
| 7 | + async function sha256(message) { |
| 8 | + // Encode the message as UTF-8 |
| 9 | + const msgBuffer = new TextEncoder().encode(message) |
| 10 | + |
| 11 | + // Generate the hash |
| 12 | + const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer) |
| 13 | + const hashArray = Array.from(new Uint8Array(hashBuffer)) |
| 14 | + return hashArray.map(b => b.toString(16).padStart(2, '0')).join('') |
| 15 | + } |
| 16 | + |
| 17 | + window.addEventListener("message", receiveMessage) |
| 18 | + |
| 19 | + async function receiveMessage(e) { |
| 20 | + // e.data has client_id and session_state |
| 21 | + if (!e.data || typeof e.data != 'string' || e.data == 'error') { |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + try { |
| 26 | + const [clientId, sessionStateImage] = e.data.split(' ') |
| 27 | + const [sessionState, salt] = sessionStateImage.split('.') |
| 28 | + |
| 29 | + let userAgentState |
| 30 | + try { |
| 31 | + userAgentState = getUserAgentState() |
| 32 | + } |
| 33 | + catch (err) { |
| 34 | + userAgentState = '' |
| 35 | + } |
| 36 | + const knownImage = await sha256(`${clientId} ${e.origin} ${userAgentState} ${salt}`) |
| 37 | + |
| 38 | + const status = sessionState == knownImage ? 'unchanged' : 'changed' |
| 39 | + e.source.postMessage(status, e.origin) |
| 40 | + } catch(err) { |
| 41 | + e.source.postMessage(`error: ${err}`, e.origin) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + function getUserAgentState() { |
| 47 | + const cookieName = "{{ cookie_name }}" |
| 48 | + if (document.cookie && document.cookie !== '') { |
| 49 | + const cookies = document.cookie.split(';') |
| 50 | + for (var i = 0; i < cookies.length; i++) { |
| 51 | + const cookie = cookies[i].trim() |
| 52 | + // Does this cookie string begin with the name we want? |
| 53 | + if (cookie.substring(0, cookieName.length + 1) === (cookieName + '=')) { |
| 54 | + return decodeURIComponent(cookie.substring(cookieName.length + 1)) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + throw new Error('OIDC Session Cookie not set') |
| 59 | + } |
| 60 | +</script> |
| 61 | +{% endblock %} |
| 62 | + |
| 63 | +{% block content %}OIDC Session Management OP Iframe{% endblock content %} |
0 commit comments