@reaktor-internal/authorization-client
Advanced tools
Comparing version 1.0.2 to 1.0.3
83
index.js
@@ -1,82 +0,7 @@ | ||
const fetch = require('node-fetch') | ||
const client = require('./client') | ||
const cachedClient = require('./cachedClient') | ||
let config = {} | ||
let infoLog = null | ||
let errorLog = null | ||
let cachedResults = null | ||
let cacheValidityEndTimestamp = new Date() | ||
const DEFAULT_TTL_SECS = 60 | ||
const ensureOk = async res => { | ||
if (res.ok) return res | ||
const body = await res.text() | ||
throw new Error(`Request to ${res.url} failed: ${res.status}\n${body}`) | ||
} | ||
const authRequest = async (path, opts) => | ||
await fetch(`${config.serviceUrl}/${path}`, Object.assign({ headers: { auth_token: config.authToken } }, opts)) | ||
.then(ensureOk) | ||
.then(res => res.json()) | ||
const invalidateCache = next => { | ||
if (config.cacheResults) { | ||
infoLog('Busting cache') | ||
cachedResults = {} | ||
cacheValidityEndTimestamp = new Date() | ||
} | ||
return next | ||
} | ||
const getCachedOrFetchedRoles = async () => { | ||
if (!cachedResults || new Date() >= cacheValidityEndTimestamp) { | ||
infoLog(`Updating cache because TTL of ${config.cacheTTL || DEFAULT_TTL_SECS}s passed`) | ||
try { | ||
cachedResults = await authRequest('') | ||
const d = new Date() | ||
d.setSeconds(d.getSeconds() + (config.cacheTTL || DEFAULT_TTL_SECS)) | ||
cacheValidityEndTimestamp = d | ||
} catch (err) { | ||
errorLog(`Could not get roles from authorization service, using cached results: ${err}`) | ||
cachedResults = { data: {} } | ||
} | ||
} | ||
return cachedResults | ||
} | ||
const getAllRoles = () => { | ||
infoLog('Fetching all roles') | ||
return config.cacheResults ? getCachedOrFetchedRoles() : authRequest('') | ||
} | ||
const getRoles = async authId => { | ||
infoLog(`Fetching roles for ${authId}`) | ||
const allRoles = await getAllRoles() | ||
return allRoles.data[authId] ? allRoles.data[authId] : { roles: [] } | ||
} | ||
const addRole = (authId, roleId) => { | ||
infoLog(`Adding role ${roleId} to ${authId}`) | ||
return authRequest(`${encodeURIComponent(authId)}/${encodeURIComponent(roleId)}`, { method: 'POST' }).then( | ||
invalidateCache | ||
) | ||
} | ||
const deleteRoles = authId => { | ||
infoLog(`Deleting roles of ${authId}`) | ||
return authRequest(`${encodeURIComponent(authId)}`, { method: 'DELETE' }).then(invalidateCache) | ||
} | ||
const initialize = cfg => { | ||
config = cfg | ||
infoLog = config.infoLog || console.log | ||
errorLog = config.errorLog || config.infoLog || console.log | ||
} | ||
module.exports = { | ||
initialize, | ||
getAllRoles, | ||
getRoles, | ||
addRole, | ||
deleteRoles | ||
client, | ||
cachedClient | ||
} |
{ | ||
"name": "@reaktor-internal/authorization-client", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Reaktor authorization service client", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/reaktor/authorization", |
@@ -10,3 +10,5 @@ # Authorization Client | ||
```javascript | ||
const client = require('authorization-client') | ||
let client = require('authorization-client').client | ||
//Or for cached version | ||
client = require('authorization-client').cachedClient | ||
const myLog = require('my-logger-func') | ||
@@ -96,2 +98,2 @@ | ||
} | ||
``` | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8613
5
107
98