41 lines
1.2 KiB
Dart
41 lines
1.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
class BackendIdentity {
|
|
BackendIdentity._();
|
|
|
|
static const internalBuild = bool.fromEnvironment('INTERNAL_BUILD');
|
|
static const configuredBaseUrl = String.fromEnvironment('API_BASE_URL');
|
|
|
|
static const String baseUrl = configuredBaseUrl != ''
|
|
? configuredBaseUrl
|
|
: 'https://api.invalid';
|
|
|
|
static final String scope = scopeForBaseUrl(baseUrl);
|
|
|
|
static String accountNamespace(int userId) => 'account_${scope}_$userId';
|
|
|
|
static String scopeForBaseUrl(String value) => sha256
|
|
.convert(utf8.encode(_normalizedBaseUrl(value)))
|
|
.toString()
|
|
.substring(0, 16);
|
|
|
|
static String accountNamespaceFor(String baseUrl, int userId) =>
|
|
'account_${scopeForBaseUrl(baseUrl)}_$userId';
|
|
|
|
static String _normalizedBaseUrl(String value) {
|
|
final uri = Uri.tryParse(value.trim());
|
|
if (uri == null || !uri.hasScheme || uri.host.isEmpty) {
|
|
return value.trim().toLowerCase();
|
|
}
|
|
final path = uri.path == '/'
|
|
? ''
|
|
: uri.path.replaceFirst(RegExp(r'/+$'), '');
|
|
return uri
|
|
.replace(path: path, query: null, fragment: null)
|
|
.toString()
|
|
.toLowerCase();
|
|
}
|
|
}
|