thibaud frere
commited on
Commit
·
8ddcc75
1
Parent(s):
25544a5
fix
Browse files- index.html +36 -5
index.html
CHANGED
|
@@ -55,13 +55,40 @@
|
|
| 55 |
document.getElementById('userinfo').textContent = '';
|
| 56 |
}
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
// Sign in button
|
| 59 |
-
document.getElementById('signin').onclick = function () {
|
| 60 |
const state = Math.random().toString(36).slice(2);
|
|
|
|
|
|
|
|
|
|
| 61 |
localStorage.setItem('hf_oauth_state', state);
|
|
|
|
|
|
|
| 62 |
const url = `${HF_OAUTH_URL}?client_id=${CLIENT_ID}` +
|
| 63 |
`&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` +
|
| 64 |
-
`&response_type=code&scope=openid%20profile
|
|
|
|
|
|
|
| 65 |
window.location = url;
|
| 66 |
};
|
| 67 |
|
|
@@ -70,6 +97,7 @@
|
|
| 70 |
localStorage.removeItem('hf_oauth_token');
|
| 71 |
localStorage.removeItem('hf_oauth_userinfo');
|
| 72 |
localStorage.removeItem('hf_oauth_state');
|
|
|
|
| 73 |
showLoggedOut();
|
| 74 |
window.history.replaceState({}, '', window.location.pathname);
|
| 75 |
};
|
|
@@ -90,12 +118,15 @@
|
|
| 90 |
console.log('window.huggingface.variables:', window.huggingface?.variables);
|
| 91 |
console.log('Toutes les clés disponibles:', Object.keys(window.huggingface?.variables || {}));
|
| 92 |
|
| 93 |
-
//
|
|
|
|
|
|
|
|
|
|
| 94 |
const body = new URLSearchParams({
|
| 95 |
-
client_id: CLIENT_ID,
|
| 96 |
grant_type: 'authorization_code',
|
| 97 |
code: code,
|
| 98 |
-
redirect_uri: REDIRECT_URI
|
|
|
|
| 99 |
});
|
| 100 |
console.log('Body de la requête:', body.toString());
|
| 101 |
document.getElementById('status').textContent = 'Exchanging code for token...';
|
|
|
|
| 55 |
document.getElementById('userinfo').textContent = '';
|
| 56 |
}
|
| 57 |
|
| 58 |
+
// Helper pour générer PKCE
|
| 59 |
+
function generateCodeVerifier() {
|
| 60 |
+
const array = new Uint8Array(32);
|
| 61 |
+
crypto.getRandomValues(array);
|
| 62 |
+
return btoa(String.fromCharCode.apply(null, array))
|
| 63 |
+
.replace(/\+/g, '-')
|
| 64 |
+
.replace(/\//g, '_')
|
| 65 |
+
.replace(/=/g, '');
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
async function generateCodeChallenge(verifier) {
|
| 69 |
+
const encoder = new TextEncoder();
|
| 70 |
+
const data = encoder.encode(verifier);
|
| 71 |
+
const digest = await crypto.subtle.digest('SHA-256', data);
|
| 72 |
+
return btoa(String.fromCharCode.apply(null, new Uint8Array(digest)))
|
| 73 |
+
.replace(/\+/g, '-')
|
| 74 |
+
.replace(/\//g, '_')
|
| 75 |
+
.replace(/=/g, '');
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
// Sign in button
|
| 79 |
+
document.getElementById('signin').onclick = async function () {
|
| 80 |
const state = Math.random().toString(36).slice(2);
|
| 81 |
+
const codeVerifier = generateCodeVerifier();
|
| 82 |
+
const codeChallenge = await generateCodeChallenge(codeVerifier);
|
| 83 |
+
|
| 84 |
localStorage.setItem('hf_oauth_state', state);
|
| 85 |
+
localStorage.setItem('hf_oauth_code_verifier', codeVerifier);
|
| 86 |
+
|
| 87 |
const url = `${HF_OAUTH_URL}?client_id=${CLIENT_ID}` +
|
| 88 |
`&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` +
|
| 89 |
+
`&response_type=code&scope=openid%20profile` +
|
| 90 |
+
`&state=${state}&prompt=consent` +
|
| 91 |
+
`&code_challenge=${codeChallenge}&code_challenge_method=S256`;
|
| 92 |
window.location = url;
|
| 93 |
};
|
| 94 |
|
|
|
|
| 97 |
localStorage.removeItem('hf_oauth_token');
|
| 98 |
localStorage.removeItem('hf_oauth_userinfo');
|
| 99 |
localStorage.removeItem('hf_oauth_state');
|
| 100 |
+
localStorage.removeItem('hf_oauth_code_verifier');
|
| 101 |
showLoggedOut();
|
| 102 |
window.history.replaceState({}, '', window.location.pathname);
|
| 103 |
};
|
|
|
|
| 118 |
console.log('window.huggingface.variables:', window.huggingface?.variables);
|
| 119 |
console.log('Toutes les clés disponibles:', Object.keys(window.huggingface?.variables || {}));
|
| 120 |
|
| 121 |
+
// Utiliser PKCE comme dans huggingface.js
|
| 122 |
+
const codeVerifier = localStorage.getItem('hf_oauth_code_verifier');
|
| 123 |
+
console.log('Code verifier:', codeVerifier ? 'présent' : 'absent');
|
| 124 |
+
|
| 125 |
const body = new URLSearchParams({
|
|
|
|
| 126 |
grant_type: 'authorization_code',
|
| 127 |
code: code,
|
| 128 |
+
redirect_uri: REDIRECT_URI,
|
| 129 |
+
code_verifier: codeVerifier
|
| 130 |
});
|
| 131 |
console.log('Body de la requête:', body.toString());
|
| 132 |
document.getElementById('status').textContent = 'Exchanging code for token...';
|