An atproto PDS written in Go

Compare changes

Choose any two refs to compare.

+1 -1
.env.example
···
COCOON_RELAYS=https://bsky.network
# Generate with `openssl rand -hex 16`
COCOON_ADMIN_PASSWORD=
-
# Generate with `openssl rand -hex 32`
+
# openssl rand -hex 32
COCOON_SESSION_SECRET=
+8 -66
.github/workflows/docker-image.yml
···
on:
workflow_dispatch:
push:
-
branches:
-
- main
-
tags:
-
- 'v*'
env:
REGISTRY: ghcr.io
···
jobs:
build-and-push-image:
-
strategy:
-
matrix:
-
include:
-
- arch: amd64
-
runner: ubuntu-latest
-
- arch: arm64
-
runner: ubuntu-24.04-arm
-
runs-on: ${{ matrix.runner }}
+
runs-on: ubuntu-latest
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
permissions:
contents: read
packages: write
attestations: write
id-token: write
-
outputs:
-
digest-amd64: ${{ matrix.arch == 'amd64' && steps.push.outputs.digest || '' }}
-
digest-arm64: ${{ matrix.arch == 'arm64' && steps.push.outputs.digest || '' }}
+
#
steps:
- name: Checkout repository
uses: actions/checkout@v4
-
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Log in to the Container registry
-
uses: docker/login-action@v3
+
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
- name: Extract metadata (tags, labels) for Docker
id: meta
···
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
-
type=raw,value=latest,enable={{is_default_branch}},suffix=-${{ matrix.arch }}
-
type=sha,suffix=-${{ matrix.arch }}
-
type=sha,format=long,suffix=-${{ matrix.arch }}
-
type=semver,pattern={{version}},suffix=-${{ matrix.arch }}
-
type=semver,pattern={{major}}.{{minor}},suffix=-${{ matrix.arch }}
-
+
type=sha
+
type=sha,format=long
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
- name: Build and push Docker image
id: push
-
uses: docker/build-push-action@v6
+
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
-
publish-manifest:
-
needs: build-and-push-image
-
runs-on: ubuntu-latest
-
permissions:
-
packages: write
-
attestations: write
-
id-token: write
-
steps:
-
- name: Log in to the Container registry
-
uses: docker/login-action@v3
-
with:
-
registry: ${{ env.REGISTRY }}
-
username: ${{ github.actor }}
-
password: ${{ secrets.GITHUB_TOKEN }}
-
-
- name: Extract metadata (tags, labels) for Docker
-
id: meta
-
uses: docker/metadata-action@v5
-
with:
-
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
-
tags: |
-
type=raw,value=latest,enable={{is_default_branch}}
-
type=sha
-
type=sha,format=long
-
type=semver,pattern={{version}}
-
type=semver,pattern={{major}}.{{minor}}
-
-
- name: Create and push manifest
-
run: |
-
# Split tags into an array
-
readarray -t tags <<< "${{ steps.meta.outputs.tags }}"
-
-
# Create and push manifest for each tag
-
for tag in "${tags[@]}"; do
-
docker buildx imagetools create -t "$tag" \
-
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-push-image.outputs.digest-amd64 }}" \
-
"${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-push-image.outputs.digest-arm64 }}"
-
done
-
# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)."
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
-
subject-digest: ${{ needs.build-and-push-image.outputs.digest-amd64 }}
-
push-to-registry: true
+
subject-digest: ${{ steps.push.outputs.digest }}
+
push-to-registry: true
-3
.gitignore
···
*.key
*.secret
.DS_Store
-
data/
-
keys/
-
dist/
-10
Caddyfile
···
-
{$COCOON_HOSTNAME} {
-
reverse_proxy localhost:8080
-
-
encode gzip
-
-
log {
-
output file /data/access.log
-
format json
-
}
-
}
-10
Caddyfile.postgres
···
-
{$COCOON_HOSTNAME} {
-
reverse_proxy cocoon:8080
-
-
encode gzip
-
-
log {
-
output file /data/access.log
-
format json
-
}
-
}
+1 -1
Dockerfile
···
### Run stage
FROM debian:bookworm-slim AS run
-
RUN apt-get update && apt-get install -y dumb-init runit ca-certificates curl && rm -rf /var/lib/apt/lists/*
+
RUN apt-get update && apt-get install -y dumb-init runit ca-certificates && rm -rf /var/lib/apt/lists/*
ENTRYPOINT ["dumb-init", "--"]
WORKDIR /
+1 -37
Makefile
···
GIT_COMMIT := $(shell git rev-parse --short=9 HEAD)
VERSION := $(if $(GIT_TAG),$(GIT_TAG),dev-$(GIT_COMMIT))
-
# Build output directory
-
BUILD_DIR := dist
-
-
# Platforms to build for
-
PLATFORMS := \
-
linux/amd64 \
-
linux/arm64 \
-
linux/arm \
-
darwin/amd64 \
-
darwin/arm64 \
-
windows/amd64 \
-
windows/arm64 \
-
freebsd/amd64 \
-
freebsd/arm64 \
-
openbsd/amd64 \
-
openbsd/arm64
-
.PHONY: help
help: ## Print info about all commands
@echo "Commands:"
···
build: ## Build all executables
go build -ldflags "-X main.Version=$(VERSION)" -o cocoon ./cmd/cocoon
-
.PHONY: build-release
-
build-all: ## Build binaries for all architectures
-
@echo "Building for all architectures..."
-
@mkdir -p $(BUILD_DIR)
-
@$(foreach platform,$(PLATFORMS), \
-
$(eval OS := $(word 1,$(subst /, ,$(platform)))) \
-
$(eval ARCH := $(word 2,$(subst /, ,$(platform)))) \
-
$(eval EXT := $(if $(filter windows,$(OS)),.exe,)) \
-
$(eval OUTPUT := $(BUILD_DIR)/cocoon-$(VERSION)-$(OS)-$(ARCH)$(EXT)) \
-
echo "Building $(OS)/$(ARCH)..."; \
-
GOOS=$(OS) GOARCH=$(ARCH) go build -ldflags "-X main.Version=$(VERSION)" -o $(OUTPUT) ./cmd/cocoon && \
-
echo " โœ“ $(OUTPUT)" || echo " โœ— Failed: $(OS)/$(ARCH)"; \
-
)
-
@echo "Done! Binaries are in $(BUILD_DIR)/"
-
-
.PHONY: clean-dist
-
clean-dist: ## Remove all built binaries
-
rm -rf $(BUILD_DIR)
-
.PHONY: run
run:
go build -ldflags "-X main.Version=dev-local" -o cocoon ./cmd/cocoon && ./cocoon run
···
.PHONY: docker-build
docker-build:
-
docker build -t cocoon .
+
docker build -t cocoon .
+12 -196
README.md
···
# Cocoon
> [!WARNING]
-
I migrated and have been running my main account on this PDS for months now without issue, however, I am still not responsible if things go awry, particularly during account migration. Please use caution.
+
You should not use this PDS. You should not rely on this code as a reference for a PDS implementation. You should not trust this code. Using this PDS implementation may result in data loss, corruption, etc.
Cocoon is a PDS implementation in Go. It is highly experimental, and is not ready for any production use.
-
## Quick Start with Docker Compose
-
-
### Prerequisites
-
-
- Docker and Docker Compose installed
-
- A domain name pointing to your server (for automatic HTTPS)
-
- Ports 80 and 443 open in i.e. UFW
-
-
### Installation
-
-
1. **Clone the repository**
-
```bash
-
git clone https://github.com/haileyok/cocoon.git
-
cd cocoon
-
```
-
-
2. **Create your configuration file**
-
```bash
-
cp .env.example .env
-
```
-
-
3. **Edit `.env` with your settings**
-
-
Required settings:
-
```bash
-
COCOON_DID="did:web:your-domain.com"
-
COCOON_HOSTNAME="your-domain.com"
-
COCOON_CONTACT_EMAIL="you@example.com"
-
COCOON_RELAYS="https://bsky.network"
-
-
# Generate with: openssl rand -hex 16
-
COCOON_ADMIN_PASSWORD="your-secure-password"
-
-
# Generate with: openssl rand -hex 32
-
COCOON_SESSION_SECRET="your-session-secret"
-
```
-
-
4. **Start the services**
-
```bash
-
# Pull pre-built image from GitHub Container Registry
-
docker-compose pull
-
docker-compose up -d
-
```
-
-
Or build locally:
-
```bash
-
docker-compose build
-
docker-compose up -d
-
```
-
-
**For PostgreSQL deployment:**
-
```bash
-
# Add POSTGRES_PASSWORD to your .env file first!
-
docker-compose -f docker-compose.postgres.yaml up -d
-
```
-
-
5. **Get your invite code**
-
-
On first run, an invite code is automatically created. View it with:
-
```bash
-
docker-compose logs create-invite
-
```
-
-
Or check the saved file:
-
```bash
-
cat keys/initial-invite-code.txt
-
```
-
-
**IMPORTANT**: Save this invite code! You'll need it to create your first account.
-
-
6. **Monitor the services**
-
```bash
-
docker-compose logs -f
-
```
-
-
### What Gets Set Up
-
-
The Docker Compose setup includes:
-
-
- **init-keys**: Automatically generates cryptographic keys (rotation key and JWK) on first run
-
- **cocoon**: The main PDS service running on port 8080
-
- **create-invite**: Automatically creates an initial invite code after Cocoon starts (first run only)
-
- **caddy**: Reverse proxy with automatic HTTPS via Let's Encrypt
-
-
### Data Persistence
-
-
The following directories will be created automatically:
-
-
- `./keys/` - Cryptographic keys (generated automatically)
-
- `rotation.key` - PDS rotation key
-
- `jwk.key` - JWK private key
-
- `initial-invite-code.txt` - Your first invite code (first run only)
-
- `./data/` - SQLite database and blockstore
-
- Docker volumes for Caddy configuration and certificates
-
-
### Optional Configuration
-
-
#### Database Configuration
-
-
By default, Cocoon uses SQLite which requires no additional setup. For production deployments with higher traffic, you can use PostgreSQL:
-
-
```bash
-
# Database type: sqlite (default) or postgres
-
COCOON_DB_TYPE="postgres"
-
-
# PostgreSQL connection string (required if db-type is postgres)
-
# Format: postgres://user:password@host:port/database?sslmode=disable
-
COCOON_DATABASE_URL="postgres://cocoon:password@localhost:5432/cocoon?sslmode=disable"
-
-
# Or use the standard DATABASE_URL environment variable
-
DATABASE_URL="postgres://cocoon:password@localhost:5432/cocoon?sslmode=disable"
-
```
-
-
For SQLite (default):
-
```bash
-
COCOON_DB_TYPE="sqlite"
-
COCOON_DB_NAME="/data/cocoon/cocoon.db"
-
```
-
-
> **Note**: When using PostgreSQL, database backups to S3 are not handled by Cocoon. Use `pg_dump` or your database provider's backup solution instead.
-
-
#### SMTP Email Settings
-
```bash
-
COCOON_SMTP_USER="your-smtp-username"
-
COCOON_SMTP_PASS="your-smtp-password"
-
COCOON_SMTP_HOST="smtp.example.com"
-
COCOON_SMTP_PORT="587"
-
COCOON_SMTP_EMAIL="noreply@example.com"
-
COCOON_SMTP_NAME="Cocoon PDS"
-
```
-
-
#### S3 Storage
-
-
Cocoon supports S3-compatible storage for both database backups (SQLite only) and blob storage (images, videos, etc.):
-
-
```bash
-
# Enable S3 backups (SQLite databases only - hourly backups)
-
COCOON_S3_BACKUPS_ENABLED=true
-
-
# Enable S3 for blob storage (images, videos, etc.)
-
# When enabled, blobs are stored in S3 instead of the database
-
COCOON_S3_BLOBSTORE_ENABLED=true
-
-
# S3 configuration (works with AWS S3, MinIO, Cloudflare R2, etc.)
-
COCOON_S3_REGION="us-east-1"
-
COCOON_S3_BUCKET="your-bucket"
-
COCOON_S3_ENDPOINT="https://s3.amazonaws.com"
-
COCOON_S3_ACCESS_KEY="your-access-key"
-
COCOON_S3_SECRET_KEY="your-secret-key"
-
-
# Optional: CDN/public URL for blob redirects
-
# When set, com.atproto.sync.getBlob redirects to this URL instead of proxying
-
COCOON_S3_CDN_URL="https://cdn.example.com"
-
```
-
-
**Blob Storage Options:**
-
- `COCOON_S3_BLOBSTORE_ENABLED=false` (default): Blobs stored in the database
-
- `COCOON_S3_BLOBSTORE_ENABLED=true`: Blobs stored in S3 bucket under `blobs/{did}/{cid}`
-
-
**Blob Serving Options:**
-
- Without `COCOON_S3_CDN_URL`: Blobs are proxied through the PDS server
-
- With `COCOON_S3_CDN_URL`: `getBlob` returns a 302 redirect to `{CDN_URL}/blobs/{did}/{cid}`
-
-
> **Tip**: For Cloudflare R2, you can use the public bucket URL as the CDN URL. For AWS S3, you can use CloudFront or the S3 bucket URL directly if public access is enabled.
-
-
### Management Commands
-
-
Create an invite code:
-
```bash
-
docker exec cocoon-pds /cocoon create-invite-code --uses 1
-
```
-
-
Reset a user's password:
-
```bash
-
docker exec cocoon-pds /cocoon reset-password --did "did:plc:xxx"
-
```
-
-
### Updating
-
-
```bash
-
docker-compose pull
-
docker-compose up -d
-
```
-
## Implemented Endpoints
> [!NOTE]
···
### Identity
-
- [x] `com.atproto.identity.getRecommendedDidCredentials`
-
- [x] `com.atproto.identity.requestPlcOperationSignature`
+
- [ ] `com.atproto.identity.getRecommendedDidCredentials`
+
- [ ] `com.atproto.identity.requestPlcOperationSignature`
- [x] `com.atproto.identity.resolveHandle`
-
- [x] `com.atproto.identity.signPlcOperation`
-
- [x] `com.atproto.identity.submitPlcOperation`
+
- [ ] `com.atproto.identity.signPlcOperation`
+
- [ ] `com.atproto.identity.submitPlcOperation`
- [x] `com.atproto.identity.updateHandle`
### Repo
···
- [x] `com.atproto.repo.deleteRecord`
- [x] `com.atproto.repo.describeRepo`
- [x] `com.atproto.repo.getRecord`
-
- [x] `com.atproto.repo.importRepo` (Works "okay". Use with extreme caution.)
+
- [x] `com.atproto.repo.importRepo` (Works "okay". You still have to handle PLC operations on your own when migrating. Use with extreme caution.)
- [x] `com.atproto.repo.listRecords`
-
- [x] `com.atproto.repo.listMissingBlobs`
+
- [ ] `com.atproto.repo.listMissingBlobs`
### Server
···
- [x] `com.atproto.server.createInviteCode`
- [x] `com.atproto.server.createInviteCodes`
- [x] `com.atproto.server.deactivateAccount`
-
- [x] `com.atproto.server.deleteAccount`
+
- [ ] `com.atproto.server.deleteAccount`
- [x] `com.atproto.server.deleteSession`
- [x] `com.atproto.server.describeServer`
- [ ] `com.atproto.server.getAccountInviteCodes`
-
- [x] `com.atproto.server.getServiceAuth`
+
- [ ] `com.atproto.server.getServiceAuth`
- ~~[ ] `com.atproto.server.listAppPasswords`~~ - not going to add app passwords
- [x] `com.atproto.server.refreshSession`
-
- [x] `com.atproto.server.requestAccountDelete`
+
- [ ] `com.atproto.server.requestAccountDelete`
- [x] `com.atproto.server.requestEmailConfirmation`
- [x] `com.atproto.server.requestEmailUpdate`
- [x] `com.atproto.server.requestPasswordReset`
-
- [x] `com.atproto.server.reserveSigningKey`
+
- [ ] `com.atproto.server.reserveSigningKey`
- [x] `com.atproto.server.resetPassword`
- ~~[] `com.atproto.server.revokeAppPassword`~~ - not going to add app passwords
- [x] `com.atproto.server.updateEmail`
···
### Other
-
- [x] `com.atproto.label.queryLabels`
+
- [ ] `com.atproto.label.queryLabels`
- [x] `com.atproto.moderation.createReport` (Note: this should be handled by proxying, not actually implemented in the PDS)
- [x] `app.bsky.actor.getPreferences`
- [x] `app.bsky.actor.putPreferences`
+43 -78
cmd/cocoon/main.go
···
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/bcrypt"
-
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
···
EnvVars: []string{"COCOON_DB_NAME"},
},
&cli.StringFlag{
-
Name: "db-type",
-
Value: "sqlite",
-
Usage: "Database type: sqlite or postgres",
-
EnvVars: []string{"COCOON_DB_TYPE"},
+
Name: "did",
+
Required: true,
+
EnvVars: []string{"COCOON_DID"},
},
&cli.StringFlag{
-
Name: "database-url",
-
Aliases: []string{"db-url"},
-
Usage: "PostgreSQL connection string (required if db-type is postgres)",
-
EnvVars: []string{"COCOON_DATABASE_URL", "DATABASE_URL"},
+
Name: "hostname",
+
Required: true,
+
EnvVars: []string{"COCOON_HOSTNAME"},
},
&cli.StringFlag{
-
Name: "did",
-
EnvVars: []string{"COCOON_DID"},
+
Name: "rotation-key-path",
+
Required: true,
+
EnvVars: []string{"COCOON_ROTATION_KEY_PATH"},
},
&cli.StringFlag{
-
Name: "hostname",
-
EnvVars: []string{"COCOON_HOSTNAME"},
+
Name: "jwk-path",
+
Required: true,
+
EnvVars: []string{"COCOON_JWK_PATH"},
},
&cli.StringFlag{
-
Name: "rotation-key-path",
-
EnvVars: []string{"COCOON_ROTATION_KEY_PATH"},
-
},
-
&cli.StringFlag{
-
Name: "jwk-path",
-
EnvVars: []string{"COCOON_JWK_PATH"},
-
},
-
&cli.StringFlag{
-
Name: "contact-email",
-
EnvVars: []string{"COCOON_CONTACT_EMAIL"},
+
Name: "contact-email",
+
Required: true,
+
EnvVars: []string{"COCOON_CONTACT_EMAIL"},
},
&cli.StringSliceFlag{
-
Name: "relays",
-
EnvVars: []string{"COCOON_RELAYS"},
+
Name: "relays",
+
Required: true,
+
EnvVars: []string{"COCOON_RELAYS"},
},
&cli.StringFlag{
-
Name: "admin-password",
-
EnvVars: []string{"COCOON_ADMIN_PASSWORD"},
-
},
-
&cli.BoolFlag{
-
Name: "require-invite",
-
EnvVars: []string{"COCOON_REQUIRE_INVITE"},
-
Value: true,
+
Name: "admin-password",
+
Required: true,
+
EnvVars: []string{"COCOON_ADMIN_PASSWORD"},
},
&cli.StringFlag{
-
Name: "smtp-user",
-
EnvVars: []string{"COCOON_SMTP_USER"},
+
Name: "smtp-user",
+
Required: false,
+
EnvVars: []string{"COCOON_SMTP_USER"},
},
&cli.StringFlag{
-
Name: "smtp-pass",
-
EnvVars: []string{"COCOON_SMTP_PASS"},
+
Name: "smtp-pass",
+
Required: false,
+
EnvVars: []string{"COCOON_SMTP_PASS"},
},
&cli.StringFlag{
-
Name: "smtp-host",
-
EnvVars: []string{"COCOON_SMTP_HOST"},
+
Name: "smtp-host",
+
Required: false,
+
EnvVars: []string{"COCOON_SMTP_HOST"},
},
&cli.StringFlag{
-
Name: "smtp-port",
-
EnvVars: []string{"COCOON_SMTP_PORT"},
+
Name: "smtp-port",
+
Required: false,
+
EnvVars: []string{"COCOON_SMTP_PORT"},
},
&cli.StringFlag{
-
Name: "smtp-email",
-
EnvVars: []string{"COCOON_SMTP_EMAIL"},
+
Name: "smtp-email",
+
Required: false,
+
EnvVars: []string{"COCOON_SMTP_EMAIL"},
},
&cli.StringFlag{
-
Name: "smtp-name",
-
EnvVars: []string{"COCOON_SMTP_NAME"},
+
Name: "smtp-name",
+
Required: false,
+
EnvVars: []string{"COCOON_SMTP_NAME"},
},
&cli.BoolFlag{
Name: "s3-backups-enabled",
···
EnvVars: []string{"COCOON_S3_SECRET_KEY"},
},
&cli.StringFlag{
-
Name: "s3-cdn-url",
-
EnvVars: []string{"COCOON_S3_CDN_URL"},
-
Usage: "Public URL for S3 blob redirects (e.g., https://cdn.example.com). When set, getBlob redirects to this URL instead of proxying.",
-
},
-
&cli.StringFlag{
Name: "session-secret",
EnvVars: []string{"COCOON_SESSION_SECRET"},
},
···
s, err := server.New(&server.Args{
Addr: cmd.String("addr"),
DbName: cmd.String("db-name"),
-
DbType: cmd.String("db-type"),
-
DatabaseURL: cmd.String("database-url"),
Did: cmd.String("did"),
Hostname: cmd.String("hostname"),
RotationKeyPath: cmd.String("rotation-key-path"),
···
Version: Version,
Relays: cmd.StringSlice("relays"),
AdminPassword: cmd.String("admin-password"),
-
RequireInvite: cmd.Bool("require-invite"),
SmtpUser: cmd.String("smtp-user"),
SmtpPass: cmd.String("smtp-pass"),
SmtpHost: cmd.String("smtp-host"),
···
Endpoint: cmd.String("s3-endpoint"),
AccessKey: cmd.String("s3-access-key"),
SecretKey: cmd.String("s3-secret-key"),
-
CDNUrl: cmd.String("s3-cdn-url"),
},
SessionSecret: cmd.String("session-secret"),
BlockstoreVariant: server.MustReturnBlockstoreVariant(cmd.String("blockstore-variant")),
···
},
},
Action: func(cmd *cli.Context) error {
-
db, err := newDb(cmd)
+
db, err := newDb()
if err != nil {
return err
}
···
},
},
Action: func(cmd *cli.Context) error {
-
db, err := newDb(cmd)
+
db, err := newDb()
if err != nil {
return err
}
···
},
}
-
func newDb(cmd *cli.Context) (*gorm.DB, error) {
-
dbType := cmd.String("db-type")
-
if dbType == "" {
-
dbType = "sqlite"
-
}
-
-
switch dbType {
-
case "postgres":
-
databaseURL := cmd.String("database-url")
-
if databaseURL == "" {
-
databaseURL = cmd.String("database-url")
-
}
-
if databaseURL == "" {
-
return nil, fmt.Errorf("COCOON_DATABASE_URL or DATABASE_URL must be set when using postgres")
-
}
-
return gorm.Open(postgres.Open(databaseURL), &gorm.Config{})
-
default:
-
dbName := cmd.String("db-name")
-
if dbName == "" {
-
dbName = "cocoon.db"
-
}
-
return gorm.Open(sqlite.Open(dbName), &gorm.Config{})
-
}
+
func newDb() (*gorm.DB, error) {
+
return gorm.Open(sqlite.Open("cocoon.db"), &gorm.Config{})
}
-56
create-initial-invite.sh
···
-
#!/bin/sh
-
-
INVITE_FILE="/keys/initial-invite-code.txt"
-
MARKER="/keys/.invite_created"
-
-
# Check if invite code was already created
-
if [ -f "$MARKER" ]; then
-
echo "โœ“ Initial invite code already created"
-
exit 0
-
fi
-
-
echo "Waiting for database to be ready..."
-
sleep 10
-
-
# Try to create invite code - retry until database is ready
-
MAX_ATTEMPTS=30
-
ATTEMPT=0
-
INVITE_CODE=""
-
-
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
-
ATTEMPT=$((ATTEMPT + 1))
-
OUTPUT=$(/cocoon create-invite-code --uses 1 2>&1)
-
INVITE_CODE=$(echo "$OUTPUT" | grep -oE '[a-zA-Z0-9]{8}-[a-zA-Z0-9]{8}' || echo "")
-
-
if [ -n "$INVITE_CODE" ]; then
-
break
-
fi
-
-
if [ $((ATTEMPT % 5)) -eq 0 ]; then
-
echo " Waiting for database... ($ATTEMPT/$MAX_ATTEMPTS)"
-
fi
-
sleep 2
-
done
-
-
if [ -n "$INVITE_CODE" ]; then
-
echo ""
-
echo "โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—"
-
echo "โ•‘ SAVE THIS INVITE CODE! โ•‘"
-
echo "โ•‘ โ•‘"
-
echo "โ•‘ $INVITE_CODE โ•‘"
-
echo "โ•‘ โ•‘"
-
echo "โ•‘ Use this to create your first โ•‘"
-
echo "โ•‘ account on your PDS. โ•‘"
-
echo "โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"
-
echo ""
-
-
echo "$INVITE_CODE" > "$INVITE_FILE"
-
echo "โœ“ Invite code saved to: $INVITE_FILE"
-
-
touch "$MARKER"
-
echo "โœ“ Initial setup complete!"
-
else
-
echo "โœ— Failed to create invite code"
-
echo "Output: $OUTPUT"
-
exit 1
-
fi
-158
docker-compose.postgres.yaml
···
-
# Docker Compose with PostgreSQL
-
#
-
# Usage:
-
# docker-compose -f docker-compose.postgres.yaml up -d
-
#
-
# This file extends the base docker-compose.yaml with a PostgreSQL database.
-
# Set the following in your .env file:
-
# COCOON_DB_TYPE=postgres
-
# POSTGRES_PASSWORD=your-secure-password
-
-
version: '3.8'
-
-
services:
-
postgres:
-
image: postgres:16-alpine
-
container_name: cocoon-postgres
-
environment:
-
POSTGRES_USER: cocoon
-
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
-
POSTGRES_DB: cocoon
-
volumes:
-
- postgres_data:/var/lib/postgresql/data
-
healthcheck:
-
test: ["CMD-SHELL", "pg_isready -U cocoon -d cocoon"]
-
interval: 10s
-
timeout: 5s
-
retries: 5
-
restart: unless-stopped
-
-
init-keys:
-
build:
-
context: .
-
dockerfile: Dockerfile
-
image: ghcr.io/haileyok/cocoon:latest
-
container_name: cocoon-init-keys
-
volumes:
-
- ./keys:/keys
-
- ./data:/data/cocoon
-
- ./init-keys.sh:/init-keys.sh:ro
-
environment:
-
COCOON_DID: ${COCOON_DID}
-
COCOON_HOSTNAME: ${COCOON_HOSTNAME}
-
COCOON_ROTATION_KEY_PATH: /keys/rotation.key
-
COCOON_JWK_PATH: /keys/jwk.key
-
COCOON_CONTACT_EMAIL: ${COCOON_CONTACT_EMAIL}
-
COCOON_RELAYS: ${COCOON_RELAYS:-https://bsky.network}
-
COCOON_ADMIN_PASSWORD: ${COCOON_ADMIN_PASSWORD}
-
entrypoint: ["/bin/sh", "/init-keys.sh"]
-
restart: "no"
-
-
cocoon:
-
build:
-
context: .
-
dockerfile: Dockerfile
-
image: ghcr.io/haileyok/cocoon:latest
-
container_name: cocoon-pds
-
depends_on:
-
init-keys:
-
condition: service_completed_successfully
-
postgres:
-
condition: service_healthy
-
ports:
-
- "8080:8080"
-
volumes:
-
- ./data:/data/cocoon
-
- ./keys/rotation.key:/keys/rotation.key:ro
-
- ./keys/jwk.key:/keys/jwk.key:ro
-
environment:
-
# Required settings
-
COCOON_DID: ${COCOON_DID}
-
COCOON_HOSTNAME: ${COCOON_HOSTNAME}
-
COCOON_ROTATION_KEY_PATH: /keys/rotation.key
-
COCOON_JWK_PATH: /keys/jwk.key
-
COCOON_CONTACT_EMAIL: ${COCOON_CONTACT_EMAIL}
-
COCOON_RELAYS: ${COCOON_RELAYS:-https://bsky.network}
-
COCOON_ADMIN_PASSWORD: ${COCOON_ADMIN_PASSWORD}
-
COCOON_SESSION_SECRET: ${COCOON_SESSION_SECRET}
-
-
# Database configuration - PostgreSQL
-
COCOON_ADDR: ":8080"
-
COCOON_DB_TYPE: postgres
-
COCOON_DATABASE_URL: postgres://cocoon:${POSTGRES_PASSWORD}@postgres:5432/cocoon?sslmode=disable
-
COCOON_BLOCKSTORE_VARIANT: ${COCOON_BLOCKSTORE_VARIANT:-sqlite}
-
-
# Optional: SMTP settings for email
-
COCOON_SMTP_USER: ${COCOON_SMTP_USER:-}
-
COCOON_SMTP_PASS: ${COCOON_SMTP_PASS:-}
-
COCOON_SMTP_HOST: ${COCOON_SMTP_HOST:-}
-
COCOON_SMTP_PORT: ${COCOON_SMTP_PORT:-}
-
COCOON_SMTP_EMAIL: ${COCOON_SMTP_EMAIL:-}
-
COCOON_SMTP_NAME: ${COCOON_SMTP_NAME:-}
-
-
# Optional: S3 configuration
-
COCOON_S3_BACKUPS_ENABLED: ${COCOON_S3_BACKUPS_ENABLED:-false}
-
COCOON_S3_BLOBSTORE_ENABLED: ${COCOON_S3_BLOBSTORE_ENABLED:-false}
-
COCOON_S3_REGION: ${COCOON_S3_REGION:-}
-
COCOON_S3_BUCKET: ${COCOON_S3_BUCKET:-}
-
COCOON_S3_ENDPOINT: ${COCOON_S3_ENDPOINT:-}
-
COCOON_S3_ACCESS_KEY: ${COCOON_S3_ACCESS_KEY:-}
-
COCOON_S3_SECRET_KEY: ${COCOON_S3_SECRET_KEY:-}
-
-
# Optional: Fallback proxy
-
COCOON_FALLBACK_PROXY: ${COCOON_FALLBACK_PROXY:-}
-
restart: unless-stopped
-
healthcheck:
-
test: ["CMD", "curl", "-f", "http://localhost:8080/xrpc/_health"]
-
interval: 30s
-
timeout: 10s
-
retries: 3
-
start_period: 40s
-
-
create-invite:
-
build:
-
context: .
-
dockerfile: Dockerfile
-
image: ghcr.io/haileyok/cocoon:latest
-
container_name: cocoon-create-invite
-
volumes:
-
- ./keys:/keys
-
- ./create-initial-invite.sh:/create-initial-invite.sh:ro
-
environment:
-
COCOON_DID: ${COCOON_DID}
-
COCOON_HOSTNAME: ${COCOON_HOSTNAME}
-
COCOON_ROTATION_KEY_PATH: /keys/rotation.key
-
COCOON_JWK_PATH: /keys/jwk.key
-
COCOON_CONTACT_EMAIL: ${COCOON_CONTACT_EMAIL}
-
COCOON_RELAYS: ${COCOON_RELAYS:-https://bsky.network}
-
COCOON_ADMIN_PASSWORD: ${COCOON_ADMIN_PASSWORD}
-
COCOON_DB_TYPE: postgres
-
COCOON_DATABASE_URL: postgres://cocoon:${POSTGRES_PASSWORD}@postgres:5432/cocoon?sslmode=disable
-
depends_on:
-
cocoon:
-
condition: service_healthy
-
entrypoint: ["/bin/sh", "/create-initial-invite.sh"]
-
restart: "no"
-
-
caddy:
-
image: caddy:2-alpine
-
container_name: cocoon-caddy
-
ports:
-
- "80:80"
-
- "443:443"
-
volumes:
-
- ./Caddyfile.postgres:/etc/caddy/Caddyfile:ro
-
- caddy_data:/data
-
- caddy_config:/config
-
restart: unless-stopped
-
environment:
-
COCOON_HOSTNAME: ${COCOON_HOSTNAME}
-
CADDY_ACME_EMAIL: ${COCOON_CONTACT_EMAIL:-}
-
-
volumes:
-
postgres_data:
-
driver: local
-
caddy_data:
-
driver: local
-
caddy_config:
-
driver: local
-130
docker-compose.yaml
···
-
version: '3.8'
-
-
services:
-
init-keys:
-
build:
-
context: .
-
dockerfile: Dockerfile
-
image: ghcr.io/haileyok/cocoon:latest
-
container_name: cocoon-init-keys
-
volumes:
-
- ./keys:/keys
-
- ./data:/data/cocoon
-
- ./init-keys.sh:/init-keys.sh:ro
-
environment:
-
COCOON_DID: ${COCOON_DID}
-
COCOON_HOSTNAME: ${COCOON_HOSTNAME}
-
COCOON_ROTATION_KEY_PATH: /keys/rotation.key
-
COCOON_JWK_PATH: /keys/jwk.key
-
COCOON_CONTACT_EMAIL: ${COCOON_CONTACT_EMAIL}
-
COCOON_RELAYS: ${COCOON_RELAYS:-https://bsky.network}
-
COCOON_ADMIN_PASSWORD: ${COCOON_ADMIN_PASSWORD}
-
entrypoint: ["/bin/sh", "/init-keys.sh"]
-
restart: "no"
-
-
cocoon:
-
build:
-
context: .
-
dockerfile: Dockerfile
-
image: ghcr.io/haileyok/cocoon:latest
-
container_name: cocoon-pds
-
network_mode: host
-
depends_on:
-
init-keys:
-
condition: service_completed_successfully
-
volumes:
-
- ./data:/data/cocoon
-
- ./keys/rotation.key:/keys/rotation.key:ro
-
- ./keys/jwk.key:/keys/jwk.key:ro
-
environment:
-
# Required settings
-
COCOON_DID: ${COCOON_DID}
-
COCOON_HOSTNAME: ${COCOON_HOSTNAME}
-
COCOON_ROTATION_KEY_PATH: /keys/rotation.key
-
COCOON_JWK_PATH: /keys/jwk.key
-
COCOON_CONTACT_EMAIL: ${COCOON_CONTACT_EMAIL}
-
COCOON_RELAYS: ${COCOON_RELAYS:-https://bsky.network}
-
COCOON_ADMIN_PASSWORD: ${COCOON_ADMIN_PASSWORD}
-
COCOON_SESSION_SECRET: ${COCOON_SESSION_SECRET}
-
-
# Server configuration
-
COCOON_ADDR: ":8080"
-
COCOON_DB_TYPE: ${COCOON_DB_TYPE:-sqlite}
-
COCOON_DB_NAME: ${COCOON_DB_NAME:-/data/cocoon/cocoon.db}
-
COCOON_DATABASE_URL: ${COCOON_DATABASE_URL:-}
-
COCOON_BLOCKSTORE_VARIANT: ${COCOON_BLOCKSTORE_VARIANT:-sqlite}
-
-
# Optional: SMTP settings for email
-
COCOON_SMTP_USER: ${COCOON_SMTP_USER:-}
-
COCOON_SMTP_PASS: ${COCOON_SMTP_PASS:-}
-
COCOON_SMTP_HOST: ${COCOON_SMTP_HOST:-}
-
COCOON_SMTP_PORT: ${COCOON_SMTP_PORT:-}
-
COCOON_SMTP_EMAIL: ${COCOON_SMTP_EMAIL:-}
-
COCOON_SMTP_NAME: ${COCOON_SMTP_NAME:-}
-
-
# Optional: S3 configuration
-
COCOON_S3_BACKUPS_ENABLED: ${COCOON_S3_BACKUPS_ENABLED:-false}
-
COCOON_S3_BLOBSTORE_ENABLED: ${COCOON_S3_BLOBSTORE_ENABLED:-false}
-
COCOON_S3_REGION: ${COCOON_S3_REGION:-}
-
COCOON_S3_BUCKET: ${COCOON_S3_BUCKET:-}
-
COCOON_S3_ENDPOINT: ${COCOON_S3_ENDPOINT:-}
-
COCOON_S3_ACCESS_KEY: ${COCOON_S3_ACCESS_KEY:-}
-
COCOON_S3_SECRET_KEY: ${COCOON_S3_SECRET_KEY:-}
-
COCOON_S3_CDN_URL: ${COCOON_S3_CDN_URL:-}
-
-
# Optional: Fallback proxy
-
COCOON_FALLBACK_PROXY: ${COCOON_FALLBACK_PROXY:-}
-
restart: unless-stopped
-
healthcheck:
-
test: ["CMD", "curl", "-f", "http://localhost:8080/xrpc/_health"]
-
interval: 30s
-
timeout: 10s
-
retries: 3
-
start_period: 40s
-
-
create-invite:
-
build:
-
context: .
-
dockerfile: Dockerfile
-
image: ghcr.io/haileyok/cocoon:latest
-
container_name: cocoon-create-invite
-
network_mode: host
-
volumes:
-
- ./keys:/keys
-
- ./create-initial-invite.sh:/create-initial-invite.sh:ro
-
environment:
-
COCOON_DID: ${COCOON_DID}
-
COCOON_HOSTNAME: ${COCOON_HOSTNAME}
-
COCOON_ROTATION_KEY_PATH: /keys/rotation.key
-
COCOON_JWK_PATH: /keys/jwk.key
-
COCOON_CONTACT_EMAIL: ${COCOON_CONTACT_EMAIL}
-
COCOON_RELAYS: ${COCOON_RELAYS:-https://bsky.network}
-
COCOON_ADMIN_PASSWORD: ${COCOON_ADMIN_PASSWORD}
-
COCOON_DB_TYPE: ${COCOON_DB_TYPE:-sqlite}
-
COCOON_DB_NAME: ${COCOON_DB_NAME:-/data/cocoon/cocoon.db}
-
COCOON_DATABASE_URL: ${COCOON_DATABASE_URL:-}
-
depends_on:
-
- init-keys
-
entrypoint: ["/bin/sh", "/create-initial-invite.sh"]
-
restart: "no"
-
-
caddy:
-
image: caddy:2-alpine
-
container_name: cocoon-caddy
-
network_mode: host
-
volumes:
-
- ./Caddyfile:/etc/caddy/Caddyfile:ro
-
- caddy_data:/data
-
- caddy_config:/config
-
restart: unless-stopped
-
environment:
-
COCOON_HOSTNAME: ${COCOON_HOSTNAME}
-
CADDY_ACME_EMAIL: ${COCOON_CONTACT_EMAIL:-}
-
-
volumes:
-
data:
-
driver: local
-
caddy_data:
-
driver: local
-
caddy_config:
-
driver: local
+1 -1
identity/types.go
···
Context []string `json:"@context"`
Id string `json:"id"`
AlsoKnownAs []string `json:"alsoKnownAs"`
-
VerificationMethods []DidDocVerificationMethod `json:"verificationMethod"`
+
VerificationMethods []DidDocVerificationMethod `json:"verificationMethods"`
Service []DidDocService `json:"service"`
}
-34
init-keys.sh
···
-
#!/bin/sh
-
set -e
-
-
mkdir -p /keys
-
mkdir -p /data/cocoon
-
-
if [ ! -f /keys/rotation.key ]; then
-
echo "Generating rotation key..."
-
/cocoon create-rotation-key --out /keys/rotation.key 2>/dev/null || true
-
if [ -f /keys/rotation.key ]; then
-
echo "โœ“ Rotation key generated at /keys/rotation.key"
-
else
-
echo "โœ— Failed to generate rotation key"
-
exit 1
-
fi
-
else
-
echo "โœ“ Rotation key already exists"
-
fi
-
-
if [ ! -f /keys/jwk.key ]; then
-
echo "Generating JWK..."
-
/cocoon create-private-jwk --out /keys/jwk.key 2>/dev/null || true
-
if [ -f /keys/jwk.key ]; then
-
echo "โœ“ JWK generated at /keys/jwk.key"
-
else
-
echo "โœ— Failed to generate JWK"
-
exit 1
-
fi
-
else
-
echo "โœ“ JWK already exists"
-
fi
-
-
echo ""
-
echo "โœ“ Key initialization complete!"
-6
internal/db/db.go
···
return db.cli.Clauses(clauses...).Create(value)
}
-
func (db *DB) Save(value any, clauses []clause.Expression) *gorm.DB {
-
db.mu.Lock()
-
defer db.mu.Unlock()
-
return db.cli.Clauses(clauses...).Save(value)
-
}
-
func (db *DB) Exec(sql string, clauses []clause.Expression, values ...any) *gorm.DB {
db.mu.Lock()
defer db.mu.Unlock()
-16
internal/helpers/helpers.go
···
return genericError(e, 400, msg)
}
-
func UnauthorizedError(e echo.Context, suffix *string) error {
-
msg := "Unauthorized"
-
if suffix != nil {
-
msg += ". " + *suffix
-
}
-
return genericError(e, 401, msg)
-
}
-
-
func ForbiddenError(e echo.Context, suffix *string) error {
-
msg := "Forbidden"
-
if suffix != nil {
-
msg += ". " + *suffix
-
}
-
return genericError(e, 403, msg)
-
}
-
func InvalidTokenError(e echo.Context) error {
return InputError(e, to.StringPtr("InvalidToken"))
}
+1 -12
models/models.go
···
EmailUpdateCodeExpiresAt *time.Time
PasswordResetCode *string
PasswordResetCodeExpiresAt *time.Time
-
PlcOperationCode *string
-
PlcOperationCodeExpiresAt *time.Time
-
AccountDeleteCode *string
-
AccountDeleteCodeExpiresAt *time.Time
Password string
SigningKey []byte
Rev string
···
Did string `gorm:"index;index:idx_blob_did_cid"`
Cid []byte `gorm:"index;index:idx_blob_did_cid"`
RefCount int
-
Storage string `gorm:"default:sqlite"`
+
Storage string `gorm:"default:sqlite;check:storage in ('sqlite', 's3')"`
}
type BlobPart struct {
···
Idx int `gorm:"primaryKey"`
Data []byte
}
-
-
type ReservedKey struct {
-
KeyDid string `gorm:"primaryKey"`
-
Did *string `gorm:"index"`
-
PrivateKey []byte
-
CreatedAt time.Time `gorm:"index"`
-
}
+28 -44
oauth/client/manager.go
···
cli *http.Client
logger *slog.Logger
jwksCache cache.Cache[string, jwk.Key]
-
metadataCache cache.Cache[string, *Metadata]
+
metadataCache cache.Cache[string, Metadata]
}
type ManagerArgs struct {
···
}
jwksCache := cache.NewCache[string, jwk.Key]().WithLRU().WithMaxKeys(500).WithTTL(5 * time.Minute)
-
metadataCache := cache.NewCache[string, *Metadata]().WithLRU().WithMaxKeys(500).WithTTL(5 * time.Minute)
+
metadataCache := cache.NewCache[string, Metadata]().WithLRU().WithMaxKeys(500).WithTTL(5 * time.Minute)
return &Manager{
cli: args.Cli,
···
}
var jwks jwk.Key
-
if metadata.TokenEndpointAuthMethod == "private_key_jwt" {
-
if metadata.JWKS != nil && len(metadata.JWKS.Keys) > 0 {
-
// TODO: this is kinda bad but whatever for now. there could obviously be more than one jwk, and we need to
-
// make sure we use the right one
-
b, err := json.Marshal(metadata.JWKS.Keys[0])
-
if err != nil {
-
return nil, err
-
}
-
-
k, err := helpers.ParseJWKFromBytes(b)
-
if err != nil {
-
return nil, err
-
}
+
if metadata.JWKS != nil && len(metadata.JWKS.Keys) > 0 {
+
// TODO: this is kinda bad but whatever for now. there could obviously be more than one jwk, and we need to
+
// make sure we use the right one
+
b, err := json.Marshal(metadata.JWKS.Keys[0])
+
if err != nil {
+
return nil, err
+
}
-
jwks = k
-
} else if metadata.JWKS != nil {
-
} else if metadata.JWKSURI != nil {
-
maybeJwks, err := cm.getClientJwks(ctx, clientId, *metadata.JWKSURI)
-
if err != nil {
-
return nil, err
-
}
+
k, err := helpers.ParseJWKFromBytes(b)
+
if err != nil {
+
return nil, err
+
}
-
jwks = maybeJwks
-
} else {
-
return nil, fmt.Errorf("no valid jwks found in oauth client metadata")
+
jwks = k
+
} else if metadata.JWKSURI != nil {
+
maybeJwks, err := cm.getClientJwks(ctx, clientId, *metadata.JWKSURI)
+
if err != nil {
+
return nil, err
}
+
+
jwks = maybeJwks
+
} else {
+
return nil, fmt.Errorf("no valid jwks found in oauth client metadata")
}
return &Client{
···
}
func (cm *Manager) getClientMetadata(ctx context.Context, clientId string) (*Metadata, error) {
-
cached, ok := cm.metadataCache.Get(clientId)
+
metadataCached, ok := cm.metadataCache.Get(clientId)
if !ok {
req, err := http.NewRequestWithContext(ctx, "GET", clientId, nil)
if err != nil {
···
return nil, err
}
-
cm.metadataCache.Set(clientId, validated, 10*time.Minute)
-
return validated, nil
} else {
-
return cached, nil
+
return &metadataCached, nil
}
}
···
return nil, fmt.Errorf("error unmarshaling metadata: %w", err)
}
-
if metadata.ClientURI == "" {
-
u, err := url.Parse(metadata.ClientID)
-
if err != nil {
-
return nil, fmt.Errorf("unable to parse client id: %w", err)
-
}
-
u.RawPath = ""
-
u.RawQuery = ""
-
metadata.ClientURI = u.String()
-
}
-
u, err := url.Parse(metadata.ClientURI)
if err != nil {
return nil, fmt.Errorf("unable to parse client uri: %w", err)
}
-
if metadata.ClientName == "" {
-
metadata.ClientName = metadata.ClientURI
-
}
-
if isLocalHostname(u.Hostname()) {
-
return nil, fmt.Errorf("`client_uri` hostname is invalid: %s", u.Hostname())
+
return nil, errors.New("`client_uri` hostname is invalid")
}
if metadata.Scope == "" {
···
if u.Scheme != "http" {
return nil, fmt.Errorf("loopback redirect uri %s must use http", ruri)
}
+
+
break
case u.Scheme == "http":
return nil, errors.New("only loopbvack redirect uris are allowed to use the `http` scheme")
case u.Scheme == "https":
if isLocalHostname(u.Hostname()) {
return nil, fmt.Errorf("redirect uri %s's domain must not be a local hostname", ruri)
}
+
break
case strings.Contains(u.Scheme, "."):
if metadata.ApplicationType != "native" {
return nil, errors.New("private-use uri scheme redirect uris are only allowed for native apps")
+2 -3
oauth/dpop/nonce.go
···
}
func (n *Nonce) Check(nonce string) bool {
-
n.mu.Lock()
-
defer n.mu.Unlock()
-
n.rotate()
+
n.mu.RLock()
+
defer n.mu.RUnlock()
return nonce == n.prev || nonce == n.curr || nonce == n.next
}
+15 -31
plc/client.go
···
}
func (c *Client) CreateDID(sigkey *atcrypto.PrivateKeyK256, recovery string, handle string) (string, *Operation, error) {
-
creds, err := c.CreateDidCredentials(sigkey, recovery, handle)
-
if err != nil {
-
return "", nil, err
-
}
-
-
op := Operation{
-
Type: "plc_operation",
-
VerificationMethods: creds.VerificationMethods,
-
RotationKeys: creds.RotationKeys,
-
AlsoKnownAs: creds.AlsoKnownAs,
-
Services: creds.Services,
-
Prev: nil,
-
}
-
-
if err := c.SignOp(sigkey, &op); err != nil {
-
return "", nil, err
-
}
-
-
did, err := DidFromOp(&op)
-
if err != nil {
-
return "", nil, err
-
}
-
-
return did, &op, nil
-
}
-
-
func (c *Client) CreateDidCredentials(sigkey *atcrypto.PrivateKeyK256, recovery string, handle string) (*DidCredentials, error) {
pubsigkey, err := sigkey.PublicKey()
if err != nil {
-
return nil, err
+
return "", nil, err
}
pubrotkey, err := c.rotationKey.PublicKey()
if err != nil {
-
return nil, err
+
return "", nil, err
}
// todo
···
}(recovery)
}
-
creds := DidCredentials{
+
op := Operation{
+
Type: "plc_operation",
VerificationMethods: map[string]string{
"atproto": pubsigkey.DIDKey(),
},
···
Endpoint: "https://" + c.pdsHostname,
},
},
+
Prev: nil,
}
-
return &creds, nil
+
if err := c.SignOp(sigkey, &op); err != nil {
+
return "", nil, err
+
}
+
+
did, err := DidFromOp(&op)
+
if err != nil {
+
return "", nil, err
+
}
+
+
return did, &op, nil
}
func (c *Client) SignOp(sigkey *atcrypto.PrivateKeyK256, op *Operation) error {
-8
plc/types.go
···
cbg "github.com/whyrusleeping/cbor-gen"
)
-
-
type DidCredentials struct {
-
VerificationMethods map[string]string `json:"verificationMethods"`
-
RotationKeys []string `json:"rotationKeys"`
-
AlsoKnownAs []string `json:"alsoKnownAs"`
-
Services map[string]identity.OperationService `json:"services"`
-
}
-
type Operation struct {
Type string `json:"type"`
VerificationMethods map[string]string `json:"verificationMethods"`
-24
server/handle_identity_get_recommended_did_credentials.go
···
-
package server
-
-
import (
-
"github.com/bluesky-social/indigo/atproto/atcrypto"
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/haileyok/cocoon/models"
-
"github.com/labstack/echo/v4"
-
)
-
-
func (s *Server) handleGetRecommendedDidCredentials(e echo.Context) error {
-
repo := e.Get("repo").(*models.RepoActor)
-
k, err := atcrypto.ParsePrivateBytesK256(repo.SigningKey)
-
if err != nil {
-
s.logger.Error("error parsing key", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
creds, err := s.plcClient.CreateDidCredentials(k, "", repo.Actor.Handle)
-
if err != nil {
-
s.logger.Error("error crating did credentials", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
return e.JSON(200, creds)
-
}
-29
server/handle_identity_request_plc_operation.go
···
-
package server
-
-
import (
-
"fmt"
-
"time"
-
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/haileyok/cocoon/models"
-
"github.com/labstack/echo/v4"
-
)
-
-
func (s *Server) handleIdentityRequestPlcOperationSignature(e echo.Context) error {
-
urepo := e.Get("repo").(*models.RepoActor)
-
-
code := fmt.Sprintf("%s-%s", helpers.RandomVarchar(5), helpers.RandomVarchar(5))
-
eat := time.Now().Add(10 * time.Minute).UTC()
-
-
if err := s.db.Exec("UPDATE repos SET plc_operation_code = ?, plc_operation_code_expires_at = ? WHERE did = ?", nil, code, eat, urepo.Repo.Did).Error; err != nil {
-
s.logger.Error("error updating user", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := s.sendPlcTokenReset(urepo.Email, urepo.Handle, code); err != nil {
-
s.logger.Error("error sending mail", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
return e.NoContent(200)
-
}
-103
server/handle_identity_sign_plc_operation.go
···
-
package server
-
-
import (
-
"context"
-
"strings"
-
"time"
-
-
"github.com/Azure/go-autorest/autorest/to"
-
"github.com/bluesky-social/indigo/atproto/atcrypto"
-
"github.com/haileyok/cocoon/identity"
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/haileyok/cocoon/models"
-
"github.com/haileyok/cocoon/plc"
-
"github.com/labstack/echo/v4"
-
)
-
-
type ComAtprotoSignPlcOperationRequest struct {
-
Token string `json:"token"`
-
VerificationMethods *map[string]string `json:"verificationMethods"`
-
RotationKeys *[]string `json:"rotationKeys"`
-
AlsoKnownAs *[]string `json:"alsoKnownAs"`
-
Services *map[string]identity.OperationService `json:"services"`
-
}
-
-
type ComAtprotoSignPlcOperationResponse struct {
-
Operation plc.Operation `json:"operation"`
-
}
-
-
func (s *Server) handleSignPlcOperation(e echo.Context) error {
-
repo := e.Get("repo").(*models.RepoActor)
-
-
var req ComAtprotoSignPlcOperationRequest
-
if err := e.Bind(&req); err != nil {
-
s.logger.Error("error binding", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if !strings.HasPrefix(repo.Repo.Did, "did:plc:") {
-
return helpers.InputError(e, nil)
-
}
-
-
if repo.PlcOperationCode == nil || repo.PlcOperationCodeExpiresAt == nil {
-
return helpers.InputError(e, to.StringPtr("InvalidToken"))
-
}
-
-
if *repo.PlcOperationCode != req.Token {
-
return helpers.InvalidTokenError(e)
-
}
-
-
if time.Now().UTC().After(*repo.PlcOperationCodeExpiresAt) {
-
return helpers.ExpiredTokenError(e)
-
}
-
-
ctx := context.WithValue(e.Request().Context(), "skip-cache", true)
-
log, err := identity.FetchDidAuditLog(ctx, nil, repo.Repo.Did)
-
if err != nil {
-
s.logger.Error("error fetching doc", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
latest := log[len(log)-1]
-
-
op := plc.Operation{
-
Type: "plc_operation",
-
VerificationMethods: latest.Operation.VerificationMethods,
-
RotationKeys: latest.Operation.RotationKeys,
-
AlsoKnownAs: latest.Operation.AlsoKnownAs,
-
Services: latest.Operation.Services,
-
Prev: &latest.Cid,
-
}
-
if req.VerificationMethods != nil {
-
op.VerificationMethods = *req.VerificationMethods
-
}
-
if req.RotationKeys != nil {
-
op.RotationKeys = *req.RotationKeys
-
}
-
if req.AlsoKnownAs != nil {
-
op.AlsoKnownAs = *req.AlsoKnownAs
-
}
-
if req.Services != nil {
-
op.Services = *req.Services
-
}
-
-
k, err := atcrypto.ParsePrivateBytesK256(repo.SigningKey)
-
if err != nil {
-
s.logger.Error("error parsing signing key", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := s.plcClient.SignOp(k, &op); err != nil {
-
s.logger.Error("error signing plc operation", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := s.db.Exec("UPDATE repos SET plc_operation_code = NULL, plc_operation_code_expires_at = NULL WHERE did = ?", nil, repo.Repo.Did).Error; err != nil {
-
s.logger.Error("error updating repo", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
return e.JSON(200, ComAtprotoSignPlcOperationResponse{
-
Operation: op,
-
})
-
}
-87
server/handle_identity_submit_plc_operation.go
···
-
package server
-
-
import (
-
"context"
-
"slices"
-
"strings"
-
"time"
-
-
"github.com/bluesky-social/indigo/api/atproto"
-
"github.com/bluesky-social/indigo/atproto/atcrypto"
-
"github.com/bluesky-social/indigo/events"
-
"github.com/bluesky-social/indigo/util"
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/haileyok/cocoon/models"
-
"github.com/haileyok/cocoon/plc"
-
"github.com/labstack/echo/v4"
-
)
-
-
type ComAtprotoSubmitPlcOperationRequest struct {
-
Operation plc.Operation `json:"operation"`
-
}
-
-
func (s *Server) handleSubmitPlcOperation(e echo.Context) error {
-
repo := e.Get("repo").(*models.RepoActor)
-
-
var req ComAtprotoSubmitPlcOperationRequest
-
if err := e.Bind(&req); err != nil {
-
s.logger.Error("error binding", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := e.Validate(req); err != nil {
-
return helpers.InputError(e, nil)
-
}
-
if !strings.HasPrefix(repo.Repo.Did, "did:plc:") {
-
return helpers.InputError(e, nil)
-
}
-
-
op := req.Operation
-
-
k, err := atcrypto.ParsePrivateBytesK256(repo.SigningKey)
-
if err != nil {
-
s.logger.Error("error parsing key", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
required, err := s.plcClient.CreateDidCredentials(k, "", repo.Actor.Handle)
-
if err != nil {
-
s.logger.Error("error crating did credentials", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
for _, expectedKey := range required.RotationKeys {
-
if !slices.Contains(op.RotationKeys, expectedKey) {
-
return helpers.InputError(e, nil)
-
}
-
}
-
if op.Services["atproto_pds"].Type != "AtprotoPersonalDataServer" {
-
return helpers.InputError(e, nil)
-
}
-
if op.Services["atproto_pds"].Endpoint != required.Services["atproto_pds"].Endpoint {
-
return helpers.InputError(e, nil)
-
}
-
if op.VerificationMethods["atproto"] != required.VerificationMethods["atproto"] {
-
return helpers.InputError(e, nil)
-
}
-
if op.AlsoKnownAs[0] != required.AlsoKnownAs[0] {
-
return helpers.InputError(e, nil)
-
}
-
-
if err := s.plcClient.SendOperation(e.Request().Context(), repo.Repo.Did, &op); err != nil {
-
return err
-
}
-
-
if err := s.passport.BustDoc(context.TODO(), repo.Repo.Did); err != nil {
-
s.logger.Warn("error busting did doc", "error", err)
-
}
-
-
s.evtman.AddEvent(context.TODO(), &events.XRPCStreamEvent{
-
RepoIdentity: &atproto.SyncSubscribeRepos_Identity{
-
Did: repo.Repo.Did,
-
Seq: time.Now().UnixMicro(), // TODO: no
-
Time: time.Now().Format(util.ISO8601),
-
},
-
})
-
-
return nil
-
}
+1 -1
server/handle_import_repo.go
···
Value: b.RawData(),
}
-
if err := tx.Save(rec).Error; err != nil {
+
if err := tx.Create(rec).Error; err != nil {
return err
}
-34
server/handle_label_query_labels.go
···
-
package server
-
-
import (
-
"github.com/labstack/echo/v4"
-
)
-
-
type Label struct {
-
Ver *int `json:"ver,omitempty"`
-
Src string `json:"src"`
-
Uri string `json:"uri"`
-
Cid *string `json:"cid,omitempty"`
-
Val string `json:"val"`
-
Neg *bool `json:"neg,omitempty"`
-
Cts string `json:"cts"`
-
Exp *string `json:"exp,omitempty"`
-
Sig []byte `json:"sig,omitempty"`
-
}
-
-
type ComAtprotoLabelQueryLabelsResponse struct {
-
Cursor *string `json:"cursor,omitempty"`
-
Labels []Label `json:"labels"`
-
}
-
-
func (s *Server) handleLabelQueryLabels(e echo.Context) error {
-
svc := e.Request().Header.Get("atproto-proxy")
-
if svc != "" || s.config.FallbackProxy != "" {
-
return s.handleProxy(e)
-
}
-
-
return e.JSON(200, ComAtprotoLabelQueryLabelsResponse{
-
Cursor: nil,
-
Labels: []Label{},
-
})
-
}
-5
server/handle_oauth_par.go
···
dpopProof, err := s.oauthProvider.DpopManager.CheckProof(e.Request().Method, "https://"+s.config.Hostname+e.Request().URL.String(), e.Request().Header, nil)
if err != nil {
if errors.Is(err, dpop.ErrUseDpopNonce) {
-
nonce := s.oauthProvider.NextNonce()
-
if nonce != "" {
-
e.Response().Header().Set("DPoP-Nonce", nonce)
-
e.Response().Header().Add("access-control-expose-headers", "DPoP-Nonce")
-
}
return e.JSON(400, map[string]string{
"error": "use_dpop_nonce",
})
-5
server/handle_oauth_token.go
···
proof, err := s.oauthProvider.DpopManager.CheckProof(e.Request().Method, e.Request().URL.String(), e.Request().Header, nil)
if err != nil {
if errors.Is(err, dpop.ErrUseDpopNonce) {
-
nonce := s.oauthProvider.NextNonce()
-
if nonce != "" {
-
e.Response().Header().Set("DPoP-Nonce", nonce)
-
e.Response().Header().Add("access-control-expose-headers", "DPoP-Nonce")
-
}
return e.JSON(400, map[string]string{
"error": "use_dpop_nonce",
})
+2 -15
server/handle_proxy.go
···
encheader := strings.TrimRight(base64.RawURLEncoding.EncodeToString(hj), "=")
-
// When proxying app.bsky.feed.getFeed the token is actually issued for the
-
// underlying feed generator and the app view passes it on. This allows the
-
// getFeed implementation to pass in the desired lxm and aud for the token
-
// and then just delegate to the general proxying logic
-
lxm, proxyTokenLxmExists := e.Get("proxyTokenLxm").(string)
-
if !proxyTokenLxmExists || lxm == "" {
-
lxm = pts[2]
-
}
-
aud, proxyTokenAudExists := e.Get("proxyTokenAud").(string)
-
if !proxyTokenAudExists || aud == "" {
-
aud = svcDid
-
}
-
payload := map[string]any{
"iss": repo.Repo.Did,
-
"aud": aud,
-
"lxm": lxm,
+
"aud": svcDid,
+
"lxm": pts[2],
"jti": uuid.NewString(),
"exp": time.Now().Add(1 * time.Minute).UTC().Unix(),
}
-35
server/handle_proxy_get_feed.go
···
-
package server
-
-
import (
-
"github.com/Azure/go-autorest/autorest/to"
-
"github.com/bluesky-social/indigo/api/atproto"
-
"github.com/bluesky-social/indigo/api/bsky"
-
"github.com/bluesky-social/indigo/atproto/syntax"
-
"github.com/bluesky-social/indigo/xrpc"
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/labstack/echo/v4"
-
)
-
-
func (s *Server) handleProxyBskyFeedGetFeed(e echo.Context) error {
-
feedUri, err := syntax.ParseATURI(e.QueryParam("feed"))
-
if err != nil {
-
return helpers.InputError(e, to.StringPtr("invalid feed uri"))
-
}
-
-
appViewEndpoint, _, err := s.getAtprotoProxyEndpointFromRequest(e)
-
if err != nil {
-
e.Logger().Error("could not get atproto proxy", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
appViewClient := xrpc.Client{
-
Host: appViewEndpoint,
-
}
-
feedRecord, err := atproto.RepoGetRecord(e.Request().Context(), &appViewClient, "", feedUri.Collection().String(), feedUri.Authority().String(), feedUri.RecordKey().String())
-
feedGeneratorDid := feedRecord.Value.Val.(*bsky.FeedGenerator).Did
-
-
e.Set("proxyTokenLxm", "app.bsky.feed.getFeedSkeleton")
-
e.Set("proxyTokenAud", feedGeneratorDid)
-
-
return s.handleProxy(e)
-
}
-112
server/handle_repo_list_missing_blobs.go
···
-
package server
-
-
import (
-
"fmt"
-
"strconv"
-
-
"github.com/bluesky-social/indigo/atproto/atdata"
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/haileyok/cocoon/models"
-
"github.com/ipfs/go-cid"
-
"github.com/labstack/echo/v4"
-
)
-
-
type ComAtprotoRepoListMissingBlobsResponse struct {
-
Cursor *string `json:"cursor,omitempty"`
-
Blobs []ComAtprotoRepoListMissingBlobsRecordBlob `json:"blobs"`
-
}
-
-
type ComAtprotoRepoListMissingBlobsRecordBlob struct {
-
Cid string `json:"cid"`
-
RecordUri string `json:"recordUri"`
-
}
-
-
func (s *Server) handleListMissingBlobs(e echo.Context) error {
-
urepo := e.Get("repo").(*models.RepoActor)
-
-
limitStr := e.QueryParam("limit")
-
cursor := e.QueryParam("cursor")
-
-
limit := 500
-
if limitStr != "" {
-
if l, err := strconv.Atoi(limitStr); err == nil && l > 0 && l <= 1000 {
-
limit = l
-
}
-
}
-
-
var records []models.Record
-
if err := s.db.Raw("SELECT * FROM records WHERE did = ?", nil, urepo.Repo.Did).Scan(&records).Error; err != nil {
-
s.logger.Error("failed to get records for listMissingBlobs", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
type blobRef struct {
-
cid cid.Cid
-
recordUri string
-
}
-
var allBlobRefs []blobRef
-
-
for _, rec := range records {
-
blobs := getBlobsFromRecord(rec.Value)
-
recordUri := fmt.Sprintf("at://%s/%s/%s", urepo.Repo.Did, rec.Nsid, rec.Rkey)
-
for _, b := range blobs {
-
allBlobRefs = append(allBlobRefs, blobRef{cid: cid.Cid(b.Ref), recordUri: recordUri})
-
}
-
}
-
-
missingBlobs := make([]ComAtprotoRepoListMissingBlobsRecordBlob, 0)
-
seenCids := make(map[string]bool)
-
-
for _, ref := range allBlobRefs {
-
cidStr := ref.cid.String()
-
-
if seenCids[cidStr] {
-
continue
-
}
-
-
if cursor != "" && cidStr <= cursor {
-
continue
-
}
-
-
var count int64
-
if err := s.db.Raw("SELECT COUNT(*) FROM blobs WHERE did = ? AND cid = ?", nil, urepo.Repo.Did, ref.cid.Bytes()).Scan(&count).Error; err != nil {
-
continue
-
}
-
-
if count == 0 {
-
missingBlobs = append(missingBlobs, ComAtprotoRepoListMissingBlobsRecordBlob{
-
Cid: cidStr,
-
RecordUri: ref.recordUri,
-
})
-
seenCids[cidStr] = true
-
-
if len(missingBlobs) >= limit {
-
break
-
}
-
}
-
}
-
-
var nextCursor *string
-
if len(missingBlobs) > 0 && len(missingBlobs) >= limit {
-
lastCid := missingBlobs[len(missingBlobs)-1].Cid
-
nextCursor = &lastCid
-
}
-
-
return e.JSON(200, ComAtprotoRepoListMissingBlobsResponse{
-
Cursor: nextCursor,
-
Blobs: missingBlobs,
-
})
-
}
-
-
func getBlobsFromRecord(data []byte) []atdata.Blob {
-
if len(data) == 0 {
-
return nil
-
}
-
-
decoded, err := atdata.UnmarshalCBOR(data)
-
if err != nil {
-
return nil
-
}
-
-
return atdata.ExtractBlobs(decoded)
-
}
+50 -92
server/handle_server_create_account.go
···
"github.com/Azure/go-autorest/autorest/to"
"github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/atproto/atcrypto"
+
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/events"
"github.com/bluesky-social/indigo/repo"
"github.com/bluesky-social/indigo/util"
···
Handle string `json:"handle" validate:"required,atproto-handle"`
Did *string `json:"did" validate:"atproto-did"`
Password string `json:"password" validate:"required"`
-
InviteCode string `json:"inviteCode" validate:"omitempty"`
+
InviteCode string `json:"inviteCode" validate:"required"`
}
type ComAtprotoServerCreateAccountResponse struct {
···
func (s *Server) handleCreateAccount(e echo.Context) error {
var request ComAtprotoServerCreateAccountRequest
+
var signupDid string
+
customDidHeader := e.Request().Header.Get("authorization")
+
if customDidHeader != "" {
+
pts := strings.Split(customDidHeader, " ")
+
if len(pts) != 2 {
+
return helpers.InputError(e, to.StringPtr("InvalidDid"))
+
}
+
+
_, err := syntax.ParseDID(pts[1])
+
if err != nil {
+
return helpers.InputError(e, to.StringPtr("InvalidDid"))
+
}
+
+
signupDid = pts[1]
+
}
+
if err := e.Bind(&request); err != nil {
s.logger.Error("error receiving request", "endpoint", "com.atproto.server.createAccount", "error", err)
return helpers.ServerError(e, nil)
···
}
}
}
-
-
var signupDid string
-
if request.Did != nil {
-
signupDid = *request.Did;
-
-
token := strings.TrimSpace(strings.Replace(e.Request().Header.Get("authorization"), "Bearer ", "", 1))
-
if token == "" {
-
return helpers.UnauthorizedError(e, to.StringPtr("must authenticate to use an existing did"))
-
}
-
authDid, err := s.validateServiceAuth(e.Request().Context(), token, "com.atproto.server.createAccount")
-
-
if err != nil {
-
s.logger.Warn("error validating authorization token", "endpoint", "com.atproto.server.createAccount", "error", err)
-
return helpers.UnauthorizedError(e, to.StringPtr("invalid authorization token"))
-
}
-
-
if authDid != signupDid {
-
return helpers.ForbiddenError(e, to.StringPtr("auth did did not match signup did"))
-
}
-
}
// see if the handle is already taken
-
actor, err := s.getActorByHandle(request.Handle)
+
_, err := s.getActorByHandle(request.Handle)
if err != nil && err != gorm.ErrRecordNotFound {
s.logger.Error("error looking up handle in db", "endpoint", "com.atproto.server.createAccount", "error", err)
return helpers.ServerError(e, nil)
}
-
if err == nil && actor.Did != signupDid {
+
if err == nil {
return helpers.InputError(e, to.StringPtr("HandleNotAvailable"))
}
-
if did, err := s.passport.ResolveHandle(e.Request().Context(), request.Handle); err == nil && did != signupDid {
+
if did, err := s.passport.ResolveHandle(e.Request().Context(), request.Handle); err == nil && did != "" {
return helpers.InputError(e, to.StringPtr("HandleNotAvailable"))
}
var ic models.InviteCode
-
if s.config.RequireInvite {
-
if strings.TrimSpace(request.InviteCode) == "" {
+
if err := s.db.Raw("SELECT * FROM invite_codes WHERE code = ?", nil, request.InviteCode).Scan(&ic).Error; err != nil {
+
if err == gorm.ErrRecordNotFound {
return helpers.InputError(e, to.StringPtr("InvalidInviteCode"))
}
+
s.logger.Error("error getting invite code from db", "error", err)
+
return helpers.ServerError(e, nil)
+
}
-
if err := s.db.Raw("SELECT * FROM invite_codes WHERE code = ?", nil, request.InviteCode).Scan(&ic).Error; err != nil {
-
if err == gorm.ErrRecordNotFound {
-
return helpers.InputError(e, to.StringPtr("InvalidInviteCode"))
-
}
-
s.logger.Error("error getting invite code from db", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if ic.RemainingUseCount < 1 {
-
return helpers.InputError(e, to.StringPtr("InvalidInviteCode"))
-
}
+
if ic.RemainingUseCount < 1 {
+
return helpers.InputError(e, to.StringPtr("InvalidInviteCode"))
}
// see if the email is already taken
-
existingRepo, err := s.getRepoByEmail(request.Email)
+
_, err = s.getRepoByEmail(request.Email)
if err != nil && err != gorm.ErrRecordNotFound {
s.logger.Error("error looking up email in db", "endpoint", "com.atproto.server.createAccount", "error", err)
return helpers.ServerError(e, nil)
}
-
if err == nil && existingRepo.Did != signupDid {
+
if err == nil {
return helpers.InputError(e, to.StringPtr("EmailNotAvailable"))
}
// TODO: unsupported domains
-
var k *atcrypto.PrivateKeyK256
-
-
if signupDid != "" {
-
reservedKey, err := s.getReservedKey(signupDid)
-
if err != nil {
-
s.logger.Error("error looking up reserved key", "error", err)
-
}
-
if reservedKey != nil {
-
k, err = atcrypto.ParsePrivateBytesK256(reservedKey.PrivateKey)
-
if err != nil {
-
s.logger.Error("error parsing reserved key", "error", err)
-
k = nil
-
} else {
-
defer func() {
-
if delErr := s.deleteReservedKey(reservedKey.KeyDid, reservedKey.Did); delErr != nil {
-
s.logger.Error("error deleting reserved key", "error", delErr)
-
}
-
}()
-
}
-
}
-
}
-
-
if k == nil {
-
k, err = atcrypto.GeneratePrivateKeyK256()
-
if err != nil {
-
s.logger.Error("error creating signing key", "endpoint", "com.atproto.server.createAccount", "error", err)
-
return helpers.ServerError(e, nil)
-
}
+
k, err := atcrypto.GeneratePrivateKeyK256()
+
if err != nil {
+
s.logger.Error("error creating signing key", "endpoint", "com.atproto.server.createAccount", "error", err)
+
return helpers.ServerError(e, nil)
}
if signupDid == "" {
···
SigningKey: k.Bytes(),
}
-
if actor == nil {
-
actor = &models.Actor{
-
Did: signupDid,
-
Handle: request.Handle,
-
}
+
actor := models.Actor{
+
Did: signupDid,
+
Handle: request.Handle,
+
}
+
+
if err := s.db.Create(&urepo, nil).Error; err != nil {
+
s.logger.Error("error inserting new repo", "error", err)
+
return helpers.ServerError(e, nil)
+
}
-
if err := s.db.Create(&urepo, nil).Error; err != nil {
-
s.logger.Error("error inserting new repo", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := s.db.Create(&actor, nil).Error; err != nil {
-
s.logger.Error("error inserting new actor", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
} else {
-
if err := s.db.Save(&actor, nil).Error; err != nil {
-
s.logger.Error("error inserting new actor", "error", err)
-
return helpers.ServerError(e, nil)
-
}
+
if err := s.db.Create(&actor, nil).Error; err != nil {
+
s.logger.Error("error inserting new actor", "error", err)
+
return helpers.ServerError(e, nil)
}
-
if request.Did == nil || *request.Did == "" {
+
if customDidHeader == "" {
bs := s.getBlockstore(signupDid)
r := repo.NewRepo(context.TODO(), signupDid, bs)
···
})
}
-
if s.config.RequireInvite {
-
if err := s.db.Raw("UPDATE invite_codes SET remaining_use_count = remaining_use_count - 1 WHERE code = ?", nil, request.InviteCode).Scan(&ic).Error; err != nil {
-
s.logger.Error("error decrementing use count", "error", err)
-
return helpers.ServerError(e, nil)
-
}
+
if err := s.db.Raw("UPDATE invite_codes SET remaining_use_count = remaining_use_count - 1 WHERE code = ?", nil, request.InviteCode).Scan(&ic).Error; err != nil {
+
s.logger.Error("error decrementing use count", "error", err)
+
return helpers.ServerError(e, nil)
}
sess, err := s.createSession(&urepo)
-145
server/handle_server_delete_account.go
···
-
package server
-
-
import (
-
"context"
-
"time"
-
-
"github.com/Azure/go-autorest/autorest/to"
-
"github.com/bluesky-social/indigo/api/atproto"
-
"github.com/bluesky-social/indigo/events"
-
"github.com/bluesky-social/indigo/util"
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/labstack/echo/v4"
-
"golang.org/x/crypto/bcrypt"
-
)
-
-
type ComAtprotoServerDeleteAccountRequest struct {
-
Did string `json:"did" validate:"required"`
-
Password string `json:"password" validate:"required"`
-
Token string `json:"token" validate:"required"`
-
}
-
-
func (s *Server) handleServerDeleteAccount(e echo.Context) error {
-
var req ComAtprotoServerDeleteAccountRequest
-
if err := e.Bind(&req); err != nil {
-
s.logger.Error("error binding", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := e.Validate(&req); err != nil {
-
s.logger.Error("error validating", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
urepo, err := s.getRepoActorByDid(req.Did)
-
if err != nil {
-
s.logger.Error("error getting repo", "error", err)
-
return echo.NewHTTPError(400, "account not found")
-
}
-
-
if err := bcrypt.CompareHashAndPassword([]byte(urepo.Repo.Password), []byte(req.Password)); err != nil {
-
s.logger.Error("password mismatch", "error", err)
-
return echo.NewHTTPError(401, "Invalid did or password")
-
}
-
-
if urepo.Repo.AccountDeleteCode == nil || urepo.Repo.AccountDeleteCodeExpiresAt == nil {
-
s.logger.Error("no deletion token found for account")
-
return echo.NewHTTPError(400, map[string]interface{}{
-
"error": "InvalidToken",
-
"message": "Token is invalid",
-
})
-
}
-
-
if *urepo.Repo.AccountDeleteCode != req.Token {
-
s.logger.Error("deletion token mismatch")
-
return echo.NewHTTPError(400, map[string]interface{}{
-
"error": "InvalidToken",
-
"message": "Token is invalid",
-
})
-
}
-
-
if time.Now().UTC().After(*urepo.Repo.AccountDeleteCodeExpiresAt) {
-
s.logger.Error("deletion token expired")
-
return echo.NewHTTPError(400, map[string]interface{}{
-
"error": "ExpiredToken",
-
"message": "Token is expired",
-
})
-
}
-
-
tx := s.db.BeginDangerously()
-
if tx.Error != nil {
-
s.logger.Error("error starting transaction", "error", tx.Error)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM blocks WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting blocks", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM records WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting records", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM blobs WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting blobs", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM tokens WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting tokens", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM refresh_tokens WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting refresh tokens", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM reserved_keys WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting reserved keys", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM invite_codes WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting invite codes", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM actors WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting actor", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Exec("DELETE FROM repos WHERE did = ?", nil, req.Did).Error; err != nil {
-
tx.Rollback()
-
s.logger.Error("error deleting repo", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if err := tx.Commit().Error; err != nil {
-
s.logger.Error("error committing transaction", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
s.evtman.AddEvent(context.TODO(), &events.XRPCStreamEvent{
-
RepoAccount: &atproto.SyncSubscribeRepos_Account{
-
Active: false,
-
Did: req.Did,
-
Status: to.StringPtr("deleted"),
-
Seq: time.Now().UnixMicro(),
-
Time: time.Now().Format(util.ISO8601),
-
},
-
})
-
-
return e.NoContent(200)
-
}
+1 -1
server/handle_server_describe_server.go
···
func (s *Server) handleDescribeServer(e echo.Context) error {
return e.JSON(200, ComAtprotoServerDescribeServerResponse{
-
InviteCodeRequired: s.config.RequireInvite,
+
InviteCodeRequired: true,
PhoneVerificationRequired: false,
AvailableUserDomains: []string{"." + s.config.Hostname}, // TODO: more
Links: ComAtprotoServerDescribeServerResponseLinks{
+3 -10
server/handle_server_get_service_auth.go
···
Aud string `query:"aud" validate:"required,atproto-did"`
// exp should be a float, as some clients will send a non-integer expiration
Exp float64 `query:"exp"`
-
Lxm string `query:"lxm"`
+
Lxm string `query:"lxm" validate:"required,atproto-nsid"`
}
func (s *Server) handleServerGetServiceAuth(e echo.Context) error {
···
return helpers.InputError(e, to.StringPtr("may not generate auth tokens recursively"))
}
-
var maxExp int64
-
if req.Lxm != "" {
-
maxExp = now + (60 * 60)
-
} else {
-
maxExp = now + 60
-
}
+
maxExp := now + (60 * 30)
if exp > maxExp {
return helpers.InputError(e, to.StringPtr("expiration too big. smoller please"))
}
···
payload := map[string]any{
"iss": repo.Repo.Did,
"aud": req.Aud,
+
"lxm": req.Lxm,
"jti": uuid.NewString(),
"exp": exp,
"iat": now,
-
}
-
if req.Lxm != "" {
-
payload["lxm"] = req.Lxm
}
pj, err := json.Marshal(payload)
if err != nil {
-49
server/handle_server_request_account_delete.go
···
-
package server
-
-
import (
-
"fmt"
-
"time"
-
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/haileyok/cocoon/models"
-
"github.com/labstack/echo/v4"
-
)
-
-
func (s *Server) handleServerRequestAccountDelete(e echo.Context) error {
-
urepo := e.Get("repo").(*models.RepoActor)
-
-
token := fmt.Sprintf("%s-%s", helpers.RandomVarchar(5), helpers.RandomVarchar(5))
-
expiresAt := time.Now().UTC().Add(15 * time.Minute)
-
-
if err := s.db.Exec("UPDATE repos SET account_delete_code = ?, account_delete_code_expires_at = ? WHERE did = ?", nil, token, expiresAt, urepo.Repo.Did).Error; err != nil {
-
s.logger.Error("error setting deletion token", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if urepo.Email != "" {
-
if err := s.sendAccountDeleteEmail(urepo.Email, urepo.Actor.Handle, token); err != nil {
-
s.logger.Error("error sending account deletion email", "error", err)
-
}
-
}
-
-
return e.NoContent(200)
-
}
-
-
func (s *Server) sendAccountDeleteEmail(email, handle, token string) error {
-
if s.mail == nil {
-
return nil
-
}
-
-
s.mailLk.Lock()
-
defer s.mailLk.Unlock()
-
-
s.mail.To(email)
-
s.mail.Subject("Account Deletion Request for " + s.config.Hostname)
-
s.mail.Plain().Set(fmt.Sprintf("Hello %s. Your account deletion code is %s. This code will expire in fifteen minutes. If you did not request this, please ignore this email.", handle, token))
-
-
if err := s.mail.Send(); err != nil {
-
return err
-
}
-
-
return nil
-
}
-95
server/handle_server_reserve_signing_key.go
···
-
package server
-
-
import (
-
"time"
-
-
"github.com/bluesky-social/indigo/atproto/atcrypto"
-
"github.com/haileyok/cocoon/internal/helpers"
-
"github.com/haileyok/cocoon/models"
-
"github.com/labstack/echo/v4"
-
)
-
-
type ServerReserveSigningKeyRequest struct {
-
Did *string `json:"did"`
-
}
-
-
type ServerReserveSigningKeyResponse struct {
-
SigningKey string `json:"signingKey"`
-
}
-
-
func (s *Server) handleServerReserveSigningKey(e echo.Context) error {
-
var req ServerReserveSigningKeyRequest
-
if err := e.Bind(&req); err != nil {
-
s.logger.Error("could not bind reserve signing key request", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
if req.Did != nil && *req.Did != "" {
-
var existing models.ReservedKey
-
if err := s.db.Raw("SELECT * FROM reserved_keys WHERE did = ?", nil, *req.Did).Scan(&existing).Error; err == nil && existing.KeyDid != "" {
-
return e.JSON(200, ServerReserveSigningKeyResponse{
-
SigningKey: existing.KeyDid,
-
})
-
}
-
}
-
-
k, err := atcrypto.GeneratePrivateKeyK256()
-
if err != nil {
-
s.logger.Error("error creating signing key", "endpoint", "com.atproto.server.reserveSigningKey", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
pubKey, err := k.PublicKey()
-
if err != nil {
-
s.logger.Error("error getting public key", "endpoint", "com.atproto.server.reserveSigningKey", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
keyDid := pubKey.DIDKey()
-
-
reservedKey := models.ReservedKey{
-
KeyDid: keyDid,
-
Did: req.Did,
-
PrivateKey: k.Bytes(),
-
CreatedAt: time.Now(),
-
}
-
-
if err := s.db.Create(&reservedKey, nil).Error; err != nil {
-
s.logger.Error("error storing reserved key", "endpoint", "com.atproto.server.reserveSigningKey", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
s.logger.Info("reserved signing key", "keyDid", keyDid, "forDid", req.Did)
-
-
return e.JSON(200, ServerReserveSigningKeyResponse{
-
SigningKey: keyDid,
-
})
-
}
-
-
func (s *Server) getReservedKey(keyDidOrDid string) (*models.ReservedKey, error) {
-
var reservedKey models.ReservedKey
-
-
if err := s.db.Raw("SELECT * FROM reserved_keys WHERE key_did = ?", nil, keyDidOrDid).Scan(&reservedKey).Error; err == nil && reservedKey.KeyDid != "" {
-
return &reservedKey, nil
-
}
-
-
if err := s.db.Raw("SELECT * FROM reserved_keys WHERE did = ?", nil, keyDidOrDid).Scan(&reservedKey).Error; err == nil && reservedKey.KeyDid != "" {
-
return &reservedKey, nil
-
}
-
-
return nil, nil
-
}
-
-
func (s *Server) deleteReservedKey(keyDid string, did *string) error {
-
if err := s.db.Exec("DELETE FROM reserved_keys WHERE key_did = ?", nil, keyDid).Error; err != nil {
-
return err
-
}
-
-
if did != nil && *did != "" {
-
if err := s.db.Exec("DELETE FROM reserved_keys WHERE did = ?", nil, *did).Error; err != nil {
-
return err
-
}
-
}
-
-
return nil
-
}
+2 -14
server/handle_sync_get_blob.go
···
for _, p := range parts {
buf.Write(p.Data)
}
-
} else if blob.Storage == "s3" {
-
if !(s.s3Config != nil && s.s3Config.BlobstoreEnabled) {
-
s.logger.Error("s3 storage disabled")
-
return helpers.ServerError(e, nil)
-
}
-
-
blobKey := fmt.Sprintf("blobs/%s/%s", urepo.Repo.Did, c.String())
-
-
if s.s3Config.CDNUrl != "" {
-
redirectUrl := fmt.Sprintf("%s/%s", s.s3Config.CDNUrl, blobKey)
-
return e.Redirect(302, redirectUrl)
-
}
-
+
} else if blob.Storage == "s3" && s.s3Config != nil && s.s3Config.BlobstoreEnabled {
config := &aws.Config{
Region: aws.String(s.s3Config.Region),
Credentials: credentials.NewStaticCredentials(s.s3Config.AccessKey, s.s3Config.SecretKey, ""),
···
svc := s3.New(sess)
if result, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.s3Config.Bucket),
-
Key: aws.String(blobKey),
+
Key: aws.String(fmt.Sprintf("blobs/%s/%s", urepo.Repo.Did, c.String())),
}); err != nil {
s.logger.Error("error getting blob from s3", "error", err)
return helpers.ServerError(e, nil)
+11 -31
server/handle_sync_subscribe_repos.go
···
package server
import (
-
"context"
-
"time"
+
"fmt"
"github.com/bluesky-social/indigo/events"
"github.com/bluesky-social/indigo/lex/util"
···
)
func (s *Server) handleSyncSubscribeRepos(e echo.Context) error {
-
ctx := e.Request().Context()
-
logger := s.logger.With("component", "subscribe-repos-websocket")
-
conn, err := websocket.Upgrade(e.Response().Writer, e.Request(), e.Response().Header(), 1<<10, 1<<10)
if err != nil {
-
logger.Error("unable to establish websocket with relay", "err", err)
return err
}
+
+
s.logger.Info("new connection", "ua", e.Request().UserAgent())
+
+
ctx := e.Request().Context()
ident := e.RealIP() + "-" + e.Request().UserAgent()
-
logger = logger.With("ident", ident)
-
logger.Info("new connection established")
evts, cancel, err := s.evtman.Subscribe(ctx, ident, func(evt *events.XRPCStreamEvent) bool {
return true
···
for evt := range evts {
wc, err := conn.NextWriter(websocket.BinaryMessage)
if err != nil {
-
logger.Error("error writing message to relay", "err", err)
-
break
-
}
-
-
if ctx.Err() != nil {
-
logger.Error("context error", "err", err)
-
break
+
return err
}
var obj util.CBOR
+
switch {
case evt.Error != nil:
header.Op = events.EvtKindErrorFrame
···
header.MsgType = "#info"
obj = evt.RepoInfo
default:
-
logger.Warn("unrecognized event kind")
-
return nil
+
return fmt.Errorf("unrecognized event kind")
}
if err := header.MarshalCBOR(wc); err != nil {
-
logger.Error("failed to write header to relay", "err", err)
-
break
+
return fmt.Errorf("failed to write header: %w", err)
}
if err := obj.MarshalCBOR(wc); err != nil {
-
logger.Error("failed to write event to relay", "err", err)
-
break
+
return fmt.Errorf("failed to write event: %w", err)
}
if err := wc.Close(); err != nil {
-
logger.Error("failed to flush-close our event write", "err", err)
-
break
+
return fmt.Errorf("failed to flush-close our event write: %w", err)
}
-
}
-
-
// we should tell the relay to request a new crawl at this point if we got disconnected
-
// use a new context since the old one might be cancelled at this point
-
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
-
defer cancel()
-
if err := s.requestCrawl(ctx); err != nil {
-
logger.Error("error requesting crawls", "err", err)
}
return nil
-33
server/handle_well_known.go
···
import (
"fmt"
-
"strings"
"github.com/Azure/go-autorest/autorest/to"
-
"github.com/haileyok/cocoon/internal/helpers"
"github.com/labstack/echo/v4"
-
"gorm.io/gorm"
)
var (
···
},
},
})
-
}
-
-
func (s *Server) handleAtprotoDid(e echo.Context) error {
-
host := e.Request().Host
-
if host == "" {
-
return helpers.InputError(e, to.StringPtr("Invalid handle."))
-
}
-
-
host = strings.Split(host, ":")[0]
-
host = strings.ToLower(strings.TrimSpace(host))
-
-
if host == s.config.Hostname {
-
return e.String(200, s.config.Did)
-
}
-
-
suffix := "." + s.config.Hostname
-
if !strings.HasSuffix(host, suffix) {
-
return e.NoContent(404)
-
}
-
-
actor, err := s.getActorByHandle(host)
-
if err != nil {
-
if err == gorm.ErrRecordNotFound {
-
return e.NoContent(404)
-
}
-
s.logger.Error("error looking up actor by handle", "error", err)
-
return helpers.ServerError(e, nil)
-
}
-
-
return e.String(200, actor.Did)
}
func (s *Server) handleOauthProtectedResource(e echo.Context) error {
-19
server/mail.go
···
return nil
}
-
func (s *Server) sendPlcTokenReset(email, handle, code string) error {
-
if s.mail == nil {
-
return nil
-
}
-
-
s.mailLk.Lock()
-
defer s.mailLk.Unlock()
-
-
s.mail.To(email)
-
s.mail.Subject("PLC token for " + s.config.Hostname)
-
s.mail.Plain().Set(fmt.Sprintf("Hello %s. Your PLC operation code is %s. This code will expire in ten minutes.", handle, code))
-
-
if err := s.mail.Send(); err != nil {
-
return err
-
}
-
-
return nil
-
}
-
func (s *Server) sendEmailUpdate(email, handle, code string) error {
if s.mail == nil {
return nil
+2 -9
server/middleware.go
···
proof, err := s.oauthProvider.DpopManager.CheckProof(e.Request().Method, "https://"+s.config.Hostname+e.Request().URL.String(), e.Request().Header, to.StringPtr(accessToken))
if err != nil {
if errors.Is(err, dpop.ErrUseDpopNonce) {
-
e.Response().Header().Set("WWW-Authenticate", `DPoP error="use_dpop_nonce"`)
-
e.Response().Header().Add("access-control-expose-headers", "WWW-Authenticate")
-
return e.JSON(401, map[string]string{
+
return e.JSON(400, map[string]string{
"error": "use_dpop_nonce",
})
}
···
}
if time.Now().After(oauthToken.ExpiresAt) {
-
e.Response().Header().Set("WWW-Authenticate", `DPoP error="invalid_token", error_description="Token expired"`)
-
e.Response().Header().Add("access-control-expose-headers", "WWW-Authenticate")
-
return e.JSON(401, map[string]string{
-
"error": "invalid_token",
-
"error_description": "Token expired",
-
})
+
return helpers.ExpiredTokenError(e)
}
repo, err := s.getRepoActorByDid(oauthToken.Sub)
+9 -87
server/server.go
···
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
slogecho "github.com/samber/slog-echo"
-
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
···
Bucket string
AccessKey string
SecretKey string
-
CDNUrl string
}
type Server struct {
···
evtman *events.EventManager
passport *identity.Passport
fallbackProxy string
-
-
lastRequestCrawl time.Time
-
requestCrawlMu sync.Mutex
dbName string
-
dbType string
s3Config *S3Config
}
type Args struct {
Addr string
DbName string
-
DbType string
-
DatabaseURL string
Logger *slog.Logger
Version string
Did string
···
ContactEmail string
Relays []string
AdminPassword string
-
RequireInvite bool
SmtpUser string
SmtpPass string
···
EnforcePeering bool
Relays []string
AdminPassword string
-
RequireInvite bool
SmtpEmail string
SmtpName string
BlockstoreVariant BlockstoreVariant
···
IdleTimeout: 5 * time.Minute,
}
-
dbType := args.DbType
-
if dbType == "" {
-
dbType = "sqlite"
-
}
-
-
var gdb *gorm.DB
-
var err error
-
switch dbType {
-
case "postgres":
-
if args.DatabaseURL == "" {
-
return nil, fmt.Errorf("database-url must be set when using postgres")
-
}
-
gdb, err = gorm.Open(postgres.Open(args.DatabaseURL), &gorm.Config{})
-
if err != nil {
-
return nil, fmt.Errorf("failed to connect to postgres: %w", err)
-
}
-
args.Logger.Info("connected to PostgreSQL database")
-
default:
-
gdb, err = gorm.Open(sqlite.Open(args.DbName), &gorm.Config{})
-
if err != nil {
-
return nil, fmt.Errorf("failed to open sqlite database: %w", err)
-
}
-
args.Logger.Info("connected to SQLite database", "path", args.DbName)
+
gdb, err := gorm.Open(sqlite.Open(args.DbName), &gorm.Config{})
+
if err != nil {
+
return nil, err
}
dbw := db.NewDB(gdb)
···
EnforcePeering: false,
Relays: args.Relays,
AdminPassword: args.AdminPassword,
-
RequireInvite: args.RequireInvite,
SmtpName: args.SmtpName,
SmtpEmail: args.SmtpEmail,
BlockstoreVariant: args.BlockstoreVariant,
···
passport: identity.NewPassport(h, identity.NewMemCache(10_000)),
dbName: args.DbName,
-
dbType: dbType,
s3Config: args.S3Config,
oauthProvider: provider.NewProvider(provider.Args{
···
s.echo.GET("/", s.handleRoot)
s.echo.GET("/xrpc/_health", s.handleHealth)
s.echo.GET("/.well-known/did.json", s.handleWellKnown)
-
s.echo.GET("/.well-known/atproto-did", s.handleAtprotoDid)
s.echo.GET("/.well-known/oauth-protected-resource", s.handleOauthProtectedResource)
s.echo.GET("/.well-known/oauth-authorization-server", s.handleOauthAuthorizationServer)
s.echo.GET("/robots.txt", s.handleRobots)
···
// public
s.echo.GET("/xrpc/com.atproto.identity.resolveHandle", s.handleResolveHandle)
s.echo.POST("/xrpc/com.atproto.server.createAccount", s.handleCreateAccount)
+
s.echo.POST("/xrpc/com.atproto.server.createAccount", s.handleCreateAccount)
s.echo.POST("/xrpc/com.atproto.server.createSession", s.handleCreateSession)
s.echo.GET("/xrpc/com.atproto.server.describeServer", s.handleDescribeServer)
-
s.echo.POST("/xrpc/com.atproto.server.reserveSigningKey", s.handleServerReserveSigningKey)
s.echo.GET("/xrpc/com.atproto.repo.describeRepo", s.handleDescribeRepo)
s.echo.GET("/xrpc/com.atproto.sync.listRepos", s.handleListRepos)
···
s.echo.GET("/xrpc/com.atproto.sync.listBlobs", s.handleSyncListBlobs)
s.echo.GET("/xrpc/com.atproto.sync.getBlob", s.handleSyncGetBlob)
-
// labels
-
s.echo.GET("/xrpc/com.atproto.label.queryLabels", s.handleLabelQueryLabels)
-
// account
s.echo.GET("/account", s.handleAccount)
s.echo.POST("/account/revoke", s.handleAccountRevoke)
···
s.echo.GET("/xrpc/com.atproto.server.getSession", s.handleGetSession, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.server.refreshSession", s.handleRefreshSession, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.server.deleteSession", s.handleDeleteSession, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
-
s.echo.GET("/xrpc/com.atproto.identity.getRecommendedDidCredentials", s.handleGetRecommendedDidCredentials, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.identity.updateHandle", s.handleIdentityUpdateHandle, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
-
s.echo.POST("/xrpc/com.atproto.identity.requestPlcOperationSignature", s.handleIdentityRequestPlcOperationSignature, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
-
s.echo.POST("/xrpc/com.atproto.identity.signPlcOperation", s.handleSignPlcOperation, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
-
s.echo.POST("/xrpc/com.atproto.identity.submitPlcOperation", s.handleSubmitPlcOperation, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.server.confirmEmail", s.handleServerConfirmEmail, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.server.requestEmailConfirmation", s.handleServerRequestEmailConfirmation, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.server.requestPasswordReset", s.handleServerRequestPasswordReset) // AUTH NOT REQUIRED FOR THIS ONE
···
s.echo.GET("/xrpc/com.atproto.server.checkAccountStatus", s.handleServerCheckAccountStatus, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.server.deactivateAccount", s.handleServerDeactivateAccount, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.server.activateAccount", s.handleServerActivateAccount, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
-
s.echo.POST("/xrpc/com.atproto.server.requestAccountDelete", s.handleServerRequestAccountDelete, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
-
s.echo.POST("/xrpc/com.atproto.server.deleteAccount", s.handleServerDeleteAccount)
// repo
-
s.echo.GET("/xrpc/com.atproto.repo.listMissingBlobs", s.handleListMissingBlobs, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.repo.createRecord", s.handleCreateRecord, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.repo.putRecord", s.handlePutRecord, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/com.atproto.repo.deleteRecord", s.handleDeleteRecord, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
···
// stupid silly endpoints
s.echo.GET("/xrpc/app.bsky.actor.getPreferences", s.handleActorGetPreferences, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
s.echo.POST("/xrpc/app.bsky.actor.putPreferences", s.handleActorPutPreferences, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
-
s.echo.GET("/xrpc/app.bsky.feed.getFeed", s.handleProxyBskyFeedGetFeed, s.handleLegacySessionMiddleware, s.handleOauthSessionMiddleware)
// admin routes
s.echo.POST("/xrpc/com.atproto.server.createInviteCode", s.handleCreateInviteCode, s.handleAdminMiddleware)
···
&models.Record{},
&models.Blob{},
&models.BlobPart{},
-
&models.ReservedKey{},
&provider.OauthToken{},
&provider.OauthAuthorizationRequest{},
)
···
go s.backupRoutine()
-
go func() {
-
if err := s.requestCrawl(ctx); err != nil {
-
s.logger.Error("error requesting crawls", "err", err)
-
}
-
}()
-
-
<-ctx.Done()
-
-
fmt.Println("shut down")
-
-
return nil
-
}
-
-
func (s *Server) requestCrawl(ctx context.Context) error {
-
logger := s.logger.With("component", "request-crawl")
-
s.requestCrawlMu.Lock()
-
defer s.requestCrawlMu.Unlock()
-
-
logger.Info("requesting crawl with configured relays")
-
-
if time.Now().Sub(s.lastRequestCrawl) <= 1*time.Minute {
-
return fmt.Errorf("a crawl request has already been made within the last minute")
-
}
-
for _, relay := range s.config.Relays {
-
logger := logger.With("relay", relay)
-
logger.Info("requesting crawl from relay")
cli := xrpc.Client{Host: relay}
-
if err := atproto.SyncRequestCrawl(ctx, &cli, &atproto.SyncRequestCrawl_Input{
+
atproto.SyncRequestCrawl(ctx, &cli, &atproto.SyncRequestCrawl_Input{
Hostname: s.config.Hostname,
-
}); err != nil {
-
logger.Error("error requesting crawl", "err", err)
-
} else {
-
logger.Info("crawl requested successfully")
-
}
+
})
}
-
s.lastRequestCrawl = time.Now()
+
<-ctx.Done()
+
+
fmt.Println("shut down")
return nil
}
func (s *Server) doBackup() {
-
if s.dbType == "postgres" {
-
s.logger.Info("skipping S3 backup - PostgreSQL backups should be handled externally (pg_dump, managed database backups, etc.)")
-
return
-
}
-
start := time.Now()
s.logger.Info("beginning backup to s3...")
-91
server/service_auth.go
···
-
package server
-
-
import (
-
"context"
-
"fmt"
-
"strings"
-
-
"github.com/bluesky-social/indigo/atproto/atcrypto"
-
"github.com/bluesky-social/indigo/atproto/identity"
-
atproto_identity "github.com/bluesky-social/indigo/atproto/identity"
-
"github.com/bluesky-social/indigo/atproto/syntax"
-
"github.com/golang-jwt/jwt/v4"
-
)
-
-
type ES256KSigningMethod struct {
-
alg string
-
}
-
-
func (m *ES256KSigningMethod) Alg() string {
-
return m.alg
-
}
-
-
func (m *ES256KSigningMethod) Verify(signingString string, signature string, key interface{}) error {
-
signatureBytes, err := jwt.DecodeSegment(signature)
-
if err != nil {
-
return err
-
}
-
return key.(atcrypto.PublicKey).HashAndVerifyLenient([]byte(signingString), signatureBytes)
-
}
-
-
func (m *ES256KSigningMethod) Sign(signingString string, key interface{}) (string, error) {
-
return "", fmt.Errorf("unimplemented")
-
}
-
-
func init() {
-
ES256K := ES256KSigningMethod{alg: "ES256K"}
-
jwt.RegisterSigningMethod(ES256K.Alg(), func() jwt.SigningMethod {
-
return &ES256K
-
})
-
}
-
-
func (s *Server) validateServiceAuth(ctx context.Context, rawToken string, nsid string) (string, error) {
-
token := strings.TrimSpace(rawToken)
-
-
parsedToken, err := jwt.ParseWithClaims(token, jwt.MapClaims{}, func(token *jwt.Token) (interface{}, error) {
-
did := syntax.DID(token.Claims.(jwt.MapClaims)["iss"].(string))
-
didDoc, err := s.passport.FetchDoc(ctx, did.String());
-
if err != nil {
-
return nil, fmt.Errorf("unable to resolve did %s: %s", did, err)
-
}
-
-
verificationMethods := make([]atproto_identity.DocVerificationMethod, len(didDoc.VerificationMethods))
-
for i, verificationMethod := range didDoc.VerificationMethods {
-
verificationMethods[i] = atproto_identity.DocVerificationMethod{
-
ID: verificationMethod.Id,
-
Type: verificationMethod.Type,
-
PublicKeyMultibase: verificationMethod.PublicKeyMultibase,
-
Controller: verificationMethod.Controller,
-
}
-
}
-
services := make([]atproto_identity.DocService, len(didDoc.Service))
-
for i, service := range didDoc.Service {
-
services[i] = atproto_identity.DocService{
-
ID: service.Id,
-
Type: service.Type,
-
ServiceEndpoint: service.ServiceEndpoint,
-
}
-
}
-
parsedIdentity := atproto_identity.ParseIdentity(&identity.DIDDocument{
-
DID: did,
-
AlsoKnownAs: didDoc.AlsoKnownAs,
-
VerificationMethod: verificationMethods,
-
Service: services,
-
})
-
-
key, err := parsedIdentity.PublicKey()
-
if err != nil {
-
return nil, fmt.Errorf("signing key not found for did %s: %s", did, err)
-
}
-
return key, nil
-
})
-
if err != nil {
-
return "", fmt.Errorf("invalid token: %s", err)
-
}
-
-
claims := parsedToken.Claims.(jwt.MapClaims)
-
if claims["lxm"] != nsid {
-
return "", fmt.Errorf("bad jwt lexicon method (\"lxm\"). must match: %s", nsid)
-
}
-
return claims["iss"].(string), nil
-
}