Add U2F support

This commit is contained in:
Jonas Lochmann
2022-09-22 08:47:06 +02:00
parent 04aa2ce517
commit 613776cbf9
64 changed files with 2501 additions and 134 deletions
+15 -2
View File
@@ -33,7 +33,12 @@ export interface FamilyAttributesVersion2 {
nextServerKeyRequestSeq: string
}
export type FamilyAttributes = FamilyAttributesVersion1 & FamilyAttributesVersion2
export interface FamilyAttributesVersion3 {
u2fKeysVersion: string
}
export type FamilyAttributes = FamilyAttributesVersion1 &
FamilyAttributesVersion2 & FamilyAttributesVersion3
export type FamilyModel = Sequelize.Model<FamilyAttributes> & FamilyAttributes
export type FamilyModelStatic = typeof Sequelize.Model & {
@@ -64,9 +69,17 @@ export const attributesVersion2: SequelizeAttributes<FamilyAttributesVersion2> =
}
}
export const attributesVersion3: SequelizeAttributes<FamilyAttributesVersion3> = {
u2fKeysVersion: {
...versionColumn,
defaultValue: '0000'
}
}
export const attributes: SequelizeAttributes<FamilyAttributes> = {
...attributesVersion1,
...attributesVersion2
...attributesVersion2,
...attributesVersion3
}
export const createFamilyModel = (sequelize: Sequelize.Sequelize): FamilyModelStatic => sequelize.define('Family', attributes) as FamilyModelStatic
+3
View File
@@ -38,6 +38,7 @@ import { createOldDeviceModel, OldDeviceModelStatic } from './olddevice'
import { createPurchaseModel, PurchaseModelStatic } from './purchase'
import { createSessionDurationModel, SessionDurationModelStatic } from './sessionduration'
import { createTimelimitRuleModel, TimelimitRuleModelStatic } from './timelimitrule'
import { createU2fKeyModel, U2fKeyModelStatic } from './u2fkey'
import { createUsedTimeModel, UsedTimeModelStatic } from './usedtime'
import { createUserModel, UserModelStatic } from './user'
import { createUserLimitLoginCategoryModel, UserLimitLoginCategoryModelStatic } from './userlimitlogincategory'
@@ -66,6 +67,7 @@ export interface Database {
purchase: PurchaseModelStatic
sessionDuration: SessionDurationModelStatic
timelimitRule: TimelimitRuleModelStatic
u2fKey: U2fKeyModelStatic
usedTime: UsedTimeModelStatic
user: UserModelStatic
userLimitLoginCategory: UserLimitLoginCategoryModelStatic
@@ -95,6 +97,7 @@ const createDatabase = (sequelize: Sequelize.Sequelize): Database => ({
purchase: createPurchaseModel(sequelize),
sessionDuration: createSessionDurationModel(sequelize),
timelimitRule: createTimelimitRuleModel(sequelize),
u2fKey: createU2fKeyModel(sequelize),
usedTime: createUsedTimeModel(sequelize),
user: createUserModel(sequelize),
userLimitLoginCategory: createUserLimitLoginCategoryModel(sequelize),
@@ -0,0 +1,70 @@
/*
* server component for the TimeLimit App
* 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
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { QueryInterface, Sequelize, Transaction } from 'sequelize'
export async function up (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
const dialect = sequelize.getDialect()
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
const isPosgresql = dialect === 'postgres'
if (isMysql) {
await sequelize.query(
'CREATE TABLE `U2fKeys` ' +
'(`familyId` VARCHAR(10) NOT NULL,' +
'`keyId` VARCHAR(8) NOT NULL,' +
'`userId` VARCHAR(6) NOT NULL,' +
'`addedAt` BIGINT NOT NULL, ' +
'`keyHandle` BLOB NOT NULL, ' +
'`publicKey` BLOB NOT NULL, ' +
'`nextCounter` BIGINT NOT NULL, ' +
'PRIMARY KEY (`familyId`, `keyId`),' +
'FOREIGN KEY (`familyId`, `userId`) REFERENCES `Users` (`familyId`, `userId`) ON UPDATE CASCADE ON DELETE CASCADE' +
')',
{ transaction }
)
} else {
await sequelize.query(
'CREATE TABLE "U2fKeys" ' +
'("familyId" VARCHAR(10) NOT NULL,' +
'"keyId" VARCHAR(8) NOT NULL,' +
'"userId" VARCHAR(6) NOT NULL,' +
'"addedAt" ' + (isPosgresql ? 'BIGINT' : 'LONG') + ' NOT NULL, ' +
'"keyHandle" ' + (isPosgresql ? 'BYTEA' : 'BLOB') + ' NOT NULL, ' +
'"publicKey" ' + (isPosgresql ? 'BYTEA' : 'BLOB') + ' NOT NULL, ' +
'"nextCounter" ' + (isPosgresql ? 'BIGINT' : 'LONG') + ' NOT NULL, ' +
'PRIMARY KEY ("familyId", "keyId"),' +
'FOREIGN KEY ("familyId", "userId") REFERENCES "Users" ("familyId", "userId") ON UPDATE CASCADE ON DELETE CASCADE' +
')',
{ transaction }
)
}
await queryInterface.addIndex('U2fKeys', ['familyId', 'userId'], { transaction })
})
}
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.dropTable('U2fKeys', { transaction })
})
}
@@ -0,0 +1,39 @@
/*
* server component for the TimeLimit App
* 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
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { QueryInterface, Sequelize, Transaction } from 'sequelize'
import { attributesVersion3 as familyAttributes } from '../../family'
export async function up (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.addColumn('Families', 'u2fKeysVersion', {
...familyAttributes.u2fKeysVersion
}, {
transaction
})
})
}
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.removeColumn('Families', 'u2fKeysVersion', { transaction })
})
}
+82
View File
@@ -0,0 +1,82 @@
/*
* server component for the TimeLimit App
* 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
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { createHash } from 'crypto'
import { familyIdColumn, idWithinFamilyColumn, timestampColumn } from './columns'
import { SequelizeAttributes } from './types'
import { intToBuffer } from '../util/binary-number'
export function getU2fKeyId({ keyHandle, publicKey }: {
keyHandle: Buffer
publicKey: Buffer
}) {
return createHash('sha256')
.update(intToBuffer(keyHandle.length))
.update(keyHandle)
.update(intToBuffer(publicKey.length))
.update(publicKey)
.digest()
.slice(0, 6)
.toString('base64')
}
export interface U2fKeyAttributes {
familyId: string
keyId: string
userId: string
addedAt: string
keyHandle: Buffer
publicKey: Buffer
nextCounter: string
}
export type U2fKeyModel = Sequelize.Model<U2fKeyAttributes> & U2fKeyAttributes
export type U2fKeyModelStatic = typeof Sequelize.Model & {
new (values?: object, options?: Sequelize.BuildOptions): U2fKeyModel;
}
export const attributes: SequelizeAttributes<U2fKeyAttributes> = {
familyId: {
...familyIdColumn,
primaryKey: true
},
keyId: {
type: Sequelize.STRING(8),
primaryKey: true
},
userId: {
...idWithinFamilyColumn
},
addedAt: {
...timestampColumn
},
keyHandle: {
type: Sequelize.BLOB,
allowNull: false
},
publicKey: {
type: Sequelize.BLOB,
allowNull: false
},
nextCounter: {
type: Sequelize.BIGINT,
allowNull: false
}
}
export const createU2fKeyModel = (sequelize: Sequelize.Sequelize): U2fKeyModelStatic => sequelize.define('U2fKey', attributes) as U2fKeyModelStatic