Skip to content

Commit b2ef084

Browse files
authored
Merge pull request #208 from devchant/new_branch
feat: strengthen device authentication, linking, session security, an…
2 parents a79f260 + 99b5e87 commit b2ef084

3 files changed

Lines changed: 237 additions & 0 deletions

File tree

apps/backend/src/db/schema.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ export const wallets = pgTable('wallets', {
2929
createdAt: timestamp('created_at').notNull().defaultNow(),
3030
});
3131

32+
export const devices = pgTable('devices', {
33+
id: uuid('id').primaryKey().defaultRandom(),
34+
userId: uuid('user_id')
35+
.notNull()
36+
.references(() => users.id, { onDelete: 'cascade' }),
37+
deviceId: text('device_id').notNull(),
38+
deviceName: text('device_name').notNull(),
39+
platform: text('platform').notNull(),
40+
identityPublicKey: text('identity_public_key').notNull(),
41+
registrationId: text('registration_id'),
42+
isRevoked: boolean('is_revoked').notNull().default(false),
43+
createdAt: timestamp('created_at').notNull().defaultNow(),
44+
updatedAt: timestamp('updated_at').notNull().defaultNow(),
45+
});
46+
47+
export const devicePrekeys = pgTable('device_prekeys', {
48+
id: uuid('id').primaryKey().defaultRandom(),
49+
deviceId: uuid('device_id')
50+
.notNull()
51+
.references(() => devices.id, { onDelete: 'cascade' }),
52+
prekey: text('prekey').notNull(),
53+
createdAt: timestamp('created_at').notNull().defaultNow(),
54+
});
55+
3256
// ─── Conversations ────────────────────────────────────────────────────────────
3357

3458
export const conversationTypeEnum = pgEnum('conversation_type', ['dm', 'group']);
@@ -183,6 +207,15 @@ export const tokenTransfersRelations = relations(tokenTransfers, ({ one }) => ({
183207
}),
184208
}));
185209

210+
export const devicesRelations = relations(devices, ({ one, many }) => ({
211+
user: one(users, { fields: [devices.userId], references: [users.id] }),
212+
prekeys: many(devicePrekeys),
213+
}));
214+
215+
export const devicePrekeysRelations = relations(devicePrekeys, ({ one }) => ({
216+
device: one(devices, { fields: [devicePrekeys.deviceId], references: [devices.id] }),
217+
}));
218+
186219
export const userDevicesRelations = relations(userDevices, ({ one }) => ({
187220
user: one(users, { fields: [userDevices.userId], references: [users.id] }),
188221
}));
@@ -200,5 +233,9 @@ export type Message = typeof messages.$inferSelect;
200233
export type NewMessage = typeof messages.$inferInsert;
201234
export type TokenTransfer = typeof tokenTransfers.$inferSelect;
202235
export type NewTokenTransfer = typeof tokenTransfers.$inferInsert;
236+
export type Device = typeof devices.$inferSelect;
237+
export type NewDevice = typeof devices.$inferInsert;
238+
export type DevicePrekey = typeof devicePrekeys.$inferSelect;
239+
export type NewDevicePrekey = typeof devicePrekeys.$inferInsert;
203240
export type UserDevice = typeof userDevices.$inferSelect;
204241
export type NewUserDevice = typeof userDevices.$inferInsert;

apps/backend/src/schemas/auth.schemas.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ export const ChallengeSchema = z.object({
44
walletAddress: z.string().min(1, 'walletAddress is required'),
55
});
66

7+
export const DeviceSchema = z.object({
8+
deviceId: z.string().min(1, 'deviceId is required'),
9+
deviceName: z.string().min(1, 'deviceName is required'),
10+
platform: z.string().min(1, 'platform is required'),
11+
identityPublicKey: z.string().min(1, 'identityPublicKey is required'),
12+
registrationId: z.string().optional(),
13+
});
14+
715
export const VerifySchema = z.object({
816
walletAddress: z.string().min(1, 'walletAddress is required'),
917
signature: z.string().min(1, 'signature is required'),
1018
nonce: z.string().min(1, 'nonce is required'),
19+
device: DeviceSchema.optional(),
1120
});
1221

1322
export type ChallengeBody = z.infer<typeof ChallengeSchema>;
23+
export type DeviceBody = z.infer<typeof DeviceSchema>;
1424
export type VerifyBody = z.infer<typeof VerifySchema>;

package-lock.json

Lines changed: 190 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)