backend-common: remove 'Server' from TokenManager method names

Signed-off-by: Mike Lewis <mtlewis@users.noreply.github.com>
This commit is contained in:
Mike Lewis
2021-11-23 18:12:28 +00:00
parent 905dd952ac
commit 4b6de2f872
9 changed files with 35 additions and 36 deletions
+2 -3
View File
@@ -95,8 +95,7 @@ async function main() {
req.cookies['token'];
// Authenticate all requests originating from backends by default
const isValidServerToken =
authEnv.tokenManager.validateServerToken(token);
const isValidServerToken = authEnv.tokenManager.validateToken(token);
if (!isValidServerToken) {
req.user = await identity.authenticate(token);
}
@@ -327,5 +326,5 @@ function makeCreateEnv(config: Config) {
With this `tokenManager`, you can then generate a server token for requests:
```
const { token } = await this.tokenManager.getServerToken();
const { token } = await this.tokenManager.getToken();
```
+4 -4
View File
@@ -533,13 +533,13 @@ export class ServerTokenManager implements TokenManager {
// (undocumented)
static fromConfig(config: Config): ServerTokenManager;
// (undocumented)
getServerToken(): Promise<{
getToken(): Promise<{
token: string;
}>;
// (undocumented)
static noop(): ServerTokenManager;
// (undocumented)
validateServerToken(token: string): void;
validateToken(token: string): void;
}
// @public (undocumented)
@@ -604,11 +604,11 @@ export interface StatusCheckHandlerOptions {
// @public (undocumented)
export interface TokenManager {
// (undocumented)
getServerToken: () => Promise<{
getToken: () => Promise<{
token: string;
}>;
// (undocumented)
validateServerToken: (token: string) => void;
validateToken: (token: string) => void;
}
// @public
@@ -26,39 +26,39 @@ describe('ServerTokenManager', () => {
expect(() => ServerTokenManager.fromConfig(emptyConfig)).toThrowError();
});
describe('getServerToken', () => {
describe('getToken', () => {
it('should return a token if secret in config exists', async () => {
const tokenManager = ServerTokenManager.fromConfig(configWithSecret);
expect((await tokenManager.getServerToken()).token).toBeDefined();
expect((await tokenManager.getToken()).token).toBeDefined();
});
it('should return an empty string if using a noop TokenManager', async () => {
const tokenManager = ServerTokenManager.noop();
expect((await tokenManager.getServerToken()).token).toBe('');
expect((await tokenManager.getToken()).token).toBe('');
});
});
describe('validateServerToken', () => {
describe('validateToken', () => {
it('should not throw if token is valid', async () => {
const tokenManager = ServerTokenManager.fromConfig(configWithSecret);
const { token } = await tokenManager.getServerToken();
expect(() => tokenManager.validateServerToken(token)).not.toThrow();
const { token } = await tokenManager.getToken();
expect(() => tokenManager.validateToken(token)).not.toThrow();
});
it('should throw if token is invalid', () => {
const tokenManager = ServerTokenManager.fromConfig(configWithSecret);
expect(() =>
tokenManager.validateServerToken('random-string'),
).toThrowError(/invalid server token/i);
expect(() => tokenManager.validateToken('random-string')).toThrowError(
/invalid server token/i,
);
});
it('should validate server tokens created by a different instance using the same secret', async () => {
const tokenManager1 = ServerTokenManager.fromConfig(configWithSecret);
const tokenManager2 = ServerTokenManager.fromConfig(configWithSecret);
const { token } = await tokenManager1.getServerToken();
const { token } = await tokenManager1.getToken();
expect(() => tokenManager2.validateServerToken(token)).not.toThrow();
expect(() => tokenManager2.validateToken(token)).not.toThrow();
});
it('should throw for server tokens created using a different secret', async () => {
@@ -69,9 +69,9 @@ describe('ServerTokenManager', () => {
new ConfigReader({ backend: { auth: { secret: 'd4e5f6' } } }),
);
const { token } = await tokenManager1.getServerToken();
const { token } = await tokenManager1.getToken();
expect(() => tokenManager2.validateServerToken(token)).toThrowError(
expect(() => tokenManager2.validateToken(token)).toThrowError(
/invalid server token/i,
);
});
@@ -85,19 +85,19 @@ describe('ServerTokenManager', () => {
});
it('should accept tokens it generates', async () => {
const { token } = await noopTokenManager.getServerToken();
const { token } = await noopTokenManager.getToken();
expect(() => noopTokenManager.validateServerToken(token)).not.toThrow();
expect(() => noopTokenManager.validateToken(token)).not.toThrow();
});
it('should accept arbitrary strings', async () => {
expect(() =>
noopTokenManager.validateServerToken('random-string'),
noopTokenManager.validateToken('random-string'),
).not.toThrow();
});
it('should accept empty strings', async () => {
expect(() => noopTokenManager.validateServerToken('')).not.toThrow();
expect(() => noopTokenManager.validateToken('')).not.toThrow();
});
});
});
@@ -36,7 +36,7 @@ export class ServerTokenManager implements TokenManager {
this.key = secret ? JWK.asKey({ kty: 'oct', k: secret }) : JWK.None;
}
async getServerToken(): Promise<{ token: string }> {
async getToken(): Promise<{ token: string }> {
if (this.key === JWK.None) {
return { token: '' };
}
@@ -47,7 +47,7 @@ export class ServerTokenManager implements TokenManager {
return { token: jwt };
}
validateServerToken(token: string): void {
validateToken(token: string): void {
if (this.key === JWK.None) {
return;
}
+2 -2
View File
@@ -15,6 +15,6 @@
*/
export interface TokenManager {
getServerToken: () => Promise<{ token: string }>;
validateServerToken: (token: string) => void;
getToken: () => Promise<{ token: string }>;
validateToken: (token: string) => void;
}
@@ -67,8 +67,8 @@ describe('DefaultCatalogCollator', () => {
getExternalBaseUrl: jest.fn(),
};
mockTokenManager = {
getServerToken: jest.fn().mockResolvedValue({ token: '' }),
validateServerToken: jest.fn(),
getToken: jest.fn().mockResolvedValue({ token: '' }),
validateToken: jest.fn(),
};
collator = new DefaultCatalogCollator({
discovery: mockDiscoveryApi,
@@ -108,7 +108,7 @@ export class DefaultCatalogCollator implements DocumentCollator {
}
async execute() {
const { token } = await this.tokenManager.getServerToken();
const { token } = await this.tokenManager.getToken();
const response = await this.catalogClient.getEntities(
{
filter: this.filter,
@@ -99,8 +99,8 @@ describe('DefaultTechDocsCollator with legacyPathCasing configuration', () => {
getExternalBaseUrl: jest.fn(),
};
mockTokenManager = {
getServerToken: jest.fn().mockResolvedValue({ token: '' }),
validateServerToken: jest.fn(),
getToken: jest.fn().mockResolvedValue({ token: '' }),
validateToken: jest.fn(),
};
const mockConfig = new ConfigReader({
techdocs: {
@@ -165,8 +165,8 @@ describe('DefaultTechDocsCollator', () => {
getExternalBaseUrl: jest.fn(),
};
mockTokenManager = {
getServerToken: jest.fn().mockResolvedValue({ token: '' }),
validateServerToken: jest.fn(),
getToken: jest.fn().mockResolvedValue({ token: '' }),
validateToken: jest.fn(),
};
collator = DefaultTechDocsCollator.fromConfig(new ConfigReader({}), {
discovery: mockDiscoveryApi,
@@ -94,7 +94,7 @@ export class DefaultTechDocsCollator implements DocumentCollator {
async execute() {
const limit = pLimit(this.parallelismLimit);
const techDocsBaseUrl = await this.discovery.getBaseUrl('techdocs');
const { token } = await this.tokenManager.getServerToken();
const { token } = await this.tokenManager.getToken();
const entities = await this.catalogClient.getEntities(
{
fields: [