getSignedJwt method

String getSignedJwt(
  1. String audience
)

Create and sign a JWT for the given audience.

The JWT is signed with the provided key. The JWT is valid for one hour.

Implementation

String getSignedJwt(String audience) {
  final claims = JsonWebTokenClaims.fromJson({
    'iss': clientId,
    'sub': clientId,
    'iat': DateTime.now().toUtc().millisecondsSinceEpoch ~/ 1000,
    'exp': DateTime.now()
            .toUtc()
            .add(Duration(hours: 1))
            .millisecondsSinceEpoch ~/
        1000,
    'aud': audience,
  });

  final builder = JsonWebSignatureBuilder();
  builder.jsonContent = claims.toJson();
  builder.addRecipient(JsonWebKey.fromPem(key, keyId: keyId),
      algorithm: 'RS256');

  final signature = builder.build();

  return signature.toCompactSerialization();
}