# egg-/danal architecture

> Architecture diagram of the GitHub repository egg-/danal, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/egg-/danal
- Repository: https://github.com/egg-/danal

## Overview

1. **Overall shape**
   - This repository is a small **Node.js library** for integrating with the **Danal UAS** identity/authentication flow.
   - It is not a full app; it exposes reusable modules that your own server mounts into an HTTP framework such as Express.

2. **Primary purpose**
   - The library orchestrates a multi-step authentication handshake:
     - create a UAS request,
     - redirect the user to the external Danal flow,
     - receive the callback or cancel response,
     - confirm the result server-to-server.
   - The README’s example shows that the consumer app owns the HTTP routes and persistence, while `danal` handles protocol logic.

3. **Main exported surface**
   - `index.js` is the package entry point and likely re-exports the main classes/modules from `lib/`.
   - The two core abstractions exposed in the README are:
     - `UAS` for request/redirect/receive/confirm behavior,
     - `API` for shaping request payloads and defaults.

4. **UAS workflow boundary**
   - `lib/uas.js` is the central subsystem.
   - It appears to encapsulate the end-to-end UAS lifecycle and provides middleware-style helpers used by the host server:
     - `request(token, cb)` to initiate a transaction,
     - `redirect()` to send the user into the external auth step,
     - `receive()` to parse callback/cancel responses,
     - `confirm(...)` to finalize the authentication and retrieve identity data.
   - This is the main boundary between your app and the Danal service.

5. **HTTP integration model**
   - The library is designed to be embedded into an HTTP server, not to run one itself.
   - The README example shows:
     - a GET route that prepares UAS data and then calls `uas.redirect()`,
     - a POST callback route that runs `uas.receive()` and then `uas.confirm()`,
     - a POST cancel route that only receives and returns the request context.
   - The host app keeps control of routing, response formatting, and user/session updates.

6. **Payload and configuration shaping**
   - `lib/api.js` likely defines the request/response field model and supports merging default values into UAS payloads.
   - The README’s use of `danal.API.defaults(...)` suggests this module is responsible for assembling the final request object with:
     - cancel URI,
     - CI logo URLs,
     - optional user profile fields such as carrier, phone number, birth year, and passvalue.
   - This is a data-munging layer between app config and external API requirements.

7. **Endpoint and naming utilities**
   - `lib/endpoint.js` and `lib/names.js` are supporting modules that likely centralize:
     - service endpoint URLs,
     - field names / canonical parameter keys,
     - possibly mappings between request and response formats.
   - These are important because they keep protocol constants out of the application layer and reduce drift if the vendor API changes.

8. **Browser handoff helper**
   - `lib/redirect.html` indicates there is a browser-side redirect or form-post helper involved in the flow.
   - In architecture terms, this is the seam where the server-generated UAS request is handed off to the user’s browser for navigation to the external authentication provider.

9. **Data flow through the system**
   - The host app builds a token object, passes it to `UAS.request()`, receives a transaction payload, enriches it with defaults via `API.defaults()`, and stores it in `res.locals`.
   - The browser is then redirected out to the Danal service.
   - On callback, the incoming request is parsed, then the app calls `confirm()` using the stored transaction id to retrieve verified identity attributes.

10. **External dependency boundary**
    - The only major external integration implied here is the **Danal UAS service**.
    - The library abstracts away the vendor-specific request/confirmation details, but the surrounding app still needs to provide:
      - `client_id`, `client_secret`, and `redirect_uri`,
      - optional consumer-facing UI/logo URLs,
      - the local callback and cancel endpoints.

11. **Testing and build**
    - `test/index.js` suggests the repo includes automated tests for the exported behavior.
    - `Gruntfile.coffee` indicates Grunt is used for task automation, likely linting, testing, or packaging.
    - `package.json` defines the Node runtime dependency boundary and package metadata.

12. **How to draw the architecture**
    - A useful diagram would show:
      - your app/server on the left,
      - `danal` as an embedded library in the middle,
      - Danal/UAS external service on the right,
      - the browser/user as the actor that is redirected between them.
    - The most important arrows are:
      - app → `UAS.request()` → payload assembly,
      - app/browser → `redirect()` → external UAS,
      - external UAS → callback/cancel routes → `receive()`,
      - app → `confirm()` → verified identity result.

Main entry points: [`index.js`](https://github.com/egg-/danal/blob/master/index.js) (Entry point), [`lib/uas.js`](https://github.com/egg-/danal/blob/master/lib/uas.js) (UAS), [`lib/api.js`](https://github.com/egg-/danal/blob/master/lib/api.js) (API), [`test/index.js`](https://github.com/egg-/danal/blob/master/test/index.js) (Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_consumer["Host app"]
  node_app["Consumer app<br/>http host"]
end

subgraph group_library["danal package"]
  node_index_entry["Entry point<br/>package export<br/>[index.js]"]
  node_uas_module["UAS<br/>flow coordinator<br/>[uas.js]"]
  node_api_module["API<br/>payload model<br/>[api.js]"]
  node_endpoint_module["Endpoints<br/>service constants<br/>[endpoint.js]"]
  node_names_module["Names<br/>field map<br/>[names.js]"]
  node_redirect_html["Redirect page<br/>browser handoff<br/>[redirect.html]"]
  node_request_step(("Request<br/>initiation step"))
  node_redirect_step(("Redirect<br/>handoff step"))
  node_receive_step(("Receive<br/>callback parser"))
  node_confirm_step(("Confirm<br/>verification step"))
  node_response_data[("Identity data<br/>verified result")]
  node_tests["Tests<br/>verification suite<br/>[index.js]"]
  node_build["Grunt<br/>task runner<br/>[Gruntfile.coffee]"]
end

subgraph group_external["External actors"]
  node_browser(("Browser<br/>user agent"))
  node_uas_service["Danal UAS<br/>vendor service"]
end

node_app -->|"imports"| node_index_entry
node_index_entry -->|"exports"| node_uas_module
node_index_entry -->|"exports"| node_api_module
node_uas_module -->|"starts"| node_request_step
node_request_step -->|"builds with"| node_api_module
node_api_module -->|"targets"| node_endpoint_module
node_api_module -->|"maps fields"| node_names_module
node_request_step -->|"returns payload"| node_app
node_app -->|"calls"| node_redirect_step
node_redirect_step -->|"sends user"| node_browser
node_browser -->|"auth flow"| node_uas_service
node_uas_service -->|"callback/cancel"| node_receive_step
node_app -->|"parses via"| node_receive_step
node_receive_step -->|"updates"| node_uas_module
node_app -->|"calls"| node_confirm_step
node_confirm_step -->|"verifies with"| node_uas_service
node_confirm_step -->|"yields"| node_response_data
node_uas_module -.->|"renders"| node_redirect_html
node_tests -.->|"covers"| node_index_entry
node_build -.->|"runs"| node_tests

click node_index_entry "https://github.com/egg-/danal/blob/master/index.js"
click node_uas_module "https://github.com/egg-/danal/blob/master/lib/uas.js"
click node_api_module "https://github.com/egg-/danal/blob/master/lib/api.js"
click node_endpoint_module "https://github.com/egg-/danal/blob/master/lib/endpoint.js"
click node_names_module "https://github.com/egg-/danal/blob/master/lib/names.js"
click node_redirect_html "https://github.com/egg-/danal/blob/master/lib/redirect.html"
click node_tests "https://github.com/egg-/danal/blob/master/test/index.js"
click node_build "https://github.com/egg-/danal/blob/master/Gruntfile.coffee"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app toneBlue
class node_index_entry,node_uas_module,node_api_module,node_endpoint_module,node_names_module,node_redirect_html,node_request_step,node_redirect_step,node_receive_step,node_confirm_step,node_response_data,node_tests,node_build toneAmber
class node_browser,node_uas_service toneMint
```

## Components

### Host app

Application embedding the library and owning routes, session state, and persistence.

- **Consumer app**: http host · Owns routes, session state, and persistence; mounts the library into an HTTP framework.

### danal package

Reusable Node.js library that implements the UAS protocol flow.

- [Entry point](https://github.com/egg-/danal/blob/master/index.js): package export · Package entry that exposes the library surface. · `index.js`
- [UAS](https://github.com/egg-/danal/blob/master/lib/uas.js): flow coordinator · Central lifecycle object for request, redirect, receive, and confirm steps. · `lib/uas.js`
- [API](https://github.com/egg-/danal/blob/master/lib/api.js): payload model · Builds request payloads and merges default values for the protocol. · `lib/api.js`
- [Endpoints](https://github.com/egg-/danal/blob/master/lib/endpoint.js): service constants · Centralizes vendor endpoint URLs and related protocol targets. · `lib/endpoint.js`
- [Names](https://github.com/egg-/danal/blob/master/lib/names.js): field map · Defines canonical parameter names and protocol field mappings. · `lib/names.js`
- [Redirect page](https://github.com/egg-/danal/blob/master/lib/redirect.html): browser handoff · HTML helper used to hand off the prepared request to the browser. · `lib/redirect.html`
- **Request**: initiation step · Creates the UAS transaction payload from the consumer token and config.
- **Redirect**: handoff step · Sends the user agent into the external Danal flow.
- **Receive**: callback parser · Parses callback or cancel responses from the vendor flow.
- **Confirm**: verification step · Performs server-to-server confirmation to obtain identity data.
- **Identity data**: verified result · Verified authentication outcome returned to the host app.
- [Tests](https://github.com/egg-/danal/blob/master/test/index.js): verification suite · Automated checks for the exported behavior. · `test/index.js`
- [Grunt](https://github.com/egg-/danal/blob/master/Gruntfile.coffee): task runner · Task automation for testing, linting, or packaging. · `Gruntfile.coffee`

### External actors

Browser and vendor service involved in the authentication handshake.

- **Browser**: user agent · Carries the user through the redirect-based authentication step.
- **Danal UAS**: vendor service · External identity/authentication service that receives redirects and returns verified results.

## Connections

- Consumer app → Entry point: imports · The host app consumes the package entry to access the library API.
- Entry point → UAS: exports · Entry point exposes the central UAS workflow object.
- Entry point → API: exports · Entry point exposes payload-shaping helpers.
- UAS → Request: starts · Initiates a transaction from the consumer token.
- Request → API: builds with · Uses API defaults and field shaping to form the outbound payload.
- API → Endpoints: targets · Resolves vendor service endpoints used by the request flow.
- API → Names: maps fields · Normalizes protocol parameter names.
- Request → Consumer app: returns payload · Provides the assembled transaction data back to the host app.
- Consumer app → Redirect: calls · The host app triggers the redirect after storing request state.
- Redirect → Browser: sends user · Moves the user agent into the external authentication flow.
- Browser → Danal UAS: auth flow · The browser interacts with the vendor-hosted UAS experience.
- Danal UAS → Receive: callback/cancel · Vendor returns control to the application callback or cancel route.
- Consumer app → Receive: parses via · The host app invokes receive handling on incoming responses.
- Receive → UAS: updates · Receive step feeds parsed response context back into the workflow.
- Consumer app → Confirm: calls · The host app confirms the transaction after receiving the callback.
- Confirm → Danal UAS: verifies with · Performs server-to-server confirmation against the vendor.
- Confirm → Identity data: yields · Produces verified identity attributes for the host app.
- UAS → Redirect page: renders · Uses the redirect helper for browser handoff when needed.
- Tests → Entry point: covers · Automated tests exercise the exported package behavior.
- Grunt → Tests: runs · Task automation likely orchestrates the test suite.
