signAssertion method
A signed JWT asking for scope, valid for lifetime (Google allows at
most one hour).
Implementation
String signAssertion({
required String scope,
DateTime? now,
Duration lifetime = const Duration(hours: 1),
}) {
if (lifetime > const Duration(hours: 1) || lifetime <= Duration.zero) {
throw ArgumentError.value(lifetime, 'lifetime',
'Google accepts an assertion that lives up to one hour.');
}
final int issuedAt =
(now ?? DateTime.now()).toUtc().millisecondsSinceEpoch ~/ 1000;
final String signingInput = '${_segment(<String, Object?>{
'alg': 'RS256',
'typ': 'JWT',
if (privateKeyId != null) 'kid': privateKeyId,
})}.${_segment(<String, Object?>{
'iss': clientEmail,
'scope': scope,
'aud': tokenUri.toString(),
'iat': issuedAt,
'exp': issuedAt + lifetime.inSeconds,
})}';
final RSAPrivateKey key;
try {
key = DVPatchSigning.privateKey(_privateKeyPem);
} on Object {
// Not the underlying message: a parser error can quote the input.
throw ArgumentError.value('<redacted>', 'private_key',
'The service account private key could not be read as PEM.');
}
final RSASigner signer = RSASigner(SHA256Digest(), '0609608648016503040201')
..init(true, PrivateKeyParameter<RSAPrivateKey>(key));
final RSASignature signature =
signer.generateSignature(Uint8List.fromList(utf8.encode(signingInput)));
return '$signingInput.${_base64Url(signature.bytes)}';
}