Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add SAML app API docs #6967

Merged
merged 9 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 56 additions & 15 deletions packages/core/src/routes/saml-application/anonymous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
import assertThat from '#src/utils/assert-that.js';
import { getConsoleLogFromContext } from '#src/utils/console.js';

const samlApplicationSignInCallbackQueryParametersGuard = z.union([
z.object({
const samlApplicationSignInCallbackQueryParametersGuard = z
.object({
code: z.string(),
state: z.string().optional(),
redirectUri: z.string().optional(),
}),
z.object({
state: z.string(),
redirectUri: z.string(),
error: z.string(),
error_description: z.string().optional(),
}),
]);
error_description: z.string(),
})
.partial();

export default function samlApplicationAnonymousRoutes<T extends AnonymousRouter>(
...[router, { id: tenantId, libraries, queries, envSet }]: RouterInitArgs<T>
Expand Down Expand Up @@ -70,28 +68,71 @@
status: [200, 400, 404],
}),
koaAuditLog(queries),
// eslint-disable-next-line complexity
async (ctx, next) => {
const consoleLog = getConsoleLogFromContext(ctx);
const {
params: { id },
query,
} = ctx.guard;

const log = ctx.createLog('SamlApplication.Callback');
/**
* When generating swagger.json, we build path/query guards and verify whether the query/path guard is an instance of ZodObject. Previously, our query guard was a Union of Zod Objects, which failed the validation. Now, we directly use ZodObject guards and perform additional validations within the API.
*/
/* === query guard === */
// Validate query parameters
if (!query.code && !query.error) {
throw new RequestError({
code: 'guard.invalid_input',
message: 'Either code or error must be present',
type: 'query',
});
}

Check warning on line 90 in packages/core/src/routes/saml-application/anonymous.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/saml-application/anonymous.ts#L79-L90

Added lines #L79 - L90 were not covered by tests

log.append({
query,
applicationId: id,
});
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (query.code && (query.error || query.error_description)) {
throw new RequestError({
code: 'guard.invalid_input',
type: 'query',
message: 'Cannot have both code and error fields',
});
}

// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (query.error && (query.code || query.state || query.redirectUri)) {
throw new RequestError({
code: 'guard.invalid_input',
type: 'query',
message: 'When error is present, only error_description is allowed',
});
}

Check warning on line 108 in packages/core/src/routes/saml-application/anonymous.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/saml-application/anonymous.ts#L92-L108

Added lines #L92 - L108 were not covered by tests

// Handle error in query parameters
if ('error' in query) {
if (query.error) {

Check warning on line 111 in packages/core/src/routes/saml-application/anonymous.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/saml-application/anonymous.ts#L111

Added line #L111 was not covered by tests
throw new RequestError({
code: 'oidc.invalid_request',
message: query.error_description,
type: 'query',

Check warning on line 115 in packages/core/src/routes/saml-application/anonymous.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/saml-application/anonymous.ts#L115

Added line #L115 was not covered by tests
});
}

assertThat(
query.code,
new RequestError({
code: 'guard.invalid_input',
type: 'query',
message: '`code` is required.',
})
);
/* === End query guard === */

const log = ctx.createLog('SamlApplication.Callback');

log.append({
query,
applicationId: id,
});

Check warning on line 135 in packages/core/src/routes/saml-application/anonymous.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/saml-application/anonymous.ts#L119-L135

Added lines #L119 - L135 were not covered by tests
const details = await getSamlApplicationDetailsById(id);
const samlApplication = new SamlApplication(details, id, envSet.oidc.issuer, tenantId);

Expand Down
Loading
Loading