π§΅ Service Layer Guide¶
CTFBridge organizes platform functionality into modular services. These services define the logic for common tasks such as logging in, fetching challenges, submitting flags, and accessing the scoreboard.
ποΈ Base and Core Services¶
Base service interfaces live in ctfbridge.base.services and define expected methods. Core logic is implemented in ctfbridge.core.services, which base service classes inherit from. This split allows reuse of shared logic across platforms, while clearly defining the public interface.
Example interface (in base.services.auth):
Common implementation (in core.services.auth):
class AuthServiceImpl(AuthService):
def login(self, username, password):
# Shared POST login logic
...
Platform services can choose to inherit directly from core classes, or override specific behavior as needed.
𧬠Platform-Specific Services¶
Located in ctfbridge.platforms.<platform>.services, these classes inherit from core or base services and implement platform-specific logic. For example:
Each platform typically includes:
auth.pychallenges.pyscoreboard.pyattachments.pysession.py(if needed)
These services are composed into the client for that platform.
π§ How They Plug Into Clients¶
Each platform-specific client (e.g. CTFdClient) is responsible for instantiating the appropriate services:
class CTFdClient(CTFBridgeClient):
def __init__(self, url: str):
self.auth = CTFdAuthService(self)
self.challenges = CTFdChallengeService(self)
...
This allows consumers of the library to use a consistent interface:
client = create_client("https://myctf.io")
client.auth.login(username="admin", password="password")
chals = client.challenges.get_all()
π¦ Shared Behavior and Utilities¶
If multiple platforms share similar logic, it can be factored into:
ctfbridge.core.services: common service behaviorctfbridge.helpers/: smaller utility modules for reuse
β¨ Adding a New Service¶
- Define a base class in
base/services/if one doesn't exist. - Implement shared logic in
core/services/if reusable. - Implement the service in
platforms/<platform>/services/. - Register it in the platform client.
- Update the models and tests if needed.
Services form the backbone of CTFBridge's extensible API. They isolate logic by concern and allow clean overrides for platform-specific behavior.