Skip to content

Commit

Permalink
fix(oidc-provider): fix oidcCtx.getGrant()
Browse files Browse the repository at this point in the history
Closes: #1942
  • Loading branch information
Romakita committed Aug 27, 2022
1 parent 999abb5 commit adbd5a2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
47 changes: 46 additions & 1 deletion docs/tutorials/oidc.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ The controller Interactions exposes the routes to display any interaction. Here
The `uid` is the unique session id used by oidc-provider to identify the current user flow.
:::

Now that we have our interactions controller, we can create our first interaction.
Now that we have our interaction controller, we can create our first interaction.

Create a new directory `interactions`. We will store all custom interactions in this directory.

Expand Down Expand Up @@ -280,6 +280,51 @@ http://localhost:8083/auth?client_id=client_id&response_type=id_token&scope=open

<figure><img alt="Oidc login page" src="./../assets/oidc/signin-page.png" style="max-height: 400px"></figure>

## Alter configuration

Some part of the OIDC provider configuration needs function to work. And ideally these functions should have access to our Ts.ED Services.

It's possible to do that by listening the `$alterOidcConfiguration` hook and inject the expected functions in the configuration:

```typescript
import {Module} from "@tsed/oidc-provider";
import {OidcSettings, OidcProviderContext} from "@tsed/oidc-provider";
import {set} from "lodash";

@Module()
class OidcResourceIndicatorsModule {
@InjectContext()
protected $ctx: PlatformContext; // retrieve the Ts.ED context

async $alterOidcConfiguration(config: OidcSettings): Promise<OidcSettings> {
// example with the
config.features.resourceIndicators = {
enabled: true,
defaultResource: this.defaultResource.bind(this),
getResourceServerInfo: this.getResourceServerInfo.bind(this),
useGrantedResource: this.useGrantedResource.bind(this)
};

return config;
}

protected async defaultResource(ctx: KoaContextWithOIDC) {
///
}

protected async getResourceServerInfo(ctx: KoaContextWithOIDC, resourceIndicator: string, client: Client): Promise<string | string[]> {
///
}

protected async useGrantedResource(
ctx: KoaContextWithOIDC,
model: AuthorizationCode | RefreshToken | DeviceCode | BackchannelAuthenticationRequest
): Promise<Boolean> {
return true;
}
}
```

## Alter OIDC policy

Ts.ED emits a special `$alterOidcPolicy` event when @tsed/oidc-provider links interactions with OIDC policy. You can change the policy configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function createOidcInteractionContextFixture(grantId: any = "grantId") {
setProviderSession: jest.fn().mockResolvedValue(undefined),
find: jest.fn().mockResolvedValue("grant"),
Grant: class {
static find = jest.fn().mockResolvedValue(undefined);
static find = jest.fn().mockResolvedValue("grant");
},
Client: {
find: jest.fn().mockResolvedValue({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class OidcInteractionContext {
if (this.grantId) {
// we'll be modifying existing grant in existing session
// @ts-ignore
return await (this.oidcProvider.get().find || Grant.find)(this.grantId);
return await Grant.find(this.grantId);
}

return new Grant({
Expand Down
3 changes: 3 additions & 0 deletions packages/security/oidc-provider/src/services/OidcProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export class OidcProvider {
async create(): Promise<void | OIDCProvider> {
const {proxy = this.env === Env.PROD, secureKey, allowHttpLocalhost = this.env !== Env.PROD} = this.oidc;
const configuration = await this.getConfiguration();

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

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

if (proxy) {
Expand Down

0 comments on commit adbd5a2

Please sign in to comment.