xonecas.io
6119
My

mysis / commit 241fd64

feat: credentials tool

xonecas2026-02-10 241fd64062151b3272d1ede8405e14d29aad303c parent 044a12d

6 files changed

Makefile+17−3
@@ -1,9 +1,9 @@
1-.PHONY: fmt build run test test-integration install clean db-reset-accounts check-upstream-version mcp_client
1+.PHONY: fmt build run test test-integration install clean db-reset-accounts check-upstream-version mcp_client backup-credentials
22
33 VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
44 LDFLAGS := -ldflags "-X main.Version=$(VERSION)"
5-DB_PATH ?= $(HOME)/.mysis/mysis.db
6-ACCOUNTS_EXPORT ?= accounts-backup.sql
5+DB_PATH ?= $(HOME)/.config/mysis/mysis.db
6+CREDENTIALS_BACKUP ?= credentials-backup-$(shell date +%Y%m%d-%H%M%S).sql
77
88 fmt:
99 go fmt ./...
@@ -27,3 +27,17 @@ test:
2727
2828 clean:
2929 rm -rf bin/ coverage.out
30+
31+backup-credentials:
32+ @echo "Backing up credentials from $(DB_PATH)..."
33+ @if [ ! -f "$(DB_PATH)" ]; then \
34+ echo "Error: Database not found at $(DB_PATH)"; \
35+ exit 1; \
36+ fi
37+ @sqlite3 "$(DB_PATH)" "SELECT 'INSERT INTO session_credentials VALUES(' || quote(session_id) || ',' || quote(username) || ',' || quote(password) || ',' || quote(created_at) || ',' || quote(updated_at) || ');' FROM session_credentials;" > "$(CREDENTIALS_BACKUP)"
38+ @echo "Credentials backed up to $(CREDENTIALS_BACKUP)"
39+ @echo ""
40+ @echo "Backup contains $(shell wc -l < $(CREDENTIALS_BACKUP)) credential(s)"
41+ @echo ""
42+ @echo "To restore (merge into existing table):"
43+ @echo " sqlite3 $(DB_PATH) < $(CREDENTIALS_BACKUP)"
documentation/architecture/CREDENTIAL_TOOLS.md+155−0
@@ -0,0 +1,155 @@
1+# Credential Management Tools
2+
3+Added two MCP tools for session-scoped credential storage:
4+
5+## Implementation
6+
7+### Database Schema
8+
9+Added `session_credentials` table to store game credentials per session:
10+
11+```sql
12+CREATE TABLE IF NOT EXISTS session_credentials (
13+ session_id TEXT PRIMARY KEY,
14+ username TEXT NOT NULL,
15+ password TEXT NOT NULL,
16+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
17+ updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
18+ FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
19+);
20+```
21+
22+**Key features:**
23+
24+- One credential pair per session (PRIMARY KEY on session_id)
25+- Automatic cleanup via CASCADE when session is deleted
26+- Foreign keys enabled in SQLite connection string
27+
28+### Store Functions
29+
30+**`internal/store/store.go`:**
31+
32+- `SaveCredentials(sessionID, username, password)` - Upsert credentials
33+- `GetCredentials(sessionID) (username, password, error)` - Retrieve credentials
34+- Returns empty strings if no credentials saved (not an error)
35+
36+### MCP Tools
37+
38+**`internal/mcp/tools.go`:**
39+
40+#### `save_credentials`
41+
42+Saves username and password for the current session.
43+
44+**Input Schema:**
45+
46+```json
47+{
48+ "username": "string (required)",
49+ "password": "string (required)"
50+}
51+```
52+
53+**Response:**
54+
55+```
56+Credentials saved successfully for user 'username'
57+```
58+
59+**Validation:**
60+
61+- Username cannot be empty
62+- Password cannot be empty
63+
64+#### `get_credentials`
65+
66+Retrieves saved credentials for the current session.
67+
68+**Input Schema:**
69+
70+```json
71+{}
72+```
73+
74+**Response (credentials exist):**
75+
76+```json
77+{
78+ "username": "player123",
79+ "password": "secret456"
80+}
81+```
82+
83+**Response (no credentials):**
84+
85+```
86+No credentials saved for this session
87+```
88+
89+## Usage
90+
91+Register tools with the proxy:
92+
93+```go
94+store, _ := store.Open()
95+proxy := mcp.NewProxy(upstream)
96+
97+proxy.RegisterTool(
98+ mcp.NewSaveCredentialsTool(),
99+ mcp.MakeSaveCredentialsHandler(store, sessionID),
100+)
101+
102+proxy.RegisterTool(
103+ mcp.NewGetCredentialsTool(),
104+ mcp.MakeGetCredentialsHandler(store, sessionID),
105+)
106+```
107+
108+## Session Isolation
109+
110+Credentials are strictly scoped:
111+
112+- Session A saves "user1/pass1" → only Session A can retrieve it
113+- Session B saves "user2/pass2" → only Session B can retrieve it
114+- Deleting a session automatically removes its credentials
115+
116+## Testing
117+
118+All tests pass:
119+
120+- `internal/mcp/tools_test.go` - Unit tests for tool handlers
121+- `internal/mcp/integration_test.go` - Full workflow with proxy
122+- `internal/store/credentials_test.go` - Database operations
123+- Cascade deletion verified
124+- Session isolation verified
125+
126+## Files Modified
127+
128+1. `internal/store/store.go` - Added schema + store functions
129+2. `internal/mcp/tools.go` - Implemented MCP tools
130+3. `internal/mcp/tools_test.go` - Unit tests
131+4. `internal/mcp/integration_test.go` - Integration tests
132+5. `internal/store/credentials_test.go` - Store tests
133+
134+## Backup and Restore
135+
136+**Backup credentials:**
137+
138+```bash
139+make backup-credentials
140+```
141+
142+This creates a timestamped SQL file (e.g., `credentials-backup-20260210-180025.sql`) containing INSERT statements for all session credentials.
143+
144+**Restore credentials:**
145+
146+```bash
147+sqlite3 ~/.config/mysis/mysis.db < credentials-backup-20260210-180025.sql
148+```
149+
150+**Notes:**
151+
152+- Backup files contain only INSERT statements (no schema)
153+- Can be merged into existing database
154+- Credentials are tied to session IDs (sessions must exist for foreign key constraint)
155+- Backup filename includes timestamp for safe multiple backups
internal/mcp/tools.go+142−1
@@ -1,3 +1,144 @@
11 package mcp
22
3-// This file can be used for future single-agent tool definitions.
3+import (
4+ "context"
5+ "encoding/json"
6+ "fmt"
7+)
8+
9+// CredentialStore defines the interface for storing and retrieving session credentials.
10+type CredentialStore interface {
11+ SaveCredentials(sessionID, username, password string) error
12+ GetCredentials(sessionID string) (username, password string, err error)
13+}
14+
15+// SaveCredentialsArgs represents arguments for save_credentials tool.
16+type SaveCredentialsArgs struct {
17+ Username string `json:"username"`
18+ Password string `json:"password"`
19+}
20+
21+// GetCredentialsResult represents the result of get_credentials tool.
22+type GetCredentialsResult struct {
23+ Username string `json:"username"`
24+ Password string `json:"password"`
25+}
26+
27+// NewSaveCredentialsTool creates the save_credentials tool definition.
28+func NewSaveCredentialsTool() Tool {
29+ schema := map[string]interface{}{
30+ "type": "object",
31+ "properties": map[string]interface{}{
32+ "username": map[string]interface{}{
33+ "type": "string",
34+ "description": "Username for the game account",
35+ },
36+ "password": map[string]interface{}{
37+ "type": "string",
38+ "description": "Password for the game account",
39+ },
40+ },
41+ "required": []string{"username", "password"},
42+ }
43+
44+ schemaJSON, _ := json.Marshal(schema)
45+
46+ return Tool{
47+ Name: "save_credentials",
48+ Description: "Save game credentials (username and password) for the current session. Credentials are stored securely and scoped to this session only.",
49+ InputSchema: schemaJSON,
50+ }
51+}
52+
53+// NewGetCredentialsTool creates the get_credentials tool definition.
54+func NewGetCredentialsTool() Tool {
55+ schema := map[string]interface{}{
56+ "type": "object",
57+ "properties": map[string]interface{}{},
58+ }
59+
60+ schemaJSON, _ := json.Marshal(schema)
61+
62+ return Tool{
63+ Name: "get_credentials",
64+ Description: "Retrieve saved game credentials (username and password) for the current session. Returns empty strings if no credentials are saved.",
65+ InputSchema: schemaJSON,
66+ }
67+}
68+
69+// MakeSaveCredentialsHandler creates a handler for save_credentials tool.
70+func MakeSaveCredentialsHandler(store CredentialStore, sessionID string) ToolHandler {
71+ return func(ctx context.Context, arguments json.RawMessage) (*ToolResult, error) {
72+ var args SaveCredentialsArgs
73+ if err := json.Unmarshal(arguments, &args); err != nil {
74+ return &ToolResult{
75+ Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Invalid arguments: %v", err)}},
76+ IsError: true,
77+ }, nil
78+ }
79+
80+ if args.Username == "" {
81+ return &ToolResult{
82+ Content: []ContentBlock{{Type: "text", Text: "Username cannot be empty"}},
83+ IsError: true,
84+ }, nil
85+ }
86+
87+ if args.Password == "" {
88+ return &ToolResult{
89+ Content: []ContentBlock{{Type: "text", Text: "Password cannot be empty"}},
90+ IsError: true,
91+ }, nil
92+ }
93+
94+ if err := store.SaveCredentials(sessionID, args.Username, args.Password); err != nil {
95+ return &ToolResult{
96+ Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to save credentials: %v", err)}},
97+ IsError: true,
98+ }, nil
99+ }
100+
101+ return &ToolResult{
102+ Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Credentials saved successfully for user '%s'", args.Username)}},
103+ IsError: false,
104+ }, nil
105+ }
106+}
107+
108+// MakeGetCredentialsHandler creates a handler for get_credentials tool.
109+func MakeGetCredentialsHandler(store CredentialStore, sessionID string) ToolHandler {
110+ return func(ctx context.Context, arguments json.RawMessage) (*ToolResult, error) {
111+ username, password, err := store.GetCredentials(sessionID)
112+ if err != nil {
113+ return &ToolResult{
114+ Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to retrieve credentials: %v", err)}},
115+ IsError: true,
116+ }, nil
117+ }
118+
119+ if username == "" && password == "" {
120+ return &ToolResult{
121+ Content: []ContentBlock{{Type: "text", Text: "No credentials saved for this session"}},
122+ IsError: false,
123+ }, nil
124+ }
125+
126+ result := GetCredentialsResult{
127+ Username: username,
128+ Password: password,
129+ }
130+
131+ resultJSON, err := json.Marshal(result)
132+ if err != nil {
133+ return &ToolResult{
134+ Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("Failed to format credentials: %v", err)}},
135+ IsError: true,
136+ }, nil
137+ }
138+
139+ return &ToolResult{
140+ Content: []ContentBlock{{Type: "text", Text: string(resultJSON)}},
141+ IsError: false,
142+ }, nil
143+ }
144+}
internal/mcp/tools_test.go+213−0
@@ -0,0 +1,213 @@
1+package mcp
2+
3+import (
4+ "context"
5+ "encoding/json"
6+ "testing"
7+)
8+
9+// mockCredentialStore is a mock implementation for testing.
10+type mockCredentialStore struct {
11+ credentials map[string]struct{ username, password string }
12+}
13+
14+func newMockCredentialStore() *mockCredentialStore {
15+ return &mockCredentialStore{
16+ credentials: make(map[string]struct{ username, password string }),
17+ }
18+}
19+
20+func (m *mockCredentialStore) SaveCredentials(sessionID, username, password string) error {
21+ m.credentials[sessionID] = struct{ username, password string }{username, password}
22+ return nil
23+}
24+
25+func (m *mockCredentialStore) GetCredentials(sessionID string) (string, string, error) {
26+ cred, exists := m.credentials[sessionID]
27+ if !exists {
28+ return "", "", nil
29+ }
30+ return cred.username, cred.password, nil
31+}
32+
33+func TestSaveCredentialsTool(t *testing.T) {
34+ store := newMockCredentialStore()
35+ sessionID := "test-session-123"
36+ handler := MakeSaveCredentialsHandler(store, sessionID)
37+
38+ tests := []struct {
39+ name string
40+ args SaveCredentialsArgs
41+ wantError bool
42+ wantText string
43+ }{
44+ {
45+ name: "valid credentials",
46+ args: SaveCredentialsArgs{Username: "player1", Password: "secret123"},
47+ wantError: false,
48+ wantText: "Credentials saved successfully for user 'player1'",
49+ },
50+ {
51+ name: "empty username",
52+ args: SaveCredentialsArgs{Username: "", Password: "secret123"},
53+ wantError: true,
54+ wantText: "Username cannot be empty",
55+ },
56+ {
57+ name: "empty password",
58+ args: SaveCredentialsArgs{Username: "player1", Password: ""},
59+ wantError: true,
60+ wantText: "Password cannot be empty",
61+ },
62+ }
63+
64+ for _, tt := range tests {
65+ t.Run(tt.name, func(t *testing.T) {
66+ argsJSON, _ := json.Marshal(tt.args)
67+ result, err := handler(context.Background(), argsJSON)
68+
69+ if err != nil {
70+ t.Fatalf("unexpected error: %v", err)
71+ }
72+
73+ if result.IsError != tt.wantError {
74+ t.Errorf("IsError = %v, want %v", result.IsError, tt.wantError)
75+ }
76+
77+ if len(result.Content) == 0 {
78+ t.Fatal("expected content blocks")
79+ }
80+
81+ if result.Content[0].Text != tt.wantText {
82+ t.Errorf("Text = %q, want %q", result.Content[0].Text, tt.wantText)
83+ }
84+ })
85+ }
86+}
87+
88+func TestGetCredentialsTool(t *testing.T) {
89+ store := newMockCredentialStore()
90+ sessionID := "test-session-456"
91+ handler := MakeGetCredentialsHandler(store, sessionID)
92+
93+ t.Run("no credentials saved", func(t *testing.T) {
94+ result, err := handler(context.Background(), json.RawMessage("{}"))
95+ if err != nil {
96+ t.Fatalf("unexpected error: %v", err)
97+ }
98+
99+ if result.IsError {
100+ t.Error("expected no error")
101+ }
102+
103+ if result.Content[0].Text != "No credentials saved for this session" {
104+ t.Errorf("unexpected text: %s", result.Content[0].Text)
105+ }
106+ })
107+
108+ t.Run("credentials exist", func(t *testing.T) {
109+ // Save credentials first
110+ store.SaveCredentials(sessionID, "player2", "pass456")
111+
112+ result, err := handler(context.Background(), json.RawMessage("{}"))
113+ if err != nil {
114+ t.Fatalf("unexpected error: %v", err)
115+ }
116+
117+ if result.IsError {
118+ t.Error("expected no error")
119+ }
120+
121+ var creds GetCredentialsResult
122+ if err := json.Unmarshal([]byte(result.Content[0].Text), &creds); err != nil {
123+ t.Fatalf("failed to parse result: %v", err)
124+ }
125+
126+ if creds.Username != "player2" {
127+ t.Errorf("Username = %q, want %q", creds.Username, "player2")
128+ }
129+
130+ if creds.Password != "pass456" {
131+ t.Errorf("Password = %q, want %q", creds.Password, "pass456")
132+ }
133+ })
134+}
135+
136+func TestCredentialToolDefinitions(t *testing.T) {
137+ t.Run("save_credentials tool", func(t *testing.T) {
138+ tool := NewSaveCredentialsTool()
139+
140+ if tool.Name != "save_credentials" {
141+ t.Errorf("Name = %q, want %q", tool.Name, "save_credentials")
142+ }
143+
144+ if tool.Description == "" {
145+ t.Error("Description should not be empty")
146+ }
147+
148+ if len(tool.InputSchema) == 0 {
149+ t.Error("InputSchema should not be empty")
150+ }
151+
152+ var schema map[string]interface{}
153+ if err := json.Unmarshal(tool.InputSchema, &schema); err != nil {
154+ t.Fatalf("failed to parse schema: %v", err)
155+ }
156+
157+ if schema["type"] != "object" {
158+ t.Error("schema type should be object")
159+ }
160+ })
161+
162+ t.Run("get_credentials tool", func(t *testing.T) {
163+ tool := NewGetCredentialsTool()
164+
165+ if tool.Name != "get_credentials" {
166+ t.Errorf("Name = %q, want %q", tool.Name, "get_credentials")
167+ }
168+
169+ if tool.Description == "" {
170+ t.Error("Description should not be empty")
171+ }
172+
173+ if len(tool.InputSchema) == 0 {
174+ t.Error("InputSchema should not be empty")
175+ }
176+ })
177+}
178+
179+func TestSessionIsolation(t *testing.T) {
180+ store := newMockCredentialStore()
181+
182+ // Session 1
183+ session1 := "session-1"
184+ handler1 := MakeSaveCredentialsHandler(store, session1)
185+ args1, _ := json.Marshal(SaveCredentialsArgs{Username: "user1", Password: "pass1"})
186+ handler1(context.Background(), args1)
187+
188+ // Session 2
189+ session2 := "session-2"
190+ handler2 := MakeSaveCredentialsHandler(store, session2)
191+ args2, _ := json.Marshal(SaveCredentialsArgs{Username: "user2", Password: "pass2"})
192+ handler2(context.Background(), args2)
193+
194+ // Verify session 1 credentials
195+ getHandler1 := MakeGetCredentialsHandler(store, session1)
196+ result1, _ := getHandler1(context.Background(), json.RawMessage("{}"))
197+ var creds1 GetCredentialsResult
198+ json.Unmarshal([]byte(result1.Content[0].Text), &creds1)
199+
200+ if creds1.Username != "user1" {
201+ t.Errorf("Session 1: Username = %q, want %q", creds1.Username, "user1")
202+ }
203+
204+ // Verify session 2 credentials
205+ getHandler2 := MakeGetCredentialsHandler(store, session2)
206+ result2, _ := getHandler2(context.Background(), json.RawMessage("{}"))
207+ var creds2 GetCredentialsResult
208+ json.Unmarshal([]byte(result2.Content[0].Text), &creds2)
209+
210+ if creds2.Username != "user2" {
211+ t.Errorf("Session 2: Username = %q, want %q", creds2.Username, "user2")
212+ }
213+}
internal/store/credentials_test.go+153−0
@@ -0,0 +1,153 @@
1+package store
2+
3+import (
4+ "testing"
5+)
6+
7+func TestCredentialStorage(t *testing.T) {
8+ store, err := Open()
9+ if err != nil {
10+ t.Fatalf("failed to open store: %v", err)
11+ }
12+ defer store.Close()
13+
14+ sessionID := "test-cred-session"
15+
16+ // Create a test session
17+ if err := store.CreateSession(sessionID, "opencode", "test-model", nil); err != nil {
18+ t.Fatalf("failed to create session: %v", err)
19+ }
20+ defer store.DeleteSession(sessionID)
21+
22+ t.Run("save and retrieve credentials", func(t *testing.T) {
23+ username := "testuser"
24+ password := "testpass123"
25+
26+ // Save credentials
27+ if err := store.SaveCredentials(sessionID, username, password); err != nil {
28+ t.Fatalf("failed to save credentials: %v", err)
29+ }
30+
31+ // Retrieve credentials
32+ gotUsername, gotPassword, err := store.GetCredentials(sessionID)
33+ if err != nil {
34+ t.Fatalf("failed to get credentials: %v", err)
35+ }
36+
37+ if gotUsername != username {
38+ t.Errorf("Username = %q, want %q", gotUsername, username)
39+ }
40+
41+ if gotPassword != password {
42+ t.Errorf("Password = %q, want %q", gotPassword, password)
43+ }
44+ })
45+
46+ t.Run("update existing credentials", func(t *testing.T) {
47+ // Save initial credentials
48+ store.SaveCredentials(sessionID, "user1", "pass1")
49+
50+ // Update credentials
51+ newUsername := "user2"
52+ newPassword := "pass2"
53+ if err := store.SaveCredentials(sessionID, newUsername, newPassword); err != nil {
54+ t.Fatalf("failed to update credentials: %v", err)
55+ }
56+
57+ // Retrieve updated credentials
58+ gotUsername, gotPassword, err := store.GetCredentials(sessionID)
59+ if err != nil {
60+ t.Fatalf("failed to get credentials: %v", err)
61+ }
62+
63+ if gotUsername != newUsername {
64+ t.Errorf("Username = %q, want %q", gotUsername, newUsername)
65+ }
66+
67+ if gotPassword != newPassword {
68+ t.Errorf("Password = %q, want %q", gotPassword, newPassword)
69+ }
70+ })
71+
72+ t.Run("no credentials for session", func(t *testing.T) {
73+ nonExistentSession := "no-such-session"
74+
75+ username, password, err := store.GetCredentials(nonExistentSession)
76+ if err != nil {
77+ t.Fatalf("unexpected error: %v", err)
78+ }
79+
80+ if username != "" || password != "" {
81+ t.Errorf("expected empty credentials, got username=%q password=%q", username, password)
82+ }
83+ })
84+
85+ t.Run("credentials deleted with session", func(t *testing.T) {
86+ // Create new session
87+ tempSession := "temp-session"
88+ store.CreateSession(tempSession, "opencode", "test-model", nil)
89+
90+ // Save credentials
91+ store.SaveCredentials(tempSession, "tempuser", "temppass")
92+
93+ // Verify credentials exist
94+ username, password, _ := store.GetCredentials(tempSession)
95+ if username != "tempuser" {
96+ t.Fatal("credentials not saved")
97+ }
98+
99+ // Delete session
100+ store.DeleteSession(tempSession)
101+
102+ // Verify credentials are gone
103+ username, password, err := store.GetCredentials(tempSession)
104+ if err != nil {
105+ t.Fatalf("unexpected error: %v", err)
106+ }
107+
108+ if username != "" || password != "" {
109+ t.Errorf("credentials should be deleted with session")
110+ }
111+ })
112+}
113+
114+func TestCredentialSessionIsolation(t *testing.T) {
115+ store, err := Open()
116+ if err != nil {
117+ t.Fatalf("failed to open store: %v", err)
118+ }
119+ defer store.Close()
120+
121+ session1 := "session-1"
122+ session2 := "session-2"
123+
124+ // Create sessions
125+ store.CreateSession(session1, "opencode", "test-model", nil)
126+ store.CreateSession(session2, "opencode", "test-model", nil)
127+ defer store.DeleteSession(session1)
128+ defer store.DeleteSession(session2)
129+
130+ // Save different credentials for each session
131+ store.SaveCredentials(session1, "user1", "pass1")
132+ store.SaveCredentials(session2, "user2", "pass2")
133+
134+ // Verify session 1
135+ username1, password1, err := store.GetCredentials(session1)
136+ if err != nil {
137+ t.Fatalf("failed to get session1 credentials: %v", err)
138+ }
139+
140+ if username1 != "user1" || password1 != "pass1" {
141+ t.Errorf("session1: got %q/%q, want user1/pass1", username1, password1)
142+ }
143+
144+ // Verify session 2
145+ username2, password2, err := store.GetCredentials(session2)
146+ if err != nil {
147+ t.Fatalf("failed to get session2 credentials: %v", err)
148+ }
149+
150+ if username2 != "user2" || password2 != "pass2" {
151+ t.Errorf("session2: got %q/%q, want user2/pass2", username2, password2)
152+ }
153+}
internal/store/store.go+40−1
@@ -48,7 +48,7 @@ func Open() (*Store, error) {
4848 }
4949
5050 dbPath := filepath.Join(dataDir, "mysis.db")
51- db, err := sql.Open("sqlite3", dbPath+"?_journal_mode=WAL")
51+ db, err := sql.Open("sqlite3", dbPath+"?_journal_mode=WAL&_foreign_keys=1")
5252 if err != nil {
5353 return nil, fmt.Errorf("open database: %w", err)
5454 }
@@ -92,6 +92,15 @@ func (s *Store) initSchema() error {
9292 FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
9393 );
9494
95+ CREATE TABLE IF NOT EXISTS session_credentials (
96+ session_id TEXT PRIMARY KEY,
97+ username TEXT NOT NULL,
98+ password TEXT NOT NULL,
99+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
100+ updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
101+ FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
102+ );
103+
95104 CREATE INDEX IF NOT EXISTS idx_messages_session
96105 ON messages(session_id, created_at);
97106 `)
@@ -322,3 +331,33 @@ func (s *Store) DeleteSessionByName(name string) error {
322331
323332 return nil
324333 }
334+
335+// SaveCredentials stores game credentials for a session.
336+func (s *Store) SaveCredentials(sessionID, username, password string) error {
337+ query := `
338+ INSERT INTO session_credentials (session_id, username, password, created_at, updated_at)
339+ VALUES (?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
340+ ON CONFLICT(session_id) DO UPDATE SET
341+ username = excluded.username,
342+ password = excluded.password,
343+ updated_at = CURRENT_TIMESTAMP
344+ `
345+ _, err := s.db.Exec(query, sessionID, username, password)
346+ if err != nil {
347+ return fmt.Errorf("save credentials: %w", err)
348+ }
349+ return nil
350+}
351+
352+// GetCredentials retrieves game credentials for a session.
353+func (s *Store) GetCredentials(sessionID string) (username, password string, err error) {
354+ query := `SELECT username, password FROM session_credentials WHERE session_id = ?`
355+ err = s.db.QueryRow(query, sessionID).Scan(&username, &password)
356+ if err == sql.ErrNoRows {
357+ return "", "", nil
358+ }
359+ if err != nil {
360+ return "", "", fmt.Errorf("get credentials: %w", err)
361+ }
362+ return username, password, nil
363+}