authorize method

String authorize({
  1. required DigestChallenge challenge,
  2. required String username,
  3. required String password,
  4. required String method,
  5. required String uri,
})

Builds the value (without the leading Digest ) for an Authorization header given a challenge and credentials. The caller writes: Authorization: Digest

Implementation

String authorize({
  required DigestChallenge challenge,
  required String username,
  required String password,
  required String method,
  required String uri,
}) {
  final ha1 = _md5('$username:${challenge.realm}:$password');
  final ha2 = _md5('$method:$uri');
  String response;
  final parts = <String>[
    'username="$username"',
    'realm="${challenge.realm}"',
    'nonce="${challenge.nonce}"',
    'uri="$uri"',
    'algorithm=${challenge.algorithm}',
  ];

  final qop = _pickQop(challenge.qop);
  if (qop == 'auth') {
    _nc++;
    final ncHex = _nc.toRadixString(16).padLeft(8, '0');
    final cnonce = _cnonce();
    response = _md5('$ha1:${challenge.nonce}:$ncHex:$cnonce:auth:$ha2');
    parts
      ..add('qop=auth')
      ..add('nc=$ncHex')
      ..add('cnonce="$cnonce"');
  } else {
    response = _md5('$ha1:${challenge.nonce}:$ha2');
  }
  parts.add('response="$response"');
  if (challenge.opaque != null) {
    parts.add('opaque="${challenge.opaque}"');
  }
  return parts.join(', ');
}