Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 163e25b

Browse files
authored
Merge pull request #462 from redpwn/fix/misc-type-fixes
misc type fixes
2 parents 6dee4cf + d78b349 commit 163e25b

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

server/auth/token.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,43 @@ export enum tokenKinds {
1414
ctftimeAuth = 4
1515
}
1616

17-
export enum VerifyTokenKinds {
18-
update = 'update',
19-
register = 'register'
20-
}
17+
export type VerifyTokenKinds = 'update' | 'register' | 'recover'
2118

2219
export type AuthTokenData = string
20+
2321
export type TeamTokenData = string
24-
export interface VerifyTokenData {
22+
23+
interface BaseVerifyTokenData {
2524
verifyId: string
2625
kind: VerifyTokenKinds
26+
}
27+
28+
export interface RegisterVerifyTokenData extends BaseVerifyTokenData {
29+
kind: 'register'
30+
email: User['email']
31+
name: User['name']
32+
division: User['division']
33+
}
34+
35+
export interface UpdateVerifyTokenData extends BaseVerifyTokenData {
36+
kind: 'update'
2737
userId: User['id']
2838
email: User['email']
2939
division: User['division']
3040
}
31-
export type CtftimeAuthTokenData = string
41+
42+
export interface RecoverTokenData extends BaseVerifyTokenData {
43+
kind: 'recover'
44+
userId: User['id']
45+
email: User['email']
46+
}
47+
48+
export type VerifyTokenData = RegisterVerifyTokenData | UpdateVerifyTokenData | RecoverTokenData
49+
50+
export interface CtftimeAuthTokenData {
51+
name: User['name']
52+
ctftimeId: User['ctftimeId']
53+
}
3254

3355
// Internal map of type definitions for typing purposes only -
3456
// this type does not describe a real data-structure

server/database/users.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { DivisionACLError } from '../errors'
66
export interface User {
77
id: string;
88
name: string;
9-
email: string;
9+
email?: string;
1010
division: keyof ServerConfig['divisions'];
11-
ctftimeId: string;
11+
ctftimeId?: string;
1212
perms: number;
1313
}
1414

server/util/restrict.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import config, { ServerConfig } from '../config/server'
22

3-
type ACLCheck = (email: string) => boolean
3+
type ACLCheck = (email: string | undefined) => boolean
44

55
export interface ACL {
66
match: string;
@@ -16,11 +16,11 @@ interface CompiledACL {
1616
let acls: CompiledACL[]
1717

1818
const restrictionChecks: { [checkType: string]: (value: string) => ACLCheck } = {
19-
domain: value => email => email.endsWith('@' + value),
19+
domain: value => email => email?.endsWith('@' + value) ?? false,
2020
email: value => email => email === value,
2121
regex: value => {
2222
const re = new RegExp(value)
23-
return email => re.test(email)
23+
return email => email === undefined ? false : re.test(email)
2424
},
2525
any: value => email => true // eslint-disable-line @typescript-eslint/no-unused-vars
2626
}
@@ -45,7 +45,7 @@ export const compileACLs = (): void => {
4545

4646
compileACLs()
4747

48-
export const allowedDivisions = (email: string): string[] => {
48+
export const allowedDivisions = (email: string | undefined): string[] => {
4949
for (const acl of acls) {
5050
if (acl.check(email)) {
5151
return acl.divisions
@@ -54,6 +54,6 @@ export const allowedDivisions = (email: string): string[] => {
5454
return []
5555
}
5656

57-
export const divisionAllowed = (email: string, division: string): boolean => {
57+
export const divisionAllowed = (email: string | undefined, division: string): boolean => {
5858
return allowedDivisions(email).includes(division)
5959
}

test/unit/restrict.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,28 @@ test.serial('throws error on invalid matcher', t => {
8585
error = t.throws(restrict.compileACLs)
8686
t.is(error.message, 'Unrecognized ACL matcher "__proto__"')
8787
})
88+
89+
test.serial('denies no email with all matchers except any', t => {
90+
config.divisionACLs = [{
91+
match: 'domain',
92+
value: 'good-domain.com',
93+
divisions: ['domain']
94+
}, {
95+
match: 'email',
96+
97+
divisions: ['email']
98+
}, {
99+
match: 'regex',
100+
value: '^regex-email(-[a-z]+)[email protected]$',
101+
divisions: ['regex']
102+
}, {
103+
match: 'any',
104+
value: '',
105+
divisions: ['any']
106+
}]
107+
restrict.compileACLs()
108+
t.false(restrict.divisionAllowed(undefined, 'domain'))
109+
t.false(restrict.divisionAllowed(undefined, 'email'))
110+
t.false(restrict.divisionAllowed(undefined, 'regex'))
111+
t.true(restrict.divisionAllowed(undefined, 'any'))
112+
})

0 commit comments

Comments
 (0)