mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 03:06:31 -05:00
refactor(auth-token): lazy-load relations
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
b25e6fc365
commit
de6d75238c
3 changed files with 15 additions and 11 deletions
|
@ -24,7 +24,7 @@ export class AuthToken {
|
||||||
@ManyToOne((_) => User, (user) => user.authTokens, {
|
@ManyToOne((_) => User, (user) => user.authTokens, {
|
||||||
onDelete: 'CASCADE', // This deletes the AuthToken, when the associated User is deleted
|
onDelete: 'CASCADE', // This deletes the AuthToken, when the associated User is deleted
|
||||||
})
|
})
|
||||||
user: User;
|
user: Promise<User>;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
label: string;
|
label: string;
|
||||||
|
@ -53,7 +53,7 @@ export class AuthToken {
|
||||||
): Omit<AuthToken, 'id' | 'createdAt'> {
|
): Omit<AuthToken, 'id' | 'createdAt'> {
|
||||||
const newToken = new AuthToken();
|
const newToken = new AuthToken();
|
||||||
newToken.keyId = keyId;
|
newToken.keyId = keyId;
|
||||||
newToken.user = user;
|
newToken.user = Promise.resolve(user);
|
||||||
newToken.label = label;
|
newToken.label = label;
|
||||||
newToken.accessTokenHash = accessToken;
|
newToken.accessTokenHash = accessToken;
|
||||||
newToken.validUntil = validUntil;
|
newToken.validUntil = validUntil;
|
||||||
|
|
|
@ -93,7 +93,7 @@ describe('AuthService', () => {
|
||||||
.digest('hex');
|
.digest('hex');
|
||||||
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
|
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
|
||||||
...authToken,
|
...authToken,
|
||||||
user: user,
|
user: Promise.resolve(user),
|
||||||
accessTokenHash: accessTokenHash,
|
accessTokenHash: accessTokenHash,
|
||||||
});
|
});
|
||||||
const authTokenFromCall = await service.getAuthTokenAndValidate(
|
const authTokenFromCall = await service.getAuthTokenAndValidate(
|
||||||
|
@ -102,7 +102,7 @@ describe('AuthService', () => {
|
||||||
);
|
);
|
||||||
expect(authTokenFromCall).toEqual({
|
expect(authTokenFromCall).toEqual({
|
||||||
...authToken,
|
...authToken,
|
||||||
user: user,
|
user: Promise.resolve(user),
|
||||||
accessTokenHash: accessTokenHash,
|
accessTokenHash: accessTokenHash,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -116,7 +116,7 @@ describe('AuthService', () => {
|
||||||
it('AuthToken has wrong hash', async () => {
|
it('AuthToken has wrong hash', async () => {
|
||||||
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
|
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
|
||||||
...authToken,
|
...authToken,
|
||||||
user: user,
|
user: Promise.resolve(user),
|
||||||
accessTokenHash: 'the wrong hash',
|
accessTokenHash: 'the wrong hash',
|
||||||
});
|
});
|
||||||
await expect(
|
await expect(
|
||||||
|
@ -127,7 +127,7 @@ describe('AuthService', () => {
|
||||||
const accessTokenHash = await hashPassword(token);
|
const accessTokenHash = await hashPassword(token);
|
||||||
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
|
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
|
||||||
...authToken,
|
...authToken,
|
||||||
user: user,
|
user: Promise.resolve(user),
|
||||||
accessTokenHash: accessTokenHash,
|
accessTokenHash: accessTokenHash,
|
||||||
validUntil: new Date(1549312452000),
|
validUntil: new Date(1549312452000),
|
||||||
});
|
});
|
||||||
|
@ -142,7 +142,7 @@ describe('AuthService', () => {
|
||||||
it('works', async () => {
|
it('works', async () => {
|
||||||
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
|
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValueOnce({
|
||||||
...authToken,
|
...authToken,
|
||||||
user: user,
|
user: Promise.resolve(user),
|
||||||
lastUsed: new Date(1549312452000),
|
lastUsed: new Date(1549312452000),
|
||||||
});
|
});
|
||||||
jest
|
jest
|
||||||
|
@ -178,7 +178,7 @@ describe('AuthService', () => {
|
||||||
});
|
});
|
||||||
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({
|
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({
|
||||||
...authToken,
|
...authToken,
|
||||||
user: user,
|
user: Promise.resolve(user),
|
||||||
accessTokenHash: accessTokenHash,
|
accessTokenHash: accessTokenHash,
|
||||||
});
|
});
|
||||||
jest
|
jest
|
||||||
|
@ -212,14 +212,14 @@ describe('AuthService', () => {
|
||||||
it('works', async () => {
|
it('works', async () => {
|
||||||
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({
|
jest.spyOn(authTokenRepo, 'findOne').mockResolvedValue({
|
||||||
...authToken,
|
...authToken,
|
||||||
user: user,
|
user: Promise.resolve(user),
|
||||||
});
|
});
|
||||||
jest
|
jest
|
||||||
.spyOn(authTokenRepo, 'remove')
|
.spyOn(authTokenRepo, 'remove')
|
||||||
.mockImplementationOnce(async (token, __): Promise<AuthToken> => {
|
.mockImplementationOnce(async (token, __): Promise<AuthToken> => {
|
||||||
expect(token).toEqual({
|
expect(token).toEqual({
|
||||||
...authToken,
|
...authToken,
|
||||||
user: user,
|
user: Promise.resolve(user),
|
||||||
});
|
});
|
||||||
return authToken;
|
return authToken;
|
||||||
});
|
});
|
||||||
|
|
|
@ -46,7 +46,11 @@ export class AuthService {
|
||||||
}
|
}
|
||||||
const accessToken = await this.getAuthTokenAndValidate(keyId, secret);
|
const accessToken = await this.getAuthTokenAndValidate(keyId, secret);
|
||||||
await this.setLastUsedToken(keyId);
|
await this.setLastUsedToken(keyId);
|
||||||
return await this.usersService.getUserByUsername(accessToken.user.username);
|
return await this.usersService.getUserByUsername(
|
||||||
|
(
|
||||||
|
await accessToken.user
|
||||||
|
).username,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async createTokenForUser(
|
async createTokenForUser(
|
||||||
|
|
Loading…
Reference in a new issue