Skip to content

Commit

Permalink
chore: upgrade mock server to oas-tools 3 (#69)
Browse files Browse the repository at this point in the history
* chore: upgrade mock server to oas-tools 3

* chore: fix async issue in middleware load
  • Loading branch information
mikelax authored Aug 21, 2023
1 parent dad5e34 commit eef6376
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 2,620 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ This list of links is written for developers that would like to contribute to Th

## What is in progress

* [ ] Implement Groups functionality (see https://github.com/DMGT-TECH/the-usher-server/issues/2)
* [ ] Implement Groups functionality (see <https://github.com/DMGT-TECH/the-usher-server/issues/2>)

## What could be added

Expand Down
74 changes: 38 additions & 36 deletions mockidentityprovider/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// Mock Identity Provider for The Usher
require('dotenv').config()
const createError = require('http-errors')
const keystore = require('./src/utils/keystore.js')
const keygen = require('./src/api_endpoints/endpoint_generate_new_keys.js')
const usherCors = require('cors')
const express = require('express')
const fs = require('fs')
const helmet = require('helmet')
const http = require('http')
const jsyaml = require('js-yaml')
const oasTools = require('oas-tools')
const path = require('path')
const http = require('node:http')
const oasTools = require('@oas-tools/core')
const path = require('node:path')

const keystore = require('./src/utils/keystore.js')
const keygen = require('./src/api_endpoints/endpoint_generate_new_keys.js')

// Normalizes a port into a number, string, or false
function normalizePort (val) {
Expand All @@ -31,33 +30,45 @@ async function seedKeysIfDbIsEmpty () {
}
}

const optionsObject = {
controllers: path.join(__dirname, 'src/api_endpoints'),
checkControllers: false,
loglevel: 'info',
logfile: './logs.txt',
strict: true,
router: true,
validator: true,
docs: null, // Swap this line with next if you want hosted Swagger docs (may not work deployed as cloud function)
// docs: { apiDocs: '/api-docs', apiDocsPrefix: '', swaggerUi: '/docs', swaggerUiPrefix: '' },
ignoreUnknownFormats: true,
oasSecurity: false,
customErrorHandling: true
const oasOptions = {
oasFile: 'mockidentityprovider-openapi-spec.yaml',
logger: {
loglevel: 'info',
logFile: 'logs.txt',
logFilePath: '.'
},
middleware: {
router: {
controllers: path.join(__dirname, 'src/api_endpoints')
},
swagger: {
disable: false,
path: '/docs'
},
validator: {
strict: true
}
}
}

const spec = fs.readFileSync('mockidentityprovider-openapi-spec.yaml', 'utf8')
const oasDoc = jsyaml.load(spec)
const expressApp = express()
expressApp.use(helmet())
expressApp.use(express.json())
expressApp.use(usherCors())
oasTools.configure(optionsObject)

oasTools.initialize(oasDoc, expressApp, function () {
oasTools.initialize(expressApp, oasOptions).then(() => {
const port = normalizePort(process.env.PORT || '3002')
http.createServer(expressApp).listen(port, function () {
console.log('Mock Identity Server up and running!')
http.createServer(expressApp).listen(port, () => {
console.log('🚀 Mock Identity Server up and running!')
})

// Default route to handle not found endpoints but return 405 for security
expressApp.use((req, res, next) => {
const notFoundResponse = {
code: 405,
message: 'Method Not Allowed'
}
res.status(405).send(notFoundResponse)
})
})

Expand All @@ -70,7 +81,7 @@ expressApp.use((err, req, res, next) => {
next(err)
})

expressApp.use(function (err, req, res, next) {
expressApp.use((err, req, res, next) => {
// handle case if headers have already been sent to client
if (res.headersSent) {
return next(err)
Expand All @@ -83,13 +94,4 @@ expressApp.use(function (err, req, res, next) {
})
})

// Default route to handle not found endpoints but return 405 for security
expressApp.use(function (req, res, next) {
const notFoundResponse = {
code: 405,
message: 'Method Not Allowed'
}
res.status(405).send(notFoundResponse)
})

seedKeysIfDbIsEmpty()
6 changes: 3 additions & 3 deletions mockidentityprovider/mockidentityprovider-openapi-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ paths:
/:
get:
operationId: getConfiguration
'x-swagger-router-controller': 'endpoint_root'
x-router-controller: 'endpoint_root'
summary: Returns basic information about this server.
description: This endpoint returns a JSON object with URIs for an authenticated persona to obtain an access token, and for an API or client application to get this server's JSON Web Key Set (JWKS).
security: [] # public endpoint
Expand All @@ -43,7 +43,7 @@ paths:

/.well-known/jwks.json:
get:
'x-swagger-router-controller': 'endpoint_jwksjson'
'x-router-controller': 'endpoint_jwksjson'
operationId: getJwks
summary: Returns this server's public key in JSON Web Key Set (JWKS) format.
tags:
Expand All @@ -62,7 +62,7 @@ paths:

/oauth/token:
post:
'x-swagger-router-controller': 'endpoint_oauth_token'
'x-router-controller': 'endpoint_oauth_token'
operationId: issueOauthToken # https://tools.ietf.org/html/rfc7523#section-2.1
summary: Issue an access token (JWT) containing permissions for the logged-in persona to cover the requested scope, and an ID token.
description: |
Expand Down
Loading

0 comments on commit eef6376

Please sign in to comment.