Skip to content

Commit 5af7472

Browse files
committed
now clients can call for multiple scopes. 'global' and 'special' should be included if needed
1 parent d67e7fc commit 5af7472

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

src/authorities.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,41 @@ export const AUTHORITIES = {
1313
// handles getting account authorities from the cache or the database
1414
// and also appends global authorities to the account authorities
1515
// usually will be used when wanting to get the account authorities
16-
export async function getAccountAuthorities(
16+
export async function getAccountAuthoritiesForScope(
1717
accountId: string,
1818
scope: string,
1919
forceDbQuery = false
2020
): Promise<string[]> {
2121
let authorities: string[] = [];
2222

23-
// handle scope authorities
24-
let currentScopeAuthorities: string[] = [];
25-
2623
if (!forceDbQuery) {
27-
currentScopeAuthorities =
28-
getAccountAuthoritiesCacheForScope(accountId, scope) || [];
24+
authorities = getAccountAuthoritiesCacheForScope(accountId, scope) || [];
2925
}
3026

31-
if (forceDbQuery || currentScopeAuthorities.length === 0) {
27+
if (forceDbQuery || authorities.length === 0) {
3228
// query from db
33-
currentScopeAuthorities =
34-
(await queryAccountAuthoritiesById(accountId, scope)) || [];
35-
setAccountAuthoritiesCacheForScope(
36-
accountId,
37-
scope,
38-
currentScopeAuthorities
39-
);
29+
authorities = (await queryAccountAuthoritiesById(accountId, scope)) || [];
30+
setAccountAuthoritiesCacheForScope(accountId, scope, authorities);
4031
}
4132

42-
authorities = currentScopeAuthorities;
43-
44-
// handle global authorities
45-
let globalAuthorities: string[] = [];
33+
return authorities;
34+
}
4635

47-
if (!forceDbQuery) {
48-
globalAuthorities =
49-
getAccountAuthoritiesCacheForScope(accountId, "global") || [];
50-
}
36+
export async function getAccountAuthorities(
37+
accountId: string,
38+
scopes: string[],
39+
forceDbQuery = false
40+
): Promise<string[]> {
41+
let authorities: string[] = [];
5142

52-
if (forceDbQuery || globalAuthorities.length === 0) {
53-
globalAuthorities =
54-
(await queryAccountAuthoritiesById(accountId, "global")) || [];
55-
setAccountAuthoritiesCacheForScope(accountId, "global", globalAuthorities);
43+
for (const scope of scopes) {
44+
const currentScopeAuthorities = await getAccountAuthoritiesForScope(
45+
accountId,
46+
scope,
47+
forceDbQuery
48+
);
49+
authorities = [...authorities, ...currentScopeAuthorities];
5650
}
5751

58-
// final result
59-
authorities = [...authorities, ...globalAuthorities];
60-
6152
return authorities;
6253
}

src/middlewares.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { Request, Response, NextFunction } from "express";
33
import config from "./config";
44
import { verifyToken } from "./utils/jwtUtils";
55
import { queryAccountAuthoritiesById } from "./helpers/dbQueries";
6-
import { getAccountAuthorities } from "./authorities";
6+
import {
7+
getAccountAuthorities,
8+
getAccountAuthoritiesForScope,
9+
} from "./authorities";
710
import { JwtPayload } from "jsonwebtoken";
811

912
// Timeout middleware
@@ -79,7 +82,7 @@ export const extractAuthorities =
7982
return next();
8083
}
8184
const accountId: string = res.locals.decodedJwt.sub as string;
82-
res.locals.authorities = await getAccountAuthorities(
85+
res.locals.authorities = await getAccountAuthoritiesForScope(
8386
accountId,
8487
"cas",
8588
forceDbQuery

src/routers.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
} from "./middlewares";
1717
import { HealthCheckResponse } from "./types";
1818
import configuration from "./config";
19-
import { AUTHORITIES, getAccountAuthorities } from "./authorities";
19+
import {
20+
AUTHORITIES,
21+
getAccountAuthorities,
22+
getAccountAuthoritiesForScope,
23+
} from "./authorities";
2024
import config from "./config";
2125

2226
export function handleRoutes(app: Express) {
@@ -119,10 +123,21 @@ export function handleRoutes(app: Express) {
119123
async (req: Request, res: Response) => {
120124
const { accountId } = res.locals;
121125

122-
const scope = req.query.scope as string;
126+
let scope = req.query.scope as string | string[] | undefined;
127+
128+
if (!scope) {
129+
scope = "global";
130+
}
123131

124-
const authorities = await getAccountAuthorities(accountId, scope);
132+
let authorities: string[];
125133

134+
// it accually the same but one uses a loop and the other doesn't
135+
// but why not make simple things complex
136+
if (Array.isArray(scope)) {
137+
authorities = await getAccountAuthorities(accountId, scope);
138+
} else {
139+
authorities = await getAccountAuthoritiesForScope(accountId, scope);
140+
}
126141
res.json({ authorities });
127142
}
128143
);

0 commit comments

Comments
 (0)