1
0
Fork 0
mirror of synced 2024-07-09 08:16:34 +12:00

Add backend for query / datasource auth

This commit is contained in:
Rory Powell 2021-12-07 22:33:26 +00:00
parent ce075c97ff
commit 9cbe03fd66
2 changed files with 98 additions and 1 deletions

View file

@ -13,6 +13,25 @@ const BodyTypes = {
TEXT: "text",
}
enum AuthType {
BASIC = "basic",
BEARER = "bearer"
}
interface AuthConfig {
id: string
type: AuthType
config: BasicAuthConfig | BearerAuthConfig
}
interface BasicAuthConfig {
username: string,
password: string,
}
interface BearerAuthConfig {
token: string,
}
const coreFields = {
path: {
type: DatasourceFieldTypes.STRING,
@ -46,6 +65,7 @@ module RestModule {
defaultHeaders: {
[key: string]: any
}
authConfigs: AuthConfig[]
}
interface Request {
@ -149,12 +169,32 @@ module RestModule {
}
}
async _req({ path = "", queryString = "", headers = {}, json = {}, method = "GET" }) {
processAuth(authConfigId: string) {
if (!this.config.authConfigs) {
return
}
const authConfig = this.config.authConfigs.filter(authConfig => authConfig.id === authConfigId)[0]
let config
switch (authConfig.type) {
case AuthType.BASIC:
config = authConfig.config as BasicAuthConfig
this.headers.Authorization = `Basic ${Buffer.from(`${config.username}:${config.password}`).toString("base64")}`
break
case AuthType.BEARER:
config = authConfig.config as BearerAuthConfig
this.headers.Authorization = `Bearer ${config.token}`
break
}
}
async _req({ path = "", queryString = "", headers = {}, json = {}, method = "GET", authConfigId = "" }) {
this.headers = {
...this.config.defaultHeaders,
...headers,
}
this.processAuth(authConfigId)
const input: any = { method, headers: this.headers }
if (json && typeof json === "object" && Object.keys(json).length > 0) {
input.body = JSON.stringify(json)
@ -189,5 +229,6 @@ module RestModule {
module.exports = {
schema: SCHEMA,
integration: RestIntegration,
AuthType
}
}

View file

@ -9,6 +9,7 @@ jest.mock("node-fetch", () =>
)
const fetch = require("node-fetch")
const RestIntegration = require("../rest")
const { AuthType } = require("../rest")
class TestConfiguration {
constructor(config = {}) {
@ -26,6 +27,10 @@ describe("REST Integration", () => {
})
})
// afterEach(() => {
// jest.clearAllMocks()
// })
it("calls the create method with the correct params", async () => {
const query = {
path: "api",
@ -103,4 +108,55 @@ describe("REST Integration", () => {
},
})
})
describe("authentication", () => {
const basicAuth = {
id: "basic-1",
type : AuthType.BASIC,
config : {
username: "user",
password: "password"
}
}
const bearerAuth = {
id: "bearer-1",
type : AuthType.BEARER,
config : {
"token": "mytoken"
}
}
beforeEach(() => {
config = new TestConfiguration({
url: BASE_URL,
authConfigs : [basicAuth, bearerAuth]
})
})
it("adds basic auth", async () => {
const query = {
authConfigId: "basic-1"
}
await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/?`, {
method: "GET",
headers: {
Authorization: "Basic dXNlcjpwYXNzd29yZA=="
},
})
})
it("adds bearer auth", async () => {
const query = {
authConfigId: "bearer-1"
}
await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/?`, {
method: "GET",
headers: {
Authorization: "Bearer mytoken"
},
})
})
})
})