generateMockIdToken method

String generateMockIdToken({
  1. required String projectId,
  2. required String uid,
  3. Map<String, dynamic>? overrides,
})

Implementation

String generateMockIdToken({
  required String projectId,
  required String uid,
  Map<String, dynamic>? overrides,
}) {
  overrides ??= {};

  final ServiceAccountCredential certificateObject =
      Credentials.applicationDefault() as ServiceAccountCredential;

  final Map<String, dynamic> claims = {
    'aud': projectId,
    'exp': clock.now().add(Duration(hours: 1)).millisecondsSinceEpoch ~/ 1000,
    'iss': 'https://securetoken.google.com/$projectId',
    'sub': uid,
    'auth_time': clock.now().millisecondsSinceEpoch ~/ 1000,
    ...overrides,
  };

  final JsonWebSignatureBuilder builder = JsonWebSignatureBuilder()
    ..jsonContent = claims
    ..setProtectedHeader(
      'kid',
      certificateObject.certificate.privateKey.keyId,
    )
    ..addRecipient(
      certificateObject.certificate.privateKey,
      algorithm: 'RS256',
    );

  return builder.build().toCompactSerialization();
}