Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource accounting implementation #1898

Merged
merged 14 commits into from
Apr 4, 2024
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = (grunt) => {
}
grunt.log.writeln(chalk.underline("\nRunning 'chel manifest'"))
// TODO: do this with JS instead of POSIX commands for Windows support
const { stdout } = await execWithErrMsg(`ls ${dir}/*-slim.js | sed -En 's/.*\\/(.*)-slim.js/\\1/p' | xargs -I {} node_modules/.bin/chel manifest -v ${version} -s ${dir}/{}-slim.js ${keyFile} ${dir}/{}.js`, 'error generating manifests')
const { stdout } = await execWithErrMsg(`ls ${dir}/*-slim.js | sed -En 's/.*\\/(.*)-slim.js/\\1/p' | xargs -I {} node_modules/.bin/chel manifest -n gi.contracts/{} -v ${version} -s ${dir}/{}-slim.js ${keyFile} ${dir}/{}.js`, 'error generating manifests')
console.log(stdout)
} else {
// Only run these in NODE_ENV=development so that production servers
Expand Down
65 changes: 38 additions & 27 deletions backend/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,55 @@
// https://hapijs.com/tutorials/auth
// https://hapijs.com/tutorials/plugins

import { verify, b64ToStr } from '~/shared/functions.js'

import { verifyShelterAuthorizationHeader } from '~/shared/domains/chelonia/utils.js'
const Boom = require('@hapi/boom')

exports.plugin = {
name: 'gi-auth',
name: 'chel-auth',
register: function (server: Object, opts: Object) {
server.auth.scheme('gi-auth', function (server, options) {
server.auth.scheme('chel-bearer', function (server, options) {
return {
authenticate: function (request, h) {
const { authorization } = request.headers
if (!authorization) return h.unauthenticated(Boom.unauthorized('Missing authorization'))

let [scheme, json] = authorization.split(/\s+/)
// NOTE: if you want to add any signature verification, do it here
// eslint-disable-next-line no-constant-condition
if (false) {
if (!scheme.includes('gi')) h.unauthenticated(Boom.badRequest('Bad authentication'))

try {
json = JSON.parse(b64ToStr(json))
} catch (e) {
return h.unauthenticated(Boom.badRequest('Invalid token format'))
}
// http://hapijs.com/api/#serverauthschemename-scheme
const isValid = verify(json.msg, json.key, json.sig)
json.userId = json.key
const credentials = { credentials: json }
if (!isValid) return h.unauthenticated(Boom.unauthorized('Bad credentials'), credentials)
return h.authenticated(credentials)
} else {
// remove this if you decide to implement it
return h.authenticated({ credentials: 'TODO: delete me' })
if (!authorization) {
return h.unauthenticated(Boom.unauthorized(null, 'bearer'))
}
// Space after 'bearer' is intentional and must be there as it
// acts as a separator
const thisScheme = 'bearer '
if (authorization.slice(0, thisScheme.length) !== thisScheme) {
return h.unauthenticated(Boom.unauthorized(null, 'bearer'))
}
const token = authorization.slice(thisScheme.length)
return h.authenticated({ credentials: { token } })
}
}
})
server.auth.scheme('chel-shelter', function (server, options) {
return {
authenticate: function (request, h) {
const { authorization } = request.headers
if (!authorization) {
return h.unauthenticated(Boom.unauthorized(null, 'shelter'))
}
// Space after 'shelter' is intentional and must be there as it
// acts as a separator
const thisScheme = 'shelter '
if (authorization.slice(0, thisScheme.length) !== thisScheme) {
return h.unauthenticated(Boom.unauthorized(null, 'shelter'))
}
try {
const billableContractID = verifyShelterAuthorizationHeader(authorization)
return h.authenticated({ credentials: { billableContractID } })
} catch (e) {
console.warn(e, 'Shelter authorization failed')
return h.unauthenticated(Boom.unauthorized('Authentication failed', 'shelter'))
}
}
}
})

server.auth.strategy('gi-auth', 'gi-auth')
server.auth.strategy('chel-bearer', 'chel-bearer')
server.auth.strategy('chel-shelter', 'chel-shelter')
}
}
Loading
Loading