mirror of https://github.com/ghostfolio/ghostfolio
12 changed files with 446 additions and 6 deletions
@ -0,0 +1,136 @@ |
|||||
|
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; |
||||
|
|
||||
|
const mockConfig: Record<string, any> = { |
||||
|
ENABLE_FEATURE_MCP: true, |
||||
|
ENABLE_FEATURE_MCP_OAUTH: true, |
||||
|
ROOT_URL: 'http://localhost:3333' |
||||
|
}; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
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('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'], |
||||
|
bearer_methods_supported: ['header'], |
||||
|
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'], |
||||
|
bearer_methods_supported: ['header'], |
||||
|
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('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', |
||||
|
authorization_response_iss_parameter_supported: true, |
||||
|
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', |
||||
|
authorization_response_iss_parameter_supported: true, |
||||
|
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,71 @@ |
|||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; |
||||
|
import { MCP_ENDPOINT } from '@ghostfolio/common/config'; |
||||
|
|
||||
|
import { |
||||
|
Controller, |
||||
|
Get, |
||||
|
Header, |
||||
|
NotFoundException, |
||||
|
VERSION_NEUTRAL, |
||||
|
Version |
||||
|
} from '@nestjs/common'; |
||||
|
|
||||
|
@Controller('.well-known') |
||||
|
export class OAuthMetadataController { |
||||
|
public constructor( |
||||
|
private readonly configurationService: ConfigurationService |
||||
|
) {} |
||||
|
|
||||
|
@Get('oauth-protected-resource/mcp') |
||||
|
@Header('Cache-Control', 'public, max-age=3600') |
||||
|
@Version(VERSION_NEUTRAL) |
||||
|
public getProtectedResourceMetadata() { |
||||
|
if ( |
||||
|
!this.configurationService.get('ENABLE_FEATURE_MCP') || |
||||
|
!this.configurationService.get('ENABLE_FEATURE_MCP_OAUTH') |
||||
|
) { |
||||
|
throw new NotFoundException(); |
||||
|
} |
||||
|
|
||||
|
const rootUrl = (this.configurationService.get('ROOT_URL') || '').replace( |
||||
|
/\/+$/, |
||||
|
'' |
||||
|
); |
||||
|
|
||||
|
return { |
||||
|
authorization_servers: [rootUrl], |
||||
|
bearer_methods_supported: ['header'], |
||||
|
resource: `${rootUrl}${MCP_ENDPOINT}`, |
||||
|
resource_name: 'Ghostfolio' |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
@Get('oauth-authorization-server') |
||||
|
@Header('Cache-Control', 'public, max-age=3600') |
||||
|
@Version(VERSION_NEUTRAL) |
||||
|
public getAuthorizationServerMetadata() { |
||||
|
if ( |
||||
|
!this.configurationService.get('ENABLE_FEATURE_MCP') || |
||||
|
!this.configurationService.get('ENABLE_FEATURE_MCP_OAUTH') |
||||
|
) { |
||||
|
throw new NotFoundException(); |
||||
|
} |
||||
|
|
||||
|
const rootUrl = (this.configurationService.get('ROOT_URL') || '').replace( |
||||
|
/\/+$/, |
||||
|
'' |
||||
|
); |
||||
|
|
||||
|
return { |
||||
|
authorization_endpoint: `${rootUrl}/oauth/authorize`, |
||||
|
authorization_response_iss_parameter_supported: true, |
||||
|
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'] |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
@ -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,167 @@ |
|||||
|
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 userService: UserService; |
||||
|
let mockRequest: Partial<Request>; |
||||
|
let mockResponse: Partial<Response>; |
||||
|
let nextFunction: jest.Mock; |
||||
|
|
||||
|
const mockConfig: Record<string, any> = { |
||||
|
ENABLE_FEATURE_MCP: true, |
||||
|
ENABLE_FEATURE_MCP_OAUTH: false, |
||||
|
ENABLE_FEATURE_SUBSCRIPTION: false, |
||||
|
ROOT_URL: 'http://localhost:3333' |
||||
|
}; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
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