Skip to content

Commit e9d07c9

Browse files
authored
feat: adding permissions checks to apollo (#307)
1 parent 70dd8c5 commit e9d07c9

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vesselapi/integrations",
3-
"version": "1.0.73",
3+
"version": "1.0.74",
44
"description": "Vessel integrations",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/platforms/apollo/client.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ const request = makeRequestFactory(async (auth, options) => {
5858
});
5959

6060
export const client = {
61+
auth: {
62+
health: request(() => ({
63+
url: `/auth/health`,
64+
method: 'GET',
65+
schema: z
66+
.object({
67+
is_logged_in: z.boolean().nullable(),
68+
})
69+
.partial(),
70+
})),
71+
},
6172
users: {
6273
search: request(
6374
({

src/platforms/apollo/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import searchUsers from './actions/users/search';
4242
import searchLabels from './actions/labels/search';
4343

4444
import searchPeople from './actions/people/search';
45+
import { permissions } from './permissions';
4546

4647
export * as types from './schemas';
4748
export default platform('apollo', {
@@ -68,6 +69,7 @@ export default platform('apollo', {
6869
},
6970
constants,
7071
client,
72+
permissions,
7173
actions: {
7274
createAccount,
7375
searchAccounts,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { tryit } from 'radash';
2+
import { PlatformPermissions } from '../../sdk';
3+
import { client } from './client';
4+
5+
// NOTE: These will be used in the UI, so they should be user-friendly.
6+
const INVALID_API_TOKEN_MESSAGE = 'Invalid API Token';
7+
8+
export const makePermissions = (): PlatformPermissions => {
9+
return {
10+
validate: async ({ auth }) => {
11+
const [err, result] = await tryit(client.auth.health)(auth, {});
12+
if (err || !result.data.is_logged_in) {
13+
console.warn({
14+
message: 'Failed to validate permissions for Apollo',
15+
metadata: { error: err, result },
16+
});
17+
return {
18+
valid: false,
19+
errorMessage: INVALID_API_TOKEN_MESSAGE,
20+
};
21+
}
22+
return {
23+
valid: true,
24+
errorMessage: null,
25+
};
26+
},
27+
};
28+
};
29+
30+
export const permissions = makePermissions();

0 commit comments

Comments
 (0)