UNPKG

integreat

Version:
75 lines 2.65 kB
import { pathGetter } from 'map-transform'; import { isObject } from '../utils/is.js'; const state = { context: [], value: undefined }; const compareValues = (expected, value) => expected !== undefined && Array.isArray(expected) ? expected.includes(value) : expected === value; const getIsMatchCount = (matches) => matches.filter(([match]) => !!match).length; const getHasPropsCount = (matches) => matches.filter(([, exists]) => !!exists).length; function validateOptions(options, action) { if (action === null) { return [false, false]; } const pathAndExpectedArr = Object.entries(options); if (pathAndExpectedArr.length === 0) { return [true, false]; } const matches = pathAndExpectedArr.map(function ([path, expected,]) { const value = pathGetter(path)(action, state); return [compareValues(expected, value), value !== undefined]; }); return [ getIsMatchCount(matches) === pathAndExpectedArr.length, getHasPropsCount(matches) > 0, ]; } const optionsAuth = { async authenticate(options) { return { status: 'granted', ...options }; }, isAuthenticated(authentication, _options, _action) { return !!(authentication && authentication.status === 'granted'); }, async validate(_authentication, options, action) { const { identId, ...authOptions } = options || {}; const [isValid, hasProps] = validateOptions(authOptions, action); if (isValid) { return { status: 'ok', access: { ident: { id: identId } } }; } else { return hasProps ? { status: 'autherror', error: 'Invalid credentials', reason: 'invalidauth', } : { status: 'noaccess', error: 'Authentication required', reason: 'noauth', }; } }, authentication: { asObject(authentication) { if (isObject(authentication)) { const { status, ...options } = authentication; if (status === 'granted') { return options; } } return {}; }, asHttpHeaders(authentication) { if (isObject(authentication)) { const { status, ...options } = authentication; if (status === 'granted') { return options; } } return {}; }, }, }; export default optionsAuth; //# sourceMappingURL=options.js.map