mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Send ClientDataStatus after registering new device
This commit is contained in:
+9
-5
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2022 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -24,10 +24,12 @@ import { logoutAtPrimaryDevice } from '../function/child/logout-at-primary-devic
|
||||
import { setPrimaryDevice } from '../function/child/set-primary-device'
|
||||
import { WebsocketApi } from '../websocket'
|
||||
import { isRegisterChildDeviceRequest, isRequestWithAuthToken, isUpdatePrimaryDeviceRequest } from './validator'
|
||||
import { EventHandler } from '../monitoring/eventhandler'
|
||||
|
||||
export const createChildRouter = ({ database, websocket }: {
|
||||
database: Database,
|
||||
export const createChildRouter = ({ database, websocket, eventHandler }: {
|
||||
database: Database
|
||||
websocket: WebsocketApi
|
||||
eventHandler: EventHandler
|
||||
}) => {
|
||||
const router = Router()
|
||||
|
||||
@@ -37,15 +39,17 @@ export const createChildRouter = ({ database, websocket }: {
|
||||
throw new BadRequest()
|
||||
}
|
||||
|
||||
const { deviceAuthToken, deviceId } = await addChildDevice({
|
||||
const { deviceAuthToken, deviceId, data } = await addChildDevice({
|
||||
request: req.body,
|
||||
database,
|
||||
eventHandler,
|
||||
websocket
|
||||
})
|
||||
|
||||
res.json({
|
||||
deviceAuthToken,
|
||||
ownDeviceId: deviceId
|
||||
ownDeviceId: deviceId,
|
||||
data
|
||||
})
|
||||
} catch (ex) {
|
||||
next(ex)
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2022 Jonas Lochmann
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
@@ -47,8 +47,8 @@ export const createApi = ({ database, websocket, connectedDevicesManager, eventH
|
||||
})
|
||||
|
||||
app.use('/auth', createAuthRouter(database))
|
||||
app.use('/child', createChildRouter({ database, websocket }))
|
||||
app.use('/parent', createParentRouter({ database, websocket }))
|
||||
app.use('/child', createChildRouter({ database, websocket, eventHandler }))
|
||||
app.use('/parent', createParentRouter({ database, websocket, eventHandler }))
|
||||
app.use('/purchase', createPurchaseRouter({ database, websocket }))
|
||||
app.use('/sync', createSyncRouter({ database, websocket, connectedDevicesManager, eventHandler }))
|
||||
|
||||
|
||||
+17
-4
@@ -31,6 +31,7 @@ import { signInIntoFamily } from '../function/parent/sign-in-into-family'
|
||||
import { validateU2fIntegrity, U2fValidationError } from '../function/u2f'
|
||||
import { createIdentityToken, MissingSignSecretException } from '../util/identity-token'
|
||||
import { WebsocketApi } from '../websocket'
|
||||
import { EventHandler } from '../monitoring/eventhandler'
|
||||
import {
|
||||
isCreateFamilyByMailTokenRequest,
|
||||
isCreateRegisterDeviceTokenRequest, isLinkParentMailAddressRequest,
|
||||
@@ -38,7 +39,13 @@ import {
|
||||
isRemoveDeviceRequest, isSignIntoFamilyRequest, isRequestIdentityTokenRequest
|
||||
} from './validator'
|
||||
|
||||
export const createParentRouter = ({ database, websocket }: {database: Database, websocket: WebsocketApi}) => {
|
||||
export const createParentRouter = ({
|
||||
database, websocket, eventHandler
|
||||
}: {
|
||||
database: Database
|
||||
websocket: WebsocketApi
|
||||
eventHandler: EventHandler
|
||||
}) => {
|
||||
const router = Router()
|
||||
|
||||
router.post('/get-status-by-mail-address', json(), async (req, res, next) => {
|
||||
@@ -75,17 +82,20 @@ export const createParentRouter = ({ database, websocket }: {database: Database,
|
||||
|
||||
const result = await createFamily({
|
||||
database,
|
||||
eventHandler,
|
||||
firstParentDevice: req.body.parentDevice,
|
||||
mailAuthToken: req.body.mailAuthToken,
|
||||
password: req.body.parentPassword,
|
||||
deviceName: req.body.deviceName,
|
||||
parentName: req.body.parentName,
|
||||
timeZone: req.body.timeZone
|
||||
timeZone: req.body.timeZone,
|
||||
clientLevel: req.body.clientLevel || null
|
||||
})
|
||||
|
||||
res.json({
|
||||
deviceAuthToken: result.deviceAuthToken,
|
||||
ownDeviceId: result.deviceId
|
||||
ownDeviceId: result.deviceId,
|
||||
data: result.data
|
||||
})
|
||||
} catch (ex) {
|
||||
next(ex)
|
||||
@@ -100,15 +110,18 @@ export const createParentRouter = ({ database, websocket }: {database: Database,
|
||||
|
||||
const result = await signInIntoFamily({
|
||||
database,
|
||||
eventHandler,
|
||||
newDeviceInfo: req.body.parentDevice,
|
||||
mailAuthToken: req.body.mailAuthToken,
|
||||
deviceName: req.body.deviceName,
|
||||
clientLevel: req.body.clientLevel || null,
|
||||
websocket
|
||||
})
|
||||
|
||||
res.json({
|
||||
deviceAuthToken: result.deviceAuthToken,
|
||||
ownDeviceId: result.deviceId
|
||||
ownDeviceId: result.deviceId,
|
||||
data: result.data
|
||||
})
|
||||
} catch (ex) {
|
||||
next(ex)
|
||||
|
||||
@@ -84,12 +84,14 @@ export interface CreateFamilyByMailTokenRequest {
|
||||
deviceName: string
|
||||
timeZone: string
|
||||
parentName: string
|
||||
clientLevel?: number
|
||||
}
|
||||
|
||||
export interface SignIntoFamilyRequest {
|
||||
mailAuthToken: string
|
||||
parentDevice: NewDeviceInfo
|
||||
deviceName: string
|
||||
clientLevel?: number
|
||||
}
|
||||
|
||||
export interface RecoverParentPasswordRequest {
|
||||
@@ -101,6 +103,7 @@ export interface RegisterChildDeviceRequest {
|
||||
registerToken: string
|
||||
childDevice: NewDeviceInfo
|
||||
deviceName: string
|
||||
clientLevel?: number
|
||||
}
|
||||
|
||||
export interface CreateRegisterDeviceTokenRequest {
|
||||
|
||||
@@ -2930,6 +2930,9 @@ export const isCreateFamilyByMailTokenRequest: (value: unknown) => value is Crea
|
||||
},
|
||||
"parentName": {
|
||||
"type": "string"
|
||||
},
|
||||
"clientLevel": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
@@ -2955,6 +2958,9 @@ export const isSignIntoFamilyRequest: (value: unknown) => value is SignIntoFamil
|
||||
},
|
||||
"deviceName": {
|
||||
"type": "string"
|
||||
},
|
||||
"clientLevel": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
@@ -2995,6 +3001,9 @@ export const isRegisterChildDeviceRequest: (value: unknown) => value is Register
|
||||
},
|
||||
"deviceName": {
|
||||
"type": "string"
|
||||
},
|
||||
"clientLevel": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
|
||||
@@ -22,13 +22,22 @@ import { generateAuthToken, generateVersionId } from '../../util/token'
|
||||
import { WebsocketApi } from '../../websocket'
|
||||
import { prepareDeviceEntry } from '../device/prepare-device-entry'
|
||||
import { notifyClientsAboutChangesDelayed } from '../websocket'
|
||||
import { generateServerDataStatus } from '../sync/get-server-data-status'
|
||||
import { EventHandler } from '../../monitoring/eventhandler'
|
||||
import { ServerDataStatus } from '../../object/serverdatastatus'
|
||||
import { createEmptyClientDataStatus } from '../../object/clientdatastatus'
|
||||
|
||||
export const addChildDevice = async ({ database, websocket, request }: {
|
||||
export const addChildDevice = async ({ database, eventHandler, websocket, request }: {
|
||||
database: Database
|
||||
eventHandler: EventHandler
|
||||
websocket: WebsocketApi
|
||||
request: RegisterChildDeviceRequest
|
||||
// no transaction here because this is directly called from an API endpoint
|
||||
}) => {
|
||||
}): Promise<{
|
||||
deviceId: string
|
||||
deviceAuthToken: string
|
||||
data: ServerDataStatus
|
||||
}> => {
|
||||
return database.transaction(async (transaction) => {
|
||||
const entry = await database.addDeviceToken.findOne({
|
||||
where: {
|
||||
@@ -75,9 +84,19 @@ export const addChildDevice = async ({ database, websocket, request }: {
|
||||
transaction
|
||||
})
|
||||
|
||||
const data = await generateServerDataStatus({
|
||||
database,
|
||||
clientStatus: createEmptyClientDataStatus({ clientLevel: request.clientLevel || null }),
|
||||
familyId: entry.familyId,
|
||||
deviceId,
|
||||
transaction,
|
||||
eventHandler
|
||||
})
|
||||
|
||||
return {
|
||||
deviceId,
|
||||
deviceAuthToken
|
||||
deviceAuthToken,
|
||||
data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -16,25 +16,38 @@
|
||||
*/
|
||||
|
||||
import { Conflict } from 'http-errors'
|
||||
import { generateServerDataStatus } from '../sync/get-server-data-status'
|
||||
import { NewDeviceInfo, PlaintextParentPassword, assertPlaintextParentPasswordValid } from '../../api/schema'
|
||||
import { Database } from '../../database'
|
||||
import { maxMailNotificationFlags } from '../../database/user'
|
||||
import { EventHandler } from '../../monitoring/eventhandler'
|
||||
import { ServerDataStatus } from '../../object/serverdatastatus'
|
||||
import { createEmptyClientDataStatus } from '../../object/clientdatastatus'
|
||||
import {
|
||||
generateAuthToken, generateFamilyId, generateIdWithinFamily, generateVersionId
|
||||
} from '../../util/token'
|
||||
import { requireMailAndLocaleByAuthToken } from '../authentication'
|
||||
import { prepareDeviceEntry } from '../device/prepare-device-entry'
|
||||
|
||||
export const createFamily = async ({ database, mailAuthToken, firstParentDevice, password, timeZone, parentName, deviceName }: {
|
||||
database: Database,
|
||||
mailAuthToken: string,
|
||||
firstParentDevice: NewDeviceInfo,
|
||||
password: PlaintextParentPassword,
|
||||
timeZone: string,
|
||||
parentName: string,
|
||||
export async function createFamily ({
|
||||
database, eventHandler, mailAuthToken, firstParentDevice,
|
||||
password, timeZone, parentName, deviceName, clientLevel
|
||||
}: {
|
||||
database: Database
|
||||
eventHandler: EventHandler
|
||||
mailAuthToken: string
|
||||
firstParentDevice: NewDeviceInfo
|
||||
password: PlaintextParentPassword
|
||||
timeZone: string
|
||||
parentName: string
|
||||
deviceName: string
|
||||
clientLevel: number | null
|
||||
// no transaction here because this is directly called from an API endpoint
|
||||
}) => {
|
||||
}): Promise<{
|
||||
deviceAuthToken: string
|
||||
deviceId: string
|
||||
data: ServerDataStatus
|
||||
}> {
|
||||
assertPlaintextParentPasswordValid(password)
|
||||
|
||||
return database.transaction(async (transaction) => {
|
||||
@@ -42,14 +55,14 @@ export const createFamily = async ({ database, mailAuthToken, firstParentDevice,
|
||||
const mailInfo = await requireMailAndLocaleByAuthToken({ database, mailAuthToken, transaction, invalidate: true })
|
||||
|
||||
// ensure that no family was created for this mail yet
|
||||
const exisitngUserEntry = await database.user.findOne({
|
||||
const existingUserEntry = await database.user.findOne({
|
||||
where: {
|
||||
mail: mailInfo.mail
|
||||
},
|
||||
transaction
|
||||
})
|
||||
|
||||
if (exisitngUserEntry) {
|
||||
if (existingUserEntry) {
|
||||
throw new Conflict()
|
||||
}
|
||||
|
||||
@@ -103,9 +116,19 @@ export const createFamily = async ({ database, mailAuthToken, firstParentDevice,
|
||||
isUserKeptSignedIn: true
|
||||
}), { transaction })
|
||||
|
||||
const data = await generateServerDataStatus({
|
||||
database,
|
||||
clientStatus: createEmptyClientDataStatus({ clientLevel }),
|
||||
familyId,
|
||||
deviceId,
|
||||
transaction,
|
||||
eventHandler
|
||||
})
|
||||
|
||||
return {
|
||||
deviceAuthToken,
|
||||
deviceId
|
||||
deviceId,
|
||||
data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -24,15 +24,21 @@ import { WebsocketApi } from '../../websocket'
|
||||
import { requireMailAndLocaleByAuthToken } from '../authentication'
|
||||
import { prepareDeviceEntry } from '../device/prepare-device-entry'
|
||||
import { notifyClientsAboutChangesDelayed } from '../websocket'
|
||||
import { generateServerDataStatus } from '../sync/get-server-data-status'
|
||||
import { EventHandler } from '../../monitoring/eventhandler'
|
||||
import { ServerDataStatus } from '../../object/serverdatastatus'
|
||||
import { createEmptyClientDataStatus } from '../../object/clientdatastatus'
|
||||
|
||||
export const signInIntoFamily = async ({ database, mailAuthToken, newDeviceInfo, deviceName, websocket }: {
|
||||
export const signInIntoFamily = async ({ database, eventHandler, mailAuthToken, newDeviceInfo, deviceName, websocket, clientLevel }: {
|
||||
database: Database
|
||||
eventHandler: EventHandler
|
||||
mailAuthToken: string
|
||||
newDeviceInfo: NewDeviceInfo
|
||||
deviceName: string
|
||||
websocket: WebsocketApi
|
||||
clientLevel: number | null
|
||||
// no transaction here because this is directly called from an API endpoint
|
||||
}): Promise<{ deviceId: string; deviceAuthToken: string }> => {
|
||||
}): Promise<{ deviceId: string; deviceAuthToken: string; data: ServerDataStatus }> => {
|
||||
return database.transaction(async (transaction) => {
|
||||
const mailInfo = await requireMailAndLocaleByAuthToken({ database, mailAuthToken, transaction, invalidate: true })
|
||||
|
||||
@@ -94,9 +100,19 @@ export const signInIntoFamily = async ({ database, mailAuthToken, newDeviceInfo,
|
||||
})
|
||||
})
|
||||
|
||||
const data = await generateServerDataStatus({
|
||||
database,
|
||||
clientStatus: createEmptyClientDataStatus({ clientLevel }),
|
||||
familyId: userEntry.familyId,
|
||||
deviceId,
|
||||
transaction,
|
||||
eventHandler
|
||||
})
|
||||
|
||||
return {
|
||||
deviceId,
|
||||
deviceAuthToken
|
||||
deviceAuthToken,
|
||||
data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -28,6 +28,18 @@ export interface ClientDataStatus {
|
||||
u2f?: string // last u2f list version
|
||||
}
|
||||
|
||||
export function createEmptyClientDataStatus({ clientLevel }: {
|
||||
clientLevel: number | null
|
||||
}): ClientDataStatus {
|
||||
return {
|
||||
devices: '',
|
||||
apps: {},
|
||||
categories: {},
|
||||
users: '',
|
||||
clientLevel: clientLevel || undefined
|
||||
}
|
||||
}
|
||||
|
||||
export type ClientDataStatusApps = {[key: string]: string} // installedAppsVersionsByDeviceId
|
||||
export type ClientDataStatusCategories = {[key: string]: CategoryDataStatus}
|
||||
export type ClientDataStatusDevicesExtended = {[key: string]: DeviceDataStatus}
|
||||
|
||||
Reference in New Issue
Block a user