# usagiapp/oauth2-rpc architecture

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

- Interactive diagram: https://gitdiagram.com/usagiapp/oauth2-rpc
- Repository: https://github.com/usagiapp/oauth2-rpc

## Overview

OAuth2 RPC is a Kotlin library and sample app for obtaining a Discord OAuth2 token, fetching the authorized user, connecting to Discord’s Gateway, and publishing Rich Presence. The sample app owns the end-to-end flow; the library supplies REST, Gateway, presence-building, and asset-registration capabilities. The Gateway and OAuth/REST flow are visible in excerpts, while some REST helper behavior and protocol details are only documented or indexed, so those links are omitted where ownership is not shown.

Main entry points: [`app/src/main/kotlin/com/discord/oauth2rpc/Main.kt`](https://github.com/usagiapp/oauth2-rpc/blob/master/app/src/main/kotlin/com/discord/oauth2rpc/Main.kt) (OAuth and demo flow), [`library/src/main/kotlin/com/discord/oauth2rpc/Gateway.kt`](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/Gateway.kt) (Gateway client), [`library/src/main/kotlin/com/discord/oauth2rpc/Rest.kt`](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/Rest.kt) (REST route client), [`library/src/main/kotlin/com/discord/oauth2rpc/utils/Util.kt`](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/Util.kt) (OAuth and JSON utilities)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Sample application"]
  node_main["OAuth and demo flow<br/>[Main.kt]"]
end

subgraph group_transport["Discord transport"]
  node_gateway["Gateway client<br/>[Gateway.kt]"]
  node_rest["REST route client<br/>[Rest.kt]"]
end

subgraph group_presence["Presence and assets"]
  node_presence["Presence builders<br/>[Presence.kt]"]
  node_registrar["Asset registrar"]
end

subgraph group_foundation["Models and utilities"]
  node_interface["Protocol models<br/>[Interface.kt]"]
  node_util["OAuth and JSON utilities<br/>[Util.kt]"]
  node_bitfield["Bit field base<br/>[BitField.kt]"]
  node_intents["Gateway intents<br/>[Intents.kt]"]
  node_capabilities["Gateway capabilities"]
  node_activityflags["Activity flags<br/>[ActivityFlags.kt]"]
  node_constants["Protocol constants<br/>[Constants.kt]"]
end

node_user(("Application user"))
node_discord(("Discord services"))

node_user -->|"starts flow"| node_main
node_main -->|"fetches user"| node_rest
node_main -->|"connects Gateway"| node_gateway
node_main -->|"builds activities"| node_presence
node_main -->|"gets external assets"| node_util
node_util -->|"posts assets"| node_rest
node_registrar -->|"registers assets"| node_rest
node_gateway -.->|"opens WebSocket"| node_discord
node_rest -.->|"sends HTTP requests"| node_discord
node_gateway -->|"uses flags"| node_intents
node_gateway -->|"uses flags"| node_capabilities
node_intents -->|"extends"| node_bitfield
node_capabilities -->|"extends"| node_bitfield
node_activityflags -->|"extends"| node_bitfield
node_presence -->|"resolves emoji"| node_util
node_main -.->|"parses token"| node_interface
node_gateway -.->|"uses packet models"| node_interface
node_main -.->|"uses defaults"| node_constants

click node_main "https://github.com/usagiapp/oauth2-rpc/blob/master/app/src/main/kotlin/com/discord/oauth2rpc/Main.kt"
click node_gateway "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/Gateway.kt"
click node_rest "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/Rest.kt"
click node_presence "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/structures/Presence.kt"
click node_registrar "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/DiscordAssetRegistrar.kt"
click node_interface "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/Interface.kt"
click node_util "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/Util.kt"
click node_bitfield "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/BitField.kt"
click node_intents "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/Intents.kt"
click node_capabilities "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/GatewayCapabilities.kt"
click node_activityflags "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/ActivityFlags.kt"
click node_constants "https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/Constants.kt"

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_main,node_user toneBlue
class node_gateway,node_rest toneAmber
class node_presence,node_registrar toneMint
class node_interface,node_util,node_bitfield,node_intents,node_capabilities,node_activityflags,node_constants toneRose
class node_discord toneIndigo
```

## Components

### Sample application

- [OAuth and demo flow](https://github.com/usagiapp/oauth2-rpc/blob/master/app/src/main/kotlin/com/discord/oauth2rpc/Main.kt): `app/src/main/kotlin/com/discord/oauth2rpc/Main.kt`

### Discord transport

- [Gateway client](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/Gateway.kt): `library/src/main/kotlin/com/discord/oauth2rpc/Gateway.kt`
- [REST route client](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/Rest.kt): `library/src/main/kotlin/com/discord/oauth2rpc/Rest.kt`

### Presence and assets

- [Presence builders](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/structures/Presence.kt): `library/src/main/kotlin/com/discord/oauth2rpc/structures/Presence.kt`
- [Asset registrar](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/DiscordAssetRegistrar.kt): `library/src/main/kotlin/com/discord/oauth2rpc/DiscordAssetRegistrar.kt`

### Models and utilities

- [Protocol models](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/Interface.kt): `library/src/main/kotlin/com/discord/oauth2rpc/Interface.kt`
- [OAuth and JSON utilities](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/Util.kt): `library/src/main/kotlin/com/discord/oauth2rpc/utils/Util.kt`
- [Bit field base](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/BitField.kt): `library/src/main/kotlin/com/discord/oauth2rpc/utils/BitField.kt`
- [Gateway intents](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/Intents.kt): `library/src/main/kotlin/com/discord/oauth2rpc/utils/Intents.kt`
- [Gateway capabilities](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/GatewayCapabilities.kt): `library/src/main/kotlin/com/discord/oauth2rpc/utils/GatewayCapabilities.kt`
- [Activity flags](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/ActivityFlags.kt): `library/src/main/kotlin/com/discord/oauth2rpc/utils/ActivityFlags.kt`
- [Protocol constants](https://github.com/usagiapp/oauth2-rpc/blob/master/library/src/main/kotlin/com/discord/oauth2rpc/utils/Constants.kt): `library/src/main/kotlin/com/discord/oauth2rpc/utils/Constants.kt`

### Other components

- **Application user**
- **Discord services**

## Connections

- Application user → OAuth and demo flow: starts flow (evidence: `app/src/main/kotlin/com/discord/oauth2rpc/Main.kt`)
- OAuth and demo flow → REST route client: fetches user (evidence: `app/src/main/kotlin/com/discord/oauth2rpc/Main.kt`)
- OAuth and demo flow → Gateway client: connects Gateway (evidence: `app/src/main/kotlin/com/discord/oauth2rpc/Main.kt`)
- OAuth and demo flow → Presence builders: builds activities (evidence: `app/src/main/kotlin/com/discord/oauth2rpc/Main.kt`)
- OAuth and demo flow → OAuth and JSON utilities: gets external assets (evidence: `app/src/main/kotlin/com/discord/oauth2rpc/Main.kt`)
- OAuth and JSON utilities → REST route client: posts assets (evidence: `library/src/main/kotlin/com/discord/oauth2rpc/utils/Util.kt`)
- Asset registrar → REST route client: registers assets (evidence: `library/src/main/kotlin/com/discord/oauth2rpc/DiscordAssetRegistrar.kt`)
- Gateway client → Discord services: opens WebSocket
- REST route client → Discord services: sends HTTP requests
- Gateway client → Gateway intents: uses flags (evidence: `library/src/main/kotlin/com/discord/oauth2rpc/Gateway.kt`)
- Gateway client → Gateway capabilities: uses flags (evidence: `library/src/main/kotlin/com/discord/oauth2rpc/Gateway.kt`)
- Gateway intents → Bit field base: extends (evidence: `library/src/main/kotlin/com/discord/oauth2rpc/utils/Intents.kt`)
- Gateway capabilities → Bit field base: extends (evidence: `library/src/main/kotlin/com/discord/oauth2rpc/utils/GatewayCapabilities.kt`)
- Activity flags → Bit field base: extends (evidence: `library/src/main/kotlin/com/discord/oauth2rpc/utils/ActivityFlags.kt`)
- Presence builders → OAuth and JSON utilities: resolves emoji (evidence: `library/src/main/kotlin/com/discord/oauth2rpc/structures/Presence.kt`)
- OAuth and demo flow → Protocol models: parses token
- Gateway client → Protocol models: uses packet models
- OAuth and demo flow → Protocol constants: uses defaults
