feat(server): add api key to openapi spec (#2362)

* feat(server): add api key to openapi spec

* regenerate api
This commit is contained in:
Michel Heusschen 2023-05-04 18:41:29 +02:00 committed by GitHub
parent af7da9d2c9
commit 15a498fd60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 903 additions and 87 deletions

View file

@ -1,5 +1,6 @@
import { IMMICH_API_KEY_NAME } from '@app/domain';
import { applyDecorators, SetMetadata } from '@nestjs/common';
import { ApiBearerAuth, ApiCookieAuth, ApiQuery } from '@nestjs/swagger';
import { ApiBearerAuth, ApiCookieAuth, ApiQuery, ApiSecurity } from '@nestjs/swagger';
interface AuthenticatedOptions {
admin?: boolean;
@ -12,10 +13,13 @@ export enum Metadata {
SHARED_ROUTE = 'shared_route',
}
export const Authenticated = (options?: AuthenticatedOptions) => {
const decorators: MethodDecorator[] = [ApiBearerAuth(), ApiCookieAuth(), SetMetadata(Metadata.AUTH_ROUTE, true)];
options = options || {};
export const Authenticated = (options: AuthenticatedOptions = {}) => {
const decorators: MethodDecorator[] = [
ApiBearerAuth(),
ApiCookieAuth(),
ApiSecurity(IMMICH_API_KEY_NAME),
SetMetadata(Metadata.AUTH_ROUTE, true),
];
if (options.admin) {
decorators.push(SetMetadata(Metadata.ADMIN_ROUTE, true));

View file

@ -9,8 +9,15 @@ import { AppModule } from './app.module';
import { RedisIoAdapter } from '@app/infra';
import { json } from 'body-parser';
import { patchOpenAPI } from './utils/patch-open-api.util';
import { getLogLevels, MACHINE_LEARNING_ENABLED } from '@app/domain';
import { SERVER_VERSION, IMMICH_ACCESS_COOKIE, SearchService } from '@app/domain';
import {
getLogLevels,
MACHINE_LEARNING_ENABLED,
SERVER_VERSION,
IMMICH_ACCESS_COOKIE,
SearchService,
IMMICH_API_KEY_HEADER,
IMMICH_API_KEY_NAME,
} from '@app/domain';
const logger = new Logger('ImmichServer');
@ -41,6 +48,14 @@ async function bootstrap() {
in: 'header',
})
.addCookieAuth(IMMICH_ACCESS_COOKIE)
.addApiKey(
{
type: 'apiKey',
in: 'header',
name: IMMICH_API_KEY_HEADER,
},
IMMICH_API_KEY_NAME,
)
.addServer('/api')
.build();