Commit 495ec912 authored by Nicolas Mérigot's avatar Nicolas Mérigot

feature: add linter

parent 54c43e24
{
"parser": "babel-eslint",
"extends": [
"airbnb-base"
],
"env": {
"browser": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
},
"rules": {
"import/no-unresolved": 0,
"import/extensions": [
0,
"always"
],
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -7,19 +7,36 @@
"type": "git",
"url": "git@git.happy-dev.fr:startinblox/framework/sib-oidc.git"
},
"scripts": {
"lint": "eslint --ext .js .",
"test": "npm run lint",
"serve": "live-server --ignore=**/node_modules/** --no-browser"
},
"author": "Startinblox",
"license": "MIT",
"release": {
"branch": "master",
"plugins": [
["@semantic-release/commit-analyzer", {
"preset": "angular",
"releaseRules": [
{"type": "major", "release": "major"},
{"type": "minor", "release": "minor"},
{"type": "/.*/", "release": "patch"}
]
}],
[
"@semantic-release/commit-analyzer",
{
"preset": "angular",
"releaseRules": [
{
"type": "major",
"release": "major"
},
{
"type": "minor",
"release": "minor"
},
{
"type": "/.*/",
"release": "patch"
}
]
}
],
"@semantic-release/release-notes-generator",
"@semantic-release/gitlab",
"@semantic-release/npm"
......@@ -30,5 +47,12 @@
},
"dependencies": {
"oidc-client": "^1.6.1"
},
"devDependencies": {
"babel-eslint": "^10.0.1",
"eslint": "^5.12.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
"live-server": "^1.2.1"
}
}
import 'https://unpkg.com/oidc-client@1.6';
import { SIBBase } from './core/src/index.js';
Log.logger = console;
Log.level = Log.INFO;
Log.logger = console; // eslint-disable-line no-undef
Log.level = Log.INFO; // eslint-disable-line no-undef
const defaultSettings = {
redirect_uri: location.origin,
post_logout_redirect_uri: location.origin,
redirect_uri: window.location.origin,
post_logout_redirect_uri: window.location.origin,
// authority: 'https://test-paris.happy-dev.fr/openid/',
// client_id: '833925',
response_type: 'id_token token',
......@@ -20,11 +20,12 @@ class SIBOidc extends HTMLElement {
* @typedef {Object} State
* @property {string} value 'login' or 'logout' or null
* @property {token} string CRSF token
* @property {previous_uri} string Previous uri
* @property {previousUri} string Previous uri
*/
constructor() {
super();
this.state = null;
this.manager = null;
this.user = null;
// duplicate default settings in this.settings
......@@ -41,7 +42,7 @@ class SIBOidc extends HTMLElement {
this.settings = Object.assign({}, defaultSettings, this.dataset);
// initialize OIDC Manager
this.manager = new UserManager(this.settings);
this.manager = new UserManager(this.settings); // eslint-disable-line no-undef
this.install();
this.processState();
......@@ -62,6 +63,7 @@ class SIBOidc extends HTMLElement {
* @name uninstall
* Remove method and property previously added on SIBBase
*/
// eslint-disable-next-line class-methods-use-this
uninstall() {
SIBBase.prototype.login = null;
SIBBase.prototype.logout = null;
......@@ -74,16 +76,19 @@ class SIBOidc extends HTMLElement {
* @returns {State} - The state
*/
getState() {
const state = localStorage.getItem('oidc_state');
if (state) {
return JSON.parse(state);
} else {
return {
value: null,
token: null,
previous_uri: null,
};
if (!this.state) {
const state = localStorage.getItem('oidc_state');
if (state) {
this.state = JSON.parse(state);
} else {
this.state = {
value: null,
token: null,
previousUri: null,
};
}
}
return this.state;
}
/** @function
......@@ -96,10 +101,12 @@ class SIBOidc extends HTMLElement {
const state = {
value,
token: (value === null) ? null : Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5),
previous_uri: (value === null) ? null : location.href,
previousUri: (value === null) ? null : window.location.href,
};
localStorage.setItem('oidc_state', JSON.stringify(state));
this.state = state;
return state;
}
......@@ -131,14 +138,14 @@ class SIBOidc extends HTMLElement {
switch (state.value) {
case 'login':
this.loginCallback(state.token);
break;
break;
case 'logout':
this.logoutCallback(state.token);
break;
break;
default:
this.setState();
}
} catch(e) {
} catch (e) {
this.clearState();
}
}
......@@ -163,14 +170,13 @@ class SIBOidc extends HTMLElement {
const processDOM = async () => {
const user = await this.getUser();
const id = user.profile.website;
const selector = `[bind-user]:not([data-src="${id}"])`;
const elements = document.querySelectorAll(selector);
for (const element of elements) {
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') {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', processDOM);
} else {
await processDOM();
......@@ -193,14 +199,14 @@ class SIBOidc extends HTMLElement {
* Finish the login procedure
*/
async loginCallback() {
const { token, previous_uri } = this.getState();
const { token, previousUri } = this.getState();
const user = await this.manager.signinRedirectCallback();
if (user.state !== token) {
throw new Error('CRSF token doesnt match');
}
this.user = user;
this.setState();
location.href = previous_uri;
window.location.href = previousUri;
}
/** @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