Skip to content

Commit

Permalink
add 4 additional methods to revoke tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
tot-kristian committed Jul 19, 2024
1 parent 2789465 commit d3dd058
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/nodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -255,6 +258,70 @@ class Nodb {

return result.data.found;
}

async createApplicationToken(props: {
appName: string;
permission: TokenPermissions;
token?: string;
}): Promise<PostApplicationTokenResponse> {
const { appName, permission, token } = props;
const result = await this.axios.post<PostApplicationTokenResponse>(
`/tokens/${appName}`,
{
permission,
},
{ ...(token && { headers: { token } }) },
);

return result.data;
}

async createEnvironmentToken(props: {
appName: string;
envName: string;
permission: TokenPermissions;
token?: string;
}): Promise<PostEnvironmentTokenResponse> {
const { appName, envName, permission, token } = props;
const result = await this.axios.post<PostEnvironmentTokenResponse>(
`/tokens/${appName}/${envName}`,
{
permission,
},
{ ...(token && { headers: { token } }) },
);

return result.data;
}

async revokeApplicationToken(props: {
appName: string;
tokenToBeRevoked: string;
token?: string;
}): Promise<boolean> {
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<boolean> {
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;
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit d3dd058

Please sign in to comment.