Skip to content

Commit

Permalink
Enable admins to see names
Browse files Browse the repository at this point in the history
  • Loading branch information
enkoder committed Mar 30, 2024
1 parent 44e03ea commit 23c54ed
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 54 deletions.
4 changes: 2 additions & 2 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { RewriteFrames, Toucan } from "toucan-js";
import { processQueueBatch, processScheduledEvent } from "./background.js";
import { ALS } from "./g.js";
import type { ABREntryType, ABRTournamentType } from "./lib/abr.js";
import { adminOnly, authenticatedUser } from "./lib/auth.js";
import { adminOnly, authMiddleware, authenticatedUser } from "./lib/auth.js";
import { errorResponse } from "./lib/errors.js";
import { trace } from "./lib/tracer.js";
import {
Expand Down Expand Up @@ -81,7 +81,7 @@ const { preflight, corsify } = createCors({

router
// un-authed endpoints
.all("*", preflight)
.all("*", preflight, authMiddleware)

.get("/auth/login_url", GetLoginUrl)
.get("/auth/token", GetTokenFromCode)
Expand Down
32 changes: 12 additions & 20 deletions api/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ export async function verifyPassword(
// return decode(token);
//}

export async function authenticatedUser(request: RequestWithDB, env: Env) {
export async function authMiddleware(request: RequestWithDB, env: Env) {
const access_token = getBearer(request);
if (!access_token) {
return errorResponse(401, "No token");
}
if (!access_token) return;

const isTestMode = env.IS_TEST && env.LOGGED_IN_USER_ID !== null;

Expand All @@ -100,27 +98,21 @@ export async function authenticatedUser(request: RequestWithDB, env: Env) {
email: accountInfo.email,
});
}
//const user = await Users.getById(Number(session.payload.sub));
//let session;

//if (token) {
// // Implement your own token validation here
// session = await validateToken(token, env);
//}

//if (!token || !session) {
// return errorResponse(401, "Authentication error. Invalid token");
//}
//const user = await Users.getById(Number(session.payload.sub));

//if (!user) {
// return errorResponse(401, "Authentication error. Invalid user.");
//}
// user_id not null implies user is logged in
// these variables can be used in vies
request.user_id = user.id;
request.is_admin = user.is_admin !== 0;
}

export function authenticatedUser(request: RequestWithDB, env: Env) {
const isTestMode = env.IS_TEST && env.LOGGED_IN_USER_ID !== null;
if (isTestMode) return;

if (!request.user_id) {
return errorResponse(401, "No token");
}
}

export function adminOnly(request: RequestWithDB, _: Env) {
if (!request.is_admin) {
return errorResponse(401, "Authentication error");
Expand Down
6 changes: 4 additions & 2 deletions api/src/models/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export class Leaderboard {
faction,
format,
tags,
isAdmin,
}: {
seasonId?: number;
faction?: Faction;
format?: Format;
tags?: string[];
isAdmin?: boolean;
}): Promise<LeaderboardRow[]> {
const results = await Results.getExpanded({
seasonId,
Expand All @@ -38,8 +40,8 @@ export class Leaderboard {
rows[result.user_id] = {
points: 0,
rank: 0,
user_id: result.disabled ? 0 : result.user_id,
user_name: result.disabled ? null : result.user_name,
user_id: result.disabled && !isAdmin ? 0 : result.user_id,
user_name: result.disabled && !isAdmin ? null : result.user_name,
attended: 0,
disabled: result.disabled,
} as LeaderboardRow;
Expand Down
1 change: 1 addition & 0 deletions api/src/routes/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class GetLeaderboard extends OpenAPIRoute {
faction,
format,
tags,
isAdmin: req.is_admin,
});
for (const result of results) {
rows.push(LeaderboardRowComponent.parse(result));
Expand Down
4 changes: 2 additions & 2 deletions app/src/client/core/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio
}), {} as Record<string, string>);

if (isStringWithValue(token)) {
headers.Authorization = `Bearer ${token}`;
headers['Authorization'] = `Bearer ${token}`;
}

if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(`${username}:${password}`);
headers.Authorization = `Basic ${credentials}`;
headers['Authorization'] = `Basic ${credentials}`;
}

if (options.body) {
Expand Down
1 change: 1 addition & 0 deletions app/src/client/models/TournamentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export enum TournamentType {
TEAM_TOURNAMENT = 'team tournament',
WORLDS_CHAMPIONSHIP = 'worlds championship',
REGIONAL_CHAMPIONSHIP = 'regional championship',
PLAYERS_CIRCUIT = 'players circuit',
}
2 changes: 1 addition & 1 deletion app/src/client/services/AdminService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class AdminService {
public static postIngestTournament(
requestBody?: {
userId?: number;
tournamentType?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14;
tournamentType?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15;
},
): CancelablePromise<any> {
return __request(OpenAPI, {
Expand Down
2 changes: 1 addition & 1 deletion app/src/client/services/AssetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class AssetsService {
method: 'GET',
url: '/api/assets/ids/{id}',
path: {
id: id,
'id': id,
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/client/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AuthService {
method: 'GET',
url: '/api/auth/token',
query: {
code: code,
'code': code,
},
});
}
Expand All @@ -53,7 +53,7 @@ export class AuthService {
method: 'GET',
url: '/api/auth/refresh_token',
query: {
refresh_token: refreshToken,
'refresh_token': refreshToken,
},
});
}
Expand Down
12 changes: 6 additions & 6 deletions app/src/client/services/LeaderboardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export class LeaderboardService {
method: 'GET',
url: '/api/leaderboard',
query: {
seasonId: seasonId,
factionCode: factionCode,
format: format,
tags: tags,
'seasonId': seasonId,
'factionCode': factionCode,
'format': format,
'tags': tags,
},
});
}
Expand All @@ -57,8 +57,8 @@ export class LeaderboardService {
method: 'GET',
url: '/api/point-distribution',
query: {
numPlayers: numPlayers,
type: type,
'numPlayers': numPlayers,
'type': type,
},
});
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/client/services/ResultsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export class ResultsService {
method: 'GET',
url: '/api/users/{user}/results',
path: {
user: user,
'user': user,
},
query: {
season: season,
factionCode: factionCode,
format: format,
tags: tags,
'season': season,
'factionCode': factionCode,
'format': format,
'tags': tags,
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/client/services/SeasonsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class SeasonsService {
method: 'GET',
url: '/api/seasons/{seasonId}/tournaments',
path: {
seasonId: seasonId,
'seasonId': seasonId,
},
});
}
Expand Down
14 changes: 7 additions & 7 deletions app/src/client/services/TagsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class TagsService {
method: 'GET',
url: '/api/tags',
query: {
owner_id: ownerId,
'owner_id': ownerId,
},
});
}
Expand Down Expand Up @@ -62,7 +62,7 @@ export class TagsService {
method: 'DELETE',
url: '/api/tags/{tag_id}',
path: {
tag_id: tagId,
'tag_id': tagId,
},
});
}
Expand All @@ -84,7 +84,7 @@ export class TagsService {
method: 'POST',
url: '/api/tags/{tag_id}',
path: {
tag_id: tagId,
'tag_id': tagId,
},
body: requestBody,
mediaType: 'application/json',
Expand All @@ -108,7 +108,7 @@ export class TagsService {
method: 'PUT',
url: '/api/tags/{tag_id}/tournament',
path: {
tag_id: tagId,
'tag_id': tagId,
},
body: requestBody,
mediaType: 'application/json',
Expand All @@ -128,7 +128,7 @@ export class TagsService {
method: 'GET',
url: '/api/tags/{tag_id}/tournament',
path: {
tag_id: tagId,
'tag_id': tagId,
},
});
}
Expand All @@ -148,8 +148,8 @@ export class TagsService {
method: 'DELETE',
url: '/api/tags/{tag_id}/tournament/{tag_tournament_id}',
path: {
tag_id: tagId,
tag_tournament_id: tagTournamentId,
'tag_id': tagId,
'tag_tournament_id': tagTournamentId,
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/client/services/TournamentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class TournamentService {
method: 'GET',
url: '/api/tournaments/{tournamentId}',
path: {
tournamentId: tournamentId,
'tournamentId': tournamentId,
},
});
}
Expand All @@ -54,7 +54,7 @@ export class TournamentService {
method: 'GET',
url: '/api/tournaments/{tournamentId}/results',
path: {
tournamentId: tournamentId,
'tournamentId': tournamentId,
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/client/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class UserService {
method: 'GET',
url: '/api/users/{userID}',
path: {
userID: userId,
'userID': userId,
},
});
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/routes/leaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ export function Leaderboard() {
)}
>
{row.disabled ? (
<text>{row.user_name}</text>
<text className="text-gray-500 opacity-30">
{row.user_name}
</text>
) : (
<Link to={getLinkToUserSearchParams(row)}>
{row.user_name}
Expand Down
2 changes: 1 addition & 1 deletion spec.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions unix.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
},
"[toml]": {
"editor.defaultFormatter": "tamasfe.even-better-toml"
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
}

0 comments on commit 23c54ed

Please sign in to comment.