OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_H_ | 5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_H_ |
6 #define NET_HTTP_HTTP_AUTH_HANDLER_H_ | 6 #define NET_HTTP_HTTP_AUTH_HANDLER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "net/base/completion_callback.h" | 10 #include "net/base/completion_callback.h" |
11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
12 #include "net/http/http_auth.h" | 12 #include "net/http/http_auth.h" |
13 #include "net/log/net_log.h" | 13 #include "net/log/net_log.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 class HttpAuthChallengeTokenizer; | 17 class HttpAuthChallengeTokenizer; |
18 struct HttpRequestInfo; | 18 struct HttpRequestInfo; |
19 | 19 |
20 // HttpAuthHandler is the interface for the authentication schemes | 20 // HttpAuthHandler is the interface for the authentication schemes |
21 // (basic, digest, NTLM, Negotiate). | 21 // (basic, digest, NTLM, Negotiate). Each authentication scheme is expected to |
22 // HttpAuthHandler objects are typically created by an HttpAuthHandlerFactory. | 22 // subclass HttpAuthHandler. |
| 23 // |
| 24 // The lifecycle of a HttpAuthHandler -- i.e. how to use HttpAuthHandler --: |
| 25 // |
| 26 // 1. Construct via HttpAuthHandlerFactory::CreateAuthHandlerForScheme() or |
| 27 // HttpAuthHandlerFactory::CreateAndInitPreemptiveAuthHandler(). |
| 28 // |
| 29 // Upon creation, an HttpAuthHandler is not associated with any HTTP |
| 30 // connection or session. That association is established by calling |
| 31 // HandleInitialChallenge(). |
| 32 // |
| 33 // 2. Call HttpAuthHandler::HandleInitialChallenge(). This *must* be the first |
| 34 // method or getter invoked by an owner of HttpAuthHandler. The invocation |
| 35 // establishes the state for HttpAuthHandler associating it with an |
| 36 // authentication target, an origin and an initial challenge from the server |
| 37 // which may establish scheme specific state. |
| 38 // |
| 39 // If the return value of HandleInitialChallenge() indicates failure, then |
| 40 // this handler can no longer be used. The owner is expected to discard the |
| 41 // handler without invoking any other methods. |
| 42 // |
| 43 // 3. Repeat the following steps: |
| 44 // |
| 45 // 3.1. Call GenerateAuthToken() to generate an authentication token. The |
| 46 // token returned by this method call should be passed via a suitable |
| 47 // authorization header to the server. E.g. "Authorization: <returned |
| 48 // token>". |
| 49 // |
| 50 // 3.2 If the server responds with an additional challenge, call |
| 51 // HandleAnotherChallenge(). This method continues an authentication |
| 52 // handshake. The AuthorizationResult return value indicates whether the |
| 53 // handler can continue with the current authentication handshake or if it |
| 54 // has encountered an error. A return value of AUTHORIZATION_RESULT_ACCEPT |
| 55 // indicates that the handler can continue. In all other cases, the handler |
| 56 // should be considered no longer usable. |
| 57 // |
23 class NET_EXPORT_PRIVATE HttpAuthHandler { | 58 class NET_EXPORT_PRIVATE HttpAuthHandler { |
24 public: | 59 public: |
25 HttpAuthHandler(); | |
26 virtual ~HttpAuthHandler(); | 60 virtual ~HttpAuthHandler(); |
27 | 61 |
28 // Initializes the handler using a challenge issued by a server. | 62 // Initializes the handler and associates it with the specified |target| and |
29 // |challenge| must be non-NULL and have already tokenized the | 63 // |origin|. The |net_log| parameter indicates BoundNetLog to be used for the |
30 // authentication scheme, but none of the tokens occurring after the | 64 // lifetime of this handler. |challenge| is required and *must* match the |
31 // authentication scheme. |target| and |origin| are both stored | 65 // authentication scheme of this handler. |
32 // for later use, and are not part of the initial challenge. | 66 // |
| 67 // Returns a Error value. The HttpAuthHandler can only be used if the return |
| 68 // value is OK. |
| 69 // |
| 70 // Note: This method *must* be the first method to be invoked on the |
| 71 // HttpAuthHandler. |
33 int HandleInitialChallenge(const HttpAuthChallengeTokenizer& challenge, | 72 int HandleInitialChallenge(const HttpAuthChallengeTokenizer& challenge, |
34 HttpAuth::Target target, | 73 HttpAuth::Target target, |
35 const GURL& origin, | 74 const GURL& origin, |
36 const BoundNetLog& net_log); | 75 const BoundNetLog& net_log); |
37 | 76 |
38 // Determines how the previous authorization attempt was received. | |
39 // | |
40 // This is called when the server/proxy responds with a 401/407 after an | |
41 // earlier authorization attempt. Although this normally means that the | |
42 // previous attempt was rejected, in multi-round schemes such as | |
43 // NTLM+Negotiate it may indicate that another round of challenge+response | |
44 // is required. For Digest authentication it may also mean that the previous | |
45 // attempt used a stale nonce (and nonce-count) and that a new attempt should | |
46 // be made with a different nonce provided in the challenge. | |
47 // | |
48 // |challenge| must be non-NULL and have already tokenized the | |
49 // authentication scheme, but none of the tokens occurring after the | |
50 // authentication scheme. | |
51 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge( | |
52 const HttpAuthChallengeTokenizer& challenge) = 0; | |
53 | |
54 // Generates an authentication token, potentially asynchronously. | 77 // Generates an authentication token, potentially asynchronously. |
55 // | 78 // |
56 // When |credentials| is NULL, the default credentials for the currently | 79 // If NeedsIdentity() is true, then the value of |credentials| indicates how |
57 // logged in user are used. |AllowsDefaultCredentials()| MUST be true in this | 80 // the authentication identity is established. |
58 // case. | |
59 // | 81 // |
60 // |request|, |callback|, and |auth_token| must be non-NULL. | 82 // 1. If |credentials| is nullptr, then the handler attempts to use ambient |
| 83 // credentials to establish an identity. Passing in a nullptr for |
| 84 // |credentials| is only valid if AllowsDefaultCredentials() returns true. |
| 85 // |
| 86 // 2. If |credentials| is is not nullptr, then it will be used to establish |
| 87 // the authentication identity. Passing in a non-null |credentials| is only |
| 88 // valid if AllowsExplicitCredentials() returns true. |
| 89 // |
| 90 // |request| is required and should represent the request that will, if the |
| 91 // call is successful, contain the generated authentication token. |
61 // | 92 // |
62 // The return value is a net error code. | 93 // The return value is a net error code. |
63 // | 94 // |
64 // If |OK| is returned, |*auth_token| is filled in with an authentication | 95 // If |OK| is returned, |*auth_token| is filled in with an authentication |
65 // token which can be inserted in the HTTP request. | 96 // token which can be sent via an appropriate authorization header. E.g. |
| 97 // "Authorization: <returned token>". |
66 // | 98 // |
67 // If |ERR_IO_PENDING| is returned, |*auth_token| will be filled in | 99 // If |ERR_IO_PENDING| is returned, |*auth_token| will be filled in |
68 // asynchronously and |callback| will be invoked. The lifetime of | 100 // asynchronously and |callback| will be invoked. The lifetime of |
69 // |request|, |callback|, and |auth_token| must last until |callback| is | 101 // |request|, |callback|, and |auth_token| must last until |callback| is |
70 // invoked, but |credentials| is only used during the initial call. | 102 // invoked, but |credentials| is only used during the initial call. |
71 // | 103 // |
72 // All other return codes indicate that there was a problem generating a | 104 // All other return codes indicate that there was a problem generating a |
73 // token, and the value of |*auth_token| is unspecified. | 105 // token, and the value of |*auth_token| is unspecified. |
74 int GenerateAuthToken(const AuthCredentials* credentials, | 106 int GenerateAuthToken(const AuthCredentials* credentials, |
75 const HttpRequestInfo& request, | 107 const HttpRequestInfo& request, |
76 const CompletionCallback& callback, | 108 const CompletionCallback& callback, |
77 std::string* auth_token); | 109 std::string* auth_token); |
78 | 110 |
| 111 // Determines how the previous authorization attempt was received. |
| 112 // |
| 113 // This is called when the server/proxy responds with a 401/407 after an |
| 114 // earlier authorization attempt. Although this normally means that the |
| 115 // previous attempt was rejected, in multi-round schemes such as |
| 116 // NTLM+Negotiate it may indicate that another round of challenge+response |
| 117 // is required. For Digest authentication it may also mean that the previous |
| 118 // attempt used a stale nonce (and nonce-count) and that a new attempt should |
| 119 // be made with a different nonce provided in the challenge. |
| 120 // |
| 121 // |challenge| must be non-NULL and have already tokenized the |
| 122 // authentication scheme, but none of the tokens occurring after the |
| 123 // authentication scheme. |
| 124 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge( |
| 125 const HttpAuthChallengeTokenizer& challenge) = 0; |
| 126 |
79 // The authentication scheme as an enumerated value. | 127 // The authentication scheme as an enumerated value. |
80 const std::string& auth_scheme() const { return auth_scheme_; } | 128 const std::string& auth_scheme() const { return auth_scheme_; } |
81 | 129 |
82 // The realm, encoded as UTF-8. This may be empty. | 130 // The realm, encoded as UTF-8. This may be empty. Only valid after |
| 131 // HandleInitialChallenge() is called. |
83 const std::string& realm() const { | 132 const std::string& realm() const { |
84 return realm_; | 133 return realm_; |
85 } | 134 } |
86 | 135 |
87 // The challenge which was issued when creating the handler. | 136 // The challenge which was issued when creating the handler. Only valid after |
| 137 // HandleInitialChallenge() is called. |
88 const std::string& challenge() const { return auth_challenge_; } | 138 const std::string& challenge() const { return auth_challenge_; } |
89 | 139 |
| 140 // Authentication target. Only valid after HandleInitialChallenge() is called. |
90 HttpAuth::Target target() const { | 141 HttpAuth::Target target() const { |
91 return target_; | 142 return target_; |
92 } | 143 } |
93 | 144 |
94 // Returns the proxy or server which issued the authentication challenge | 145 // Returns the proxy or server which issued the authentication challenge |
95 // that this HttpAuthHandler is handling. The URL includes scheme, host, and | 146 // that this HttpAuthHandler is handling. The URL includes scheme, host, and |
96 // port, but does not include path. | 147 // port, but does not include path. Only valid after HandleInitialChallenge() |
| 148 // is called. |
97 const GURL& origin() const { | 149 const GURL& origin() const { |
98 return origin_; | 150 return origin_; |
99 } | 151 } |
100 | 152 |
101 // Returns true if the response to the current authentication challenge | 153 // Returns true if the response to the current authentication challenge |
102 // requires an identity. | 154 // requires an identity. This function can be called after a successful call |
| 155 // to HandleInitialChallenge() or HandleAnotherChallenge() to determine |
| 156 // whether the next GenerateAuthToken() call should specify an identity. |
| 157 // |
103 // TODO(wtc): Find a better way to handle a multi-round challenge-response | 158 // TODO(wtc): Find a better way to handle a multi-round challenge-response |
104 // sequence used by a connection-based authentication scheme. | 159 // sequence used by a connection-based authentication scheme. |
105 virtual bool NeedsIdentity(); | 160 virtual bool NeedsIdentity(); |
106 | 161 |
107 // Returns whether the default credentials may be used for the |origin| passed | 162 // Returns whether the default credentials may be used for the |origin| passed |
108 // into |HandleInitialChallenge|. If true, the user does not need to be | 163 // into |HandleInitialChallenge|. If true, the user does not need to be |
109 // prompted for username and password to establish credentials. NOTE: SSO is | 164 // prompted for username and password to establish credentials. NOTE: SSO is |
110 // a potential security risk. | 165 // a potential security risk. |
111 // TODO(cbentzel): Add a pointer to Firefox documentation about risk. | 166 // TODO(cbentzel): Add a pointer to Firefox documentation about risk. |
112 virtual bool AllowsDefaultCredentials(); | 167 virtual bool AllowsDefaultCredentials(); |
113 | 168 |
114 // Returns whether explicit credentials can be used with this handler. If | 169 // Returns whether explicit credentials can be used with this handler. If true |
115 // true the user may be prompted for credentials if an implicit identity | 170 // the credentials passed in to GenerateAuthToken() may specify explicit |
116 // cannot be determined. | 171 // credentials. |
117 virtual bool AllowsExplicitCredentials(); | 172 virtual bool AllowsExplicitCredentials(); |
118 | 173 |
119 protected: | 174 protected: |
| 175 // |scheme| sets the return value for auth_scheme(). |
| 176 HttpAuthHandler(const std::string& scheme); |
| 177 |
120 // Initializes the handler using a challenge issued by a server. |challenge| | 178 // Initializes the handler using a challenge issued by a server. |challenge| |
121 // must be non-NULL and have already tokenized the authentication scheme, but | 179 // must be non-NULL and have already tokenized the authentication scheme, but |
122 // none of the tokens occurring after the authentication scheme. | 180 // none of the tokens occurring after the authentication scheme. |
123 // Implementations are expected to initialize the following members: scheme_, | 181 // Implementations are expected to initialize the following members: scheme_, |
124 // realm_ | 182 // realm_ |
125 virtual int Init(const HttpAuthChallengeTokenizer& challenge) = 0; | 183 virtual int Init(const HttpAuthChallengeTokenizer& challenge) = 0; |
126 | 184 |
127 // |GenerateAuthTokenImpl()} is the auth-scheme specific implementation | 185 // |GenerateAuthTokenImpl()} is the auth-scheme specific implementation |
128 // of generating the next auth token. Callers should use |GenerateAuthToken()| | 186 // of generating the next auth token. Callers should use |GenerateAuthToken()| |
129 // which will in turn call |GenerateAuthTokenImpl()| | 187 // which will in turn call |GenerateAuthTokenImpl()| |
(...skipping 24 matching lines...) Expand all Loading... |
154 private: | 212 private: |
155 void OnGenerateAuthTokenComplete(int rv); | 213 void OnGenerateAuthTokenComplete(int rv); |
156 void FinishGenerateAuthToken(); | 214 void FinishGenerateAuthToken(); |
157 | 215 |
158 CompletionCallback callback_; | 216 CompletionCallback callback_; |
159 }; | 217 }; |
160 | 218 |
161 } // namespace net | 219 } // namespace net |
162 | 220 |
163 #endif // NET_HTTP_HTTP_AUTH_HANDLER_H_ | 221 #endif // NET_HTTP_HTTP_AUTH_HANDLER_H_ |
OLD | NEW |