This commit is contained in:
162
app/ayanova/src/utils/authUtil.js
Normal file
162
app/ayanova/src/utils/authUtil.js
Normal file
@@ -0,0 +1,162 @@
|
||||
import decode from "jwt-decode";
|
||||
import config from "./config";
|
||||
import logger from "./logit";
|
||||
//import axios from 'axios';
|
||||
//import auth0 from 'auth0-js';
|
||||
//import Router from 'vue-router';
|
||||
//import Auth0Lock from 'auth0-lock';
|
||||
|
||||
//https://stackoverflow.com/questions/15551652/javascript-enum-flag-check
|
||||
const AuthorizationRoles = {
|
||||
///<summary>No role set</summary>
|
||||
NoRole: 0,
|
||||
///<summary>BizAdminLimited</summary>
|
||||
BizAdminLimited: 1,
|
||||
///<summary>BizAdminFull</summary>
|
||||
BizAdminFull: 2,
|
||||
///<summary>DispatchLimited</summary>
|
||||
DispatchLimited: 4,
|
||||
///<summary>DispatchFull</summary>
|
||||
DispatchFull: 8,
|
||||
///<summary>InventoryLimited</summary>
|
||||
InventoryLimited: 16,
|
||||
///<summary>InventoryFull</summary>
|
||||
InventoryFull: 32,
|
||||
///<summary>AccountingFull</summary>
|
||||
AccountingFull: 64, //No limited role, not sure if there is a need
|
||||
///<summary>TechLimited</summary>
|
||||
TechLimited: 128,
|
||||
///<summary>TechFull</summary>
|
||||
TechFull: 256,
|
||||
///<summary>SubContractorLimited</summary>
|
||||
SubContractorLimited: 512,
|
||||
///<summary>SubContractorFull</summary>
|
||||
SubContractorFull: 1024,
|
||||
///<summary>ClientLimited</summary>
|
||||
ClientLimited: 2048,
|
||||
///<summary>ClientFull</summary>
|
||||
ClientFull: 4096,
|
||||
///<summary>OpsAdminLimited</summary>
|
||||
OpsAdminLimited: 8192,
|
||||
///<summary>OpsAdminFull</summary>
|
||||
OpsAdminFull: 16384
|
||||
}; //end AuthorizationRoles
|
||||
|
||||
const TOKEN_KEY = "apitoken";
|
||||
|
||||
const USER_ROLES = AuthorizationRoles.NoRole;
|
||||
|
||||
// const CLIENT_ID = '{AUTH0_CLIENT_ID}';
|
||||
// const CLIENT_DOMAIN = '{AUTH0_DOMAIN}';
|
||||
// const REDIRECT = 'YOUR_CALLBACK_URL';
|
||||
// const SCOPE = '{SCOPE}';
|
||||
// const AUDIENCE = 'AUDIENCE_ATTRIBUTE';
|
||||
|
||||
// var auth = new auth0.WebAuth({
|
||||
// clientID: CLIENT_ID,
|
||||
// domain: CLIENT_DOMAIN
|
||||
// });
|
||||
|
||||
export function processLogin(response) {
|
||||
//validate token (ensure it's *our* token at least, the server will do the real validation on requests)
|
||||
//response.data.token
|
||||
//store token in central store
|
||||
//todo: put token into localstorage later once this validation is worked out
|
||||
|
||||
//is token present?
|
||||
if (!response || !response.data || !response.data.token) {
|
||||
logger.log("auth::processLogin -> token empty");
|
||||
return Promise.reject();
|
||||
}
|
||||
const token = decode(response.data.token);
|
||||
|
||||
if (!token || !token.iss) {
|
||||
logger.log("auth::processLogin -> token empty");
|
||||
return Promise.reject();
|
||||
}
|
||||
|
||||
if (token.iss != "ayanova.com") {
|
||||
logger.log("auth::processLogin -> token invalid (iss)", token.iss);
|
||||
return Promise.reject();
|
||||
}
|
||||
|
||||
config.apiToken = response.data.token;
|
||||
config.userId = Number(token.id);
|
||||
config.roles = token["ayanova/roles"];
|
||||
|
||||
logger.log("User " + token.id + " logged in");
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
// var router = new Router({
|
||||
// mode: 'history',
|
||||
// });
|
||||
|
||||
export function processLogout() {
|
||||
logger.log("Logout");
|
||||
clearToken();
|
||||
//router.go('/');
|
||||
}
|
||||
|
||||
// export function requireAuth(to, from, next) {
|
||||
// if (!isLoggedIn()) {
|
||||
// next({
|
||||
// path: '/',
|
||||
// query: { redirect: to.fullPath }
|
||||
// });
|
||||
// } else {
|
||||
// next();
|
||||
// }
|
||||
// }
|
||||
|
||||
export function getIdToken() {
|
||||
return localStorage.getItem(TOKEN_KEY);
|
||||
}
|
||||
|
||||
function clearToken() {
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
}
|
||||
|
||||
// // Helper function that will allow us to extract the access_token and id_token
|
||||
// function getParameterByName(name) {
|
||||
// let match = RegExp("[#&]" + name + "=([^&]*)").exec(window.location.hash);
|
||||
// return match && decodeURIComponent(match[1].replace(/\+/g, " "));
|
||||
// }
|
||||
|
||||
// Get and store id_token in local storage
|
||||
export function setToken(token) {
|
||||
localStorage.setItem(TOKEN_KEY, token);
|
||||
}
|
||||
|
||||
export function isLoggedIn() {
|
||||
//const token = getToken();
|
||||
return !!config.apiToken && !isTokenExpired(config.apiToken);
|
||||
}
|
||||
|
||||
function getTokenExpirationDate(encodedToken) {
|
||||
const token = decode(encodedToken);
|
||||
if (!token.exp) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const date = new Date(0);
|
||||
date.setUTCSeconds(token.exp);
|
||||
|
||||
return date;
|
||||
}
|
||||
|
||||
function isTokenExpired(token) {
|
||||
const expirationDate = getTokenExpirationDate(token);
|
||||
return expirationDate < new Date();
|
||||
}
|
||||
|
||||
//================ ROLES =================
|
||||
//https://stackoverflow.com/questions/39359740/what-are-enum-flags-in-typescript
|
||||
export function hasRole(role) {
|
||||
return role === (USER_ROLES & role);
|
||||
// if ((role & flags.ERROR) == flags.ERROR) {
|
||||
// alert("ERROR IS SET");
|
||||
// }
|
||||
}
|
||||
|
||||
//TODO: Auth JWT needs to return roles as an int enum
|
||||
Reference in New Issue
Block a user