Skip to content

Service Layer

dev-mondoshawan edited this page Apr 16, 2026 · 1 revision

Service Layer

**Referenced Files in This Document** - [backend/server.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/server.js) - [backend/src/config/index.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/config/index.js) - [backend/src/models/db.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/models/db.js) - [backend/src/models/queries.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/models/queries.js) - [backend/src/middleware/errorHandler.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/middleware/errorHandler.js) - [backend/src/middleware/rateLimit.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/middleware/rateLimit.js) - [backend/src/routes/agents.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/routes/agents.js) - [backend/src/routes/badge.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/routes/badge.js) - [backend/src/routes/reputation.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/routes/reputation.js) - [backend/src/routes/widget.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/routes/widget.js) - [backend/src/services/bagsAuthVerifier.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/services/bagsAuthVerifier.js) - [backend/src/services/bagsReputation.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/services/bagsReputation.js) - [backend/src/services/saidBinding.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/services/saidBinding.js) - [backend/src/services/badgeBuilder.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/services/badgeBuilder.js) - [backend/src/services/pkiChallenge.js](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/src/services/pkiChallenge.js) - [backend/package.json](https://github.com/RunTimeAdmin/AgentID/blob/main/backend/package.json)

Table of Contents

  1. Introduction
  2. Project Structure
  3. Core Components
  4. Architecture Overview
  5. Detailed Component Analysis
  6. Dependency Analysis
  7. Performance Considerations
  8. Troubleshooting Guide
  9. Conclusion
  10. Appendices

Introduction

This document describes the AgentID service layer with a focus on business logic and integration services. It covers authentication services, identity services, reputation services, and badge services.

Project Structure

The backend is organized around layered concerns:

  • Server bootstrap and routing
  • Middleware (security, rate limiting, error handling)
  • Models (database and Redis)
  • Services (authentication, identity, reputation, badges, PKI)
  • Routes (HTTP endpoints)
graph TB
Server["Express Server<br/>server.js"] --> Routes["Route Modules<br/>agents.js / badge.js / reputation.js / widget.js"]
Routes --> Services["Service Layer<br/>bagsAuthVerifier.js / bagsReputation.js / saidBinding.js / badgeBuilder.js / pkiChallenge.js"]
Services --> Models["Models & Queries<br/>db.js / queries.js"]
Services --> Config["Config<br/>config/index.js"]
Routes --> Middleware["Middleware<br/>rateLimit.js / errorHandler.js"]
Server --> Middleware
Models --> DB["PostgreSQL"]
Models --> Redis["Redis"]
Loading

Core Components

  • Authentication services: Bags authentication wrapper, PKI challenge-response
  • Identity services: SAID registration, trust score retrieval, discovery
  • Reputation services: BAGS reputation computation
  • Badge services: JSON, SVG, and HTML widget generation

Architecture Overview

The service layer follows a clean architecture pattern:

  • Routes define HTTP endpoints
  • Services encapsulate business logic
  • Models abstract database operations
  • Configuration centralizes settings

Detailed Component Analysis

Authentication Services

Bags Authentication Wrapper

  • Initialization: requests challenge from BAGS API
  • Signature verification: validates Ed25519 signatures
  • Completion: submits signature to finalize authentication

PKI Challenge-Response

  • Issues time-bound challenges
  • Verifies signatures against stored challenges
  • Updates last verified timestamp

Identity Services

SAID Protocol Integration

  • Registration with SAID gateway
  • Trust score retrieval
  • Capability-based discovery

Reputation Services

BAGS Ecosystem Scoring

  • Computes score from five factors
  • Stores computed score
  • Returns labeled score

Badge Services

Trust Badge Generation

  • JSON badge with caching
  • SVG badge generation
  • HTML widget with auto-refresh

Dependency Analysis

The service layer exhibits low coupling:

  • Routes depend on services
  • Services depend on models and config
  • External libraries isolated in services

Performance Considerations

  • Caching: Badge JSON cached in Redis
  • External API timeouts: bounded latency
  • Pagination: enforced in listings
  • Database indexing: on frequently queried columns

Troubleshooting Guide

  • 404: Agent not found
  • 401: Invalid signature
  • 429: Rate limit exceeded
  • External failures: fallbacks in services

Conclusion

The AgentID service layer cleanly separates concerns across authentication, identity, reputation, and badge services with robust error handling and security.

Appendices

Service Interfaces Summary

  • Authentication: initBagsAuth(), verifyBagsSignature(), completeBagsAuth()
  • PKI Challenge: issueChallenge(), verifyChallenge()
  • Identity: registerWithSAID(), getSAIDTrustScore(), discoverSAIDAgents()
  • Reputation: computeBagsScore(), refreshAndStoreScore()
  • Badge: getBadgeJSON(), getBadgeSVG(), getWidgetHTML()

Clone this wiki locally