@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
65 lines (64 loc) • 2.66 kB
JavaScript
export default class GMFManager {
context;
constructor(context) {
this.context = context;
}
get isOAuth() {
return this.context.configManager.Config.oauth !== undefined;
}
get state() {
return this.context.stateManager.state;
}
get gmfConfigForOAuth() {
return this.context.configManager.Config.oauth.geomapfish;
}
get gmfConfigForGmfAuth() {
return this.context.configManager.Config.gmfauth;
}
async loginWithToken() {
// NOTE: With the new OICD Worflow that has been integrated in GMF 2.9, this is not needed any more.
// But for OICD integration before 2.9, this is still needed (This is for the moment specific to workflow at BS)
// TOOD REG : Remove this code when MapBS will be migrated to the standard GMF flow
if (this.gmfConfigForOAuth.loginUrl) {
console.debug('Auth: 2.1. Backend login with token');
if (!this.isOAuth) {
throw new Error('Login with JWT token if the issuer does not support oAuth is not supported.');
}
const gmfLoginResponse = await fetch(this.gmfConfigForOAuth.loginUrl, {
headers: {
Authorization: `Bearer ${this.state.oauth.tokens?.access_token}`
}
});
if (!gmfLoginResponse.ok) {
throw new Error(gmfLoginResponse.statusText);
}
}
}
async logout() {
console.debug('Auth: 4. Backend logout');
let logoutUrl = null;
if (this.isOAuth && this.gmfConfigForOAuth.logoutUrl) {
// OIDC but Logout from Backend (case for BS)
logoutUrl = this.gmfConfigForOAuth.logoutUrl;
}
else if (!this.isOAuth) {
// GMF-Auth
logoutUrl = `${this.gmfConfigForGmfAuth.url}logout`;
}
// If logoutUrl is not set, it means we do not need to logout from backend.
// The token will be removed when loging out from the OIDC provider.
if (logoutUrl) {
const gmfLogoutResponse = await fetch(logoutUrl);
if (!gmfLogoutResponse.ok) {
throw new Error(gmfLogoutResponse.statusText);
}
}
this.state.oauth.userInfo = undefined;
this.state.oauth.status = 'backend.loggedOut';
}
async getUserInfo() {
console.debug('Auth: 3. Get UserInfo');
const userInfoUrl = this.isOAuth ? this.gmfConfigForOAuth.userInfoUrl : `${this.gmfConfigForGmfAuth.url}loginuser`;
this.state.oauth.userInfo = await fetch(userInfoUrl).then((r) => r.json());
}
}