2021-08-08 15:53:20 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
Column,
|
|
|
|
CreateDateColumn,
|
|
|
|
Entity,
|
|
|
|
ManyToOne,
|
|
|
|
PrimaryGeneratedColumn,
|
|
|
|
UpdateDateColumn,
|
|
|
|
} from 'typeorm';
|
|
|
|
|
|
|
|
import { User } from '../users/user.entity';
|
|
|
|
import { ProviderType } from './provider-type.enum';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The identity represents a single way for a user to login.
|
|
|
|
* A 'user' can have any number of these.
|
|
|
|
* Each one holds a type (local, github, twitter, etc.), if this type can have multiple instances (e.g. gitlab),
|
|
|
|
* it also saves the name of the instance. Also if this identity shall be the syncSource is saved.
|
|
|
|
*/
|
|
|
|
@Entity()
|
|
|
|
export class Identity {
|
|
|
|
@PrimaryGeneratedColumn()
|
|
|
|
id: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User that this identity corresponds to
|
|
|
|
*/
|
|
|
|
@ManyToOne((_) => User, (user) => user.identities, {
|
|
|
|
onDelete: 'CASCADE', // This deletes the Identity, when the associated User is deleted
|
|
|
|
})
|
2021-12-05 16:10:59 -05:00
|
|
|
user: Promise<User>;
|
2021-08-08 15:53:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The ProviderType of the identity
|
|
|
|
*/
|
|
|
|
@Column()
|
|
|
|
providerType: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the provider.
|
|
|
|
* Only set if there are multiple provider of that type (e.g. gitlab)
|
|
|
|
*/
|
|
|
|
@Column({
|
|
|
|
nullable: true,
|
|
|
|
type: 'text',
|
|
|
|
})
|
|
|
|
providerName: string | null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the identity should be used as the sync source.
|
|
|
|
* See [authentication doc](../../docs/content/dev/authentication.md) for clarification
|
|
|
|
*/
|
|
|
|
@Column()
|
|
|
|
syncSource: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the identity was created.
|
|
|
|
*/
|
|
|
|
@CreateDateColumn()
|
|
|
|
createdAt: Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the identity was last updated.
|
|
|
|
*/
|
|
|
|
@UpdateDateColumn()
|
|
|
|
updatedAt: Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The unique identifier of a user from the login provider
|
|
|
|
*/
|
|
|
|
@Column({
|
|
|
|
nullable: true,
|
|
|
|
type: 'text',
|
|
|
|
})
|
|
|
|
providerUserId: string | null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Token used to access the OAuth provider in the users name.
|
|
|
|
*/
|
|
|
|
@Column({
|
|
|
|
nullable: true,
|
|
|
|
type: 'text',
|
|
|
|
})
|
|
|
|
oAuthAccessToken: string | null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The hash of the password
|
|
|
|
* Only set when the type of the identity is local
|
|
|
|
*/
|
|
|
|
@Column({
|
|
|
|
nullable: true,
|
|
|
|
type: 'text',
|
|
|
|
})
|
|
|
|
passwordHash: string | null;
|
|
|
|
|
|
|
|
public static create(
|
|
|
|
user: User,
|
|
|
|
providerType: ProviderType,
|
2021-11-14 14:56:17 -05:00
|
|
|
syncSource: boolean,
|
2021-09-25 05:50:28 -04:00
|
|
|
): Omit<Identity, 'id' | 'createdAt' | 'updatedAt'> {
|
2021-08-08 15:53:20 -04:00
|
|
|
const newIdentity = new Identity();
|
2021-12-05 16:10:59 -05:00
|
|
|
newIdentity.user = Promise.resolve(user);
|
2021-08-08 15:53:20 -04:00
|
|
|
newIdentity.providerType = providerType;
|
2021-09-25 05:50:28 -04:00
|
|
|
newIdentity.providerName = null;
|
2021-08-08 15:53:20 -04:00
|
|
|
newIdentity.syncSource = syncSource;
|
2021-09-25 05:50:28 -04:00
|
|
|
newIdentity.providerUserId = null;
|
|
|
|
newIdentity.oAuthAccessToken = null;
|
|
|
|
newIdentity.passwordHash = null;
|
2021-08-08 15:53:20 -04:00
|
|
|
return newIdentity;
|
|
|
|
}
|
|
|
|
}
|