generate method

  1. @override
Future<Token> generate()
override

generate a OAuth2 refresh token from JWT credentials

Implementation

@override
Future<Token> generate() async {
  final key = JsonWebKey.fromPem(jwtCredentials.settings.privateKey);

  final privateKey = key.cryptoKeyPair;

  final signer = privateKey.createSigner(algorithms.signing.rsa.sha256);

  final header = Util.base64GCloudString('{"alg":"RS256","typ":"JWT"}');

  final claim = Util.base64GCloudString(
      '{"iss": "${jwtCredentials.settings.clientEmail}","scope": "${jwtCredentials.scope}","aud": "https://www.googleapis.com/oauth2/v4/token",' +
          '"exp": ${Util.unixTimeStamp(DateTime.now().add(Duration(seconds: 3599)))},"iat": ${Util.unixTimeStamp(DateTime.now())}}');

  final signature = signer.sign('${header}.${claim}'.codeUnits);

  final jwt = '${header}.${claim}.' + Util.base64GCloudList(signature.data);

  final OAuthClient oAuthClient = new OAuthClient(dio);

  return await oAuthClient.getToken({
    'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion': jwt
  });
}