|
1 | 1 | // This function runs after the common function,
|
2 | 2 | // `src/execute/index.js#buildRequest`
|
| 3 | +import assign from 'lodash/assign' |
| 4 | +import get from 'lodash/get' |
| 5 | +import btoa from 'btoa' |
3 | 6 |
|
4 | 7 | export default function (options, req) {
|
5 | 8 | const {
|
6 | 9 | operation,
|
7 |
| - requestBody |
| 10 | + requestBody, |
| 11 | + securities, |
| 12 | + spec |
8 | 13 | } = options
|
9 | 14 |
|
10 | 15 | let {
|
11 | 16 | requestContentType
|
12 | 17 | } = options
|
13 | 18 |
|
| 19 | + req = applySecurities({request: req, securities, operation, spec}) |
| 20 | + |
14 | 21 | const requestBodyDef = operation.requestBody || {}
|
15 | 22 | const requestBodyMediaTypes = Object.keys(requestBodyDef.content || {})
|
16 | 23 |
|
@@ -64,3 +71,73 @@ export default function (options, req) {
|
64 | 71 |
|
65 | 72 | return req
|
66 | 73 | }
|
| 74 | + |
| 75 | +// Add security values, to operations - that declare their need on them |
| 76 | +// Adapted from the Swagger2 implementation |
| 77 | +export function applySecurities({request, securities = {}, operation = {}, spec}) { |
| 78 | + const result = assign({}, request) |
| 79 | + const {authorized = {}} = securities |
| 80 | + const security = operation.security || spec.security || [] |
| 81 | + const isAuthorized = authorized && !!Object.keys(authorized).length |
| 82 | + const securityDef = get(spec, ['components', 'securitySchemes']) || {} |
| 83 | + |
| 84 | + result.headers = result.headers || {} |
| 85 | + result.query = result.query || {} |
| 86 | + |
| 87 | + if (!Object.keys(securities).length || !isAuthorized || !security || |
| 88 | + (Array.isArray(operation.security) && !operation.security.length)) { |
| 89 | + return request |
| 90 | + } |
| 91 | + |
| 92 | + security.forEach((securityObj, index) => { |
| 93 | + for (const key in securityObj) { |
| 94 | + const auth = authorized[key] |
| 95 | + const schema = securityDef[key] |
| 96 | + |
| 97 | + if (!auth) { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + const value = auth.value || auth |
| 102 | + const {type} = schema |
| 103 | + |
| 104 | + if (auth) { |
| 105 | + if (type === 'apiKey') { |
| 106 | + if (schema.in === 'query') { |
| 107 | + result.query[schema.name] = value |
| 108 | + } |
| 109 | + if (schema.in === 'header') { |
| 110 | + result.headers[schema.name] = value |
| 111 | + } |
| 112 | + if (schema.in === 'cookie') { |
| 113 | + result.cookies[schema.name] = value |
| 114 | + } |
| 115 | + } |
| 116 | + else if (type === 'http') { |
| 117 | + if (schema.scheme === 'basic') { |
| 118 | + const {username, password} = value |
| 119 | + const encoded = btoa(`${username}:${password}`) |
| 120 | + result.headers.Authorization = `Basic ${encoded}` |
| 121 | + } |
| 122 | + |
| 123 | + if (schema.scheme === 'bearer') { |
| 124 | + result.headers.Authorization = `Bearer ${value}` |
| 125 | + } |
| 126 | + } |
| 127 | + else if (type === 'oauth2') { |
| 128 | + const token = auth.token || {} |
| 129 | + const accessToken = token.access_token |
| 130 | + let tokenType = token.token_type |
| 131 | + |
| 132 | + if (!tokenType || tokenType.toLowerCase() === 'bearer') { |
| 133 | + tokenType = 'Bearer' |
| 134 | + } |
| 135 | + |
| 136 | + result.headers.Authorization = `${tokenType} ${accessToken}` |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + return result |
| 143 | +} |
0 commit comments