#!/bin/bash # Script to create Matrix registration tokens # Usage: ./create-token.sh [uses_allowed] [token_name] HOMESERVER="https://matrix.fig.systems" USERNAME="${1}" PASSWORD="${2}" USES_ALLOWED="${3:-1}" # Default: 1 use TOKEN_NAME="${4:-}" # Optional custom token if [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then echo "Usage: $0 [uses_allowed] [token_name]" echo "" echo "Examples:" echo " $0 admin mypassword # Create single-use token" echo " $0 admin mypassword 10 # Create token with 10 uses" echo " $0 admin mypassword 5 invite123 # Create custom token 'invite123' with 5 uses" exit 1 fi echo "🔐 Logging in as $USERNAME..." # Get access token LOGIN_RESPONSE=$(curl -s -X POST "${HOMESERVER}/_matrix/client/v3/login" \ -H 'Content-Type: application/json' \ -d "{ \"type\": \"m.login.password\", \"identifier\": { \"type\": \"m.id.user\", \"user\": \"${USERNAME}\" }, \"password\": \"${PASSWORD}\" }") ACCESS_TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4) if [ -z "$ACCESS_TOKEN" ]; then echo "❌ Login failed!" echo "$LOGIN_RESPONSE" | jq . 2>/dev/null || echo "$LOGIN_RESPONSE" exit 1 fi echo "✅ Login successful!" echo "" echo "🎟️ Creating registration token..." # Create registration token if [ -n "$TOKEN_NAME" ]; then # Custom token TOKEN_DATA="{ \"token\": \"${TOKEN_NAME}\", \"uses_allowed\": ${USES_ALLOWED} }" else # Random token TOKEN_DATA="{ \"uses_allowed\": ${USES_ALLOWED}, \"length\": 16 }" fi TOKEN_RESPONSE=$(curl -s -X POST "${HOMESERVER}/_synapse/admin/v1/registration_tokens/new" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H 'Content-Type: application/json' \ -d "$TOKEN_DATA") TOKEN=$(echo "$TOKEN_RESPONSE" | grep -o '"token":"[^"]*' | cut -d'"' -f4) if [ -z "$TOKEN" ]; then echo "❌ Token creation failed!" echo "$TOKEN_RESPONSE" | jq . 2>/dev/null || echo "$TOKEN_RESPONSE" exit 1 fi echo "✅ Registration token created!" echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "📋 TOKEN: ${TOKEN}" echo "📊 Uses allowed: ${USES_ALLOWED}" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" echo "Share this token with users who should be able to register." echo "They'll enter it during signup at: https://chat.fig.systems" echo "" echo "Full response:" echo "$TOKEN_RESPONSE" | jq . 2>/dev/null || echo "$TOKEN_RESPONSE"