|
| 1 | +// ****************************************************************************** |
| 2 | +// Copyright 2024 TypeFox GmbH |
| 3 | +// This program and the accompanying materials are made available under the |
| 4 | +// terms of the MIT License, which is available in the project root. |
| 5 | +// ****************************************************************************** |
| 6 | +import { inject, injectable, postConstruct } from 'inversify'; |
| 7 | +import { type Express } from 'express'; |
| 8 | +import { Emitter, FormAuthProvider, Info } from 'open-collaboration-protocol'; |
| 9 | +import { AuthEndpoint, AuthSuccessEvent } from './auth-endpoint.js'; |
| 10 | +import { Logger } from '../utils/logging.js'; |
| 11 | +import { Configuration } from '../utils/configuration.js'; |
| 12 | +import { generateSecureId } from '../utils/cryptography.js'; |
| 13 | + |
| 14 | +@injectable() |
| 15 | +export class ApiKeyAuthEndpoint implements AuthEndpoint { |
| 16 | + |
| 17 | + protected static readonly ENDPOINT = '/api/login/apikey'; |
| 18 | + |
| 19 | + @inject(Logger) protected logger: Logger; |
| 20 | + |
| 21 | + @inject(Configuration) protected configuration: Configuration; |
| 22 | + |
| 23 | + private authSuccessEmitter = new Emitter<AuthSuccessEvent>(); |
| 24 | + onDidAuthenticate = this.authSuccessEmitter.event; |
| 25 | + |
| 26 | + private apiKey: string = ''; |
| 27 | + |
| 28 | + @postConstruct() |
| 29 | + protected initialize(): void { |
| 30 | + if (!this.shouldActivate()) { |
| 31 | + return; |
| 32 | + } |
| 33 | + const configuredKey = this.configuration.getValue('oct-api-key'); |
| 34 | + if (configuredKey) { |
| 35 | + this.apiKey = configuredKey; |
| 36 | + this.logger.info('API key authentication enabled (key provided via configuration)'); |
| 37 | + } else { |
| 38 | + this.apiKey = generateSecureId(48); |
| 39 | + this.logger.info(`API key authentication enabled (auto-generated key): ${this.apiKey}`); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + shouldActivate(): boolean { |
| 44 | + return this.configuration.getValue('oct-activate-api-key-auth', 'boolean') ?? false; |
| 45 | + } |
| 46 | + |
| 47 | + getProtocolProvider(): FormAuthProvider { |
| 48 | + return { |
| 49 | + type: 'form', |
| 50 | + name: 'apikey', |
| 51 | + endpoint: ApiKeyAuthEndpoint.ENDPOINT, |
| 52 | + label: { |
| 53 | + code: 'ApiKeyLoginLabel', |
| 54 | + message: 'API Key', |
| 55 | + params: [] |
| 56 | + }, |
| 57 | + details: { |
| 58 | + code: 'ApiKeyLoginDetails', |
| 59 | + message: 'Login with a server API key for non-interactive use', |
| 60 | + params: [] |
| 61 | + }, |
| 62 | + group: { |
| 63 | + code: Info.Codes.BuiltinsGroup, |
| 64 | + message: 'Builtins', |
| 65 | + params: [] |
| 66 | + }, |
| 67 | + fields: [ |
| 68 | + { |
| 69 | + name: 'apiKey', |
| 70 | + label: { |
| 71 | + code: 'ApiKeyLabel', |
| 72 | + message: 'API Key', |
| 73 | + params: [] |
| 74 | + }, |
| 75 | + required: true, |
| 76 | + placeHolder: { |
| 77 | + code: 'ApiKeyPlaceholder', |
| 78 | + message: 'The server API key', |
| 79 | + params: [] |
| 80 | + } |
| 81 | + } |
| 82 | + ] |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + onStart(app: Express, _hostname: string, _port: number): void { |
| 87 | + app.post(ApiKeyAuthEndpoint.ENDPOINT, async (req, res) => { |
| 88 | + try { |
| 89 | + const token = req.body.token as string; |
| 90 | + const apiKey = req.body.apiKey as string; |
| 91 | + if (!token || !apiKey) { |
| 92 | + res.status(400); |
| 93 | + res.send('Missing token or apiKey'); |
| 94 | + return; |
| 95 | + } |
| 96 | + if (apiKey !== this.apiKey) { |
| 97 | + res.status(403); |
| 98 | + res.send('Invalid API key'); |
| 99 | + return; |
| 100 | + } |
| 101 | + await Promise.all(this.authSuccessEmitter.fire({ |
| 102 | + token, |
| 103 | + userInfo: { name: 'API Key User', authProvider: 'API Key' } |
| 104 | + })); |
| 105 | + res.send('Ok'); |
| 106 | + } catch (err) { |
| 107 | + this.logger.error('Failed to perform API key login', err); |
| 108 | + res.status(400); |
| 109 | + res.send('Failed to perform API key login'); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | +} |
0 commit comments