2024-02-06 15:14:08 +01:00
|
|
|
import { IUnleashConfig } from '../types';
|
2022-09-01 15:26:26 +02:00
|
|
|
import { Logger } from '../logger';
|
|
|
|
import { EdgeTokenSchema } from '../openapi/spec/edge-token-schema';
|
|
|
|
import { constantTimeCompare } from '../util/constantTimeCompare';
|
2023-04-14 15:39:39 +02:00
|
|
|
import { ValidatedEdgeTokensSchema } from '../openapi/spec/validated-edge-tokens-schema';
|
2024-02-06 15:14:08 +01:00
|
|
|
import { ApiTokenService } from './api-token-service';
|
2022-09-01 15:26:26 +02:00
|
|
|
|
|
|
|
export default class EdgeService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
2024-02-06 15:14:08 +01:00
|
|
|
private apiTokenService: ApiTokenService;
|
2022-09-01 15:26:26 +02:00
|
|
|
|
|
|
|
constructor(
|
2024-02-06 15:14:08 +01:00
|
|
|
{ apiTokenService }: { apiTokenService: ApiTokenService },
|
2022-09-01 15:26:26 +02:00
|
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
|
|
|
) {
|
|
|
|
this.logger = getLogger('lib/services/edge-service.ts');
|
2024-02-06 15:14:08 +01:00
|
|
|
this.apiTokenService = apiTokenService;
|
2022-09-01 15:26:26 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 15:39:39 +02:00
|
|
|
async getValidTokens(tokens: string[]): Promise<ValidatedEdgeTokensSchema> {
|
2024-02-06 15:14:08 +01:00
|
|
|
const activeTokens = await this.apiTokenService.getAllActiveTokens();
|
2022-09-01 15:26:26 +02:00
|
|
|
const edgeTokens = tokens.reduce((result: EdgeTokenSchema[], token) => {
|
|
|
|
const dbToken = activeTokens.find((activeToken) =>
|
|
|
|
constantTimeCompare(activeToken.secret, token),
|
|
|
|
);
|
|
|
|
if (dbToken) {
|
|
|
|
result.push({
|
|
|
|
token: token,
|
|
|
|
type: dbToken.type,
|
|
|
|
projects: dbToken.projects,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
return { tokens: edgeTokens };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EdgeService;
|