Spaces:
Running
Running
Download shared/js/auth.js from Tonic/uas-eus-webhook: direct link, hf CLI and curl.
- Browser
- Download file 4.48 kB
-
https://huggingface.co/spaces/Tonic/uas-eus-webhook/resolve/main/shared/js/auth.js
- Command line
-
hf download hf://spaces/Tonic/uas-eus-webhook/shared/js/auth.js
-
curl -L -o auth.js https://huggingface.co/spaces/Tonic/uas-eus-webhook/resolve/main/shared/js/auth.js
4.48 kB
| /* UASAuth — customer login via Hugging Face OAuth2 + PKCE (no client secret in the browser). | |
| Flow: browser -> huggingface.co/oauth/authorize -> api.uas.eus/auth/hf/callback (holds the client | |
| secret) -> HF /oauth/token -> /api/whoami-v2 -> JWT issued -> redirect back to /account.html. | |
| Interim (hf.space, no shared domain): API returns the access JWT in the URL fragment; we keep it in | |
| sessionStorage. Once api.uas.eus is live, the API also sets an httpOnly refresh cookie (Domain=.uas.eus) | |
| and the site calls /api/me with credentials:'include'. */ | |
| (function () { | |
| var TOKEN_KEY = 'uas_access_token'; | |
| function cfg() { return (window.SITE_CONFIG && window.SITE_CONFIG.auth) || {}; } | |
| function b64url(buf) { | |
| return btoa(String.fromCharCode.apply(null, new Uint8Array(buf))) | |
| .replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); | |
| } | |
| async function pkce() { | |
| var verifier = b64url(crypto.getRandomValues(new Uint8Array(32))); | |
| var digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier)); | |
| return { verifier: verifier, challenge: b64url(digest) }; | |
| } | |
| function decodeJwt(token) { | |
| try { | |
| var p = token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'); | |
| return JSON.parse(atob(p)); | |
| } catch (e) { return null; } | |
| } | |
| var listeners = []; | |
| function emit() { var s = api.session(); listeners.forEach(function (f) { f(s); }); } | |
| var api = { | |
| login: async function () { | |
| var c = cfg(); | |
| if (!c.hfClientId || c.hfClientId.indexOf('PENDING') === 0) { | |
| alert('HF OAuth client id not configured yet (see websites/README.md).'); | |
| return; | |
| } | |
| var p = await pkce(); | |
| var state = b64url(crypto.getRandomValues(new Uint8Array(16))); | |
| sessionStorage.setItem('oauth_state', state); | |
| sessionStorage.setItem('oauth_verifier', p.verifier); | |
| var q = new URLSearchParams({ | |
| client_id: c.hfClientId, | |
| redirect_uri: c.redirectUri, | |
| response_type: 'code', | |
| scope: 'openid profile email', | |
| state: state, | |
| code_challenge: p.challenge, | |
| code_challenge_method: 'S256' | |
| }); | |
| location.href = 'https://huggingface.co/oauth/authorize?' + q.toString(); | |
| }, | |
| logout: function () { | |
| sessionStorage.removeItem(TOKEN_KEY); | |
| api._session = null; | |
| emit(); | |
| }, | |
| session: function () { | |
| if (api._session) return api._session; | |
| var tok = sessionStorage.getItem(TOKEN_KEY); | |
| if (!tok) return null; | |
| var payload = decodeJwt(tok); | |
| if (!payload || (payload.exp && payload.exp * 1000 < Date.now())) { | |
| sessionStorage.removeItem(TOKEN_KEY); | |
| return null; | |
| } | |
| api._session = { token: tok, hfId: payload.sub, name: payload.name, entitlements: payload.ent || [] }; | |
| return api._session; | |
| }, | |
| isPro: function () { | |
| var s = api.session(); | |
| return !!(s && s.entitlements.indexOf('pro') >= 0); | |
| }, | |
| onChange: function (f) { listeners.push(f); }, | |
| /* called on pages that receive the API redirect: /account.html#access_token=... */ | |
| handleFragment: function () { | |
| if (location.hash.indexOf('#access_token=') === 0) { | |
| sessionStorage.setItem(TOKEN_KEY, location.hash.slice('#access_token='.length)); | |
| history.replaceState(null, '', location.pathname + location.search); | |
| api._session = null; | |
| emit(); | |
| return true; | |
| } | |
| return false; | |
| }, | |
| /* authoritative check against the API (works with httpOnly cookie too) */ | |
| me: async function () { | |
| var c = cfg(); | |
| if (!c.apiBase) return api.session(); // config not loaded yet — local session only | |
| try { | |
| var r = await fetch(c.apiBase + '/api/me', { credentials: 'include' }); | |
| if (!r.ok) return null; | |
| var j = await r.json(); | |
| api._session = { token: sessionStorage.getItem(TOKEN_KEY), hfId: j.hf_id, name: j.name, entitlements: j.entitlements || [] }; | |
| emit(); | |
| return api._session; | |
| } catch (e) { return api.session(); } | |
| }, | |
| simToken: async function () { | |
| var c = cfg(); var s = api.session(); | |
| if (!c.apiBase) return null; | |
| var headers = {}; | |
| if (s && s.token) headers['Authorization'] = 'Bearer ' + s.token; | |
| var r = await fetch(c.apiBase + '/api/sim-token', { credentials: 'include', headers: headers }); | |
| if (!r.ok) return null; | |
| return (await r.json()).access; | |
| } | |
| }; | |
| window.UASAuth = api; | |
| })(); | |