1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00
unleash.unleash/src/lib/services/edge-service.ts
Christopher Kolstad 6cc51b7ba0
docs: Added docs for edge endpoints (#3501)
Adding documentation for the edge endpoints. Also separating request and
response schema for our validate endpoint to make clear that we expect a
list of strings as input, but yield tokens as output.

---------

Co-authored-by: Gastón Fournier <gaston@getunleash.io>
Co-authored-by: Thomas Heartman <thomas@getunleash.ai>
2023-04-14 15:39:39 +02:00

41 lines
1.4 KiB
TypeScript

import { IUnleashStores, IUnleashConfig } from '../types';
import { Logger } from '../logger';
import { IApiTokenStore } from '../types/stores/api-token-store';
import { EdgeTokenSchema } from '../openapi/spec/edge-token-schema';
import { constantTimeCompare } from '../util/constantTimeCompare';
import { ValidatedEdgeTokensSchema } from '../openapi/spec/validated-edge-tokens-schema';
export default class EdgeService {
private logger: Logger;
private apiTokenStore: IApiTokenStore;
constructor(
{ apiTokenStore }: Pick<IUnleashStores, 'apiTokenStore'>,
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
) {
this.logger = getLogger('lib/services/edge-service.ts');
this.apiTokenStore = apiTokenStore;
}
async getValidTokens(tokens: string[]): Promise<ValidatedEdgeTokensSchema> {
const activeTokens = await this.apiTokenStore.getAllActive();
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;