OpenContacts/lib/models/authentication_data.dart

40 lines
1.3 KiB
Dart
Raw Normal View History

2023-09-29 03:51:46 -04:00
import 'package:contacts_plus_plus/config.dart';
2023-04-29 13:18:46 -04:00
class AuthenticationData {
2023-09-29 03:51:46 -04:00
static const _unauthenticated = AuthenticationData(userId: "", token: "", secretMachineIdHash: "", isAuthenticated: false);
2023-04-29 13:18:46 -04:00
final String userId;
final String token;
2023-09-29 03:51:46 -04:00
final String secretMachineIdHash;
2023-04-29 13:18:46 -04:00
final bool isAuthenticated;
const AuthenticationData({
2023-09-29 03:51:46 -04:00
required this.userId, required this.token, required this.secretMachineIdHash, required this.isAuthenticated
2023-04-29 13:18:46 -04:00
});
2023-04-29 15:26:12 -04:00
factory AuthenticationData.fromMap(Map map) {
2023-09-29 03:51:46 -04:00
map = map["entity"];
2023-04-29 15:26:12 -04:00
final userId = map["userId"];
final token = map["token"];
2023-09-29 03:51:46 -04:00
final machineId = map["secretMachineIdHash"];
2023-04-29 13:18:46 -04:00
if (userId == null || token == null || machineId == null) {
return _unauthenticated;
}
2023-09-29 03:51:46 -04:00
return AuthenticationData(userId: userId, token: token, secretMachineIdHash: machineId, isAuthenticated: true);
2023-04-29 13:18:46 -04:00
}
factory AuthenticationData.unauthenticated() => _unauthenticated;
Map<String, String> get authorizationHeader => {
2023-09-29 03:51:46 -04:00
"Authorization": "res $userId:$token",
"SecretClientAccessKey": Config.secretClientKey,
"UID":"2cde2bd72c104d1785af28ae77c29fc2",
};
Map<String, dynamic> toMap() {
return {
"userId": userId,
"token": token,
2023-09-29 03:51:46 -04:00
"secretMachineId": secretMachineIdHash,
};
}
2023-04-29 13:18:46 -04:00
}