-
-
Notifications
You must be signed in to change notification settings - Fork 820
feat(core): enforce application access during OIDC flows #8882
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
Merged
charIeszhao
merged 7 commits into
master
from
charles-log-13486-enforce-app-access-during-authorization-and-token
Jun 2, 2026
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cf15973
feat(core): enforce application access during oidc flows
charIeszhao 6ee5bc7
refactor(core): clarify oidc app access checks
charIeszhao 4de43f7
refactor(core): centralize consent app access checks
charIeszhao aa4d77d
refactor(core): dedupe app access checks per interaction
charIeszhao 33eee8b
refactor(core): limit app access marker persistence
charIeszhao 9e1b502
refactor(core): write app access marker with consent result
charIeszhao a582d69
fix(core): recheck app access on consent submit
charIeszhao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
packages/core/src/middleware/koa-app-access-control.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import type { Provider } from 'oidc-provider'; | ||
|
|
||
| import RequestError from '#src/errors/RequestError/index.js'; | ||
| import { markAppLevelAccessControlChecked } from '#src/oidc/application-access-control.js'; | ||
| import { MockTenant } from '#src/test-utils/tenant.js'; | ||
| import { createContextWithRouteParameters } from '#src/utils/test-utils.js'; | ||
|
|
||
| import koaAppAccessControl from './koa-app-access-control.js'; | ||
|
|
||
| const { jest } = import.meta; | ||
| type InteractionDetails = Awaited<ReturnType<Provider['interactionDetails']>>; | ||
|
|
||
| describe('koaAppAccessControl middleware', () => { | ||
| const assertUserHasApplicationAccess = jest.fn(async () => { | ||
| await Promise.resolve(); | ||
| }); | ||
| const mockTenant = new MockTenant(undefined, undefined, undefined, { | ||
| applicationAccessControl: { assertUserHasApplicationAccess }, | ||
| }); | ||
| const next = jest.fn(); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| const createContext = (interactionDetails: Partial<InteractionDetails>) => { | ||
| // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- minimal interaction details stub for middleware testing | ||
| const details = { | ||
| persist: jest.fn(), | ||
| ...interactionDetails, | ||
| } as InteractionDetails; | ||
|
|
||
| return { | ||
| ...createContextWithRouteParameters({ | ||
| url: `/consent`, | ||
| }), | ||
| interactionDetails: details, | ||
| }; | ||
| }; | ||
|
|
||
| it('should assert application access before next middleware', async () => { | ||
| const ctx = createContext({ | ||
| params: { client_id: 'app-id' }, | ||
| // @ts-expect-error | ||
| session: { accountId: 'user-id' }, | ||
| }); | ||
| const guard = koaAppAccessControl(mockTenant.libraries); | ||
|
|
||
| await guard(ctx, next); | ||
|
|
||
| expect(assertUserHasApplicationAccess).toHaveBeenCalledWith('app-id', 'user-id'); | ||
| expect(ctx.interactionDetails.persist).not.toHaveBeenCalled(); | ||
| expect(next).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should mark application access checked when configured', async () => { | ||
| const ctx = createContext({ | ||
| params: { client_id: 'app-id' }, | ||
| // @ts-expect-error | ||
| session: { accountId: 'user-id' }, | ||
| }); | ||
| const guard = koaAppAccessControl(mockTenant.libraries, { markInteractionResult: true }); | ||
|
|
||
| await guard(ctx, next); | ||
|
|
||
| expect(assertUserHasApplicationAccess).toHaveBeenCalledWith('app-id', 'user-id'); | ||
| expect(ctx.interactionDetails.persist).toHaveBeenCalled(); | ||
| expect(next).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should mark application access checked when the mark predicate returns true', async () => { | ||
| const ctx = createContext({ | ||
| params: { client_id: 'app-id' }, | ||
| // @ts-expect-error | ||
| session: { accountId: 'user-id' }, | ||
| }); | ||
| const guard = koaAppAccessControl(mockTenant.libraries, { | ||
| markInteractionResult: async () => true, | ||
| }); | ||
|
|
||
| await guard(ctx, next); | ||
|
|
||
| expect(assertUserHasApplicationAccess).toHaveBeenCalledWith('app-id', 'user-id'); | ||
| expect(ctx.interactionDetails.persist).toHaveBeenCalled(); | ||
| expect(next).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not mark application access checked when the mark predicate returns false', async () => { | ||
| const ctx = createContext({ | ||
| params: { client_id: 'app-id' }, | ||
| // @ts-expect-error | ||
| session: { accountId: 'user-id' }, | ||
| }); | ||
| const guard = koaAppAccessControl(mockTenant.libraries, { | ||
| markInteractionResult: async () => false, | ||
| }); | ||
|
|
||
| await guard(ctx, next); | ||
|
|
||
| expect(assertUserHasApplicationAccess).toHaveBeenCalledWith('app-id', 'user-id'); | ||
| expect(ctx.interactionDetails.persist).not.toHaveBeenCalled(); | ||
| expect(next).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should skip duplicated application access check in the same interaction', async () => { | ||
| const ctx = createContext({ | ||
| params: { client_id: 'app-id' }, | ||
| // @ts-expect-error | ||
| session: { accountId: 'user-id' }, | ||
| result: markAppLevelAccessControlChecked(undefined, 'app-id', 'user-id'), | ||
| }); | ||
| const guard = koaAppAccessControl(mockTenant.libraries); | ||
|
|
||
| await guard(ctx, next); | ||
|
|
||
| expect(assertUserHasApplicationAccess).not.toHaveBeenCalled(); | ||
| expect(ctx.interactionDetails.persist).not.toHaveBeenCalled(); | ||
| expect(next).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should throw when access is denied', async () => { | ||
| const ctx = createContext({ | ||
| params: { client_id: 'app-id' }, | ||
| // @ts-expect-error | ||
| session: { accountId: 'user-id' }, | ||
| }); | ||
| assertUserHasApplicationAccess.mockRejectedValueOnce(new RequestError('oidc.access_denied')); | ||
| const guard = koaAppAccessControl(mockTenant.libraries); | ||
|
|
||
| await expect(guard(ctx, next)).rejects.toMatchObject({ code: 'oidc.access_denied' }); | ||
| expect(ctx.interactionDetails.persist).not.toHaveBeenCalled(); | ||
| expect(next).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should throw an error if session is not found', async () => { | ||
| const ctx = createContext({ | ||
| params: { client_id: 'app-id' }, | ||
| session: undefined, | ||
| }); | ||
| const guard = koaAppAccessControl(mockTenant.libraries); | ||
|
|
||
| await expect(guard(ctx, next)).rejects.toThrow(new RequestError({ code: 'session.not_found' })); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { type MiddlewareType } from 'koa'; | ||
| import { errors } from 'oidc-provider'; | ||
|
|
||
| import RequestError from '#src/errors/RequestError/index.js'; | ||
| import type { WithInteractionDetailsContext } from '#src/middleware/koa-interaction-details.js'; | ||
| import { | ||
| hasAppLevelAccessControlChecked, | ||
| markAppLevelAccessControlChecked, | ||
| } from '#src/oidc/application-access-control.js'; | ||
| import type Libraries from '#src/tenants/Libraries.js'; | ||
| import assertThat from '#src/utils/assert-that.js'; | ||
|
|
||
| type KoaAppAccessControlOptions<ContextT> = { | ||
| readonly markInteractionResult?: boolean | ((ctx: ContextT) => boolean | Promise<boolean>); | ||
| }; | ||
|
|
||
| const shouldMarkInteractionResult = async <ContextT>( | ||
| ctx: ContextT, | ||
| option: KoaAppAccessControlOptions<ContextT>['markInteractionResult'] | ||
| ) => { | ||
| if (typeof option === 'function') { | ||
| return option(ctx); | ||
| } | ||
|
|
||
| return option === true; | ||
| }; | ||
|
|
||
| export default function koaAppAccessControl< | ||
| StateT, | ||
| ContextT extends WithInteractionDetailsContext, | ||
| ResponseBodyT, | ||
| >( | ||
| libraries: Libraries, | ||
| options: KoaAppAccessControlOptions<ContextT> = {} | ||
| ): MiddlewareType<StateT, ContextT, ResponseBodyT> { | ||
| return async (ctx, next) => { | ||
| const { | ||
| params: { client_id: clientId }, | ||
| session, | ||
| } = ctx.interactionDetails; | ||
|
|
||
| assertThat(session, new RequestError({ code: 'session.not_found' })); | ||
| assertThat( | ||
| clientId && typeof clientId === 'string', | ||
| new errors.InvalidClient('client must be available') | ||
| ); | ||
|
|
||
| if ( | ||
| hasAppLevelAccessControlChecked(ctx.interactionDetails.result, clientId, session.accountId) || | ||
| hasAppLevelAccessControlChecked( | ||
| ctx.interactionDetails.lastSubmission, | ||
|
charIeszhao marked this conversation as resolved.
Outdated
|
||
| clientId, | ||
| session.accountId | ||
| ) | ||
| ) { | ||
| return next(); | ||
| } | ||
|
|
||
| await libraries.applicationAccessControl.assertUserHasApplicationAccess( | ||
|
charIeszhao marked this conversation as resolved.
|
||
| clientId, | ||
| session.accountId | ||
| ); | ||
|
|
||
| if (await shouldMarkInteractionResult(ctx, options.markInteractionResult)) { | ||
| ctx.interactionDetails.result = markAppLevelAccessControlChecked( | ||
| ctx.interactionDetails.result ?? ctx.interactionDetails.lastSubmission, | ||
| clientId, | ||
| session.accountId | ||
| ); | ||
| await ctx.interactionDetails.persist(); | ||
| } | ||
|
charIeszhao marked this conversation as resolved.
Outdated
|
||
|
|
||
| return next(); | ||
|
charIeszhao marked this conversation as resolved.
|
||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.