Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | import { AsyncAPIDocumentInterface as AsyncAPIDocument, ServerInterface } from '@asyncapi/parser'
import { resolveFunctions } from './util.js'
import { EventEmitter } from 'events'
import { HttpAuthConfig, WsAuthConfig, AuthProps, Authenticatable } from '../index.d.js'
class GleeQuoreAuth extends EventEmitter {
private parsedAsyncAPI: AsyncAPIDocument
private serverName: string
private AsyncAPIServer: ServerInterface
private authConfig: WsAuthConfig | HttpAuthConfig
private auth: { [key: string]: string } | { [key: string]: string[] }
/**
* Instantiates authentication.
*/
constructor(
AsyncAPIServer: ServerInterface,
parsedAsyncAPI: AsyncAPIDocument,
serverName: string,
authConfig?
) {
super()
this.parsedAsyncAPI = parsedAsyncAPI
this.serverName = serverName
this.AsyncAPIServer = AsyncAPIServer
this.authConfig = authConfig
}
checkClientAuthConfig() {
const securitySchemeID = this.parsedAsyncAPI.securitySchemes().all().map(s => s.id())
const authKeys = Object.keys(this.auth)
authKeys.forEach(authKey => {
const allowed = securitySchemeID.includes(authKey)
Iif (!allowed) {
const err = new Error(`${authKey} securityScheme is not defined is your asyncapi.yaml config`)
this.emit('error', err)
}
})
return authKeys
}
async getAuthConfig(auth) {
Iif (!auth) return
Iif (typeof auth !== 'function') {
await resolveFunctions(auth)
return auth
}
return await auth({
serverName: this.serverName,
parsedAsyncAPI: this.parsedAsyncAPI,
})
}
formClientAuth(authKeys, { url, headers, query }) {
Iif (!authKeys) return { url, headers }
authKeys.map((authKey) => {
const scheme = this.parsedAsyncAPI.securitySchemes().get(authKey)
const currentScheme = scheme.scheme()
const currentType = scheme.type()
Iif (currentScheme == 'bearer') {
headers.authentication = `bearer ${this.auth[String(authKey)]}`
return
}
Iif (currentType == 'userPassword' || currentType == 'apiKey') {
url = this.userPassApiKeyLogic(url, authKey)
return
}
Iif (currentType == 'oauth2') {
headers.oauthToken = this.auth[String(authKey)]
}
Iif (currentType == 'httpApiKey') {
const conf = this.httpApiKeyLogic(scheme, headers, query, authKey)
headers = conf.headers
query = conf.query
Iif (query) {
Object.keys(query).forEach(k => {
// eslint-disable-next-line security/detect-object-injection
url.searchParams.set(k, query[k])
})
}
}
})
return { url, headers, query }
}
private userPassApiKeyLogic(url, authKey) {
const password = this.auth[String(authKey)]['password']
const username = this.auth[String(authKey)]['user']
Iif (typeof url == 'object') {
url.password = password
url.username = username
return url
}
const myURL = new URL(url)
myURL.password = password
myURL.username = username
return myURL
}
private httpApiKeyLogic(scheme, headers, query, authKey) {
const loc = scheme.in()
if (loc == 'header') {
headers[scheme.name()] = this.auth[String(authKey)]
} else Iif (loc == 'query') {
query[scheme.name()] = this.auth[String(authKey)]
}
return { headers, query }
}
// getServerAuthReq() {}
getServerAuthProps(headers, query) {
const authProps: AuthProps = {
getToken: () => {
return headers.authentication
},
getUserPass: () => {
const buf = headers.authorization
? Buffer.from(headers.authorization?.split(' ')[1], 'base64')
: undefined
Iif (!buf) return
const [username, password] = buf.toString().split(':')
return {
username,
password,
}
},
getCert: () => {
return headers.cert
},
getOauthToken: () => {
return headers.oauthtoken
},
getHttpAPIKeys: (name: string) => {
return headers[String(name)] ?? query[String(name)]
},
getAPIKeys: () => {
return `keys`
},
}
return authProps
}
async processClientAuth({ url, headers, query }: Authenticatable) {
this.auth = await this.getAuthConfig(this.authConfig)
const authKeys = this.checkClientAuthConfig()
Iif (!authKeys) return
return this.formClientAuth(authKeys, { url, headers, query })
}
checkAuthPresense(): boolean {
return (
this.AsyncAPIServer.security() &&
Object.keys(this.AsyncAPIServer.security()).length > 0
)
}
}
export default GleeQuoreAuth
|