http_auth_multi_round_parse.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/http/http_auth_multi_round_parse.h"
  5. #include "base/base64.h"
  6. #include "base/strings/string_piece.h"
  7. #include "base/strings/string_util.h"
  8. #include "net/http/http_auth_challenge_tokenizer.h"
  9. namespace net {
  10. namespace {
  11. // Check that the scheme in the challenge matches the expected scheme
  12. bool SchemeIsValid(HttpAuth::Scheme scheme,
  13. HttpAuthChallengeTokenizer* challenge) {
  14. return challenge->auth_scheme() == HttpAuth::SchemeToString(scheme);
  15. }
  16. } // namespace
  17. HttpAuth::AuthorizationResult ParseFirstRoundChallenge(
  18. HttpAuth::Scheme scheme,
  19. HttpAuthChallengeTokenizer* challenge) {
  20. if (!SchemeIsValid(scheme, challenge))
  21. return HttpAuth::AUTHORIZATION_RESULT_INVALID;
  22. std::string encoded_auth_token = challenge->base64_param();
  23. if (!encoded_auth_token.empty()) {
  24. return HttpAuth::AUTHORIZATION_RESULT_INVALID;
  25. }
  26. return HttpAuth::AUTHORIZATION_RESULT_ACCEPT;
  27. }
  28. HttpAuth::AuthorizationResult ParseLaterRoundChallenge(
  29. HttpAuth::Scheme scheme,
  30. HttpAuthChallengeTokenizer* challenge,
  31. std::string* encoded_token,
  32. std::string* decoded_token) {
  33. if (!SchemeIsValid(scheme, challenge))
  34. return HttpAuth::AUTHORIZATION_RESULT_INVALID;
  35. *encoded_token = challenge->base64_param();
  36. if (encoded_token->empty())
  37. return HttpAuth::AUTHORIZATION_RESULT_REJECT;
  38. if (!base::Base64Decode(*encoded_token, decoded_token))
  39. return HttpAuth::AUTHORIZATION_RESULT_INVALID;
  40. return HttpAuth::AUTHORIZATION_RESULT_ACCEPT;
  41. }
  42. } // namespace net