Commit 48ea2c21 authored by Jean-Baptiste's avatar Jean-Baptiste Committed by Clément

update: Use solid auth client instead of oidc client

parent 447b35d0
......@@ -26,14 +26,12 @@
<sib-auth>
<sib-auth-provider
data-authority="https://test-paris.happy-dev.fr/openid/"
data-client_id="234528"
data-authority="http://127.0.0.1:8000/"
data-id="paris"
>
</sib-auth-provider>
<sib-auth-provider
data-authority="https://test-paris.happy-dev.fr/openid/"
data-client_id="234528"
data-authority="http://127.0.0.1:8000/"
data-id="paris2"
>
</sib-auth-provider>
......@@ -74,7 +72,7 @@
update() {
const user = this.getUser();
if (user) {
result.innerHTML = `Bonjour ${user.profile.name} !`;
result.innerHTML = `Bonjour ${user} !`;
} else {
result.innerHTML = `Vous n'êtes pas connecté !`;
}
......
import 'https://unpkg.com/oidc-client@1.6';
Log.logger = console; // eslint-disable-line no-undef
Log.level = Log.INFO; // eslint-disable-line no-undef
import auth from 'https://dev.jspm.io/solid-auth-client';
const defaultSettings = {
redirect_uri: window.location.origin,
......@@ -24,55 +21,11 @@ class SIBAuthProvider extends HTMLElement {
constructor() {
super();
this.manager = null;
this.id = null;
}
/** @function
* @name connectedCallback
* When called, the OIDC manager is intancied
* with the params set in the component, render view
*/
async connectedCallback() {
const { authority, client_id, id } = this.dataset; // eslint-disable-line camelcase
this.id = id;
const settings = Object.assign({}, defaultSettings, { authority, client_id });
this.manager = new UserManager(settings); // eslint-disable-line no-undef
this.render(this.dataset);
}
/** @function
* @name processState
* Try to get user, the if a state is set, call the appropriate method
* If failed, clear state
*/
async processState(parent) {
const user = await this.manager.getUser();
const state = parent.getState();
if (user) {
parent.setUser(user);
}
if (state && state.value) {
try {
switch (state.value) {
case 'login':
this.loginCallback(parent);
break;
case 'logout':
this.logoutCallback(parent);
break;
default:
parent.clearState();
}
} catch (e) {
parent.clearState();
}
}
}
/** @function
*
* @name disconnectedCallback
* Remove manager
*/
......@@ -86,54 +39,9 @@ class SIBAuthProvider extends HTMLElement {
* @param {SIBAuth} parent - SIBAuth parent instance
*/
async login(parent) {
const { token } = parent.setState('login', this.id);
await this.manager.signinRedirect({
state: token,
});
}
/** @function
* @name loginCallback
* Finish the login procedure
* @param {SIBAuth} parent - SIBAuth parent instance
*/
async loginCallback(parent) {
const { token, previousUri } = parent.getState();
const user = await this.manager.signinRedirectCallback();
if (user.state !== token) {
throw new Error('CRSF token doesnt match');
}
parent.setUser(user);
parent.clearState();
window.location.href = previousUri;
auth.login(this.dataset.authority);
}
/** @function
* @name logout
* Start a logout procedure
* @param {SIBAuth} parent - SIBAuth parent instance
*/
async logout(parent) {
const { token } = parent.setState('logout', this.id);
await this.manager.signoutRedirect({
state: token,
});
}
/** @function
* @name logoutCallback
* Finish the logout procedure
* @param {SIBAuth} parent - SIBAuth parent instance
*/
async logoutCallback(parent) {
const { token } = parent.getState();
const signout = await this.manager.signoutRedirectCallback();
if (signout.state !== token) {
throw new Error('CRSF token doesnt match');
}
parent.setUser(null);
parent.clearState();
}
/** @function
* @name render
......
import 'https://unpkg.com/oidc-client@1.6';
import auth from 'https://dev.jspm.io/solid-auth-client';
import { SIBBase, Helpers } from 'https://unpkg.com/@startinblox/core@0.7';
const baseUrl = import.meta.url.replace(/\/[^/]*$/, '');
......@@ -27,9 +27,9 @@ class SIBAuth extends HTMLElement {
*/
async connectedCallback() {
this.install();
this.processState();
if (this.getUser()) {
this.dispatchUserInfo();
// this.processState();
if (await this.getUserWebId()) {
this.dispatchUserInfo(this.getUserWebId());
}
}
......@@ -38,7 +38,8 @@ class SIBAuth extends HTMLElement {
* Clear state and user, uninstall
*/
disconnectedCallback() {
this.setUser();
solid.auth.logout()
.then(() => alert('Goodbye!'));
this.clearState();
this.uninstall();
}
......@@ -51,7 +52,7 @@ class SIBAuth extends HTMLElement {
install() {
SIBBase.prototype.login = () => this.login();
SIBBase.prototype.logout = () => this.logout();
SIBBase.prototype.getUser = () => this.getUser();
SIBBase.prototype.getUser = () => this.getUserWebId();
this.bindUserObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes') {
......@@ -134,57 +135,6 @@ class SIBAuth extends HTMLElement {
}
/** @function
* @name getState
* Search in localStorage for previous OIDC state
* @returns {State} - The state
*/
getState() {
if (!this.state) {
const state = localStorage.getItem('oidc_state');
if (state) {
this.state = JSON.parse(state);
} else {
this.state = {
provider: null,
value: null,
token: null,
previousUri: null,
};
}
}
return this.state;
}
/** @function
* @name setState
* Set state in localStorage
* @param {string} value - 'login' or 'logout', default null
* @param {string} provider - the id of the provider
* @returns {State} - The state
*/
setState(value = null, provider = null) {
const state = {
value,
provider,
token: (value === null) ? null : Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5),
previousUri: (value === null) ? null : window.location.href,
};
localStorage.setItem('oidc_state', JSON.stringify(state));
this.state = state;
return state;
}
/** @function
* @name clearState
* Reset current state
*/
clearState() {
this.setState();
}
/** @function
* @name processState
* Try to get user, the if a state is set, call the appropriate provider
......@@ -199,13 +149,20 @@ class SIBAuth extends HTMLElement {
/** @function
* @name dispatchUserInfo
* Try to replace data-src by user iri on [bind-user] elements
* @param {User} userWebId - User
* Try to replace data-src by userWebId iri on [bind-user] elements
*/
// eslint-disable-next-line class-methods-use-this
async dispatchUserInfo() {
async dispatchUserInfo(id) {
const processDOM = async () => {
const elements = document.querySelectorAll('[bind-user]');
elements.forEach(element => this.applyUser(element));
/*
const elements = document.querySelectorAll(`[bind-user]:not([data-src="${id}"])`);
elements.forEach((element) => {
element.setAttribute('data-src', id);
});
*/
};
// check document state and add a hook on DOMContentLoaded if needed
if (document.readyState === 'loading') {
......@@ -228,34 +185,22 @@ class SIBAuth extends HTMLElement {
}
/** @function
* @name getUser
* @name getUserWebId
* Return User or undefined
* @return {User}
*/
getUser() {
if (this.user) {
return this.user;
}
const storedUser = localStorage.getItem('oidc_user');
if (!storedUser) {
return null;
}
this.user = JSON.parse(storedUser);
return this.user;
}
/** @function
* @name setUser
* Set User
* @param {User} user
*/
setUser(user) {
localStorage.setItem('oidc_user', JSON.stringify(user));
this.user = user;
this.dispatchUserInfo();
getUserWebId() {
return new Promise((resolve, error) => {
auth.trackSession(session => {
if (!session) {
console.log('The user is not logged in')
return resolve(null);
} else {
console.log(`The user is ${session.webId}`)
return resolve(session.webId)
}
})
});
}
/** @function
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment