AccessTokenResponse.fromHttpResponse constructor

  1. @override
AccessTokenResponse.fromHttpResponse(
  1. Response response, {
  2. List<String>? requestedScopes,
})

Implementation

@override
factory AccessTokenResponse.fromHttpResponse(http.Response response,
    {List<String>? requestedScopes}) {
  AccessTokenResponse resp;

  var defMap = {'http_status_code': response.statusCode};
  if (response.body != '') {
    Map<String, dynamic> rMap = jsonDecode(response.body);
    //From Section 4.2.2. (Access Token Response) of OAuth2 rfc, the "scope" parameter in the Access Token Response is
    //"OPTIONAL, if identical to the scope requested by the client; otherwise, REQUIRED."
    if ((!rMap.containsKey('scope') ||
        rMap['scope'] == null ||
        rMap['scope'].isEmpty)) {
      if (requestedScopes != null) {
        rMap['scope'] = requestedScopes;
      }
    }

    if (rMap.containsKey('expires_in')) {
      int expiresIn;

      try {
        expiresIn = rMap['expires_in'] is String
            ? int.parse(rMap['expires_in'])
            : rMap['expires_in'];
      } on FormatException {
        expiresIn = 0;
      }

      rMap['expires_in'] = expiresIn;

      rMap['expiration_date'] = DateTime.now()
          .add(Duration(seconds: expiresIn))
          .millisecondsSinceEpoch;
    }

    resp = AccessTokenResponse.fromMap({...rMap, ...defMap});
  } else {
    resp = AccessTokenResponse.fromMap({
      ...defMap,
      ...{'scope': requestedScopes}
    });
  }

  return resp;
}