-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathcredential.dart
More file actions
97 lines (79 loc) · 2.65 KB
/
credential.dart
File metadata and controls
97 lines (79 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import 'dart:io';
import 'package:googleapis_auth/auth_io.dart' as googleapis_auth;
import 'package:meta/meta.dart';
sealed class Credential {
factory Credential.fromApplicationDefaultCredentials({
String? serviceAccountId,
}) {
return ApplicationDefaultCredential._(serviceAccountId: serviceAccountId);
}
factory Credential.fromServiceAccount(File serviceAccountFile) {
try {
final json = serviceAccountFile.readAsStringSync();
final credentials = googleapis_auth.ServiceAccountCredentials.fromJson(
json,
);
return ServiceAccountCredential._(credentials);
} catch (e) {
throw ArgumentError('Failed to parse service account JSON: $e');
}
}
factory Credential.fromServiceAccountParams({
String? clientId,
required String privateKey,
required String email,
required String projectId,
}) {
try {
final json = {
'type': 'service_account',
'project_id': projectId,
'private_key': privateKey,
'client_email': email,
'client_id': clientId ?? '',
};
final credentials = googleapis_auth.ServiceAccountCredentials.fromJson(
json,
);
return ServiceAccountCredential._(credentials);
} catch (e) {
throw ArgumentError('Failed to create service account credentials: $e');
}
}
Credential._();
@internal
googleapis_auth.ServiceAccountCredentials? get serviceAccountCredentials;
@internal
String? get serviceAccountId;
}
@internal
final class ServiceAccountCredential extends Credential {
ServiceAccountCredential._(this._serviceAccountCredentials) : super._() {
if (_serviceAccountCredentials.projectId == null) {
throw ArgumentError(
'Service account JSON must contain a "project_id" property',
);
}
}
final googleapis_auth.ServiceAccountCredentials _serviceAccountCredentials;
String get projectId => _serviceAccountCredentials.projectId!;
String get clientEmail => _serviceAccountCredentials.email;
String get privateKey => _serviceAccountCredentials.privateKey;
@override
googleapis_auth.ServiceAccountCredentials? get serviceAccountCredentials =>
_serviceAccountCredentials;
@override
String? get serviceAccountId => _serviceAccountCredentials.email;
}
@internal
final class ApplicationDefaultCredential extends Credential {
ApplicationDefaultCredential._({String? serviceAccountId})
: _serviceAccountId = serviceAccountId,
super._();
final String? _serviceAccountId;
@override
googleapis_auth.ServiceAccountCredentials? get serviceAccountCredentials =>
null;
@override
String? get serviceAccountId => _serviceAccountId;
}