2019-07-18 02:20:04 +00:00
|
|
|
part of 'repositories.dart';
|
2019-07-04 21:45:05 +00:00
|
|
|
|
2019-07-11 06:32:17 +00:00
|
|
|
class GRPCCredentialsClient implements CredentialsRepo {
|
2019-07-13 19:48:32 +00:00
|
|
|
static GRPCCredentialsClient _instance;
|
2019-07-12 03:15:30 +00:00
|
|
|
grpc.CredentialsClient _client;
|
2019-07-04 21:45:05 +00:00
|
|
|
|
2019-07-11 06:32:17 +00:00
|
|
|
GRPCCredentialsClient(ConnectionConfig config) {
|
2019-07-04 21:45:05 +00:00
|
|
|
final caCert = utf8.encode(config.caCertificate);
|
|
|
|
final cert = utf8.encode(config.certificate);
|
|
|
|
final privateCert = utf8.encode(config.privateCertificate);
|
|
|
|
|
|
|
|
final splitHost = config.host.split(':');
|
|
|
|
final hostname = splitHost[0];
|
|
|
|
final port = int.parse(splitHost[1]);
|
|
|
|
|
2019-07-12 03:15:30 +00:00
|
|
|
_client = grpc.CredentialsClient(ClientChannel(
|
2019-07-04 21:45:05 +00:00
|
|
|
hostname,
|
|
|
|
port: port,
|
|
|
|
options: ChannelOptions(
|
|
|
|
credentials: _ChannelCredentials(caCert, cert, privateCert),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-07-13 19:48:32 +00:00
|
|
|
factory GRPCCredentialsClient.getInstance({ConnectionConfig config}) =>
|
|
|
|
config == null ? _instance : _instance = GRPCCredentialsClient(config);
|
2019-07-07 04:14:09 +00:00
|
|
|
|
2019-07-04 21:45:05 +00:00
|
|
|
Stream<Metadata> getAllMetadata(String sourceHost) {
|
2019-07-12 03:15:30 +00:00
|
|
|
final request = grpc.SourceHostRequest();
|
2019-07-04 21:45:05 +00:00
|
|
|
request.sourceHost = sourceHost;
|
|
|
|
|
|
|
|
return _client.getAllMetadata(request).map<Metadata>(
|
|
|
|
(protobuf.Metadata pbMetadata) => Metadata.fromProtobuf(pbMetadata));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Credential> get(String id) async {
|
|
|
|
final request = grpc.IdRequest();
|
|
|
|
request.id = id;
|
|
|
|
|
|
|
|
return Credential.fromProtobuf(await _client.get(request));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Credential> create(CredentialInput input) async {
|
|
|
|
return Credential();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Credential> update(String id, CredentialInput input) async {
|
|
|
|
return Credential();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> delete(String id) {
|
|
|
|
final request = grpc.IdRequest();
|
|
|
|
request.id = id;
|
|
|
|
|
|
|
|
return _client.delete(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChannelCredentials extends ChannelCredentials {
|
|
|
|
final List<int> _key;
|
|
|
|
final List<int> _cert;
|
|
|
|
|
|
|
|
const _ChannelCredentials(List<int> caCert, this._cert, this._key)
|
|
|
|
: super.secure(certificates: caCert);
|
|
|
|
|
|
|
|
@override
|
|
|
|
SecurityContext get securityContext {
|
|
|
|
return super.securityContext
|
|
|
|
..usePrivateKeyBytes(_key)
|
|
|
|
..useCertificateChainBytes(_cert);
|
|
|
|
}
|
|
|
|
}
|