managarten/services/mana-matrix-bot/internal/services/auth.go
Till JS 819568c3df feat(infra): consolidate 21 Matrix bots into Go binary + add Go API gateway
Replace 21 separate NestJS Matrix bot processes (~2.1 GB RAM, ~4.2 GB Docker images)
with a single Go binary using plugin architecture (8.6 MB binary, ~30 MB RAM).

New services:
- services/mana-matrix-bot/ — Go Matrix bot with 21 plugins (mautrix-go, Redis sessions)
- services/mana-api-gateway-go/ — Go API gateway (rate limiting, API keys, credit billing)

Deleted:
- 21 services/matrix-*-bot/ directories
- packages/bot-services/ and packages/matrix-bot-common/
- Legacy deploy scripts and CI build jobs

Updated:
- docker-compose.macmini.yml: new Go services, legacy bots removed
- CI/CD: change detection + build jobs for Go services
- Root package.json: new dev:matrix, build:matrix, test:matrix scripts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 21:03:00 +01:00

52 lines
1.3 KiB
Go

package services
import (
"context"
"fmt"
"time"
)
// AuthClient handles login/logout via mana-core-auth.
type AuthClient struct {
backend *BackendClient
}
// LoginResponse holds the auth service login response.
type LoginResponse struct {
Token string `json:"token"`
RefreshToken string `json:"refreshToken"`
ExpiresIn int `json:"expiresIn"` // seconds
}
// NewAuthClient creates a new auth service client.
func NewAuthClient(authURL string) *AuthClient {
return &AuthClient{backend: NewBackendClient(authURL)}
}
// Login authenticates a user and returns a JWT token.
func (c *AuthClient) Login(ctx context.Context, email, password string) (*LoginResponse, error) {
body := map[string]string{
"email": email,
"password": password,
}
var resp LoginResponse
if err := c.backend.Post(ctx, "/api/v1/auth/login", "", body, &resp); err != nil {
return nil, fmt.Errorf("login failed: %w", err)
}
if resp.Token == "" {
return nil, fmt.Errorf("login: no token in response")
}
return &resp, nil
}
// TokenExpiresAt calculates the expiry time from the login response.
func TokenExpiresAt(resp *LoginResponse) time.Time {
if resp.ExpiresIn > 0 {
return time.Now().Add(time.Duration(resp.ExpiresIn) * time.Second)
}
// Default: 24 hours
return time.Now().Add(24 * time.Hour)
}