mirror of https://github.com/ghostfolio/ghostfolio
Browse Source
* Support MCP OAuth discovery and metadata endpoints * Update changelogpull/8407/head
committed by
GitHub
15 changed files with 658 additions and 5 deletions
@ -0,0 +1,153 @@ |
|||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
|
||||
|
import { NotFoundException } from '@nestjs/common'; |
||||
|
|
||||
|
import { OAuthMetadataController } from './oauth-metadata.controller'; |
||||
|
|
||||
|
describe('OAuthMetadataController', () => { |
||||
|
let configurationService: ConfigurationService; |
||||
|
let controller: OAuthMetadataController; |
||||
|
let mockConfig: Record<string, any>; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
mockConfig = { |
||||
|
ENABLE_FEATURE_MCP: true, |
||||
|
ENABLE_FEATURE_MCP_OAUTH: true, |
||||
|
ROOT_URL: 'http://localhost:3333' |
||||
|
}; |
||||
|
|
||||
|
configurationService = { |
||||
|
get: jest.fn((key: string) => { |
||||
|
return mockConfig[key]; |
||||
|
}) |
||||
|
} as unknown as ConfigurationService; |
||||
|
|
||||
|
controller = new OAuthMetadataController(configurationService); |
||||
|
}); |
||||
|
|
||||
|
describe('getProtectedResourceMetadata', () => { |
||||
|
it('throws NotFoundException when ENABLE_FEATURE_MCP is false', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = false; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
|
||||
|
expect(() => { |
||||
|
return controller.getProtectedResourceMetadata(); |
||||
|
}).toThrow(NotFoundException); |
||||
|
}); |
||||
|
|
||||
|
it('throws NotFoundException when ENABLE_FEATURE_MCP_OAUTH is false', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = false; |
||||
|
|
||||
|
expect(() => { |
||||
|
return controller.getProtectedResourceMetadata(); |
||||
|
}).toThrow(NotFoundException); |
||||
|
}); |
||||
|
|
||||
|
it('throws NotFoundException when ROOT_URL is not valid as the issuer', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
mockConfig.ROOT_URL = 'http://0.0.0.0:3333'; |
||||
|
|
||||
|
expect(() => { |
||||
|
return controller.getProtectedResourceMetadata(); |
||||
|
}).toThrow(NotFoundException); |
||||
|
}); |
||||
|
|
||||
|
it('returns RFC 9728 protected resource metadata when feature flags are enabled', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
mockConfig.ROOT_URL = 'http://localhost:3333'; |
||||
|
|
||||
|
const result = controller.getProtectedResourceMetadata(); |
||||
|
|
||||
|
expect(result).toEqual({ |
||||
|
authorization_servers: ['http://localhost:3333'], |
||||
|
resource: 'http://localhost:3333/mcp', |
||||
|
resource_name: 'Ghostfolio' |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('normalizes ROOT_URL with trailing slash', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
mockConfig.ROOT_URL = 'https://ghostfol.io///'; |
||||
|
|
||||
|
const result = controller.getProtectedResourceMetadata(); |
||||
|
|
||||
|
expect(result).toEqual({ |
||||
|
authorization_servers: ['https://ghostfol.io'], |
||||
|
resource: 'https://ghostfol.io/mcp', |
||||
|
resource_name: 'Ghostfolio' |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('getAuthorizationServerMetadata', () => { |
||||
|
it('throws NotFoundException when ENABLE_FEATURE_MCP is false', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = false; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
|
||||
|
expect(() => { |
||||
|
return controller.getAuthorizationServerMetadata(); |
||||
|
}).toThrow(NotFoundException); |
||||
|
}); |
||||
|
|
||||
|
it('throws NotFoundException when ENABLE_FEATURE_MCP_OAUTH is false', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = false; |
||||
|
|
||||
|
expect(() => { |
||||
|
return controller.getAuthorizationServerMetadata(); |
||||
|
}).toThrow(NotFoundException); |
||||
|
}); |
||||
|
|
||||
|
it('throws NotFoundException when ROOT_URL is not valid as the issuer', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
mockConfig.ROOT_URL = 'http://0.0.0.0:3333'; |
||||
|
|
||||
|
expect(() => { |
||||
|
return controller.getAuthorizationServerMetadata(); |
||||
|
}).toThrow(NotFoundException); |
||||
|
}); |
||||
|
|
||||
|
it('returns RFC 8414 authorization server metadata when feature flags are enabled', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
mockConfig.ROOT_URL = 'http://localhost:3333'; |
||||
|
|
||||
|
const result = controller.getAuthorizationServerMetadata(); |
||||
|
|
||||
|
expect(result).toEqual({ |
||||
|
authorization_endpoint: 'http://localhost:3333/oauth/authorize', |
||||
|
client_id_metadata_document_supported: true, |
||||
|
code_challenge_methods_supported: ['S256'], |
||||
|
grant_types_supported: ['authorization_code', 'refresh_token'], |
||||
|
issuer: 'http://localhost:3333', |
||||
|
response_types_supported: ['code'], |
||||
|
token_endpoint: 'http://localhost:3333/api/v1/oauth/token', |
||||
|
token_endpoint_auth_methods_supported: ['none'] |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('normalizes ROOT_URL with trailing slash', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
mockConfig.ROOT_URL = 'https://ghostfol.io/'; |
||||
|
|
||||
|
const result = controller.getAuthorizationServerMetadata(); |
||||
|
|
||||
|
expect(result).toEqual({ |
||||
|
authorization_endpoint: 'https://ghostfol.io/oauth/authorize', |
||||
|
client_id_metadata_document_supported: true, |
||||
|
code_challenge_methods_supported: ['S256'], |
||||
|
grant_types_supported: ['authorization_code', 'refresh_token'], |
||||
|
issuer: 'https://ghostfol.io', |
||||
|
response_types_supported: ['code'], |
||||
|
token_endpoint: 'https://ghostfol.io/api/v1/oauth/token', |
||||
|
token_endpoint_auth_methods_supported: ['none'] |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,49 @@ |
|||||
|
import { |
||||
|
getMcpAuthorizationServerMetadata, |
||||
|
getMcpResourceUrl, |
||||
|
isMcpOAuthEnabled |
||||
|
} from '@ghostfolio/api/helper/mcp-oauth.helper'; |
||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { MCP_ENDPOINT } from '@ghostfolio/common/config'; |
||||
|
|
||||
|
import { buildOAuthProtectedResourceMetadata } from '@modelcontextprotocol/server'; |
||||
|
import { |
||||
|
Controller, |
||||
|
Get, |
||||
|
NotFoundException, |
||||
|
VERSION_NEUTRAL, |
||||
|
Version |
||||
|
} from '@nestjs/common'; |
||||
|
|
||||
|
@Controller('.well-known') |
||||
|
export class OAuthMetadataController { |
||||
|
public constructor( |
||||
|
private readonly configurationService: ConfigurationService |
||||
|
) {} |
||||
|
|
||||
|
@Get(`oauth-protected-resource${MCP_ENDPOINT}`) |
||||
|
@Version(VERSION_NEUTRAL) |
||||
|
public getProtectedResourceMetadata() { |
||||
|
if (!isMcpOAuthEnabled(this.configurationService)) { |
||||
|
throw new NotFoundException(); |
||||
|
} |
||||
|
|
||||
|
return buildOAuthProtectedResourceMetadata({ |
||||
|
oauthMetadata: getMcpAuthorizationServerMetadata( |
||||
|
this.configurationService |
||||
|
), |
||||
|
resourceName: 'Ghostfolio', |
||||
|
resourceServerUrl: getMcpResourceUrl(this.configurationService) |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
@Get('oauth-authorization-server') |
||||
|
@Version(VERSION_NEUTRAL) |
||||
|
public getAuthorizationServerMetadata() { |
||||
|
if (!isMcpOAuthEnabled(this.configurationService)) { |
||||
|
throw new NotFoundException(); |
||||
|
} |
||||
|
|
||||
|
return getMcpAuthorizationServerMetadata(this.configurationService); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; |
||||
|
|
||||
|
import { Module } from '@nestjs/common'; |
||||
|
|
||||
|
import { OAuthMetadataController } from './oauth-metadata.controller'; |
||||
|
|
||||
|
@Module({ |
||||
|
controllers: [OAuthMetadataController], |
||||
|
imports: [ConfigurationModule] |
||||
|
}) |
||||
|
export class OAuthModule {} |
||||
@ -0,0 +1,131 @@ |
|||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
|
||||
|
import { |
||||
|
getMcpAuthorizationServerMetadata, |
||||
|
getMcpOAuthBaseUrl, |
||||
|
getMcpProtectedResourceMetadataUrl, |
||||
|
getMcpResourceUrl, |
||||
|
isMcpOAuthEnabled, |
||||
|
isValidMcpOAuthIssuerUrl |
||||
|
} from './mcp-oauth.helper'; |
||||
|
|
||||
|
describe('mcp-oauth.helper', () => { |
||||
|
let configurationService: ConfigurationService; |
||||
|
let mockConfig: Record<string, any>; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
mockConfig = { |
||||
|
ENABLE_FEATURE_MCP: true, |
||||
|
ENABLE_FEATURE_MCP_OAUTH: true, |
||||
|
ROOT_URL: 'http://localhost:3333' |
||||
|
}; |
||||
|
|
||||
|
configurationService = { |
||||
|
get: jest.fn((key: string) => { |
||||
|
return mockConfig[key]; |
||||
|
}) |
||||
|
} as unknown as ConfigurationService; |
||||
|
}); |
||||
|
|
||||
|
describe('getMcpAuthorizationServerMetadata', () => { |
||||
|
it('returns RFC 8414 authorization server metadata', () => { |
||||
|
mockConfig.ROOT_URL = 'http://localhost:3333/'; |
||||
|
|
||||
|
expect(getMcpAuthorizationServerMetadata(configurationService)).toEqual({ |
||||
|
authorization_endpoint: 'http://localhost:3333/oauth/authorize', |
||||
|
client_id_metadata_document_supported: true, |
||||
|
code_challenge_methods_supported: ['S256'], |
||||
|
grant_types_supported: ['authorization_code', 'refresh_token'], |
||||
|
issuer: 'http://localhost:3333', |
||||
|
response_types_supported: ['code'], |
||||
|
token_endpoint: 'http://localhost:3333/api/v1/oauth/token', |
||||
|
token_endpoint_auth_methods_supported: ['none'] |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('getMcpOAuthBaseUrl', () => { |
||||
|
it('returns the root URL without trailing slashes', () => { |
||||
|
mockConfig.ROOT_URL = 'https://ghostfol.io///'; |
||||
|
|
||||
|
expect(getMcpOAuthBaseUrl(configurationService)).toBe( |
||||
|
'https://ghostfol.io' |
||||
|
); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('getMcpProtectedResourceMetadataUrl', () => { |
||||
|
it('returns the full protected resource metadata URL', () => { |
||||
|
mockConfig.ROOT_URL = 'http://localhost:3333/'; |
||||
|
|
||||
|
expect(getMcpProtectedResourceMetadataUrl(configurationService)).toBe( |
||||
|
'http://localhost:3333/.well-known/oauth-protected-resource/mcp' |
||||
|
); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('getMcpResourceUrl', () => { |
||||
|
it('returns the resource URL as a URL instance', () => { |
||||
|
mockConfig.ROOT_URL = 'http://localhost:3333/'; |
||||
|
|
||||
|
expect(getMcpResourceUrl(configurationService)).toEqual( |
||||
|
new URL('http://localhost:3333/mcp') |
||||
|
); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('isMcpOAuthEnabled', () => { |
||||
|
it('returns true when both ENABLE_FEATURE_MCP and ENABLE_FEATURE_MCP_OAUTH are true', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
|
||||
|
expect(isMcpOAuthEnabled(configurationService)).toBe(true); |
||||
|
}); |
||||
|
|
||||
|
it('returns false when ENABLE_FEATURE_MCP is false', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = false; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
|
||||
|
expect(isMcpOAuthEnabled(configurationService)).toBe(false); |
||||
|
}); |
||||
|
|
||||
|
it('returns false when ENABLE_FEATURE_MCP_OAUTH is false', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = false; |
||||
|
|
||||
|
expect(isMcpOAuthEnabled(configurationService)).toBe(false); |
||||
|
}); |
||||
|
|
||||
|
it('returns false when ROOT_URL is not valid as the issuer', () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP = true; |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
mockConfig.ROOT_URL = 'http://192.168.1.10:3333'; |
||||
|
|
||||
|
expect(isMcpOAuthEnabled(configurationService)).toBe(false); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('isValidMcpOAuthIssuerUrl', () => { |
||||
|
it.each([ |
||||
|
'http://127.0.0.1:3333', |
||||
|
'http://localhost:3333/', |
||||
|
'https://192.168.1.10:3333', |
||||
|
'https://ghostfol.io' |
||||
|
])('returns true for %s', (rootUrl) => { |
||||
|
mockConfig.ROOT_URL = rootUrl; |
||||
|
|
||||
|
expect(isValidMcpOAuthIssuerUrl(configurationService)).toBe(true); |
||||
|
}); |
||||
|
|
||||
|
it.each([ |
||||
|
'http://0.0.0.0:3333', |
||||
|
'http://192.168.1.10:3333', |
||||
|
'http://[::1]:3333', |
||||
|
'http://ghostfolio.local' |
||||
|
])('returns false for %s', (rootUrl) => { |
||||
|
mockConfig.ROOT_URL = rootUrl; |
||||
|
|
||||
|
expect(isValidMcpOAuthIssuerUrl(configurationService)).toBe(false); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,88 @@ |
|||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { MCP_ENDPOINT } from '@ghostfolio/common/config'; |
||||
|
|
||||
|
import { |
||||
|
getOAuthProtectedResourceMetadataUrl, |
||||
|
OAuthMetadata |
||||
|
} from '@modelcontextprotocol/server'; |
||||
|
|
||||
|
/** |
||||
|
* Gives the metadata of the authorization server (RFC 8414). The protected |
||||
|
* resource metadata (RFC 9728) names its issuer. |
||||
|
*/ |
||||
|
export function getMcpAuthorizationServerMetadata( |
||||
|
configurationService: ConfigurationService |
||||
|
): OAuthMetadata { |
||||
|
const rootUrl = getMcpOAuthBaseUrl(configurationService); |
||||
|
|
||||
|
return { |
||||
|
authorization_endpoint: `${rootUrl}/oauth/authorize`, |
||||
|
client_id_metadata_document_supported: true, |
||||
|
code_challenge_methods_supported: ['S256'], |
||||
|
grant_types_supported: ['authorization_code', 'refresh_token'], |
||||
|
issuer: rootUrl, |
||||
|
response_types_supported: ['code'], |
||||
|
token_endpoint: `${rootUrl}/api/v1/oauth/token`, |
||||
|
token_endpoint_auth_methods_supported: ['none'] |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Gives the root URL without a trailing slash. The issuer and the resource in |
||||
|
* the metadata have to be identical to the URL which the client uses. |
||||
|
*/ |
||||
|
export function getMcpOAuthBaseUrl(configurationService: ConfigurationService) { |
||||
|
return configurationService.get('ROOT_URL').replace(/\/+$/, ''); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Gives the URL of the protected resource metadata (RFC 9728) which the |
||||
|
* challenge of the authorization middleware names |
||||
|
*/ |
||||
|
export function getMcpProtectedResourceMetadataUrl( |
||||
|
configurationService: ConfigurationService |
||||
|
) { |
||||
|
return getOAuthProtectedResourceMetadataUrl( |
||||
|
getMcpResourceUrl(configurationService) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Gives the URL of the server of the Model Context Protocol (MCP), which is the |
||||
|
* resource of the protected resource metadata (RFC 9728) |
||||
|
*/ |
||||
|
export function getMcpResourceUrl(configurationService: ConfigurationService) { |
||||
|
return new URL(`${getMcpOAuthBaseUrl(configurationService)}${MCP_ENDPOINT}`); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Tells if the authorization with OAuth 2.1 is enabled. It depends on the |
||||
|
* server of the Model Context Protocol (MCP) and on a valid issuer URL. |
||||
|
*/ |
||||
|
export function isMcpOAuthEnabled(configurationService: ConfigurationService) { |
||||
|
return ( |
||||
|
configurationService.get('ENABLE_FEATURE_MCP') && |
||||
|
configurationService.get('ENABLE_FEATURE_MCP_OAUTH') && |
||||
|
isValidMcpOAuthIssuerUrl(configurationService) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Tells if the root URL is valid as the issuer of the authorization server: |
||||
|
* OAuth 2.1 requires HTTPS, except for localhost. This is the same rule as in |
||||
|
* buildOAuthProtectedResourceMetadata() of @modelcontextprotocol/server, which |
||||
|
* throws an error for any other issuer. |
||||
|
*/ |
||||
|
export function isValidMcpOAuthIssuerUrl( |
||||
|
configurationService: ConfigurationService |
||||
|
) { |
||||
|
const { hostname, protocol } = new URL( |
||||
|
getMcpOAuthBaseUrl(configurationService) |
||||
|
); |
||||
|
|
||||
|
return ( |
||||
|
protocol === 'https:' || |
||||
|
hostname === '127.0.0.1' || |
||||
|
hostname === 'localhost' |
||||
|
); |
||||
|
} |
||||
@ -0,0 +1,168 @@ |
|||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { ImpersonationService } from '@ghostfolio/api/services/impersonation/impersonation.service'; |
||||
|
import { UserService } from '@ghostfolio/api/services/user/user.service'; |
||||
|
import { HEADER_KEY_TOKEN } from '@ghostfolio/common/config'; |
||||
|
|
||||
|
import { Request, Response } from 'express'; |
||||
|
import { StatusCodes } from 'http-status-codes'; |
||||
|
|
||||
|
import { createMcpAuthorizationMiddleware } from './mcp-authorization.middleware'; |
||||
|
|
||||
|
describe('createMcpAuthorizationMiddleware', () => { |
||||
|
let configurationService: ConfigurationService; |
||||
|
let impersonationService: ImpersonationService; |
||||
|
let mockConfig: Record<string, any>; |
||||
|
let mockRequest: Partial<Request>; |
||||
|
let mockResponse: Partial<Response>; |
||||
|
let nextFunction: jest.Mock; |
||||
|
let userService: UserService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
mockConfig = { |
||||
|
ENABLE_FEATURE_MCP: true, |
||||
|
ENABLE_FEATURE_MCP_OAUTH: false, |
||||
|
ENABLE_FEATURE_SUBSCRIPTION: false, |
||||
|
ROOT_URL: 'http://localhost:3333' |
||||
|
}; |
||||
|
|
||||
|
configurationService = { |
||||
|
get: jest.fn((key: string) => { |
||||
|
return mockConfig[key]; |
||||
|
}) |
||||
|
} as unknown as ConfigurationService; |
||||
|
|
||||
|
impersonationService = { |
||||
|
resolve: jest.fn() |
||||
|
} as unknown as ImpersonationService; |
||||
|
|
||||
|
userService = { |
||||
|
isDailyRequestLimitExceeded: jest.fn(), |
||||
|
updateAnalytics: jest.fn(), |
||||
|
user: jest.fn() |
||||
|
} as unknown as UserService; |
||||
|
|
||||
|
mockRequest = { |
||||
|
headers: {} |
||||
|
}; |
||||
|
|
||||
|
mockResponse = { |
||||
|
json: jest.fn().mockReturnThis(), |
||||
|
setHeader: jest.fn().mockReturnThis(), |
||||
|
status: jest.fn().mockReturnThis() |
||||
|
}; |
||||
|
|
||||
|
nextFunction = jest.fn(); |
||||
|
}); |
||||
|
|
||||
|
it('returns 401 challenge with only realm when token is missing and OAuth is disabled', async () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = false; |
||||
|
jest.mocked(impersonationService.resolve).mockResolvedValueOnce({ |
||||
|
isActive: false |
||||
|
} as any); |
||||
|
|
||||
|
const middleware = createMcpAuthorizationMiddleware({ |
||||
|
configurationService, |
||||
|
impersonationService, |
||||
|
userService |
||||
|
}); |
||||
|
|
||||
|
await middleware( |
||||
|
mockRequest as Request, |
||||
|
mockResponse as Response, |
||||
|
nextFunction |
||||
|
); |
||||
|
|
||||
|
expect(mockResponse.status).toHaveBeenCalledWith(StatusCodes.UNAUTHORIZED); |
||||
|
expect(mockResponse.setHeader).toHaveBeenCalledWith( |
||||
|
'WWW-Authenticate', |
||||
|
'Bearer realm="Ghostfolio"' |
||||
|
); |
||||
|
expect(nextFunction).not.toHaveBeenCalled(); |
||||
|
}); |
||||
|
|
||||
|
it('returns 401 challenge with resource_metadata when token is missing and OAuth is enabled', async () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
jest.mocked(impersonationService.resolve).mockResolvedValueOnce({ |
||||
|
isActive: false |
||||
|
} as any); |
||||
|
|
||||
|
const middleware = createMcpAuthorizationMiddleware({ |
||||
|
configurationService, |
||||
|
impersonationService, |
||||
|
userService |
||||
|
}); |
||||
|
|
||||
|
await middleware( |
||||
|
mockRequest as Request, |
||||
|
mockResponse as Response, |
||||
|
nextFunction |
||||
|
); |
||||
|
|
||||
|
expect(mockResponse.status).toHaveBeenCalledWith(StatusCodes.UNAUTHORIZED); |
||||
|
expect(mockResponse.setHeader).toHaveBeenCalledWith( |
||||
|
'WWW-Authenticate', |
||||
|
'Bearer realm="Ghostfolio", resource_metadata="http://localhost:3333/.well-known/oauth-protected-resource/mcp"' |
||||
|
); |
||||
|
expect(nextFunction).not.toHaveBeenCalled(); |
||||
|
}); |
||||
|
|
||||
|
it('returns 401 with invalid_token when token is invalid and OAuth is enabled', async () => { |
||||
|
mockConfig.ENABLE_FEATURE_MCP_OAUTH = true; |
||||
|
mockRequest.headers = { |
||||
|
[HEADER_KEY_TOKEN.toLowerCase()]: 'Bearer invalid-token' |
||||
|
}; |
||||
|
|
||||
|
jest.mocked(impersonationService.resolve).mockResolvedValueOnce({ |
||||
|
isActive: false |
||||
|
} as any); |
||||
|
|
||||
|
const middleware = createMcpAuthorizationMiddleware({ |
||||
|
configurationService, |
||||
|
impersonationService, |
||||
|
userService |
||||
|
}); |
||||
|
|
||||
|
await middleware( |
||||
|
mockRequest as Request, |
||||
|
mockResponse as Response, |
||||
|
nextFunction |
||||
|
); |
||||
|
|
||||
|
expect(mockResponse.status).toHaveBeenCalledWith(StatusCodes.UNAUTHORIZED); |
||||
|
expect(mockResponse.setHeader).toHaveBeenCalledWith( |
||||
|
'WWW-Authenticate', |
||||
|
'Bearer realm="Ghostfolio", resource_metadata="http://localhost:3333/.well-known/oauth-protected-resource/mcp", error="invalid_token", error_description="The access cannot be resolved"' |
||||
|
); |
||||
|
expect(nextFunction).not.toHaveBeenCalled(); |
||||
|
}); |
||||
|
|
||||
|
it('calls next() when access resolves successfully', async () => { |
||||
|
const impersonation = { |
||||
|
isActive: true, |
||||
|
userId: 'test-user-id' |
||||
|
}; |
||||
|
|
||||
|
mockRequest.headers = { |
||||
|
[HEADER_KEY_TOKEN.toLowerCase()]: 'Bearer valid-access-id' |
||||
|
}; |
||||
|
|
||||
|
jest |
||||
|
.mocked(impersonationService.resolve) |
||||
|
.mockResolvedValueOnce(impersonation as any); |
||||
|
|
||||
|
const middleware = createMcpAuthorizationMiddleware({ |
||||
|
configurationService, |
||||
|
impersonationService, |
||||
|
userService |
||||
|
}); |
||||
|
|
||||
|
await middleware( |
||||
|
mockRequest as any, |
||||
|
mockResponse as Response, |
||||
|
nextFunction |
||||
|
); |
||||
|
|
||||
|
expect(nextFunction).toHaveBeenCalled(); |
||||
|
expect((mockRequest as any).impersonationOfBearerToken).toBe(impersonation); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue