2023-01-24 05:13:42 +01:00
|
|
|
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
|
2024-02-02 04:18:00 +01:00
|
|
|
import { IncomingHttpHeaders } from 'node:http';
|
2023-09-04 21:45:59 +02:00
|
|
|
import { Issuer, generators } from 'openid-client';
|
2023-06-16 21:54:17 +02:00
|
|
|
import { Socket } from 'socket.io';
|
2024-03-21 04:15:09 +01:00
|
|
|
import { AuthType } from 'src/constants';
|
2024-03-20 23:53:07 +01:00
|
|
|
import { AuthDto, SignUpDto } from 'src/dtos/auth.dto';
|
2024-05-22 14:13:36 +02:00
|
|
|
import { UserMetadataEntity } from 'src/entities/user-metadata.entity';
|
2024-03-20 22:02:51 +01:00
|
|
|
import { UserEntity } from 'src/entities/user.entity';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IKeyRepository } from 'src/interfaces/api-key.interface';
|
|
|
|
import { ICryptoRepository } from 'src/interfaces/crypto.interface';
|
2024-04-16 01:39:06 +02:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-04-19 12:47:29 +02:00
|
|
|
import { ISessionRepository } from 'src/interfaces/session.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { ISharedLinkRepository } from 'src/interfaces/shared-link.interface';
|
2024-05-16 00:58:23 +02:00
|
|
|
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IUserRepository } from 'src/interfaces/user.interface';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { AuthService } from 'src/services/auth.service';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { keyStub } from 'test/fixtures/api-key.stub';
|
|
|
|
import { authStub, loginResponseStub } from 'test/fixtures/auth.stub';
|
2024-04-19 12:47:29 +02:00
|
|
|
import { sessionStub } from 'test/fixtures/session.stub';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { sharedLinkStub } from 'test/fixtures/shared-link.stub';
|
|
|
|
import { systemConfigStub } from 'test/fixtures/system-config.stub';
|
|
|
|
import { userStub } from 'test/fixtures/user.stub';
|
|
|
|
import { newKeyRepositoryMock } from 'test/repositories/api-key.repository.mock';
|
|
|
|
import { newCryptoRepositoryMock } from 'test/repositories/crypto.repository.mock';
|
2024-04-16 01:39:06 +02:00
|
|
|
import { newLoggerRepositoryMock } from 'test/repositories/logger.repository.mock';
|
2024-04-19 12:47:29 +02:00
|
|
|
import { newSessionRepositoryMock } from 'test/repositories/session.repository.mock';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { newSharedLinkRepositoryMock } from 'test/repositories/shared-link.repository.mock';
|
2024-05-16 00:58:23 +02:00
|
|
|
import { newSystemMetadataRepositoryMock } from 'test/repositories/system-metadata.repository.mock';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { newUserRepositoryMock } from 'test/repositories/user.repository.mock';
|
2024-04-16 16:44:45 +02:00
|
|
|
import { Mock, Mocked, vitest } from 'vitest';
|
2023-01-31 19:11:49 +01:00
|
|
|
|
|
|
|
// const token = Buffer.from('my-api-key', 'utf8').toString('base64');
|
2023-01-24 05:13:42 +01:00
|
|
|
|
|
|
|
const email = 'test@immich.com';
|
|
|
|
const sub = 'my-auth-user-sub';
|
2023-04-26 04:19:23 +02:00
|
|
|
const loginDetails = {
|
|
|
|
isSecure: true,
|
|
|
|
clientIp: '127.0.0.1',
|
|
|
|
deviceOS: '',
|
|
|
|
deviceType: '',
|
|
|
|
};
|
2023-01-24 05:13:42 +01:00
|
|
|
|
|
|
|
const fixtures = {
|
|
|
|
login: {
|
|
|
|
email,
|
|
|
|
password: 'password',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-03-02 21:18:56 +01:00
|
|
|
const oauthUserWithDefaultQuota = {
|
|
|
|
email: email,
|
|
|
|
name: ' ',
|
|
|
|
oauthId: sub,
|
|
|
|
quotaSizeInBytes: 1_073_741_824,
|
|
|
|
storageLabel: null,
|
|
|
|
};
|
|
|
|
|
2023-01-24 05:13:42 +01:00
|
|
|
describe('AuthService', () => {
|
|
|
|
let sut: AuthService;
|
2024-04-16 16:44:45 +02:00
|
|
|
let cryptoMock: Mocked<ICryptoRepository>;
|
|
|
|
let userMock: Mocked<IUserRepository>;
|
|
|
|
let loggerMock: Mocked<ILoggerRepository>;
|
2024-05-16 00:58:23 +02:00
|
|
|
let systemMock: Mocked<ISystemMetadataRepository>;
|
2024-04-19 12:47:29 +02:00
|
|
|
let sessionMock: Mocked<ISessionRepository>;
|
2024-04-16 16:44:45 +02:00
|
|
|
let shareMock: Mocked<ISharedLinkRepository>;
|
|
|
|
let keyMock: Mocked<IKeyRepository>;
|
2023-01-24 05:13:42 +01:00
|
|
|
|
2024-04-16 16:44:45 +02:00
|
|
|
let callbackMock: Mock;
|
|
|
|
let userinfoMock: Mock;
|
2023-01-24 05:13:42 +01:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
beforeEach(() => {
|
2024-04-16 16:44:45 +02:00
|
|
|
callbackMock = vitest.fn().mockReturnValue({ access_token: 'access-token' });
|
|
|
|
userinfoMock = vitest.fn().mockResolvedValue({ sub, email });
|
2023-01-24 05:13:42 +01:00
|
|
|
|
2024-04-16 16:44:45 +02:00
|
|
|
vitest.spyOn(generators, 'state').mockReturnValue('state');
|
|
|
|
vitest.spyOn(Issuer, 'discover').mockResolvedValue({
|
2024-02-02 06:27:54 +01:00
|
|
|
id_token_signing_alg_values_supported: ['RS256'],
|
2024-04-16 16:44:45 +02:00
|
|
|
Client: vitest.fn().mockResolvedValue({
|
2023-01-24 05:13:42 +01:00
|
|
|
issuer: {
|
|
|
|
metadata: {
|
|
|
|
end_session_endpoint: 'http://end-session-endpoint',
|
|
|
|
},
|
|
|
|
},
|
2024-04-16 16:44:45 +02:00
|
|
|
authorizationUrl: vitest.fn().mockReturnValue('http://authorization-url'),
|
|
|
|
callbackParams: vitest.fn().mockReturnValue({ state: 'state' }),
|
2023-01-24 05:13:42 +01:00
|
|
|
callback: callbackMock,
|
2024-03-02 01:46:07 +01:00
|
|
|
userinfo: userinfoMock,
|
2023-01-24 05:13:42 +01:00
|
|
|
}),
|
|
|
|
} as any);
|
|
|
|
|
|
|
|
cryptoMock = newCryptoRepositoryMock();
|
|
|
|
userMock = newUserRepositoryMock();
|
2024-04-16 01:39:06 +02:00
|
|
|
loggerMock = newLoggerRepositoryMock();
|
2024-05-16 00:58:23 +02:00
|
|
|
systemMock = newSystemMetadataRepositoryMock();
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock = newSessionRepositoryMock();
|
2023-01-31 19:11:49 +01:00
|
|
|
shareMock = newSharedLinkRepositoryMock();
|
|
|
|
keyMock = newKeyRepositoryMock();
|
2023-01-24 05:13:42 +01:00
|
|
|
|
2024-05-24 22:37:29 +02:00
|
|
|
sut = new AuthService(cryptoMock, systemMock, loggerMock, userMock, sessionMock, shareMock, keyMock);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be defined', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('login', () => {
|
|
|
|
it('should throw an error if password login is disabled', async () => {
|
2024-05-16 00:58:23 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.disabled);
|
2023-04-26 04:19:23 +02:00
|
|
|
await expect(sut.login(fixtures.login, loginDetails)).rejects.toBeInstanceOf(UnauthorizedException);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should check the user exists', async () => {
|
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
2023-08-01 17:49:50 +02:00
|
|
|
await expect(sut.login(fixtures.login, loginDetails)).rejects.toBeInstanceOf(UnauthorizedException);
|
2023-01-24 05:13:42 +01:00
|
|
|
expect(userMock.getByEmail).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check the user has a password', async () => {
|
|
|
|
userMock.getByEmail.mockResolvedValue({} as UserEntity);
|
2023-08-01 17:49:50 +02:00
|
|
|
await expect(sut.login(fixtures.login, loginDetails)).rejects.toBeInstanceOf(UnauthorizedException);
|
2023-01-24 05:13:42 +01:00
|
|
|
expect(userMock.getByEmail).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should successfully log the user in', async () => {
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.getByEmail.mockResolvedValue(userStub.user1);
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.create.mockResolvedValue(sessionStub.valid);
|
2023-04-26 04:19:23 +02:00
|
|
|
await expect(sut.login(fixtures.login, loginDetails)).resolves.toEqual(loginResponseStub.user1password);
|
2023-01-24 05:13:42 +01:00
|
|
|
expect(userMock.getByEmail).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('changePassword', () => {
|
|
|
|
it('should change the password', async () => {
|
2023-12-10 05:34:12 +01:00
|
|
|
const auth = { user: { email: 'test@imimch.com' } } as AuthDto;
|
2023-01-24 05:13:42 +01:00
|
|
|
const dto = { password: 'old-password', newPassword: 'new-password' };
|
|
|
|
|
|
|
|
userMock.getByEmail.mockResolvedValue({
|
|
|
|
email: 'test@immich.com',
|
|
|
|
password: 'hash-password',
|
|
|
|
} as UserEntity);
|
2024-05-27 00:15:52 +02:00
|
|
|
userMock.update.mockResolvedValue(userStub.user1);
|
2023-01-24 05:13:42 +01:00
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await sut.changePassword(auth, dto);
|
2023-01-24 05:13:42 +01:00
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(userMock.getByEmail).toHaveBeenCalledWith(auth.user.email, true);
|
2023-01-27 21:50:07 +01:00
|
|
|
expect(cryptoMock.compareBcrypt).toHaveBeenCalledWith('old-password', 'hash-password');
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when auth user email is not found', async () => {
|
2023-12-10 05:34:12 +01:00
|
|
|
const auth = { user: { email: 'test@imimch.com' } } as AuthDto;
|
2023-01-24 05:13:42 +01:00
|
|
|
const dto = { password: 'old-password', newPassword: 'new-password' };
|
|
|
|
|
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.changePassword(auth, dto)).rejects.toBeInstanceOf(UnauthorizedException);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when password does not match existing password', async () => {
|
2023-12-10 05:34:12 +01:00
|
|
|
const auth = { user: { email: 'test@imimch.com' } as UserEntity };
|
2023-01-24 05:13:42 +01:00
|
|
|
const dto = { password: 'old-password', newPassword: 'new-password' };
|
|
|
|
|
2023-01-27 21:50:07 +01:00
|
|
|
cryptoMock.compareBcrypt.mockReturnValue(false);
|
2023-01-24 05:13:42 +01:00
|
|
|
|
|
|
|
userMock.getByEmail.mockResolvedValue({
|
|
|
|
email: 'test@immich.com',
|
|
|
|
password: 'hash-password',
|
|
|
|
} as UserEntity);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.changePassword(auth, dto)).rejects.toBeInstanceOf(BadRequestException);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when user does not have a password', async () => {
|
2023-12-10 05:34:12 +01:00
|
|
|
const auth = { user: { email: 'test@imimch.com' } } as AuthDto;
|
2023-01-24 05:13:42 +01:00
|
|
|
const dto = { password: 'old-password', newPassword: 'new-password' };
|
|
|
|
|
|
|
|
userMock.getByEmail.mockResolvedValue({
|
|
|
|
email: 'test@immich.com',
|
|
|
|
password: '',
|
|
|
|
} as UserEntity);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.changePassword(auth, dto)).rejects.toBeInstanceOf(BadRequestException);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('logout', () => {
|
|
|
|
it('should return the end session endpoint', async () => {
|
2024-05-16 00:58:23 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.enabled);
|
2023-12-10 05:34:12 +01:00
|
|
|
const auth = { user: { id: '123' } } as AuthDto;
|
|
|
|
await expect(sut.logout(auth, AuthType.OAUTH)).resolves.toEqual({
|
2023-01-24 05:13:42 +01:00
|
|
|
successful: true,
|
|
|
|
redirectUri: 'http://end-session-endpoint',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return the default redirect', async () => {
|
2023-12-10 05:34:12 +01:00
|
|
|
const auth = { user: { id: '123' } } as AuthDto;
|
2023-02-06 06:31:16 +01:00
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.logout(auth, AuthType.PASSWORD)).resolves.toEqual({
|
2023-01-24 05:13:42 +01:00
|
|
|
successful: true,
|
|
|
|
redirectUri: '/auth/login?autoLaunch=0',
|
|
|
|
});
|
|
|
|
});
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
it('should delete the access token', async () => {
|
2024-04-19 12:47:29 +02:00
|
|
|
const auth = { user: { id: '123' }, session: { id: 'token123' } } as AuthDto;
|
2023-02-25 15:12:03 +01:00
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.logout(auth, AuthType.PASSWORD)).resolves.toEqual({
|
2023-02-25 15:12:03 +01:00
|
|
|
successful: true,
|
|
|
|
redirectUri: '/auth/login?autoLaunch=0',
|
|
|
|
});
|
|
|
|
|
2024-04-19 12:47:29 +02:00
|
|
|
expect(sessionMock.delete).toHaveBeenCalledWith('token123');
|
2023-02-25 15:12:03 +01:00
|
|
|
});
|
2023-09-11 17:56:38 +02:00
|
|
|
|
|
|
|
it('should return the default redirect if auth type is OAUTH but oauth is not enabled', async () => {
|
2023-12-10 05:34:12 +01:00
|
|
|
const auth = { user: { id: '123' } } as AuthDto;
|
2023-09-11 17:56:38 +02:00
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.logout(auth, AuthType.OAUTH)).resolves.toEqual({
|
2023-09-11 17:56:38 +02:00
|
|
|
successful: true,
|
|
|
|
redirectUri: '/auth/login?autoLaunch=0',
|
|
|
|
});
|
|
|
|
});
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('adminSignUp', () => {
|
2023-11-12 02:03:32 +01:00
|
|
|
const dto: SignUpDto = { email: 'test@immich.com', password: 'password', name: 'immich admin' };
|
2023-01-24 05:13:42 +01:00
|
|
|
|
|
|
|
it('should only allow one admin', async () => {
|
|
|
|
userMock.getAdmin.mockResolvedValue({} as UserEntity);
|
|
|
|
await expect(sut.adminSignUp(dto)).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
expect(userMock.getAdmin).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should sign up the admin', async () => {
|
|
|
|
userMock.getAdmin.mockResolvedValue(null);
|
2024-05-22 14:13:36 +02:00
|
|
|
userMock.create.mockResolvedValue({
|
|
|
|
...dto,
|
|
|
|
id: 'admin',
|
|
|
|
createdAt: new Date('2021-01-01'),
|
|
|
|
metadata: [] as UserMetadataEntity[],
|
|
|
|
} as UserEntity);
|
|
|
|
await expect(sut.adminSignUp(dto)).resolves.toMatchObject({
|
2023-11-14 04:10:35 +01:00
|
|
|
avatarColor: expect.any(String),
|
2023-01-24 05:13:42 +01:00
|
|
|
id: 'admin',
|
2023-05-30 15:15:56 +02:00
|
|
|
createdAt: new Date('2021-01-01'),
|
2023-01-24 05:13:42 +01:00
|
|
|
email: 'test@immich.com',
|
2023-11-12 02:03:32 +01:00
|
|
|
name: 'immich admin',
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
expect(userMock.getAdmin).toHaveBeenCalled();
|
|
|
|
expect(userMock.create).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-27 21:50:07 +01:00
|
|
|
describe('validate - socket connections', () => {
|
2023-01-31 19:11:49 +01:00
|
|
|
it('should throw token is not provided', async () => {
|
|
|
|
await expect(sut.validate({}, {})).rejects.toBeInstanceOf(UnauthorizedException);
|
|
|
|
});
|
|
|
|
|
2023-01-24 05:13:42 +01:00
|
|
|
it('should validate using authorization header', async () => {
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.get.mockResolvedValue(userStub.user1);
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.getByToken.mockResolvedValue(sessionStub.valid);
|
2023-01-27 21:50:07 +01:00
|
|
|
const client = { request: { headers: { authorization: 'Bearer auth_token' } } };
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.validate((client as Socket).request.headers, {})).resolves.toEqual({
|
|
|
|
user: userStub.user1,
|
2024-04-19 12:47:29 +02:00
|
|
|
session: sessionStub.valid,
|
2023-12-10 05:34:12 +01:00
|
|
|
});
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-31 19:11:49 +01:00
|
|
|
describe('validate - shared key', () => {
|
|
|
|
it('should not accept a non-existent key', async () => {
|
|
|
|
shareMock.getByKey.mockResolvedValue(null);
|
|
|
|
const headers: IncomingHttpHeaders = { 'x-immich-share-key': 'key' };
|
|
|
|
await expect(sut.validate(headers, {})).rejects.toBeInstanceOf(UnauthorizedException);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
2023-01-31 19:11:49 +01:00
|
|
|
it('should not accept an expired key', async () => {
|
|
|
|
shareMock.getByKey.mockResolvedValue(sharedLinkStub.expired);
|
|
|
|
const headers: IncomingHttpHeaders = { 'x-immich-share-key': 'key' };
|
|
|
|
await expect(sut.validate(headers, {})).rejects.toBeInstanceOf(UnauthorizedException);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
2023-01-31 19:11:49 +01:00
|
|
|
it('should not accept a key without a user', async () => {
|
|
|
|
shareMock.getByKey.mockResolvedValue(sharedLinkStub.expired);
|
|
|
|
userMock.get.mockResolvedValue(null);
|
|
|
|
const headers: IncomingHttpHeaders = { 'x-immich-share-key': 'key' };
|
|
|
|
await expect(sut.validate(headers, {})).rejects.toBeInstanceOf(UnauthorizedException);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
2023-06-01 22:56:37 +02:00
|
|
|
it('should accept a base64url key', async () => {
|
2023-01-31 19:11:49 +01:00
|
|
|
shareMock.getByKey.mockResolvedValue(sharedLinkStub.valid);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
2023-06-01 22:56:37 +02:00
|
|
|
const headers: IncomingHttpHeaders = { 'x-immich-share-key': sharedLinkStub.valid.key.toString('base64url') };
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.validate(headers, {})).resolves.toEqual({
|
|
|
|
user: userStub.admin,
|
|
|
|
sharedLink: sharedLinkStub.valid,
|
|
|
|
});
|
2023-06-01 22:56:37 +02:00
|
|
|
expect(shareMock.getByKey).toHaveBeenCalledWith(sharedLinkStub.valid.key);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should accept a hex key', async () => {
|
|
|
|
shareMock.getByKey.mockResolvedValue(sharedLinkStub.valid);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.get.mockResolvedValue(userStub.admin);
|
2023-06-01 22:56:37 +02:00
|
|
|
const headers: IncomingHttpHeaders = { 'x-immich-share-key': sharedLinkStub.valid.key.toString('hex') };
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.validate(headers, {})).resolves.toEqual({
|
|
|
|
user: userStub.admin,
|
|
|
|
sharedLink: sharedLinkStub.valid,
|
|
|
|
});
|
2023-06-01 22:56:37 +02:00
|
|
|
expect(shareMock.getByKey).toHaveBeenCalledWith(sharedLinkStub.valid.key);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
2023-01-31 19:11:49 +01:00
|
|
|
});
|
2023-01-24 05:13:42 +01:00
|
|
|
|
2023-01-31 19:11:49 +01:00
|
|
|
describe('validate - user token', () => {
|
|
|
|
it('should throw if no token is found', async () => {
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.getByToken.mockResolvedValue(null);
|
2023-01-31 19:11:49 +01:00
|
|
|
const headers: IncomingHttpHeaders = { 'x-immich-user-token': 'auth_token' };
|
|
|
|
await expect(sut.validate(headers, {})).rejects.toBeInstanceOf(UnauthorizedException);
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
2023-01-31 19:11:49 +01:00
|
|
|
it('should return an auth dto', async () => {
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.getByToken.mockResolvedValue(sessionStub.valid);
|
2023-01-31 19:11:49 +01:00
|
|
|
const headers: IncomingHttpHeaders = { cookie: 'immich_access_token=auth_token' };
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.validate(headers, {})).resolves.toEqual({
|
|
|
|
user: userStub.user1,
|
2024-04-19 12:47:29 +02:00
|
|
|
session: sessionStub.valid,
|
2023-12-10 05:34:12 +01:00
|
|
|
});
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
2023-04-26 04:19:23 +02:00
|
|
|
|
|
|
|
it('should update when access time exceeds an hour', async () => {
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.getByToken.mockResolvedValue(sessionStub.inactive);
|
|
|
|
sessionMock.update.mockResolvedValue(sessionStub.valid);
|
2023-04-26 04:19:23 +02:00
|
|
|
const headers: IncomingHttpHeaders = { cookie: 'immich_access_token=auth_token' };
|
2024-04-20 22:15:25 +02:00
|
|
|
await expect(sut.validate(headers, {})).resolves.toBeDefined();
|
2024-04-19 12:47:29 +02:00
|
|
|
expect(sessionMock.update.mock.calls[0][0]).toMatchObject({ id: 'not_active', updatedAt: expect.any(Date) });
|
2023-04-26 04:19:23 +02:00
|
|
|
});
|
2023-01-31 19:11:49 +01:00
|
|
|
});
|
2023-01-24 05:13:42 +01:00
|
|
|
|
2023-01-31 19:11:49 +01:00
|
|
|
describe('validate - api key', () => {
|
|
|
|
it('should throw an error if no api key is found', async () => {
|
|
|
|
keyMock.getKey.mockResolvedValue(null);
|
|
|
|
const headers: IncomingHttpHeaders = { 'x-api-key': 'auth_token' };
|
|
|
|
await expect(sut.validate(headers, {})).rejects.toBeInstanceOf(UnauthorizedException);
|
|
|
|
expect(keyMock.getKey).toHaveBeenCalledWith('auth_token (hashed)');
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
|
2023-01-31 19:11:49 +01:00
|
|
|
it('should return an auth dto', async () => {
|
|
|
|
keyMock.getKey.mockResolvedValue(keyStub.admin);
|
|
|
|
const headers: IncomingHttpHeaders = { 'x-api-key': 'auth_token' };
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.validate(headers, {})).resolves.toEqual({ user: userStub.admin, apiKey: keyStub.admin });
|
2023-01-31 19:11:49 +01:00
|
|
|
expect(keyMock.getKey).toHaveBeenCalledWith('auth_token (hashed)');
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|
|
|
|
});
|
2023-04-26 04:19:23 +02:00
|
|
|
|
2023-07-15 06:03:56 +02:00
|
|
|
describe('getMobileRedirect', () => {
|
|
|
|
it('should pass along the query params', () => {
|
|
|
|
expect(sut.getMobileRedirect('http://immich.app?code=123&state=456')).toEqual('app.immich:/?code=123&state=456');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work if called without query params', () => {
|
|
|
|
expect(sut.getMobileRedirect('http://immich.app')).toEqual('app.immich:/?');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('callback', () => {
|
|
|
|
it('should throw an error if OAuth is not enabled', async () => {
|
|
|
|
await expect(sut.callback({ url: '' }, loginDetails)).rejects.toBeInstanceOf(BadRequestException);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow auto registering', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthEnabled);
|
2023-07-15 06:03:56 +02:00
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
expect(userMock.getByEmail).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should link an existing user', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthEnabled);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.getByEmail.mockResolvedValue(userStub.user1);
|
|
|
|
userMock.update.mockResolvedValue(userStub.user1);
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.create.mockResolvedValue(sessionStub.valid);
|
2023-07-15 06:03:56 +02:00
|
|
|
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual(
|
|
|
|
loginResponseStub.user1oauth,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(userMock.getByEmail).toHaveBeenCalledTimes(1);
|
2023-08-01 03:28:07 +02:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(userStub.user1.id, { oauthId: sub });
|
2023-07-15 06:03:56 +02:00
|
|
|
});
|
|
|
|
|
2024-05-21 15:07:34 +02:00
|
|
|
it('should not link to a user with a different oauth sub', async () => {
|
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthWithAutoRegister);
|
|
|
|
userMock.getByEmail.mockResolvedValueOnce({ ...userStub.user1, oauthId: 'existing-sub' });
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.user1);
|
|
|
|
userMock.create.mockResolvedValue(userStub.user1);
|
|
|
|
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual(
|
|
|
|
loginResponseStub.user1oauth,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(userMock.update).not.toHaveBeenCalled();
|
|
|
|
expect(userMock.create).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-07-15 06:03:56 +02:00
|
|
|
it('should allow auto registering by default', async () => {
|
2024-05-16 00:58:23 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.enabled);
|
2023-07-15 06:03:56 +02:00
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.user1);
|
|
|
|
userMock.create.mockResolvedValue(userStub.user1);
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.create.mockResolvedValue(sessionStub.valid);
|
2023-07-15 06:03:56 +02:00
|
|
|
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual(
|
|
|
|
loginResponseStub.user1oauth,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(userMock.getByEmail).toHaveBeenCalledTimes(2); // second call is for domain check before create
|
|
|
|
expect(userMock.create).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use the mobile redirect override', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthWithMobileOverride);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.getByOAuthId.mockResolvedValue(userStub.user1);
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.create.mockResolvedValue(sessionStub.valid);
|
2023-07-15 06:03:56 +02:00
|
|
|
|
|
|
|
await sut.callback({ url: `app.immich:/?code=abc123` }, loginDetails);
|
|
|
|
|
|
|
|
expect(callbackMock).toHaveBeenCalledWith('http://mobile-redirect', { state: 'state' }, { state: 'state' });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use the mobile redirect override for ios urls with multiple slashes', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthWithMobileOverride);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.getByOAuthId.mockResolvedValue(userStub.user1);
|
2024-04-19 12:47:29 +02:00
|
|
|
sessionMock.create.mockResolvedValue(sessionStub.valid);
|
2023-07-15 06:03:56 +02:00
|
|
|
|
|
|
|
await sut.callback({ url: `app.immich:///?code=abc123` }, loginDetails);
|
|
|
|
|
|
|
|
expect(callbackMock).toHaveBeenCalledWith('http://mobile-redirect', { state: 'state' }, { state: 'state' });
|
|
|
|
});
|
2024-03-02 01:46:07 +01:00
|
|
|
|
|
|
|
it('should use the default quota', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthWithStorageQuota);
|
2024-03-02 01:46:07 +01:00
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.user1);
|
|
|
|
userMock.create.mockResolvedValue(userStub.user1);
|
|
|
|
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual(
|
|
|
|
loginResponseStub.user1oauth,
|
|
|
|
);
|
|
|
|
|
2024-03-02 21:18:56 +01:00
|
|
|
expect(userMock.create).toHaveBeenCalledWith(oauthUserWithDefaultQuota);
|
2024-03-02 01:46:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore an invalid storage quota', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthWithStorageQuota);
|
2024-03-02 01:46:07 +01:00
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.user1);
|
|
|
|
userMock.create.mockResolvedValue(userStub.user1);
|
|
|
|
userinfoMock.mockResolvedValue({ sub, email, immich_quota: 'abc' });
|
|
|
|
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual(
|
|
|
|
loginResponseStub.user1oauth,
|
|
|
|
);
|
|
|
|
|
2024-03-02 21:18:56 +01:00
|
|
|
expect(userMock.create).toHaveBeenCalledWith(oauthUserWithDefaultQuota);
|
2024-03-02 01:46:07 +01:00
|
|
|
});
|
2024-03-02 21:18:56 +01:00
|
|
|
|
2024-03-02 01:46:07 +01:00
|
|
|
it('should ignore a negative quota', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthWithStorageQuota);
|
2024-03-02 01:46:07 +01:00
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.user1);
|
|
|
|
userMock.create.mockResolvedValue(userStub.user1);
|
|
|
|
userinfoMock.mockResolvedValue({ sub, email, immich_quota: -5 });
|
|
|
|
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual(
|
|
|
|
loginResponseStub.user1oauth,
|
|
|
|
);
|
|
|
|
|
2024-03-02 21:18:56 +01:00
|
|
|
expect(userMock.create).toHaveBeenCalledWith(oauthUserWithDefaultQuota);
|
2024-03-02 01:46:07 +01:00
|
|
|
});
|
|
|
|
|
2024-03-02 21:18:56 +01:00
|
|
|
it('should not set quota for 0 quota', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthWithStorageQuota);
|
2024-03-02 01:46:07 +01:00
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.user1);
|
|
|
|
userMock.create.mockResolvedValue(userStub.user1);
|
|
|
|
userinfoMock.mockResolvedValue({ sub, email, immich_quota: 0 });
|
|
|
|
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual(
|
|
|
|
loginResponseStub.user1oauth,
|
|
|
|
);
|
|
|
|
|
2024-03-02 21:18:56 +01:00
|
|
|
expect(userMock.create).toHaveBeenCalledWith({
|
|
|
|
email: email,
|
|
|
|
name: ' ',
|
|
|
|
oauthId: sub,
|
|
|
|
quotaSizeInBytes: null,
|
|
|
|
storageLabel: null,
|
|
|
|
});
|
2024-03-02 01:46:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should use a valid storage quota', async () => {
|
2024-05-21 15:07:34 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.oauthWithStorageQuota);
|
2024-03-02 01:46:07 +01:00
|
|
|
userMock.getByEmail.mockResolvedValue(null);
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.user1);
|
|
|
|
userMock.create.mockResolvedValue(userStub.user1);
|
|
|
|
userinfoMock.mockResolvedValue({ sub, email, immich_quota: 5 });
|
|
|
|
|
|
|
|
await expect(sut.callback({ url: 'http://immich/auth/login?code=abc123' }, loginDetails)).resolves.toEqual(
|
|
|
|
loginResponseStub.user1oauth,
|
|
|
|
);
|
|
|
|
|
2024-03-02 21:18:56 +01:00
|
|
|
expect(userMock.create).toHaveBeenCalledWith({
|
|
|
|
email: email,
|
|
|
|
name: ' ',
|
|
|
|
oauthId: sub,
|
|
|
|
quotaSizeInBytes: 5_368_709_120,
|
|
|
|
storageLabel: null,
|
|
|
|
});
|
2024-03-02 01:46:07 +01:00
|
|
|
});
|
2023-07-15 06:03:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('link', () => {
|
|
|
|
it('should link an account', async () => {
|
2024-05-16 00:58:23 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.enabled);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.update.mockResolvedValue(userStub.user1);
|
2023-07-15 06:03:56 +02:00
|
|
|
|
|
|
|
await sut.link(authStub.user1, { url: 'http://immich/user-settings?code=abc123' });
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(authStub.user1.user.id, { oauthId: sub });
|
2023-07-15 06:03:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not link an already linked oauth.sub', async () => {
|
2024-05-16 00:58:23 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.enabled);
|
2023-07-15 06:03:56 +02:00
|
|
|
userMock.getByOAuthId.mockResolvedValue({ id: 'other-user' } as UserEntity);
|
|
|
|
|
|
|
|
await expect(sut.link(authStub.user1, { url: 'http://immich/user-settings?code=abc123' })).rejects.toBeInstanceOf(
|
|
|
|
BadRequestException,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(userMock.update).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('unlink', () => {
|
|
|
|
it('should unlink an account', async () => {
|
2024-05-16 00:58:23 +02:00
|
|
|
systemMock.get.mockResolvedValue(systemConfigStub.enabled);
|
2023-08-01 03:28:07 +02:00
|
|
|
userMock.update.mockResolvedValue(userStub.user1);
|
2023-07-15 06:03:56 +02:00
|
|
|
|
|
|
|
await sut.unlink(authStub.user1);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(userMock.update).toHaveBeenCalledWith(authStub.user1.user.id, { oauthId: '' });
|
2023-07-15 06:03:56 +02:00
|
|
|
});
|
|
|
|
});
|
2023-01-24 05:13:42 +01:00
|
|
|
});
|