Created
June 8, 2022 13:54
-
-
Save kalgon/368c28cb2599454509ce8bd10d8c06a5 to your computer and use it in GitHub Desktop.
OpenID Connect
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.11.5/oidc-client.js"></script> | |
<script>new Oidc.UserManager().signinPopupCallback()</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
<html> | |
<head> | |
<style> | |
@keyframes loading { | |
to { | |
background-position: 2rem; | |
} | |
} | |
.loading { | |
position: relative; | |
user-select: none; | |
} | |
.loading:after { | |
inset: 0; | |
content: ''; | |
position: absolute; | |
animation: loading .5s linear infinite; | |
background: linear-gradient(45deg, #0004 25%, 25%, #0008 50%, 50%, #0004 75%, 75%, #0008) 0 / 2rem 2rem; | |
} | |
</style> | |
</head> | |
<body> | |
<button>fetch pools</button> | |
<pre></pre> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/oidc-client/1.11.5/oidc-client.js"></script> | |
<script> | |
const loading = (callback) => async (event) => { | |
event.target.classList.add('loading'); | |
try { | |
await callback(); | |
} finally { | |
event.target.classList.remove('loading'); | |
} | |
}; | |
const secure = (userManager, fetch = window.fetch) => async (url, options = {}) => { | |
const user = await userManager.getUser() ?? await userManager.signinPopup(); | |
return fetch(url, (({ headers, ...rest }) => ({ headers: { ...headers, authorization: `Bearer ${user.id_token}` }, ...rest }))(options)); | |
}; | |
const userManager = new Oidc.UserManager({ | |
authority: 'https://my.authority/url', | |
client_id: 'my-client-id', | |
redirect_uri: new URL('callback.html', window.location), | |
popupWindowFeatures: 'popup' | |
}); | |
const secureFetch = secure(userManager); | |
document.querySelector('button').addEventListener('click', loading(async () => { | |
const response = await secureFetch('https://my.protected/resource'); | |
document.querySelector('pre').innerHTML = JSON.stringify(await response.json(), null, 4); | |
})); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment