authenticate method

Future<String> authenticate(
  1. AuthOptions options
)

Authenticate the service account with AuthOptions against the provided issuer. The issuer must be provided. You may use ZitadelIssuer.

Implementation

Future<String> authenticate(AuthOptions options) async {
  final discoveryResponse = await http.get(
    Uri.parse(
        _discoveryEndpoint(options.discoveryEndpoint ?? options.issuer)),
    headers: {HttpHeaders.acceptHeader: 'application/json; charset=UTF-8'},
  );
  final issuerJson = jsonDecode(utf8.decode(discoveryResponse.bodyBytes));
  final tokenEndpoint = issuerJson['token_endpoint'];
  final jwt = getSignedJwt(options.issuer);

  final response = await http.post(
    Uri.parse(tokenEndpoint),
    headers: {HttpHeaders.acceptHeader: 'application/json; charset=UTF-8'},
    body: {
      'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      'assertion': jwt,
      'scope': options._createScopes(),
    },
  );

  if (response.statusCode > 299) {
    throw Exception(
        'An error occurred. Status Code: ${response.statusCode}.');
  }

  final json = jsonDecode(utf8.decode(response.bodyBytes));

  return json['access_token'];
}