todo-app/lib/mcp/token_file.dart
Krzysztof kuhy Rudnicki 2a9332094a
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
MCP: Dart task server replaces Python todo_mcp
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-02 09:36:44 +01:00

32 lines
942 B
Dart

import 'dart:io';
import 'package:crdt_sync/crdt_sync.dart';
/// sha256 hex of [token] (crdt_sync's `revisionOf` is exactly that).
String tokenHash(String token) => revisionOf(token);
Set<String> _hashes(File tokens) {
final String text;
try {
text = tokens.readAsStringSync();
} on FileSystemException {
return const {};
}
return {
for (final line in text.split('\n'))
if (line.trim() case final t when t.isNotEmpty && !t.startsWith('#'))
t.split(RegExp(r'\s+')).first.toLowerCase(),
};
}
/// Whether [authorization] bears a token hashed in [tokens], read per call.
bool tokenAccepted(File tokens, String? authorization) {
const scheme = 'bearer ';
if (authorization == null ||
!authorization.toLowerCase().startsWith(scheme)) {
return false;
}
final token = authorization.substring(scheme.length).trim();
return token.isNotEmpty && _hashes(tokens).contains(tokenHash(token));
}