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