invalid client id gmgngngngngng ntm
This commit is contained in:
parent
d8a650bdd3
commit
c3e9ffcb6d
3 changed files with 92 additions and 16 deletions
|
|
@ -1,2 +1,20 @@
|
||||||
// FIXME: This file should handle the auth redirection
|
import * as auth from "../../../utils/auth"
|
||||||
// Get the code from the URL parameters and redirect to the relevant page
|
|
||||||
|
let code;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const params = new URLSearchParams(window.location.search)
|
||||||
|
code = params.get("code")
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
console.error("Unable to retrieve code")
|
||||||
|
alert("Unable to retrieve code")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! await auth.getToken(code)) {
|
||||||
|
console.error("Unable to retrieve token")
|
||||||
|
alert("Unable to retrieve token")
|
||||||
|
}
|
||||||
|
|
||||||
|
console.debug("Redirecting...")
|
||||||
|
window.location = import.meta.env.VITE_URL
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,17 @@ import * as redirect from "./redirect";
|
||||||
*/
|
*/
|
||||||
async function getToken(code) {
|
async function getToken(code) {
|
||||||
|
|
||||||
const endpoint = `${VITE_AUTH_URL}/...`;
|
const auth_url = import.meta.env.VITE_URL;
|
||||||
|
const endpoint = `${auth_url}/auth-api/token`;
|
||||||
const formData = {
|
const formData = {
|
||||||
"grant_type": "authorization_code",
|
"grant_type": "authorization_code",
|
||||||
"code": code,
|
"code": code,
|
||||||
"redirect_uri": redirect.createLink(),
|
"redirect_uri": redirect.createLink(),
|
||||||
"client_id": process.env.VITE_CLIENT_ID
|
"client_id": import.meta.env.VITE_CLIENT_ID
|
||||||
}
|
}
|
||||||
const request = {
|
const request = {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
// mode: 'cors',
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
},
|
},
|
||||||
|
|
@ -23,10 +25,17 @@ async function getToken(code) {
|
||||||
body: new URLSearchParams(formData)
|
body: new URLSearchParams(formData)
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await fetch(endpoint, request)
|
try {
|
||||||
if (!response.ok) {
|
const response = await fetch(endpoint, request)
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(response.statusText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
console.error("Failed to retrieve OIDC token")
|
console.error("Failed to retrieve OIDC token")
|
||||||
console.debug(response)
|
alert("Failed to retrieve OIDC token")
|
||||||
|
|
||||||
|
console.error(err)
|
||||||
|
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
redirect.redirectToLoginPage()
|
redirect.redirectToLoginPage()
|
||||||
|
|
@ -41,9 +50,10 @@ async function getToken(code) {
|
||||||
id_token
|
id_token
|
||||||
} = response.body
|
} = response.body
|
||||||
|
|
||||||
|
// console.debug("Saving token and refresh tokens...")
|
||||||
localStorage.setItem("token", id_token);
|
localStorage.setItem("token", id_token);
|
||||||
localStorage.setItem("refresh_token", refresh_token);
|
localStorage.setItem("refresh_token", refresh_token);
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,10 +61,56 @@ async function getToken(code) {
|
||||||
/**
|
/**
|
||||||
* @param {string} refreshToken the refresh token (optional)
|
* @param {string} refreshToken the refresh token (optional)
|
||||||
* @returns {boolean} whether the token has been refreshed or not
|
* @returns {boolean} whether the token has been refreshed or not
|
||||||
* @warn NOT IMPLEMENTED
|
|
||||||
*/
|
*/
|
||||||
// TODO
|
async function refreshToken(refreshToken) {
|
||||||
// async function refreshToken(refreshToken) {}
|
|
||||||
|
const auth_url = import.meta.env.VITE_URL;
|
||||||
|
const endpoint = `${auth_url}/auth-api/token`;
|
||||||
|
const formData = {
|
||||||
|
client_id: import.meta.env.VITE_CLIENT_ID,
|
||||||
|
client_secret: "...",
|
||||||
|
grant_type: "code",
|
||||||
|
refresh_token: refreshToken,
|
||||||
|
scope: "epita profile picture",
|
||||||
|
}
|
||||||
|
const request = {
|
||||||
|
method: "POST",
|
||||||
|
// mode: 'cors',
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
},
|
||||||
|
|
||||||
|
body: new URLSearchParams(formData)
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(endpoint, request)
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(response.statusText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error("Failed to retrieve OIDC token")
|
||||||
|
|
||||||
|
localStorage.clear();
|
||||||
|
redirect.redirectToLoginPage()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
// access_token,
|
||||||
|
// token_type,
|
||||||
|
refresh_token,
|
||||||
|
// expires_in,
|
||||||
|
id_token
|
||||||
|
} = response.body
|
||||||
|
|
||||||
|
// console.debug("Saving token and refresh tokens...")
|
||||||
|
localStorage.setItem("token", id_token);
|
||||||
|
localStorage.setItem("refresh_token", refresh_token);
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {boolean} true if the user is authenticated, false otherwise
|
* @returns {boolean} true if the user is authenticated, false otherwise
|
||||||
|
|
@ -75,6 +131,8 @@ async function authenticate() {
|
||||||
// missing functions:
|
// missing functions:
|
||||||
// - authedAPIRequest (makes an authenticated request to the API)
|
// - authedAPIRequest (makes an authenticated request to the API)
|
||||||
|
|
||||||
module.exports = {
|
export {
|
||||||
|
getToken,
|
||||||
|
// refreshToken,
|
||||||
|
authenticate
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,13 @@
|
||||||
*/
|
*/
|
||||||
function createLink() {
|
function createLink() {
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
client_id: process.env.VITE_CLIENT_ID,
|
client_id: import.meta.env.VITE_CLIENT_ID,
|
||||||
response_type: "code",
|
response_type: "code",
|
||||||
redirect_uri: process.env.VITE_URL,
|
redirect_uri: import.meta.env.VITE_URL + "/complete/epita/",
|
||||||
scope: "epita profile picture"
|
scope: "epita profile picture"
|
||||||
});
|
});
|
||||||
|
|
||||||
const base_url = process.env.VITE_AUTH_URL
|
const base_url = import.meta.env.VITE_AUTH_URL
|
||||||
return `${base_url}/authorize?${params}`
|
return `${base_url}/authorize?${params}`
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue