Skip to content

Commit

Permalink
refactor: private endpoint with api keys, Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Sep 27, 2022
1 parent 8638635 commit 9d57872
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
31 changes: 0 additions & 31 deletions src/events/http/Http.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { log } from '@serverless/utils/log.js'
import HttpEventDefinition from './HttpEventDefinition.js'
import HttpServer from './HttpServer.js'
import { orange } from '../../config/colors.js'

export default class Http {
#hasPrivateHttpEvent = false

#httpServer = null

#options = null

constructor(serverless, options, lambda) {
this.#httpServer = new HttpServer(serverless, options, lambda)
this.#options = options
}

start() {
Expand All @@ -37,32 +30,8 @@ export default class Http {
create(events) {
events.forEach(({ functionKey, handler, http }) => {
this.#createEvent(functionKey, http, handler)

if (http.private) {
this.#hasPrivateHttpEvent = true
}
})

if (this.#hasPrivateHttpEvent) {
if (this.#options.apiKey) {
log.notice()
log.warning(
orange(`'--apiKey' is deprecated and will be removed in the next major version.
Please define the apiKey value in the 'provider.apiGateway.apiKeys' section of the serverless config.
If you are experiencing any issues please let us know: https://github.com/dherault/serverless-offline/issues`),
)
log.notice()
}

if (this.#options.noAuth) {
log.notice(
`Authorizers are turned off. You do not need to use 'x-api-key' header.`,
)
} else {
log.notice(`Remember to use 'x-api-key' on the request headers.`)
}
}

this.#httpServer.writeRoutesTerminal()
}

Expand Down
33 changes: 29 additions & 4 deletions src/events/http/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import LambdaProxyIntegrationEventV2 from './lambda-events/LambdaProxyIntegrationEventV2.js'
import parseResources from './parseResources.js'
import payloadSchemaValidator from './payloadSchemaValidator.js'
import { orange } from '../../config/colors.js'
import logRoutes from '../../utils/logRoutes.js'
import {
createApiKey,
Expand All @@ -37,6 +38,8 @@ const { assign, entries, keys } = Object
export default class HttpServer {
#apiKeysValues = null

#hasPrivateHttpEvent = false

#lambda = null

#options = null
Expand Down Expand Up @@ -893,12 +896,34 @@ export default class HttpServer {
}

createRoutes(functionKey, httpEvent, handler) {
if (httpEvent.private && this.#apiKeysValues.size === 0) {
const apiKey = this.#options.apiKey ?? createApiKey()
if (!this.#hasPrivateHttpEvent && httpEvent.private) {
this.#hasPrivateHttpEvent = true

if (this.#options.apiKey) {
log.notice()
log.warning(
orange(`'--apiKey' is deprecated and will be removed in the next major version.
Please define the apiKey value in the 'provider.apiGateway.apiKeys' section of the serverless config.
If you are experiencing any issues please let us know: https://github.com/dherault/serverless-offline/issues`),
)
log.notice()
}

if (this.#options.noAuth) {
log.notice(
`Authorizers are turned off. You do not need to use 'x-api-key' header.`,
)
} else {
log.notice(`Remember to use 'x-api-key' on the request headers.`)
}

if (this.#apiKeysValues.size === 0) {
const apiKey = this.#options.apiKey ?? createApiKey()

log.notice(`Key with token: ${apiKey}`)
log.notice(`Key with token: ${apiKey}`)

this.#apiKeysValues.add(apiKey)
this.#apiKeysValues.add(apiKey)
}
}

let method
Expand Down

0 comments on commit 9d57872

Please sign in to comment.