Skip to content

Commit f1cb348

Browse files
committed
Refactor authoritiesCache to include getAllAuthoritiesCacheForAccount function
1 parent b408dee commit f1cb348

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

src/routers.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Express, Request, Response } from "express";
55
import {
66
clearAllAccountsAuthoritiesCache,
77
clearAuthoritiesCacheForAccount,
8+
getAllAuthoritiesCacheForAccount,
89
getAllAuthoritiesCacheKeys,
910
} from "./utils/authoritiesCache";
1011
import {
@@ -15,7 +16,7 @@ import {
1516
} from "./middlewares";
1617
import { HealthCheckResponse } from "./types";
1718
import configuration from "./config";
18-
import { AUTHORITIES } from "./authorities";
19+
import { AUTHORITIES, getAccountAuthorities } from "./authorities";
1920

2021
export function handleRoutes(app: Express) {
2122
app.get("/", (req: Request, res: Response) => {
@@ -64,6 +65,7 @@ export function handleRoutes(app: Express) {
6465
return res.status(statusCode).json(result);
6566
});
6667

68+
// Login route
6769
app.post("/api/token", async (req: Request, res: Response) => {
6870
const { email, password } = req.body;
6971

@@ -88,29 +90,28 @@ export function handleRoutes(app: Express) {
8890
res.json({ token });
8991
});
9092

91-
app.get("/api/token/verify", (req: Request, res: Response) => {
92-
const token = req.headers.authorization?.split(" ")[1]; // Bearer token
93-
94-
if (!token) {
95-
return res.status(401).json({ message: "Token is missing" });
96-
}
97-
98-
try {
99-
const decoded = verifyToken(token);
100-
101-
res.json({ message: "Access granted", data: decoded });
102-
} catch (err) {
103-
res.status(401).json({ message: "Invalid token" });
93+
app.get(
94+
"/api/token/verify",
95+
extractJwt,
96+
requireJwt,
97+
(req: Request, res: Response) => {
98+
res.json({ message: "Token is valid" });
10499
}
105-
});
100+
);
106101

102+
// extract my authorities
107103
app.get(
108104
"/api/token/authorities",
109105
extractJwt,
110106
requireJwt,
111-
extractAuthorities(false),
112107
async (req: Request, res: Response) => {
113-
res.json({ authorities: res.locals.authorities });
108+
const { accountId } = res.locals;
109+
110+
const scope = req.query.scope as string;
111+
112+
const authorities = await getAccountAuthorities(accountId, scope);
113+
114+
res.json({ authorities });
114115
}
115116
);
116117

@@ -121,19 +122,23 @@ export function handleRoutes(app: Express) {
121122
extractAuthorities(true),
122123
requireAuthority(AUTHORITIES["view_account_cache"]),
123124
async (req, res) => {
124-
const { accountId } = req.query;
125-
126-
if (!accountId) {
127-
return res.status(400).json({ message: "Account ID is required" });
128-
}
129-
130125
const authoritiesAccountsIds = getAllAuthoritiesCacheKeys();
126+
131127
return res.json({
132128
authoritiesAccountsIds,
133129
});
134130
}
135131
);
136132

133+
app.get("/api/token/authorities/cache/:accountId", async (req, res) => {
134+
const { accountId } = req.params;
135+
136+
const authorities = getAllAuthoritiesCacheForAccount(accountId);
137+
return res.json({
138+
authorities,
139+
});
140+
});
141+
137142
app.delete(
138143
"/api/token/authorities/cache/:accountId",
139144
extractJwt,

src/utils/authoritiesCache.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export interface AccountAuthoritiesCache {
88
[scope: string]: string[] | undefined;
99
}
1010

11+
export function getAllAuthoritiesCacheForAccount(
12+
accountId: string
13+
): AccountAuthoritiesCache | undefined {
14+
return authoritiesCache.get<AccountAuthoritiesCache>(accountId);
15+
}
16+
1117
// used to get all of the saved authorities cache keys
1218
export function getAllAccountsAuthoritiesCache() {
1319
return authoritiesCache.keys();

0 commit comments

Comments
 (0)