-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.ts
227 lines (196 loc) · 5.86 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import jwt from 'jsonwebtoken'
import { createHash } from 'crypto'
import libsodium from 'libsodium-wrappers'
import { Database, Challenge, Solves } from './src/app'
import { cryptoSignSeedKeypair, cryptoSign } from './src/libsodium'
import { SemanticError, NotFoundError } from './src/types/errors.type'
import { FIREBASE_CREDENTIALS } from './src/config'
export type APIError = {
code: string
message: string
location?: string
param?: string
}
export interface DatabaseStructure {
teams: {
[key: string]: {
name: string
countries: string[]
members: string[]
}
}
users: {
[key: string]: {
displayName: string
email: string
password: string
}
}
usersInfo: {
[uid: string]: {
shareInfo: boolean
}
}
solves: {
[key: string]: {
[key: string]: { timestamp: number, flag: string }
}
}
challenges: {
[id: string]: {
id: string
name: string
pk: string
salt: string
opslimit: number
memlimit: number
}
}
}
export function prepareDatabase (store: DatabaseStructure): Database {
const teams: Database['teams'] = {
register: async ({ name, countries, members }) => {
if (members.length !== 1 || typeof members[0] !== 'string') {
throw new Error('Members required')
}
const id = createHash('sha256')
.update(name)
.digest('hex')
if (store.teams[id]) {
throw new SemanticError('This team already exists')
}
store.teams[id] = { name, countries, members }
return { id, name, countries, members }
},
get: async id => {
const item = store.teams[id]
if (!item) {
throw new NotFoundError('Not found')
}
return { id, ...item }
},
list: async () => {
const teams = Object.entries(store.teams).map(([id, { name, countries }]) => ({ id, name, countries }))
return teams
}
}
const users: Database['users'] = {
current: async token => {
const userData = store.users[token]
if (!userData) {
throw new NotFoundError('User not found')
}
return { uid: token, ...userData }
},
register: async ({ uid, shareInfo }) => {
store.usersInfo[uid] = { shareInfo }
}
}
const solves: Database['solves'] = {
all: async () => {
const solves = Object.entries(store.solves).reduce(
(obj: { [teamId: string]: Solves }, [teamId, challenges]) => {
const teamSolves: Solves = Object.entries(challenges).reduce((obj: { [challengeId: string]: number }, [challengeId, { timestamp }]) => {
obj[challengeId] = timestamp
return obj
}, {})
obj[teamId] = teamSolves
return obj
},
{}
)
return solves
},
allWithFlag: async () => {
const solves: Array<{ teamId:string, challengeId:string, moment:number, flag:string }> = []
Object.entries(store.solves).forEach(([teamId, challenges]) => {
Object.entries(challenges).forEach(([challengeId, { timestamp, flag }]) => {
solves.push({ teamId, challengeId, moment: timestamp, flag })
})
})
return solves
},
register: async (teamId, challengeId, flag) => {
const team = store.teams[teamId]
if (!team) {
throw new NotFoundError('Team not found')
}
const challenge = store.challenges[challengeId]
if (!challenge) {
throw new NotFoundError('Challenge not found')
}
const previousData = store.solves[teamId] || {}
store.solves[teamId] = {
...previousData,
[challengeId]: { timestamp: new Date().getTime(), flag }
}
return Object.entries(store.solves[teamId]).reduce((obj: { [challengeId: string]: number }, [challengeId, { timestamp }]) => {
obj[challengeId] = timestamp
return obj
}, {})
},
get: async teamId => {
return Object.entries(store.solves[teamId]).reduce((obj: { [challengeId: string]: number }, [challengeId, { timestamp }]) => {
obj[challengeId] = timestamp
return obj
}, {})
}
}
const challenges: Database['challenges'] = {
all: async () => {
return store.challenges
},
get: async id => {
return store.challenges[id]
}
}
return {
teams,
users,
solves,
challenges
}
}
async function lookupFlag (flag: string, challenge: Challenge) {
await libsodium.ready
const decodedPk = Buffer.from(challenge.pk, 'base64')
const decodedSalt: Uint8Array = Buffer.from(challenge.salt, 'base64')
const { opslimit, memlimit } = challenge
const challengeSeed = await libsodium.crypto_pwhash(
libsodium.crypto_sign_SEEDBYTES,
flag,
decodedSalt,
opslimit,
memlimit,
libsodium.crypto_pwhash_ALG_ARGON2ID13
)
const keys = await cryptoSignSeedKeypair(challengeSeed)
if (decodedPk.compare(Buffer.from(keys.publicKey)) !== 0) {
return null
}
return keys
}
async function createProof (teamNameSha: string, privateKey: Uint8Array) {
const proof = await cryptoSign(teamNameSha, privateKey)
return proof
}
export async function claimFlag (
teamName: string,
flag: string,
challenge: Challenge
): Promise<string> {
const keys = await lookupFlag(flag, challenge)
if (!keys) {
throw new Error('This is not the correct flag.')
}
const sha = createHash('sha256')
.update(teamName)
.digest('hex')
const proof = await createProof(sha, keys.privateKey)
const encodedProof = Buffer.from(proof).toString('base64')
return encodedProof
}
export function createJWTToken ({ userId, email, verified, displayName }: { userId: string, email: string, verified: boolean, displayName: string }, privateKey?: string): string {
const token = jwt.sign({ user_id: userId, display_name: displayName, email, verified }, privateKey || FIREBASE_CREDENTIALS.private_key, { algorithm: 'RS256' })
return token
}