Skip to content

Commit ac14370

Browse files
authored
Merge pull request #115 from JaredCE/generate-x-api-key
Generate x api key
2 parents 70c9801 + 4726d52 commit ac14370

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,46 @@ functions:
622622
- {}
623623
```
624624

625+
##### private
626+
627+
If you use the [private](https://www.serverless.com/framework/docs/providers/aws/events/apigateway#setting-api-keys-for-your-rest-api) property on your event:
628+
629+
```yml
630+
functions:
631+
getData:
632+
events:
633+
- http:
634+
path: /
635+
method: get
636+
private: true
637+
```
638+
639+
It will automatically setup an apiKey security scheme of `x-api-key` attached to that method. You don't need to add this to the [Security Scheme](#securityschemes) in the main documentation. If you have already added a Security Scheme of an `apiKey` with a name of `x-api-key`, it will associate with that key.
640+
641+
```yml
642+
custom:
643+
documentation:
644+
securitySchemes:
645+
my_api_key:
646+
type: apiKey
647+
name: x-api-key
648+
in: header
649+
security:
650+
- my_api_key: []
651+
652+
functions:
653+
getData:
654+
events:
655+
- http:
656+
path: /
657+
method: get
658+
private: true
659+
documentation:
660+
...
661+
```
662+
663+
Will set the Security Scheme to `my_api_key` for that operation.
664+
625665
#### `requestModels`
626666

627667
The `requestModels` property allows you to define models for the HTTP Request of the function event. You can define a different model for each different `Content-Type`. You can define a reference to the relevant request model named in the `models` section of your configuration (see [Defining Models](#models) section).

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-openapi-documenter",
3-
"version": "0.0.60",
3+
"version": "0.0.61",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [

src/definitionGenerator.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,29 @@ class DefinitionGenerator {
312312
obj.security = documentation.security
313313
}
314314

315+
if (this.currentEvent?.private && this.currentEvent.private === true) {
316+
let apiKeyName = 'x-api-key'
317+
let hasXAPIKey = false
318+
if (this.openAPI?.components?.[this.componentTypes.securitySchemes]) {
319+
for (const [schemeName, schemeValue] of Object.entries(this.openAPI.components[this.componentTypes.securitySchemes])) {
320+
if (schemeValue.type === 'apiKey' && schemeValue.name === 'x-api-key') {
321+
apiKeyName = schemeName
322+
hasXAPIKey = true
323+
}
324+
}
325+
}
326+
327+
if (hasXAPIKey === false) {
328+
this.createSecuritySchemes({[apiKeyName]: {type: 'apiKey', name: apiKeyName, in: 'header'}})
329+
}
330+
331+
if (obj.security) {
332+
obj.security.push({[apiKeyName]: []})
333+
} else {
334+
obj.security = [{[apiKeyName]: []}]
335+
}
336+
}
337+
315338
if (Object.keys(documentation).includes('deprecated'))
316339
obj.deprecated = documentation.deprecated
317340

0 commit comments

Comments
 (0)