From d3dd0587bb4df54b547eebcdae4ea46b40aa4104 Mon Sep 17 00:00:00 2001 From: Kristian Tot Date: Fri, 19 Jul 2024 13:13:02 +0200 Subject: [PATCH] add 4 additional methods to revoke tokens --- src/nodb.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/types.ts | 12 ++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/nodb.ts b/src/nodb.ts index 12c4e5a..473806c 100644 --- a/src/nodb.ts +++ b/src/nodb.ts @@ -10,9 +10,12 @@ import { PatchRequestBody, PostApplicationBody, PostApplicationResponse, + PostApplicationTokenResponse, PostEntityRequestBody, PostEnvironmentBody, PostEnvironmentResponse, + PostEnvironmentTokenResponse, + TokenPermissions, } from "./types"; import { NodbError } from "./errors"; import axios, { Axios, AxiosError } from "axios"; @@ -255,6 +258,70 @@ class Nodb { return result.data.found; } + + async createApplicationToken(props: { + appName: string; + permission: TokenPermissions; + token?: string; + }): Promise { + const { appName, permission, token } = props; + const result = await this.axios.post( + `/tokens/${appName}`, + { + permission, + }, + { ...(token && { headers: { token } }) }, + ); + + return result.data; + } + + async createEnvironmentToken(props: { + appName: string; + envName: string; + permission: TokenPermissions; + token?: string; + }): Promise { + const { appName, envName, permission, token } = props; + const result = await this.axios.post( + `/tokens/${appName}/${envName}`, + { + permission, + }, + { ...(token && { headers: { token } }) }, + ); + + return result.data; + } + + async revokeApplicationToken(props: { + appName: string; + tokenToBeRevoked: string; + token?: string; + }): Promise { + const { appName, tokenToBeRevoked, token } = props; + const result = await this.axios.delete<{ success: boolean }>( + `/tokens/${appName}/${tokenToBeRevoked}`, + { ...(token && { headers: { token } }) }, + ); + + return result.data.success; + } + + async revokeEnvironmentToken(props: { + appName: string; + envName: string; + tokenToBeRevoked: string; + token?: string; + }): Promise { + const { appName, envName, tokenToBeRevoked, token } = props; + const result = await this.axios.delete<{ success: boolean }>( + `/tokens/${appName}/${envName}/${tokenToBeRevoked}`, + { ...(token && { headers: { token } }) }, + ); + + return result.data.success; + } } export default Nodb; diff --git a/src/types.ts b/src/types.ts index 1f63fc8..f6ba671 100644 --- a/src/types.ts +++ b/src/types.ts @@ -81,3 +81,15 @@ export type PostEnvironmentBody = { description?: string; token?: string; }; + +export type TokenPermissions = "ALL" | "READ_ONLY"; + +export type PostApplicationTokenResponse = { + appName: string; + permission: TokenPermissions; + token: string; +}; + +export type PostEnvironmentTokenResponse = PostApplicationTokenResponse & { + envName: string; +};