|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* vim:set et cin ts=4 sw=4 sts=4: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef nsHttpChannelAuthProvider_h__ |
|
8 #define nsHttpChannelAuthProvider_h__ |
|
9 |
|
10 #include "nsIHttpChannelAuthProvider.h" |
|
11 #include "nsIAuthPromptCallback.h" |
|
12 #include "nsString.h" |
|
13 #include "nsCOMPtr.h" |
|
14 #include "nsHttpAuthCache.h" |
|
15 #include "nsProxyInfo.h" |
|
16 #include "nsCRT.h" |
|
17 |
|
18 class nsIHttpAuthenticableChannel; |
|
19 class nsIHttpAuthenticator; |
|
20 class nsIURI; |
|
21 |
|
22 namespace mozilla { namespace net { |
|
23 |
|
24 class nsHttpHandler; |
|
25 |
|
26 class nsHttpChannelAuthProvider : public nsIHttpChannelAuthProvider |
|
27 , public nsIAuthPromptCallback |
|
28 { |
|
29 public: |
|
30 NS_DECL_ISUPPORTS |
|
31 NS_DECL_NSICANCELABLE |
|
32 NS_DECL_NSIHTTPCHANNELAUTHPROVIDER |
|
33 NS_DECL_NSIAUTHPROMPTCALLBACK |
|
34 |
|
35 nsHttpChannelAuthProvider(); |
|
36 virtual ~nsHttpChannelAuthProvider(); |
|
37 |
|
38 private: |
|
39 const char *ProxyHost() const |
|
40 { return mProxyInfo ? mProxyInfo->Host().get() : nullptr; } |
|
41 |
|
42 int32_t ProxyPort() const |
|
43 { return mProxyInfo ? mProxyInfo->Port() : -1; } |
|
44 |
|
45 const char *Host() const { return mHost.get(); } |
|
46 int32_t Port() const { return mPort; } |
|
47 bool UsingSSL() const { return mUsingSSL; } |
|
48 |
|
49 bool UsingHttpProxy() const |
|
50 { return !!(mProxyInfo && !nsCRT::strcmp(mProxyInfo->Type(), "http")); } |
|
51 |
|
52 nsresult PrepareForAuthentication(bool proxyAuth); |
|
53 nsresult GenCredsAndSetEntry(nsIHttpAuthenticator *, bool proxyAuth, |
|
54 const char *scheme, const char *host, |
|
55 int32_t port, const char *dir, |
|
56 const char *realm, const char *challenge, |
|
57 const nsHttpAuthIdentity &ident, |
|
58 nsCOMPtr<nsISupports> &session, char **result); |
|
59 nsresult GetAuthenticator(const char *challenge, nsCString &scheme, |
|
60 nsIHttpAuthenticator **auth); |
|
61 void ParseRealm(const char *challenge, nsACString &realm); |
|
62 void GetIdentityFromURI(uint32_t authFlags, nsHttpAuthIdentity&); |
|
63 |
|
64 /** |
|
65 * Following three methods return NS_ERROR_IN_PROGRESS when |
|
66 * nsIAuthPrompt2.asyncPromptAuth method is called. This result indicates |
|
67 * the user's decision will be gathered in a callback and is not an actual |
|
68 * error. |
|
69 */ |
|
70 nsresult GetCredentials(const char *challenges, bool proxyAuth, |
|
71 nsAFlatCString &creds); |
|
72 nsresult GetCredentialsForChallenge(const char *challenge, |
|
73 const char *scheme, bool proxyAuth, |
|
74 nsIHttpAuthenticator *auth, |
|
75 nsAFlatCString &creds); |
|
76 nsresult PromptForIdentity(uint32_t level, bool proxyAuth, |
|
77 const char *realm, const char *authType, |
|
78 uint32_t authFlags, nsHttpAuthIdentity &); |
|
79 |
|
80 bool ConfirmAuth(const nsString &bundleKey, bool doYesNoPrompt); |
|
81 void SetAuthorizationHeader(nsHttpAuthCache *, nsHttpAtom header, |
|
82 const char *scheme, const char *host, |
|
83 int32_t port, const char *path, |
|
84 nsHttpAuthIdentity &ident); |
|
85 nsresult GetCurrentPath(nsACString &); |
|
86 /** |
|
87 * Return all information needed to build authorization information, |
|
88 * all parameters except proxyAuth are out parameters. proxyAuth specifies |
|
89 * with what authorization we work (WWW or proxy). |
|
90 */ |
|
91 nsresult GetAuthorizationMembers(bool proxyAuth, nsCSubstring& scheme, |
|
92 const char*& host, int32_t& port, |
|
93 nsCSubstring& path, |
|
94 nsHttpAuthIdentity*& ident, |
|
95 nsISupports**& continuationState); |
|
96 /** |
|
97 * Method called to resume suspended transaction after we got credentials |
|
98 * from the user. Called from OnAuthAvailable callback or OnAuthCancelled |
|
99 * when credentials for next challenge were obtained synchronously. |
|
100 */ |
|
101 nsresult ContinueOnAuthAvailable(const nsCSubstring& creds); |
|
102 |
|
103 nsresult DoRedirectChannelToHttps(); |
|
104 |
|
105 /** |
|
106 * A function that takes care of reading STS headers and enforcing STS |
|
107 * load rules. After a secure channel is erected, STS requires the channel |
|
108 * to be trusted or any STS header data on the channel is ignored. |
|
109 * This is called from ProcessResponse. |
|
110 */ |
|
111 nsresult ProcessSTSHeader(); |
|
112 |
|
113 private: |
|
114 nsIHttpAuthenticableChannel *mAuthChannel; // weak ref |
|
115 |
|
116 nsCOMPtr<nsIURI> mURI; |
|
117 nsCOMPtr<nsProxyInfo> mProxyInfo; |
|
118 nsCString mHost; |
|
119 int32_t mPort; |
|
120 bool mUsingSSL; |
|
121 bool mIsPrivate; |
|
122 |
|
123 nsISupports *mProxyAuthContinuationState; |
|
124 nsCString mProxyAuthType; |
|
125 nsISupports *mAuthContinuationState; |
|
126 nsCString mAuthType; |
|
127 nsHttpAuthIdentity mIdent; |
|
128 nsHttpAuthIdentity mProxyIdent; |
|
129 |
|
130 // Reference to the prompt waiting in prompt queue. The channel is |
|
131 // responsible to call its cancel method when user in any way cancels |
|
132 // this request. |
|
133 nsCOMPtr<nsICancelable> mAsyncPromptAuthCancelable; |
|
134 // Saved in GetCredentials when prompt is asynchronous, the first challenge |
|
135 // we obtained from the server with 401/407 response, will be processed in |
|
136 // OnAuthAvailable callback. |
|
137 nsCString mCurrentChallenge; |
|
138 // Saved in GetCredentials when prompt is asynchronous, remaning challenges |
|
139 // we have to process when user cancels the auth dialog for the current |
|
140 // challenge. |
|
141 nsCString mRemainingChallenges; |
|
142 |
|
143 // True when we need to authenticate to proxy, i.e. when we get 407 |
|
144 // response. Used in OnAuthAvailable and OnAuthCancelled callbacks. |
|
145 uint32_t mProxyAuth : 1; |
|
146 uint32_t mTriedProxyAuth : 1; |
|
147 uint32_t mTriedHostAuth : 1; |
|
148 uint32_t mSuppressDefensiveAuth : 1; |
|
149 |
|
150 nsRefPtr<nsHttpHandler> mHttpHandler; // keep gHttpHandler alive |
|
151 }; |
|
152 |
|
153 }} // namespace mozilla::net |
|
154 |
|
155 #endif // nsHttpChannelAuthProvider_h__ |