15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_BASE_AUTH_H__
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_BASE_AUTH_H__
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
115e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string16.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/host_port_pair.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Holds info about an authentication challenge that we may want to display
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to the user.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT AuthChallengeInfo :
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public base::RefCountedThreadSafe<AuthChallengeInfo> {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AuthChallengeInfo();
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Determines whether two AuthChallengeInfo's are equivalent.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Equals(const AuthChallengeInfo& other) const;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether this came from a server or a proxy.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_proxy;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The service issuing the challenge.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HostPortPair challenger;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The authentication scheme used, such as "basic" or "digest". If the
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |source| is FTP_SERVER, this is an empty string. The encoding is ASCII.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string scheme;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The realm of the challenge. May be empty. The encoding is UTF-8.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string realm;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::RefCountedThreadSafe<AuthChallengeInfo>;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~AuthChallengeInfo();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Authentication Credentials for an authentication credentials.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT AuthCredentials {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AuthCredentials();
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AuthCredentials(const base::string16& username,
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  const base::string16& password);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~AuthCredentials();
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set the |username| and |password|.
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void Set(const base::string16& username, const base::string16& password);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Determines if |this| is equivalent to |other|.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Equals(const AuthCredentials& other) const;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if all credentials are empty.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Empty() const;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overwrites the password memory to prevent it from being read if
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it's paged out to disk.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Zap();
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const base::string16& username() const { return username_; }
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const base::string16& password() const { return password_; }
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The username to provide, possibly empty. This should be ASCII only to
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // minimize compatibility problems, but arbitrary UTF-16 strings are allowed
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and will be attempted.
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::string16 username_;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The password to provide, possibly empty. This should be ASCII only to
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // minimize compatibility problems, but arbitrary UTF-16 strings are allowed
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and will be attempted.
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::string16 password_;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Intentionally allowing the implicit copy constructor and assignment
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // operators.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Authentication structures
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum AuthState {
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AUTH_STATE_DONT_NEED_AUTH,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AUTH_STATE_NEED_AUTH,
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AUTH_STATE_HAVE_AUTH,
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AUTH_STATE_CANCELED
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AuthData : public base::RefCountedThreadSafe<AuthData> {
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AuthState state;  // whether we need, have, or gave up on authentication.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AuthCredentials credentials; // The credentials to use for auth.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We wouldn't instantiate this class if we didn't need authentication.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AuthData();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::RefCountedThreadSafe<AuthData>;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~AuthData();
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_BASE_AUTH_H__
108