server/database/entities/User.ts

128 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-09-11 05:54:14 +02:00
import { getConfig, getHost } from "@config";
2023-09-13 02:29:13 +02:00
import {
BaseEntity,
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from "typeorm";
2023-09-12 22:48:10 +02:00
import { APIAccount } from "~types/entities/account";
2023-09-11 05:54:14 +02:00
const config = getConfig();
2023-09-11 05:31:08 +02:00
2023-09-12 22:48:10 +02:00
/**
* Stores local and remote users
*/
2023-09-11 05:31:08 +02:00
@Entity({
name: "users",
})
2023-09-12 22:48:10 +02:00
export class User extends BaseEntity {
2023-09-11 05:31:08 +02:00
@PrimaryGeneratedColumn("uuid")
id!: string;
2023-09-11 05:54:14 +02:00
@Column("varchar", {
unique: true,
})
2023-09-11 05:31:08 +02:00
username!: string;
2023-09-12 22:48:10 +02:00
@Column("varchar", {
2023-09-13 07:30:45 +02:00
unique: true,
2023-09-12 22:48:10 +02:00
})
2023-09-13 07:30:45 +02:00
display_name!: string;
@Column("varchar")
password!: string;
2023-09-11 05:31:08 +02:00
2023-09-11 05:54:14 +02:00
@Column("varchar", {
unique: true,
})
email!: string;
@Column("varchar", {
default: "",
})
bio!: string;
2023-09-11 05:31:08 +02:00
@CreateDateColumn()
created_at!: Date;
@UpdateDateColumn()
updated_at!: Date;
2023-09-11 05:54:14 +02:00
2023-09-18 07:38:08 +02:00
@Column("varchar")
public_key!: string;
@Column("varchar")
private_key!: string;
async generateKeys(): Promise<void> {
// openssl genrsa -out private.pem 2048
// openssl rsa -in private.pem -outform PEM -pubout -out public.pem
const keys = await crypto.subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256",
modulusLength: 4096,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
},
true,
["sign", "verify"]
);
const privateKey = btoa(
String.fromCharCode.apply(null, [
...new Uint8Array(
await crypto.subtle.exportKey("pkcs8", keys.privateKey)
),
])
);
const publicKey = btoa(
String.fromCharCode(
...new Uint8Array(
await crypto.subtle.exportKey("spki", keys.publicKey)
)
)
);
// Add header, footer and newlines later on
// These keys are PEM encrypted
this.private_key = privateKey;
this.public_key = publicKey;
}
2023-09-12 22:48:10 +02:00
// eslint-disable-next-line @typescript-eslint/require-await
async toAPI(): Promise<APIAccount> {
2023-09-11 05:54:14 +02:00
return {
acct: `@${this.username}@${getHost()}`,
avatar: "",
avatar_static: "",
bot: false,
created_at: this.created_at.toISOString(),
display_name: "",
followers_count: 0,
following_count: 0,
group: false,
header: "",
header_static: "",
id: this.id,
locked: false,
moved: null,
noindex: false,
note: this.bio,
suspended: false,
url: `${config.http.base_url}/@${this.username}`,
username: this.username,
emojis: [],
fields: [],
limited: false,
source: undefined,
statuses_count: 0,
discoverable: undefined,
role: undefined,
mute_expires_at: undefined,
2023-09-13 02:29:13 +02:00
};
2023-09-11 05:54:14 +02:00
}
2023-09-13 02:29:13 +02:00
}