This is a reworking of #3410 based on some feedback @calebcartwright left on that PR. The goals of injecting the secrets are threefold: 1. Simplify testing 2. Be consistent with all of the other config (which is injected) 3. Encapsulate the sensitive auth-related code in one place so it can be studied and tested thoroughly - Rather than add more code to BaseService to handle authorization logic, it delegates that to an AuthHelper class. - When the server starts, it fetches the credentials from `config` and injects them into `BaseService.register()` which passes them to `invoke()`. - In `invoke()` the service's auth configuration is checked (`static get auth()`, much like `static get route()`). - If the auth config is present, an AuthHelper instance is created and attached to the new instance. - Then within the service, the password, basic auth config, or bearer authentication can be accessed via e.g. `this.authHelper.basicAuth` and passed to `this._requestJson()` and friends. - Everything is being done very explicitly, so it should be very clear where and how the configured secrets are being used. - Testing different configurations of services can now be done by injecting the config into `invoke()` in `.spec` files instead of mocking global state in the service tests as was done before. See the new Jira spec files for a good example of this. Ref #3393
72 lines
1.5 KiB
JavaScript
72 lines
1.5 KiB
JavaScript
'use strict'
|
|
|
|
const Joi = require('@hapi/joi')
|
|
const { BaseJsonService } = require('..')
|
|
|
|
const schema = Joi.object({
|
|
node: Joi.object({
|
|
wheelchair: Joi.string().required(),
|
|
}).required(),
|
|
}).required()
|
|
|
|
module.exports = class Wheelmap extends BaseJsonService {
|
|
static get category() {
|
|
return 'other'
|
|
}
|
|
|
|
static get route() {
|
|
return {
|
|
base: 'wheelmap/a',
|
|
pattern: ':nodeId(-?[0-9]+)',
|
|
}
|
|
}
|
|
|
|
static get auth() {
|
|
return { passKey: 'wheelmap_token', isRequired: true }
|
|
}
|
|
|
|
static get examples() {
|
|
return [
|
|
{
|
|
title: 'Wheelmap',
|
|
namedParams: { nodeId: '26699541' },
|
|
staticPreview: this.render({ accessibility: 'yes' }),
|
|
},
|
|
]
|
|
}
|
|
|
|
static get defaultBadgeData() {
|
|
return { label: 'accessibility' }
|
|
}
|
|
|
|
static render({ accessibility }) {
|
|
let color
|
|
if (accessibility === 'yes') {
|
|
color = 'brightgreen'
|
|
} else if (accessibility === 'limited') {
|
|
color = 'yellow'
|
|
} else if (accessibility === 'no') {
|
|
color = 'red'
|
|
}
|
|
return { message: accessibility, color }
|
|
}
|
|
|
|
async fetch({ nodeId }) {
|
|
return this._requestJson({
|
|
schema,
|
|
url: `https://wheelmap.org/api/nodes/${nodeId}`,
|
|
options: { qs: { api_key: this.authHelper.pass } },
|
|
errorMessages: {
|
|
401: 'invalid token',
|
|
404: 'node not found',
|
|
},
|
|
})
|
|
}
|
|
|
|
async handle({ nodeId }) {
|
|
const json = await this.fetch({ nodeId })
|
|
const accessibility = json.node.wheelchair
|
|
return this.constructor.render({ accessibility })
|
|
}
|
|
}
|