1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef NET_HTTP_HTTP_AUTH_CHALLENGE_TOKENIZER_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define NET_HTTP_HTTP_AUTH_CHALLENGE_TOKENIZER_
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <string>
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/base/net_export.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/http/http_util.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace net {
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Breaks up a challenge string into the the auth scheme and parameter list,
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// according to RFC 2617 Sec 1.2:
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//     challenge = auth-scheme 1*SP 1#auth-param
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Depending on the challenge scheme, it may be appropriate to interpret the
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// parameters as either a base-64 encoded string or a comma-delimited list
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// of name-value pairs. param_pairs() and base64_param() methods are provided
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// to support either usage.
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class NET_EXPORT_PRIVATE HttpAuthChallengeTokenizer {
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  HttpAuthChallengeTokenizer(std::string::const_iterator begin,
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                             std::string::const_iterator end);
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Get the original text.
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string challenge_text() const {
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return std::string(begin_, end_);
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Get the auth scheme of the challenge.
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string::const_iterator scheme_begin() const { return scheme_begin_; }
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string::const_iterator scheme_end() const { return scheme_end_; }
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string scheme() const {
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return std::string(scheme_begin_, scheme_end_);
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
4023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  std::string::const_iterator params_begin() const { return params_begin_; }
4123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  std::string::const_iterator params_end() const { return params_end_; }
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  HttpUtil::NameValuePairsIterator param_pairs() const;
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string base64_param() const;
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Init(std::string::const_iterator begin,
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            std::string::const_iterator end);
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string::const_iterator begin_;
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string::const_iterator end_;
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string::const_iterator scheme_begin_;
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string::const_iterator scheme_end_;
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string::const_iterator params_begin_;
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string::const_iterator params_end_;
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace net
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // NET_HTTP_HTTP_AUTH_CHALLENGE_TOKENIZER_
62