fromMap static method

Token fromMap(
  1. Map? map
)

Implementation

static Token fromMap(Map? map) {
  if (map == null) throw Exception('No token received');
  //error handling as described in https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#error-response-1
  if (map['error'] != null) {
    throw Exception('Error during token request: ${map['error']}:'
        ' ${map['error_description']}');
  }

  Token model = Token();
  model.accessToken = map['access_token'];
  model.idToken = map['id_token'];

  Map<String, dynamic> payload = {...Jwt.parseJwt(model.idToken!)};
  final iat = DateTime.fromMillisecondsSinceEpoch(0, isUtc: true)
      .add(Duration(seconds: payload['iat']));
  final exp = DateTime.fromMillisecondsSinceEpoch(0, isUtc: true)
      .add(Duration(seconds: payload['exp']))
      .add(Duration(seconds: -model.tokenRefreshAdvanceInSeconds));

  model.tokenType = map['token_type'];
  model.expiresIn = map['expires_in'] is int
      ? map['expires_in']
      : int.tryParse(map['expires_in'].toString()) ?? 60;
  model.refreshToken = map['refresh_token'];
  model.issueTimeStamp = iat.toUtc();
  model.expireTimeStamp = exp.toUtc();
  return model;
}