-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
samples/Contruum/Contruum.Server/Pages/Connect/CheckSession.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
@page | ||
@{ | ||
Layout = null; | ||
} | ||
|
||
<!DOCTYPE html> | ||
<title>OIDC Check Session</title> | ||
<script> | ||
const sessionCookieName = '@Contruum.Server.SessionIdExtensions.CookieName'; | ||
async function checkSession(origin, message) { | ||
try { | ||
if (!origin || !message) { | ||
return 'error'; | ||
} | ||
const [clientId, sessionState] = message.split(' '); | ||
if (!clientId || !sessionState) { | ||
return 'error'; | ||
} | ||
const [clientHash, salt] = sessionState.split('.'); | ||
if (!clientHash || !salt) { | ||
return 'error'; | ||
} | ||
const sessionId = document.cookie.split('; ').find(c => c.startsWith(sessionCookieName + '='))?.split('=')?.[1]; | ||
if (!sessionId) { | ||
return 'changed'; | ||
} | ||
const utf8Bytes = new TextEncoder().encode(clientId + origin + sessionId + salt); | ||
const hashBuffer = await crypto.subtle.digest('SHA-256', utf8Bytes); | ||
const hashBytes = new Uint8Array(hashBuffer); | ||
const hashBase64 = btoa(Array.from(hashBytes, byte => String.fromCharCode(byte)).join('')); | ||
const hashBase64Url = hashBase64.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', ''); | ||
return clientHash === hashBase64Url ? 'unchanged' : 'changed'; | ||
} catch (e) { | ||
console.error('OIDC Check Session error', e) | ||
return 'error'; | ||
} | ||
} | ||
window.addEventListener('message', async function (e) { | ||
const result = await checkSession(e.origin, e.data); | ||
e.source.postMessage(result, e.origin); | ||
}, false); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Security.Cryptography; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Contruum.Server; | ||
|
||
public static class SessionIdExtensions | ||
{ | ||
public const string CookieName = "oidc_session"; | ||
|
||
public static void IssueSessionCookie(this HttpResponse response) | ||
{ | ||
var sessionId = RandomNumberGenerator.GetHexString(32); | ||
response.Cookies.Append( | ||
CookieName, | ||
sessionId, | ||
new() | ||
{ | ||
SameSite = SameSiteMode.None, | ||
Secure = true, | ||
HttpOnly = false, | ||
}); | ||
} | ||
|
||
public static void DeleteSessionCookie(this HttpResponse response) | ||
{ | ||
response.Cookies.Delete(CookieName); | ||
} | ||
|
||
public static string? GetSessionId(this HttpRequest request) | ||
{ | ||
return request.Cookies[CookieName]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters