fix: Output required OAuth error fields

This commit is contained in:
Joachim Van Herwegen
2023-03-06 16:01:07 +01:00
parent 7eb938044d
commit 63fd062f16
8 changed files with 124 additions and 11 deletions

View File

@@ -12,6 +12,7 @@ import type { Interaction, InteractionHandler } from '../../../../src/identity/i
import type { AdapterFactory } from '../../../../src/identity/storage/AdapterFactory';
import type { KeyValueStorage } from '../../../../src/storage/keyvalue/KeyValueStorage';
import { FoundHttpError } from '../../../../src/util/errors/FoundHttpError';
import { OAuthHttpError } from '../../../../src/util/errors/OAuthHttpError';
/* eslint-disable @typescript-eslint/naming-convention */
jest.mock('oidc-provider', (): any => ({
@@ -229,14 +230,16 @@ describe('An IdentityProviderFactory', (): void => {
error.error_description = 'more info';
error.error_detail = 'more details';
const oAuthError = new OAuthHttpError(error, error.name, 500, 'bad data - more info - more details');
await expect((config.renderError as any)(ctx, {}, error)).resolves.toBeUndefined();
expect(errorHandler.handleSafe).toHaveBeenCalledTimes(1);
expect(errorHandler.handleSafe)
.toHaveBeenLastCalledWith({ error, request: ctx.req });
.toHaveBeenLastCalledWith({ error: oAuthError, request: ctx.req });
expect(responseWriter.handleSafe).toHaveBeenCalledTimes(1);
expect(responseWriter.handleSafe).toHaveBeenLastCalledWith({ response: ctx.res, result: { statusCode: 500 }});
expect(error.message).toBe('bad data - more info - more details');
expect(error.stack).toContain('Error: bad data - more info - more details');
expect(oAuthError.message).toBe('bad data - more info - more details');
expect(oAuthError.stack).toContain('Error: bad data - more info - more details');
});
it('throws a specific error for unknown clients.', async(): Promise<void> => {

View File

@@ -1,6 +1,8 @@
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import { ErrorToJsonConverter } from '../../../../src/storage/conversion/ErrorToJsonConverter';
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
import type { OAuthErrorFields } from '../../../../src/util/errors/OAuthHttpError';
import { OAuthHttpError } from '../../../../src/util/errors/OAuthHttpError';
import { readJsonStream } from '../../../../src/util/StreamUtil';
describe('An ErrorToJsonConverter', (): void => {
@@ -47,6 +49,35 @@ describe('An ErrorToJsonConverter', (): void => {
});
});
it('adds OAuth fields if present.', async(): Promise<void> => {
const out: OAuthErrorFields = {
error: 'error',
// eslint-disable-next-line @typescript-eslint/naming-convention
error_description: 'error_description',
scope: 'scope',
state: 'state',
};
const error = new OAuthHttpError(out, 'InvalidRequest', 400, 'error text');
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('application/json');
await expect(readJsonStream(result.data)).resolves.toEqual({
name: 'InvalidRequest',
message: 'error text',
statusCode: 400,
errorCode: 'H400',
stack: error.stack,
error: 'error',
// eslint-disable-next-line @typescript-eslint/naming-convention
error_description: 'error_description',
scope: 'scope',
state: 'state',
});
});
it('does not copy the details if they are not serializable.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text', { details: { object: BigInt(1) }});
const representation = new BasicRepresentation([ error ], 'internal/error', false);

View File

@@ -0,0 +1,24 @@
import { OAuthHttpError } from '../../../../src/util/errors/OAuthHttpError';
describe('An OAuthHttpError', (): void => {
it('contains relevant information.', async(): Promise<void> => {
const error = new OAuthHttpError({ error: 'error!' }, 'InvalidRequest', 400, 'message!');
expect(error.mandatoryFields.error).toBe('error!');
expect(error.name).toBe('InvalidRequest');
expect(error.statusCode).toBe(400);
expect(error.message).toBe('message!');
});
it('has optional fields.', async(): Promise<void> => {
const error = new OAuthHttpError({ error: 'error!' });
expect(error.mandatoryFields.error).toBe('error!');
expect(error.name).toBe('OAuthHttpError');
expect(error.statusCode).toBe(500);
});
it('can identify OAuth errors.', async(): Promise<void> => {
const error = new OAuthHttpError({ error: 'error!' });
expect(OAuthHttpError.isInstance('apple')).toBe(false);
expect(OAuthHttpError.isInstance(error)).toBe(true);
});
});