Skip to content

Commit

Permalink
fix(oidc-provider): remove OIDC_PROVIDER_NODE_MODULE symbol
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove OIDC_PROVIDER_NODE_MODULE symbol. Use import Provider from "oidc-provider" instead
  • Loading branch information
Romakita committed Oct 6, 2024
1 parent 688e201 commit 7b33ea7
Show file tree
Hide file tree
Showing 17 changed files with 20 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Constant, Inject, Module} from "@tsed/di";
import {Logger} from "@tsed/logger";
import {OIDC_PROVIDER_NODE_MODULE, OidcSettings} from "@tsed/oidc-provider";
// @ts-ignore
import type {default as Provider, KoaContextWithOIDC} from "oidc-provider";
import {OidcSettings} from "@tsed/oidc-provider";
import Provider, {errors, type KoaContextWithOIDC} from "oidc-provider";
// @ts-ignore
import psl from "psl";

Expand All @@ -26,8 +25,6 @@ export class OidcWildcardRedirectUriModule {
@Inject(Logger)
protected logger: Logger;

constructor(@Inject(OIDC_PROVIDER_NODE_MODULE) protected module: OIDC_PROVIDER_NODE_MODULE) {}

$onCreateOIDC(provider: Provider) {
if (this.enabled) {
const {redirectUriAllowed, postLogoutRedirectUriAllowed} = provider.Client.prototype;
Expand Down Expand Up @@ -61,15 +58,15 @@ export class OidcWildcardRedirectUriModule {
const {hostname, href} = new URL(redirectUri);

if (href.split("*").length !== 2) {
throw new this.module.errors.InvalidClientMetadata("redirect_uris with a wildcard may only contain a single one");
throw new errors.InvalidClientMetadata("redirect_uris with a wildcard may only contain a single one");
}

if (!hostname.includes("*")) {
throw new this.module.errors.InvalidClientMetadata("redirect_uris may only have a wildcard in the hostname");
throw new errors.InvalidClientMetadata("redirect_uris may only have a wildcard in the hostname");
}

if (!psl.get(hostname.split("*.")[1])) {
throw new this.module.errors.InvalidClientMetadata(
throw new errors.InvalidClientMetadata(
"redirect_uris with a wildcard must not match an eTLD+1 of a known public suffix domain"
);
}
Expand All @@ -81,15 +78,15 @@ export class OidcWildcardRedirectUriModule {
const {hostname, href} = new URL(postLogoutRedirectUri);

if (href.split("*").length !== 2) {
throw new this.module.errors.InvalidClientMetadata("post_logout_redirect_uris with a wildcard may only contain a single one");
throw new errors.InvalidClientMetadata("post_logout_redirect_uris with a wildcard may only contain a single one");
}

if (!hostname.includes("*")) {
throw new this.module.errors.InvalidClientMetadata("post_logout_redirect_uris may only have a wildcard in the hostname");
throw new errors.InvalidClientMetadata("post_logout_redirect_uris may only have a wildcard in the hostname");
}

if (!psl.get(hostname.split("*.")[1])) {
throw new this.module.errors.InvalidClientMetadata(
throw new errors.InvalidClientMetadata(
"post_logout_redirect_uris with a wildcard must not match an eTLD+1 of a known public suffix domain"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export default defineConfig(
coverage: {
...presets.test.coverage,
thresholds: {
statements: 98.78,
branches: 86.95,
statements: 98.76,
branches: 86.36,
functions: 100,
lines: 98.78
lines: 98.76
}
}
}
}
);
);
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Context} from "@tsed/common";
import {useDecorators} from "@tsed/core";
// @ts-ignore
import type {default as Provider} from "oidc-provider";

import {INTERACTION_SESSION} from "../constants/constants.js";
Expand Down
1 change: 0 additions & 1 deletion packages/security/oidc-provider/src/decorators/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Context} from "@tsed/common";
import {useDecorators} from "@tsed/core";
// @ts-ignore
import type {PromptDetail as P} from "oidc-provider";

import {INTERACTION_PROMPT} from "../constants/constants.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore
import type {CanBePromise, interactionPolicy, KoaContextWithOIDC, UnknownObject} from "oidc-provider";

export interface InteractionMethods {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore
import type {Account, default as Provider} from "oidc-provider";

import {AccessToken, AuthorizationCode, BackchannelAuthenticationRequest, DeviceCode} from "./interfaces.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore
import type {CanBePromise, interactionPolicy, KoaContextWithOIDC, UnknownObject} from "oidc-provider";
export interface OidcInteractionOptions {
name: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore
import type {UnknownObject} from "oidc-provider";

import {OidcClient} from "./interfaces.js";
Expand Down
1 change: 0 additions & 1 deletion packages/security/oidc-provider/src/domain/OidcSettings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {Adapter} from "@tsed/adapters";
import type {Type} from "@tsed/core";
import type {JwksKeyParameters} from "@tsed/jwks";
// @ts-ignore
import type {Configuration} from "oidc-provider";

import type {OidcAccountsMethods} from "./OidcAccountsMethods.js";
Expand Down
1 change: 0 additions & 1 deletion packages/security/oidc-provider/src/domain/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore
import type {default as Provider, interactionPolicy} from "oidc-provider";

export type OIDCContext = InstanceType<Provider["OIDCContext"]>;
Expand Down
1 change: 0 additions & 1 deletion packages/security/oidc-provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ export * from "./services/OidcInteractions.js";
export * from "./services/OidcJwks.js";
export * from "./services/OidcPolicy.js";
export * from "./services/OidcProvider.js";
export * from "./services/OidcProviderNodeModule.js";
export * from "./utils/debug.js";
export * from "./utils/events.js";
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Adapter, Adapters} from "@tsed/adapters";
import {Configuration, Inject, Injectable} from "@tsed/di";
// @ts-ignore
import type {Adapter as OidcAdapter, AdapterConstructor} from "oidc-provider";

export type OidcAdapterMethods<Model = any> = Adapter<Model> & Partial<Omit<OidcAdapter, "upsert">>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import {Constant, InjectContext, PlatformContext} from "@tsed/common";
import {Env} from "@tsed/core";
import {Inject, Injectable} from "@tsed/di";
import {Unauthorized} from "@tsed/exceptions";
import {serialize} from "@tsed/json-mapper";
import omit from "lodash/omit.js";
// @ts-ignore
import type {Account, default as Provider, InteractionResults, PromptDetail} from "oidc-provider";

import {
Expand Down
8 changes: 2 additions & 6 deletions packages/security/oidc-provider/src/services/OidcPolicy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {Inject, Injectable, InjectorService, Provider} from "@tsed/di";
// @ts-ignore
import {interactionPolicy} from "oidc-provider";

import {InteractionMethods} from "../domain/InteractionMethods.js";
import {OidcInteractionOptions} from "../domain/OidcInteractionOptions.js";
import {OidcInteractions} from "./OidcInteractions.js";
import {OIDC_PROVIDER_NODE_MODULE} from "./OidcProviderNodeModule.js";
import Prompt = interactionPolicy.Prompt;

@Injectable()
Expand All @@ -16,10 +14,8 @@ export class OidcPolicy {
@Inject()
protected oidcInteractions: OidcInteractions;

constructor(@Inject(OIDC_PROVIDER_NODE_MODULE) protected module: OIDC_PROVIDER_NODE_MODULE) {}

public getPolicy() {
let policy = this.module.interactionPolicy.base();
let policy = interactionPolicy.base();
const {usePriority, interactions} = this.getInteractions();

if (interactions.size) {
Expand Down Expand Up @@ -53,7 +49,7 @@ export class OidcPolicy {
const {checks: originalChecks = [], details, ...promptOptions} = options;
const checks = [...(instance.checks ? instance.checks() : originalChecks)].filter(Boolean);

return new this.module.interactionPolicy.Prompt(promptOptions, instance.details ? instance.details.bind(instance) : details, ...checks);
return new interactionPolicy.Prompt(promptOptions, instance.details ? instance.details.bind(instance) : details, ...checks);
}

private getInteractions() {
Expand Down
8 changes: 2 additions & 6 deletions packages/security/oidc-provider/src/services/OidcProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {InjectContext, PlatformApplication, PlatformContext} from "@tsed/common";
import {Env, setValue} from "@tsed/core";
import {Constant, Inject, Injectable, InjectorService} from "@tsed/di";
// @ts-ignore
import type {Configuration, default as OIDCProvider, KoaContextWithOIDC} from "oidc-provider";
import Provider, {type Configuration, type KoaContextWithOIDC} from "oidc-provider";

import {INTERACTIONS} from "../constants/constants.js";
import {OidcAccountsMethods} from "../domain/OidcAccountsMethods.js";
Expand All @@ -12,7 +11,6 @@ import {OidcAdapters} from "./OidcAdapters.js";
import {OidcInteractions} from "./OidcInteractions.js";
import {OidcJwks} from "./OidcJwks.js";
import {OidcPolicy} from "./OidcPolicy.js";
import {OIDC_PROVIDER_NODE_MODULE, Provider} from "./OidcProviderNodeModule.js";

function mapError(error: any) {
return Object.getOwnPropertyNames(error).reduce((obj: any, key) => {
Expand Down Expand Up @@ -66,8 +64,6 @@ export class OidcProvider {
@InjectContext()
protected $ctx?: PlatformContext;

constructor(@Inject(OIDC_PROVIDER_NODE_MODULE) protected module: OIDC_PROVIDER_NODE_MODULE) {}

get logger() {
return this.$ctx?.logger || this.injector.logger;
}
Expand Down Expand Up @@ -150,7 +146,7 @@ export class OidcProvider {

await this.injector.alterAsync("$alterOidcConfiguration", configuration);

const oidcProvider = new this.module.Provider(this.getIssuer(), configuration);
const oidcProvider = new Provider(this.getIssuer(), configuration);

if (proxy) {
// istanbul ignore next
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions packages/security/oidc-provider/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export default defineConfig(
coverage: {
...presets.test.coverage,
thresholds: {
statements: 97.33,
statements: 97.26,
branches: 90.9,
functions: 98.63,
lines: 97.33
functions: 98.57,
lines: 97.26
}
}
}
}
);
);

0 comments on commit 7b33ea7

Please sign in to comment.