Skip to content

Cloudflare Worker that handles Discord OAuth authentication for the XIV Dye Tools ecosystem.

License

Notifications You must be signed in to change notification settings

FlashGalatine/xivdyetools-oauth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

69 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

XIV Dye Tools OAuth Worker

v2.2.2 | Cloudflare Worker that handles OAuth authentication for the XIV Dye Tools ecosystem.

TypeScript Cloudflare Workers

Overview

This Worker provides multi-provider OAuth authentication for the XIV Dye Tools web application. It supports both Discord and XIVAuth as authentication providers, with D1 database for user management and account merging.

Features

๐Ÿ” PKCE-Secured OAuth - Proof Key for Code Exchange for secure authorization ๐ŸŽซ JWT Issuance - JSON Web Tokens with HS256 signing ๐Ÿ”„ Token Refresh - 24-hour grace period for expired token refresh ๐ŸŒ CORS Support - Localhost allowed for development, configurable for production โšก Edge Deployment - Global low-latency via Cloudflare Workers ๐ŸŽฎ XIVAuth Support - Login with FFXIV character (via XIVAuth.net) ๐Ÿ—„๏ธ D1 Database - User management and account merging ๐Ÿ”— Account Merging - Link Discord and XIVAuth accounts automatically ๐Ÿ›ก๏ธ Security Hardened - Validated redirect URIs, API call timeouts, structured logging

API Endpoints

Discord OAuth

Endpoint Method Description
/auth/discord GET Start Discord OAuth flow (requires PKCE params)
/auth/callback GET Discord redirect handler
/auth/callback POST SPA token exchange (code + code_verifier)

XIVAuth OAuth

Endpoint Method Description
/auth/xivauth GET Start XIVAuth OAuth flow (requires PKCE params)
/auth/xivauth/callback GET XIVAuth redirect handler
/auth/xivauth/callback POST SPA token exchange (code + code_verifier)

Common Endpoints

Endpoint Method Description
/auth/refresh POST Refresh JWT (24h grace period)
/auth/me GET Get user info from JWT (Bearer token)
/auth/revoke POST Logout (client-side token clear)

OAuth Flow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Frontend   โ”‚     โ”‚  OAuth Worker โ”‚     โ”‚   Discord   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚                     โ”‚                    โ”‚
       โ”‚  1. Generate PKCE   โ”‚                    โ”‚
       โ”‚     code_verifier   โ”‚                    โ”‚
       โ”‚     code_challenge  โ”‚                    โ”‚
       โ”‚                     โ”‚                    โ”‚
       โ”‚  2. GET /auth/discord โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚
       โ”‚     + code_challenge                     โ”‚
       โ”‚                     โ”‚                    โ”‚
       โ”‚                     โ”‚โ—„โ”€โ”€โ”€โ”€โ”€ 3. Redirect โ”€โ”ค
       โ”‚                     โ”‚     with auth code โ”‚
       โ”‚                     โ”‚                    โ”‚
       โ”‚  4. Exchange code   โ”‚                    โ”‚
       โ”‚     + code_verifier โ”‚                    โ”‚
       โ”‚                     โ”‚                    โ”‚
       โ”‚โ—„โ”€โ”€โ”€โ”€ 5. JWT โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค                    โ”‚
       โ”‚                     โ”‚                    โ”‚

Development

Prerequisites

  • Node.js 18+
  • Cloudflare account with Workers enabled
  • Discord application with OAuth2 configured

Setup

  1. Install dependencies:

    npm install
  2. Set up secrets:

    wrangler secret put DISCORD_CLIENT_SECRET
    wrangler secret put XIVAUTH_CLIENT_SECRET  # Optional for public client mode
    wrangler secret put JWT_SECRET  # Generate with: openssl rand -hex 32
  3. Set up D1 database:

    wrangler d1 create xivdyetools-users
    # Update wrangler.toml with generated database ID
    wrangler d1 execute xivdyetools-users --file=./schema/users.sql --remote
  4. Start local development server:

    npm run dev

Commands

Command Description
npm run dev Start local dev server (localhost:8788)
npm run deploy Deploy to Cloudflare
npm run type-check TypeScript validation

Environment Variables

Configured in wrangler.toml

Variable Description
ENVIRONMENT "production" or "development"
DISCORD_CLIENT_ID Discord application client ID
XIVAUTH_CLIENT_ID XIVAuth application client ID
FRONTEND_URL Allowed CORS origin and redirect target
WORKER_URL This worker's URL (for JWT issuer claim)
JWT_EXPIRY Token lifetime in seconds (default: 3600)

D1 Database Binding

Binding Description
DB D1 database for user management

Secrets (via wrangler secret put)

Secret Description
DISCORD_CLIENT_SECRET Discord OAuth client secret
XIVAUTH_CLIENT_SECRET XIVAuth OAuth client secret (optional for public client)
JWT_SECRET Shared secret for HS256 JWT signing

JWT Claims

Issued tokens include:

{
  "sub": "internal_user_uuid",
  "username": "username",
  "global_name": "Display Name",
  "avatar": "avatar_hash",
  "auth_provider": "discord|xivauth",
  "discord_id": "discord_snowflake",
  "xivauth_id": "xivauth_uuid",
  "primary_character": {
    "name": "Character Name",
    "server": "Server Name",
    "verified": true
  },
  "iss": "worker_url",
  "iat": 1234567890,
  "exp": 1234571490,
  "jti": "unique_token_id"
}

Note: primary_character is only included for XIVAuth users.

Architecture

src/
โ”œโ”€โ”€ index.ts              # Hono app, middleware, route mounting
โ”œโ”€โ”€ types.ts              # TypeScript interfaces
โ”œโ”€โ”€ handlers/
โ”‚   โ”œโ”€โ”€ authorize.ts      # GET /auth/discord - Initiates Discord OAuth
โ”‚   โ”œโ”€โ”€ callback.ts       # Discord token exchange, JWT issuance
โ”‚   โ”œโ”€โ”€ xivauth.ts        # XIVAuth OAuth endpoints
โ”‚   โ””โ”€โ”€ refresh.ts        # Token refresh, user info, revoke
โ””โ”€โ”€ services/
    โ”œโ”€โ”€ jwt-service.ts    # JWT creation/verification (Web Crypto API)
    โ””โ”€โ”€ user-service.ts   # D1 user management and account merging

schema/
โ””โ”€โ”€ users.sql             # D1 database schema

Related Projects

License

MIT ยฉ 2025 Flash Galatine

See LICENSE for full details.

Legal Notice

This is a fan-made tool and is not affiliated with or endorsed by Square Enix Co., Ltd. FINAL FANTASY is a registered trademark of Square Enix Holdings Co., Ltd.

Connect With Me

Flash Galatine | Balmung (Crystal)

๐ŸŽฎ FFXIV: Lodestone Character ๐Ÿ“ Blog: Project Galatine ๐Ÿ’ป GitHub: @FlashGalatine ๐Ÿฆ X / Twitter: @AsheJunius ๐Ÿ“บ Twitch: flashgalatine ๐ŸŒ BlueSky: projectgalatine.com โค๏ธ Patreon: ProjectGalatine โ˜• Ko-Fi: flashgalatine ๐Ÿ’ฌ Discord: Join Server

Support


Made with โค๏ธ for the FFXIV community

About

Cloudflare Worker that handles Discord OAuth authentication for the XIV Dye Tools ecosystem.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •