Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
feat: Support authorization #21 by options
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Jul 27, 2020
1 parent 74eadd3 commit dbbb80c
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 8 deletions.
43 changes: 35 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { promises: { writeFile, readFile } } = require('fs')
const { safeDump } = require('js-yaml')

async function postmanToOpenApi (input, output, { save = true, info = {}, defaultTag = 'default' } = {}) {
async function postmanToOpenApi (input, output, { save = true, info = {}, defaultTag = 'default', auth } = {}) {
// TODO validate?
const collectionFile = await readFile(input)
const postmanJson = JSON.parse(collectionFile)
Expand Down Expand Up @@ -43,7 +43,7 @@ async function postmanToOpenApi (input, output, { save = true, info = {}, defaul
const openApi = {
openapi: '3.0.0',
info: compileInfo(postmanJson, info),
...parseAuth(postmanJson),
...parseAuth(postmanJson, auth),
paths
}

Expand Down Expand Up @@ -143,11 +143,18 @@ function inferType (value) {
return 'string'
}

function parseAuth (postmanJson) {
const { auth } = postmanJson
if (auth == null) return {}
const { type } = auth
return {
/* Calculate the global auth based on options and postman definition */
function parseAuth ({ auth }, optAuth) {
if (optAuth != null) {
return parseOptsAuth(optAuth)
}
return parsePostmanAuth(auth)
}

/* Parse a postman auth definition */
function parsePostmanAuth (postmanAuth = {}) {
const { type } = postmanAuth
return (type != null) ? {
components: {
securitySchemes: {
[type + 'Auth']: {
Expand All @@ -159,7 +166,27 @@ function parseAuth (postmanJson) {
security: [{
[type + 'Auth']: []
}]
}
} : {}
}

/* Parse a options global auth */
function parseOptsAuth (optAuth) {
const securitySchemes = {}
const security = []
for (const [secName, secDefinition] of Object.entries(optAuth)) {
const { type, scheme, ...rest } = secDefinition
if (type === 'http' && ['bearer', 'basic'].includes(scheme)) {
securitySchemes[secName] = {
type: 'http',
scheme,
...rest
}
security.push({ [secName]: [] })
}
}
return {
components: { securitySchemes },
security
}
}
module.exports = postmanToOpenApi
19 changes: 19 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const EXPECTED_GET_METHODS = readFileSync('./test/resources/output/GetMethods.ym
const EXPECTED_HEADERS = readFileSync('./test/resources/output/Headers.yml', 'utf8')
const EXPECTED_AUTH_BEARER = readFileSync('./test/resources/output/AuthBearer.yml', 'utf8')
const EXPECTED_AUTH_BASIC = readFileSync('./test/resources/output/AuthBasic.yml', 'utf8')
const EXPECTED_BASIC_WITH_AUTH = readFileSync('./test/resources/output/BasicWIthAuth.yml', 'utf8')

describe('Library specs', function () {
afterEach('remove file', function () {
Expand Down Expand Up @@ -90,4 +91,22 @@ describe('Library specs', function () {
const result = await postmanToOpenApi(COLLECTION_AUTH_BASIC, OUTPUT_PATH)
equal(EXPECTED_AUTH_BASIC, result)
})

it('should use global authorization by configuration', async function () {
const authDefinition = {
myCustomAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'A resource owner JWT',
description: 'My awesome authentication using bearer'
},
myCustomAuth2: {
type: 'http',
scheme: 'basic',
description: 'My awesome authentication using user and password'
}
}
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, { auth: authDefinition })
equal(EXPECTED_BASIC_WITH_AUTH, result)
})
})
71 changes: 71 additions & 0 deletions test/resources/output/BasicWithAuth.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
openapi: 3.0.0
info:
title: Postman to OpenAPI
description: Mi super test collection from postman
version: 1.1.0
components:
securitySchemes:
myCustomAuth:
type: http
scheme: bearer
bearerFormat: A resource owner JWT
description: My awesome authentication using bearer
myCustomAuth2:
type: http
scheme: basic
description: My awesome authentication using user and password
security:
- myCustomAuth: []
- myCustomAuth2: []
paths:
/users:
post:
tags:
- default
summary: Create new User
description: Create a new user into your amazing API
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
/posts:
post:
tags:
- default
summary: Create a post
requestBody:
content:
text/plain: {}
responses:
'200':
description: Successful response
content:
application/json: {}
/note:
post:
tags:
- default
summary: Create a note
description: Just an example of text raw body
requestBody:
content:
application/json:
schema:
type: string
example: This is an example Note
responses:
'200':
description: Successful response
content:
application/json: {}

0 comments on commit dbbb80c

Please sign in to comment.