Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

authentication-adal-pkg

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

authentication-adal-pkg - npm Package Compare versions

Comparing version 1.0.10 to 1.0.11

840

lib/index.js
'use strict';
function _defineProperty(e, n, t) {
return (
n in e
? Object.defineProperty(e, n, {
value: t,
enumerable: !0,
configurable: !0,
writable: !0
})
: (e[n] = t),
e
);
}
var _slicedToArray = (function() {
function e(e, n) {
var t = [],
r = !0,
o = !1,
i = void 0;
function sliceIterator(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (
var u, a = e[Symbol.iterator]();
!(r = (u = a.next()).done) && (t.push(u.value), !n || t.length !== n);
r = !0
);
} catch (e) {
(o = !0), (i = e);
var _i = arr[Symbol.iterator](), _s;
!(_n = (_s = _i.next()).done);
_n = true
) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
!r && a.return && a.return();
if (!_n && _i['return']) _i['return']();
} finally {
if (o) throw i;
if (_d) throw _e;
}
}
return t;
return _arr;
}
return function(n, t) {
if (Array.isArray(n)) return n;
if (Symbol.iterator in Object(n)) return e(n, t);
throw new TypeError('Invalid attempt to destructure non-iterable instance');
return function(arr, i) {
if (Array.isArray(arr)) {
return arr;
} else if (Symbol.iterator in Object(arr)) {
return sliceIterator(arr, i);
} else {
throw new TypeError(
'Invalid attempt to destructure non-iterable instance'
);
}
};
})();
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
window.AuthenticationContext = require('adal-angular');
var jwtDecode = require('jwt-decode'),
eandisAuthenticator = (function(e) {
var n = {
MOBILE: 'mobile',
BROWSER: 'browser',
STATE: 'state',
NONCE: 'nonce',
TOKEN: 'token',
TOKEN_EXPIRATION: 'token.expiration',
IN_PROGRESS: 'progess'
},
t = null,
r = null,
o = null,
i = null,
u = null,
a = null,
c = null,
l = null,
s = null,
f = null,
d = null,
O = null,
h = null,
m = '',
E = {},
g = function(e) {
(t = e.authority),
(r = e.tenant),
(o = e.redirectUriWeb),
(i = e.redirectUriIOS),
(u = e.redirectUriAndroid),
(a = e.redirectUriWindows),
(c = e.resourceUri),
(s = e.clientId),
(f = e.userId),
(l = e.loginResource);
},
v = function(e) {
if (e === n.BROWSER) {
var i = { tenant: r, clientId: s, redirectUri: o, loginResource: l };
d = new AuthenticationContext(i);
} else O = new Microsoft.ADAL.AuthenticationContext(t);
},
R = {
prefix: 'auth',
set: function(e, n) {
localStorage.setItem(this.prefix + '-' + e, n);
},
get: function(e) {
return localStorage.getItem(this.prefix + '-' + e);
},
remove: function(e) {
localStorage.removeItem(this.prefix + '-' + e);
var jwtDecode = require('jwt-decode');
var eandisAuthenticator = (function auth(config) {
var constants = {
MOBILE: 'mobile',
BROWSER: 'browser',
STATE: 'state',
NONCE: 'nonce',
TOKEN: 'token',
TOKEN_EXPIRATION: 'token.expiration',
IN_PROGRESS: 'progess'
};
var authority = null;
var tenant = null;
var redirectUriWeb = null;
var redirectUriIOS = null;
var redirectUriAndroid = null;
var redirectUriWindows = null;
var resourceUri = null;
var loginResource = null;
var clientId = null;
var userId = null;
var authenticationResult = null;
var auth = null;
var authContext = null;
var platformRedirectUri = null;
var localError = '';
var user = {};
var initialize = function initialize(config) {
authority = config.authority;
tenant = config.tenant;
redirectUriWeb = config.redirectUriWeb;
redirectUriIOS = config.redirectUriIOS;
redirectUriAndroid = config.redirectUriAndroid;
redirectUriWindows = config.redirectUriWindows;
resourceUri = config.resourceUri;
clientId = config.clientId;
userId = config.userId;
loginResource = config.loginResource;
};
var initAuthenticationContext = function initAuthenticationContext(platform) {
if (platform === constants.BROWSER) {
var _config = {
tenant: tenant,
clientId: clientId,
redirectUri: redirectUriWeb,
loginResource: loginResource
};
auth = new AuthenticationContext(_config);
} else {
authContext = new Microsoft.ADAL.AuthenticationContext(authority);
}
};
var storage = {
prefix: 'auth',
set: function set(key, value) {
localStorage.setItem(this.prefix + '-' + key, value);
},
get: function get(key) {
return localStorage.getItem(this.prefix + '-' + key);
},
remove: function remove(key) {
localStorage.removeItem(this.prefix + '-' + key);
}
};
storage.set(constants.IN_PROGRESS, false);
var getPlatform = function getPlatform() {
return (window.cordova && window.cordova.platformId) || 'browser';
};
var signIn = function signIn() {
return new Promise(function(resolve, reject) {
authenticate(resolve, reject);
});
};
var signOutMobile = function signOutMobile() {
return new Promise(function(resolve, reject) {
try {
if (!authContext) {
initAuthenticationContext(constants.MOBILE);
}
authContext.tokenCache.clear();
resolve();
} catch (err) {
reject(err);
}
// request naar https://login.windows.net/{tenantid or "common"}/oauth2/logout?post_logout_redirect_uri={URL}
});
};
var signOutBrowser = function signOutBrowser() {
return new Promise(function(resolve, reject) {
try {
if (!auth) {
initAuthenticationContext(constants.BROWSER);
}
auth.logOut();
storage.remove(constants.TOKEN);
resolve();
} catch (err) {
reject(err);
}
});
};
var signOut = function signOut() {
if (getPlatform() === constants.BROWSER) {
return signOutBrowser();
} else {
return signOutMobile();
}
};
var onAcquireToken = function onAcquireToken(errorDesc, token, error) {
return new Promise(function(resolve, reject) {
var acquireTokenCallback = function acquireTokenCallback(
desc,
token,
err
) {
if (err) reject(err);
else resolve(token);
};
R.set(n.IN_PROGRESS, !1);
var w = function() {
return (window.cordova && window.cordova.platformId) || 'browser';
},
I = function() {
return new Promise(function(e, n) {
K(e, n);
});
},
p = function() {
return new Promise(function(e, t) {
try {
O || v(n.MOBILE), O.tokenCache.clear(), e();
} catch (e) {
t(e);
}
});
},
T = function() {
return new Promise(function(e, t) {
try {
d || v(n.BROWSER), d.logOut(), R.remove(n.TOKEN), e();
} catch (e) {
t(e);
}
});
},
N = function() {
return w() === n.BROWSER ? T() : p();
},
S = function(e, t, r) {
return new Promise(function(e, o) {
var i = function(n, t, r) {
r ? o(r) : e(t);
};
r ? (d || v(n.BROWSER), d.acquireTokenPopup(s, null, null, i)) : e(t);
});
},
y = function(e, t, r) {
var o = R.get(n.TOKEN);
if (o) e(o);
else {
d || v(n.BROWSER), d.handleWindowCallback();
var i = function(n, r, o) {
S(n, r, o)
.then(e)
.catch(function(e) {
if ('login required' === e)
new Promise(function(e, n) {
K(e, n);
});
else t(e);
if (error) {
if (!auth) {
initAuthenticationContext(constants.BROWSER);
}
auth.acquireTokenPopup(clientId, null, null, acquireTokenCallback);
} else {
resolve(token);
}
});
};
var acquireTokenBrowser = function acquireTokenBrowser(resolve, reject, cb) {
var token = storage.get(constants.TOKEN);
if (!token) {
if (!auth) {
initAuthenticationContext(constants.BROWSER);
}
// Saves the token in the cache and calls the callback method (onAcquireToken) with error or token
auth.handleWindowCallback();
var acquireTokenCallback = function acquireTokenCallback(
errorDesc,
token,
error
) {
onAcquireToken(errorDesc, token, error)
.then(resolve)
.catch(function(e) {
if (e === 'login required') {
var authPromise = new Promise(function(resolve, reject) {
authenticate(resolve, reject);
});
};
d.acquireToken(s, i);
}
},
k = function(e, t) {
O || v(n.MOBILE),
O.tokenCache.readItems().then(function(n) {
var r = n.length - 1,
o = n[r] && n[r].accessToken;
o ? e(o) : t(new Error('No token found'));
} else {
reject(e);
}
});
},
A = function(e) {
return new Promise(function(t, r) {
try {
w() === n.BROWSER ? y(t, r, e) : k(t, r);
} catch (e) {
r(e);
}
});
},
P = function(e) {
switch (e.toLowerCase()) {
case 'android':
h = u;
break;
case 'ios':
h = i;
break;
case 'windows':
h = a;
};
auth.acquireToken(clientId, acquireTokenCallback);
} else {
resolve(token);
}
};
var acquireTokenMobile = function acquireTokenMobile(resolve, reject) {
if (!authContext) {
initAuthenticationContext(constants.MOBILE);
}
authContext.tokenCache.readItems().then(function(items) {
var index = items.length - 1;
var token = items[index] && items[index].accessToken;
if (token) {
resolve(token);
} else {
reject(new Error('No token found'));
}
});
};
var acquireToken = function acquireToken(cb) {
return new Promise(function(resolve, reject) {
try {
if (getPlatform() === constants.BROWSER) {
acquireTokenBrowser(resolve, reject, cb);
} else {
acquireTokenMobile(resolve, reject);
}
},
_ = function() {
return 'true' === R.get(n.IN_PROGRESS);
},
b = function() {
var e = R.get(n.TOKEN),
t = R.get(n.TOKEN_EXPIRATION);
return t && parseInt(t, 10) > Date.now() / 1e3
? { response: e, error: null }
: m ? { response: null, error: m } : { response: null, error: null };
},
x = function(e, r) {
Microsoft.ADAL.AuthenticationSettings.setUseBroker(!0).then(function() {
v(n.MOBILE),
P(w()),
O.tokenCache.readItems().then(function(o) {
if (o.length > 0) {
var i = o.length - 1;
(t = o[i].authority), v(n.MOBILE);
}
O.acquireTokenSilentAsync(c, s, f).then(
function(t) {
var r = t.idToken,
o = C(r).exp;
R.set(n.IN_PROGRESS, !1),
R.set(n.TOKEN, r),
R.set(n.TOKEN_EXPIRATION, o),
e(t);
},
function() {
O.acquireTokenAsync(c, s, h).then(function(t) {
var r = t.idToken,
o = C(r).exp;
R.set(n.IN_PROGRESS, !1),
R.set(n.TOKEN, r),
R.set(n.TOKEN_EXPIRATION, o),
e(t);
}, r);
}
);
});
});
},
B = function(e, t) {
v(n.BROWSER);
var r = b().response;
r || _() || m
? (d.saveTokenFromHash(d.getRequestInfo(window.location.hash)),
e({ token: r, error: null }))
: (R.set(n.IN_PROGRESS, !1),
d.login(),
e({ token: null, error: null }));
},
K = function(e, t) {
try {
w() === n.BROWSER ? B(e, t) : x(e, t);
} catch (e) {
t(e);
} catch (err) {
reject(err);
}
});
};
var setPlatformRedirectUri = function setPlatformRedirectUri(platform) {
switch (platform.toLowerCase()) {
case 'android':
platformRedirectUri = redirectUriAndroid;
break;
case 'ios':
platformRedirectUri = redirectUriIOS;
break;
case 'windows':
platformRedirectUri = redirectUriWindows;
break;
}
};
var isInProgress = function isInProgress() {
return storage.get(constants.IN_PROGRESS) === 'true';
};
var getCachedToken = function getCachedToken() {
var token = storage.get(constants.TOKEN);
var expiration = storage.get(constants.TOKEN_EXPIRATION);
if (expiration && parseInt(expiration, 10) > Date.now() / 1000) {
// log.info('valid cached token');
return { response: token, error: null };
} else if (localError) {
return { response: null, error: localError };
}
return { response: null, error: null };
};
var acquireTokenAsyncMobile = function(rosolve, reject) {
authContext
.acquireTokenAsync(resourceUri, clientId, platformRedirectUri)
.then(function(authResult) {
var idToken = authResult.idToken;
var _decodeTokenPayload3 = decodeTokenPayload(idToken);
var exp = _decodeTokenPayload3.exp;
storage.set(constants.IN_PROGRESS, false);
storage.set(constants.TOKEN, idToken);
storage.set(constants.TOKEN_EXPIRATION, exp);
resolve(authResult);
}, reject);
};
var authenticateMobile = function authenticateMobile(resolve, reject) {
// Check if MS ADAL cordova library is installed
Microsoft.ADAL.AuthenticationSettings.setUseBroker(true).then(function() {
initAuthenticationContext(constants.MOBILE);
setPlatformRedirectUri(getPlatform());
// Check if token already exist. If so, take the tenant where the token came from.
// This prevents an additional prompt
authContext.tokenCache.readItems().then(function(items) {
if (items.length > 0) {
var index = items.length - 1;
authority = items[index].authority;
initAuthenticationContext(constants.MOBILE);
}
},
C = function(e) {
try {
return jwtDecode(e);
} catch (e) {
return console.error('unable to decode token', e), {};
// Attempt to authorize user silently
authContext.acquireTokenSilentAsync(resourceUri, clientId, userId).then(
function(authResult) {
var token = authResult.token;
if (isValtoken(token)) {
var _decodeTokenPayload3 = decodeTokenPayload(token);
var exp = _decodeTokenPayload3.exp;
storage.set(constants.IN_PROGRESS, false);
storage.set(constants.TOKEN, token);
storage.set(constants.TOKEN_EXPIRATION, exp);
resolve(authResult);
} else {
// When token has expired, acquire new token
acquireTokenAsyncMobile(resolve, reject);
}
},
function() {
// We require user credentials so triggers authentication dialog
acquireTokenAsyncMobile(resolve, reject);
}
);
});
});
};
var authenticateBrowser = function authenticateBrowser(resolve, reject) {
initAuthenticationContext(constants.BROWSER);
var _getCachedToken = getCachedToken(),
token = _getCachedToken.response;
if (!token && !isInProgress() && !localError) {
storage.set(constants.IN_PROGRESS, false);
auth.login();
resolve({ token: null, error: null });
} else {
auth.saveTokenFromHash(auth.getRequestInfo(window.location.hash));
resolve({ token: token, error: null });
}
};
var authenticate = function authenticate(resolve, reject) {
try {
// Check platformId
if (getPlatform() === constants.BROWSER) {
authenticateBrowser(resolve, reject);
} else {
authenticateMobile(resolve, reject);
}
} catch (err) {
reject(err);
}
};
var decodeTokenPayload = function decodeTokenPayload(token) {
try {
return jwtDecode(token);
} catch (e) {
console.error('unable to decode token', e);
return {};
}
};
var getUser = function getUser() {
if (!Object.keys(user).length) {
return acquireToken(getUser).then(function(token) {
var _decodeTokenPayload = decodeTokenPayload(token),
family_name = _decodeTokenPayload.family_name,
given_name = _decodeTokenPayload.given_name,
name = _decodeTokenPayload.name,
upn = _decodeTokenPayload.upn;
if (!upn) {
upn = decodeTokenPayload(token).email;
}
},
W = function e() {
return Object.keys(E).length
? Promise.resolve(E)
: A(e).then(function(e) {
var n = C(e),
t = n.family_name,
r = n.given_name,
o = n.name,
i = n.upn;
return i || (i = C(e).email), (E = j(t, r, o, i));
});
},
U = function(e) {
var t = R.get(n.TOKEN_EXPIRATION);
return t && parseInt(t, 10) > Date.now() / 1e3;
},
q = function() {
var e = R.get(n.TOKEN);
return U(e) ? e : (R.remove(n.TOKEN), null);
},
L = function() {
var e = R.get(n.TOKEN);
if (U(e)) {
var t = C(e),
r = t.family_name,
o = t.given_name,
i = t.name,
u = t.upn;
return u || (u = C(e).email), (E = j(r, o, i, u));
}
},
j = function(e, n, t, r) {
return { familyName: e, givenName: n, name: t, upn: r };
},
M = function(e) {
function n(e) {
var n,
t = /\+/g,
r = /([^&=]+)=?([^&]*)/g,
o = function(e) {
return decodeURIComponent(e.replace(t, ' '));
},
i = {};
for (n = r.exec(e); n; ) (i[o(n[1])] = o(n[2])), (n = r.exec(e));
return i;
}
function t(e) {
var t = '';
return (
e.indexOf('#/') > -1
? (t = e.substring(e.indexOf('#/') + 2))
: e.indexOf('#') > -1 && (t = e.substring(1)),
(t && n(t)) || null
);
}
function r(e) {
return e
.substr(1)
.split('&')
.reduce(function(e, n) {
var t = n.split('='),
r = _slicedToArray(t, 2),
o = r[0],
i = r[1];
return e.hasOwnProperty(o)
? e
: Object.assign(
{},
e,
_defineProperty({}, o, decodeURIComponent(i))
);
}, {});
}
return t(e || window.location.hash) || r(window.location.search);
user = createUser(family_name, given_name, name, upn);
return user;
});
}
return Promise.resolve(user);
};
var isValidToken = function(token) {
var expiration = storage.get(constants.TOKEN_EXPIRATION);
return expiration && parseInt(expiration, 10) > Date.now() / 1000;
};
var getTokenFromCache = function getTokenFromCache() {
var token = storage.get(constants.TOKEN);
if (!isValidToken(token)) {
storage.remove(constants.TOKEN);
return null;
}
return token;
};
var getUserFromCache = function getUserFromCache() {
var token = storage.get(constants.TOKEN);
if (isValidToken(token)) {
var _decodeTokenPayload2 = decodeTokenPayload(token),
family_name = _decodeTokenPayload2.family_name,
given_name = _decodeTokenPayload2.given_name,
name = _decodeTokenPayload2.name,
upn = _decodeTokenPayload2.upn;
if (!upn) {
upn = decodeTokenPayload(token).email;
}
user = createUser(family_name, given_name, name, upn);
return user;
}
};
var createUser = function createUser(familyName, givenName, name, upn) {
return {
familyName: familyName,
givenName: givenName,
name: name,
upn: upn
};
};
var getUrlParameters = function getUrlParameters(hash) {
function deserializeHash(query) {
var match;
var pl = /\+/g; // Regex for replacing addition symbol with a space
var search = /([^&=]+)=?([^&]*)/g;
var decode = function(s) {
return decodeURIComponent(s.replace(pl, ' '));
};
var parameters = {};
match = search.exec(query);
while (match) {
parameters[decode(match[1])] = decode(match[2]);
match = search.exec(query);
}
return parameters;
}
function getHash(hash) {
var hashString = '';
if (hash.indexOf('#/') > -1) {
hashString = hash.substring(hash.indexOf('#/') + 2);
} else if (hash.indexOf('#') > -1) {
hashString = hash.substring(1);
}
return (hashString && deserializeHash(hashString)) || null;
}
function getQueryParams(url) {
return url
.substr(1)
.split('&')
.reduce(function(params, item) {
var _item$split = item.split('='),
_item$split2 = _slicedToArray(_item$split, 2),
key = _item$split2[0],
value = _item$split2[1];
if (!params.hasOwnProperty(key)) {
// eslint-disable-line no-prototype-builtins
return Object.assign(
{},
params,
_defineProperty({}, key, decodeURIComponent(value))
);
}
return params;
}, {});
}
return (
(function() {
var e = M(),
t = function(n) {
return e[n];
},
r = t('id_token'),
o = (t('state'), !0);
if (r) {
var i = C(r),
u = (i.nonce, i.aud, i.exp);
i.family_name, i.given_name, i.name, i.upn;
o &&
(R.set(n.IN_PROGRESS, !1),
R.set(n.TOKEN, r),
R.set(n.TOKEN_EXPIRATION, u));
}
})(),
{
signIn: I,
signOut: N,
acquireToken: A,
initialize: g,
getUser: W,
getUserFromCache: L,
getTokenFromCache: q
getHash(hash || window.location.hash) ||
getQueryParams(window.location.search)
);
};
var handleWindowCallback = function handleWindowCallback() {
//get token from parameter and push to session storage
var parameters = getUrlParameters();
var getParameter = function getParameter(key) {
return parameters[key];
};
var token = getParameter('id_token');
var state = getParameter('state');
var validToken = true;
if (token) {
var _decodeTokenPayload3 = decodeTokenPayload(token),
nonce = _decodeTokenPayload3.nonce,
aud = _decodeTokenPayload3.aud,
exp = _decodeTokenPayload3.exp,
family_name = _decodeTokenPayload3.family_name,
given_name = _decodeTokenPayload3.given_name,
name = _decodeTokenPayload3.name,
upn = _decodeTokenPayload3.upn;
if (validToken) {
storage.set(constants.IN_PROGRESS, false);
storage.set(constants.TOKEN, token);
storage.set(constants.TOKEN_EXPIRATION, exp);
}
);
})();
module.exports = { authentication: eandisAuthenticator };
}
};
handleWindowCallback();
return {
signIn: signIn,
signOut: signOut,
acquireToken: acquireToken,
initialize: initialize,
getUser: getUser,
getUserFromCache: getUserFromCache,
getTokenFromCache: getTokenFromCache
};
})();
module.exports = {
authentication: eandisAuthenticator
};

@@ -288,2 +288,16 @@ 'use strict';

var acquireTokenAsyncMobile = function(rosolve, reject) {
authContext
.acquireTokenAsync(resourceUri, clientId, platformRedirectUri)
.then(function(authResult) {
var idToken = authResult.idToken;
var _decodeTokenPayload3 = decodeTokenPayload(idToken);
var exp = _decodeTokenPayload3.exp;
storage.set(constants.IN_PROGRESS, false);
storage.set(constants.TOKEN, idToken);
storage.set(constants.TOKEN_EXPIRATION, exp);
resolve(authResult);
}, reject);
};
var authenticateMobile = function authenticateMobile(resolve, reject) {

@@ -305,23 +319,18 @@ // Check if MS ADAL cordova library is installed

function(authResult) {
var idToken = authResult.idToken;
var _decodeTokenPayload3 = decodeTokenPayload(idToken);
var exp = _decodeTokenPayload3.exp;
storage.set(constants.IN_PROGRESS, false);
storage.set(constants.TOKEN, idToken);
storage.set(constants.TOKEN_EXPIRATION, exp);
resolve(authResult);
var token = authResult.token;
if (isValtoken(token)) {
var _decodeTokenPayload3 = decodeTokenPayload(token);
var exp = _decodeTokenPayload3.exp;
storage.set(constants.IN_PROGRESS, false);
storage.set(constants.TOKEN, token);
storage.set(constants.TOKEN_EXPIRATION, exp);
resolve(authResult);
} else {
// When token has expired, acquire new token
acquireTokenAsyncMobile(resolve, reject);
}
},
function() {
// We require user credentials so triggers authentication dialog
authContext
.acquireTokenAsync(resourceUri, clientId, platformRedirectUri)
.then(function(authResult) {
var idToken = authResult.idToken;
var _decodeTokenPayload3 = decodeTokenPayload(idToken);
var exp = _decodeTokenPayload3.exp;
storage.set(constants.IN_PROGRESS, false);
storage.set(constants.TOKEN, idToken);
storage.set(constants.TOKEN_EXPIRATION, exp);
resolve(authResult);
}, reject);
acquireTokenAsyncMobile(resolve, reject);
}

@@ -328,0 +337,0 @@ );

{
"name": "authentication-adal-pkg",
"version": "1.0.10",
"version": "1.0.11",
"description": "An authentication SDK based on MS ADAL ",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc