Skip to content

Commit

Permalink
add defaultDeviceSyncTOTP (#345)
Browse files Browse the repository at this point in the history
* add defaultDeviceSyncTOTP

* always sync TOTP for master device

Former-commit-id: ffe818611f5198946da566475fcb891c8dbcde5b [formerly ffa1c7a]
Former-commit-id: 627a022
  • Loading branch information
capaj authored Mar 19, 2023
1 parent eba009a commit d3c2835
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 6 deletions.
3 changes: 3 additions & 0 deletions backend/gqlSchemas/authier.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type UserQuery {
autofillTOTPEnabled: Boolean!
uiLanguage: String!
defaultDeviceTheme: String!
defaultDeviceSyncTOTP: Boolean!
Token: [TokenGQL!]!
masterDevice: DeviceGQL
recoveryDecryptionChallenge: DecryptionChallengeGQL
Expand Down Expand Up @@ -96,6 +97,7 @@ type UserGQL {
autofillTOTPEnabled: Boolean!
uiLanguage: String!
defaultDeviceTheme: String!
defaultDeviceSyncTOTP: Boolean!
Token: [TokenGQL!]!
masterDevice: DeviceGQL
recoveryDecryptionChallenge: DecryptionChallengeGQL
Expand Down Expand Up @@ -368,6 +370,7 @@ type UserMutation {
autofillTOTPEnabled: Boolean!
uiLanguage: String!
defaultDeviceTheme: String!
defaultDeviceSyncTOTP: Boolean!
Token: [TokenGQL!]!
masterDevice: DeviceGQL
recoveryDecryptionChallenge: DecryptionChallengeGQL
Expand Down
7 changes: 3 additions & 4 deletions backend/models/DecryptionChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ export class DecryptionChallengeApproved extends DecryptionChallengeGQL {
) {
const { id, deviceId, userId } = this

const where = { id: userId }

// TODO use findUnique when prisma bug gets fixed
const user = await ctx.prisma.user.findFirst({
where: where,
where: { id: userId },
include: {
EncryptedSecrets: true
}
Expand Down Expand Up @@ -110,7 +108,7 @@ export class DecryptionChallengeApproved extends DecryptionChallengeGQL {

if (device) {
if (device.userId !== user.id) {
throw new GraphqlError('Device is already registered for another user')
throw new GraphqlError('Device is already registered for another user') // prevents users from circumventing our limits by using multiple accounts
}

device = await ctx.prisma.device.update({
Expand All @@ -121,6 +119,7 @@ export class DecryptionChallengeApproved extends DecryptionChallengeGQL {
device = await ctx.prisma.device.create({
data: {
id: deviceId,
syncTOTP: user.defaultDeviceSyncTOTP,
firstIpAddress: ipAddress,
lastIpAddress: ipAddress,
firebaseToken: firebaseToken,
Expand Down
25 changes: 24 additions & 1 deletion backend/models/Device.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ describe('Device', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
reply: { setCookie: () => {}, clearCookie: () => vi.fn() },
request: { headers: {} },
device: {
syncTOTP: true
},
prisma: prismaClient,
jwtPayload: { userId: userId },
masterDeviceId: masterDeviceId,
Expand Down Expand Up @@ -296,6 +299,26 @@ describe('Device', () => {
expect(secrets).toHaveLength(user.loginCredentialsLimit)
})

it('should not sync TOTP when device has syncTOTP set to false', async () => {
testData.push({
encrypted: faker.datatype.string(25),
kind: EncryptedSecretTypeGQL.TOTP,
userId: userId,
version: 1
})
fakeCtx.device.syncTOTP = false
await prismaClient.encryptedSecret.createMany({
data: testData
})

const res = await deviceQuery.encryptedSecretsToSync(fakeCtx)
expect(res).toHaveLength(0)

fakeCtx.device.syncTOTP = true

testData.length = 0
})

it("should show 'TOTP limit exceeded, remove TOTP secrets'", async () => {
//Generate fake secrets for user
const numOverLimit = 5
Expand All @@ -304,7 +327,7 @@ describe('Device', () => {
for (let i = 0; i < user.TOTPlimit + numOverLimit; i++) {
testData.push({
encrypted: faker.datatype.string(25),
kind: 'TOTP',
kind: EncryptedSecretTypeGQL.TOTP,
userId: userId,
version: 1
})
Expand Down
8 changes: 8 additions & 0 deletions backend/models/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,27 @@ export class DeviceQuery extends DeviceGQL {
)
}

const kindOfSecret =
ctx.device.syncTOTP === true
? undefined // returns all secrets
: EncryptedSecretTypeGQL.LOGIN_CREDENTIALS // returns only login credentials

const res = await ctx.prisma.encryptedSecret.findMany({
where: {
OR: [
{
userId: this.userId,
kind: kindOfSecret,
createdAt: lastSyncCondition
},
{
userId: this.userId,
kind: kindOfSecret,
updatedAt: lastSyncCondition
},
{
userId: this.userId,
kind: kindOfSecret,
deletedAt: lastSyncCondition
}
]
Expand Down
3 changes: 3 additions & 0 deletions backend/models/generated/UserGQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export class UserGQLScalars {

@Field()
defaultDeviceTheme: string

@Field()
defaultDeviceSyncTOTP: boolean
}

@ObjectType()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "defaultDeviceSyncTOTP" BOOLEAN NOT NULL DEFAULT true;
4 changes: 3 additions & 1 deletion backend/prisma/prismaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ console.log('~ dbUrl', dbUrl)
const workerId = process.env.VITEST_WORKER_ID
if (workerId) {
const vitestWorkerId = Number(process.env.VITEST_WORKER_ID) % getDbCount()
dbUrl = `${dbUrl}_test_${vitestWorkerId + 1}` // this allows us to run tests in parallel against multiple dbs without conflicts
dbUrl = `${dbUrl}_test_${
vitestWorkerId + 1
}?connection_limit=500&pool_timeout=0&connect_timeout=0` // this allows us to run tests in parallel against multiple dbs without conflicts
} else {
log('DATABASE_URL', dbUrl)
}
Expand Down
1 change: 1 addition & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ model User {
autofillTOTPEnabled Boolean @default(true)
uiLanguage String @default("en")
defaultDeviceTheme String @default("dark")
defaultDeviceSyncTOTP Boolean @default(true)
UsageEvents SecretUsageEvent[]
EncryptedSecrets EncryptedSecret[]
Expand Down
1 change: 1 addition & 0 deletions backend/schemas/RootResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class RootResolver {
TOTPlimit: 3,
Devices: {
create: {
syncTOTP: true,
platform: input.devicePlatform,
id: deviceId,
firstIpAddress: ipAddress,
Expand Down

0 comments on commit d3c2835

Please sign in to comment.