|
6 | 6 | [](https://pkg.go.dev/github.com/m-lab/token-exchange) |
7 | 7 |
|
8 | 8 | A Cloud Run service that exchanges API keys for signed JWTs for M-Lab services. |
| 9 | + |
| 10 | +## API Endpoints |
| 11 | + |
| 12 | +| Method | Path | Description | |
| 13 | +|--------|------|-------------| |
| 14 | +| `POST` | `/v0/token/autojoin` | Exchange autojoin API key for JWT token | |
| 15 | +| `POST` | `/v0/token/integration` | Exchange client integration API key for JWT token | |
| 16 | +| `GET` | `/.well-known/jwks.json` | Serve JSON Web Key Set for token verification | |
| 17 | +| `GET` | `/health` | Health check endpoint | |
| 18 | + |
| 19 | +## Development |
| 20 | + |
| 21 | +### Build and Test |
| 22 | + |
| 23 | +```bash |
| 24 | +# Build all packages |
| 25 | +go build -v ./... |
| 26 | + |
| 27 | +# Run all tests with race detection |
| 28 | +go test -race -v ./... |
| 29 | + |
| 30 | +# Run tests with coverage |
| 31 | +go test -coverprofile=coverage.out -coverpkg=./... ./... |
| 32 | +``` |
| 33 | + |
| 34 | +### Docker |
| 35 | + |
| 36 | +```bash |
| 37 | +# Build Docker image |
| 38 | +docker build -t token-exchange . |
| 39 | +``` |
| 40 | + |
| 41 | +### Deploy to Cloud Run |
| 42 | + |
| 43 | +```bash |
| 44 | +gcloud builds submit --config cloudbuild.yaml |
| 45 | +``` |
| 46 | + |
| 47 | +## Configuration |
| 48 | + |
| 49 | +Configuration is via command-line flags. All flags can also be set via environment variables (e.g., `-port` becomes `PORT`, `-platform-ns` becomes `PLATFORM_NS`). |
| 50 | + |
| 51 | +| Flag | Default | Description | |
| 52 | +|------|---------|-------------| |
| 53 | +| `-port` | `8080` | Port to listen on | |
| 54 | +| `-private-key-path` | `/secrets/jwk-priv.json` | Path to JWK private key file | |
| 55 | +| `-platform-ns` | `platform-credentials` | Datastore namespace for autojoin credentials | |
| 56 | +| `-client-integration-ns` | `client-integration` | Datastore namespace for client integration credentials | |
| 57 | +| `-project-id` | `mlab-sandbox` | Google Cloud project ID | |
| 58 | + |
| 59 | +## CLI Tools |
| 60 | + |
| 61 | +### create-integration |
| 62 | + |
| 63 | +Creates a new client integration and API key in Datastore. |
| 64 | + |
| 65 | +```bash |
| 66 | +go run ./cmd/create-integration -project=mlab-sandbox -integration-id=my-integration |
| 67 | +``` |
| 68 | + |
| 69 | +| Flag | Default | Description | |
| 70 | +|------|---------|-------------| |
| 71 | +| `-project` | (required) | Google Cloud project ID | |
| 72 | +| `-integration-id` | (required) | Integration ID | |
| 73 | +| `-namespace` | `client-integration` | Datastore namespace | |
| 74 | +| `-key-id` | (auto-generated) | Key ID | |
| 75 | +| `-integration-description` | | Human-readable description for the integration | |
| 76 | +| `-key-description` | | Human-readable description for the API key | |
| 77 | +| `-key-tier` | `0` | Service tier for the API key | |
| 78 | + |
| 79 | +## Usage |
| 80 | + |
| 81 | +### Integration Token Exchange |
| 82 | + |
| 83 | +Exchange an API key for a short-lived JWT token. |
| 84 | + |
| 85 | +**Request:** |
| 86 | + |
| 87 | +```bash |
| 88 | +curl -X POST https://your-service-url/v0/token/integration \ |
| 89 | + -H "Content-Type: application/json" \ |
| 90 | + -d '{"api_key": "your-api-key"}' |
| 91 | +``` |
| 92 | + |
| 93 | +**Response:** |
| 94 | + |
| 95 | +```json |
| 96 | +{ |
| 97 | + "token": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ii..." |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +**JWT Claims:** |
| 102 | + |
| 103 | +| Claim | Description | |
| 104 | +|-------|-------------| |
| 105 | +| `int_id` | Integration ID | |
| 106 | +| `key_id` | API Key ID | |
| 107 | +| `tier` | Service tier | |
| 108 | +| `aud` | Audience (`integration`) | |
| 109 | +| `exp` | Expiration time (20 seconds from issue) | |
| 110 | +| `iat` | Issued at time | |
| 111 | +| `iss` | Issuer | |
| 112 | +| `jti` | Unique token ID | |
| 113 | +| `nbf` | Not valid before time | |
| 114 | + |
| 115 | +**Error Responses:** |
| 116 | + |
| 117 | +| Status | Description | |
| 118 | +|--------|-------------| |
| 119 | +| `400 Bad Request` | Invalid request body | |
| 120 | +| `401 Unauthorized` | Invalid API key | |
| 121 | +| `405 Method Not Allowed` | Request method is not POST | |
| 122 | +| `500 Internal Server Error` | Failed to generate token | |
| 123 | + |
| 124 | +## Architecture |
| 125 | + |
| 126 | +The service validates API keys stored in Google Cloud Datastore and returns signed JWT tokens. Tokens are signed using RS256 with keys loaded from a JWK file. |
| 127 | + |
| 128 | +### Token Flow |
| 129 | + |
| 130 | +```mermaid |
| 131 | +sequenceDiagram |
| 132 | + participant Client |
| 133 | + participant TokenExchange |
| 134 | + participant Datastore |
| 135 | +
|
| 136 | + Client->>TokenExchange: POST /v0/token/integration (API key) |
| 137 | + TokenExchange->>Datastore: Validate API key |
| 138 | + Datastore-->>TokenExchange: Integration info |
| 139 | + TokenExchange->>TokenExchange: Generate JWT (20-second expiry) |
| 140 | + TokenExchange-->>Client: Signed JWT token |
| 141 | +``` |
0 commit comments