1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is mozilla.org embedding code.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
22 * Christian Biesinger <cbiesinger@web.de> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef NSPROMPTUTILS_H_
39 #define NSPROMPTUTILS_H_
41 #include "nsIHttpChannel.h"
45 * This file defines some helper functions that simplify interaction
46 * with authentication prompts.
50 * Given a username (possibly in DOMAIN\user form) and password, parses the
51 * domain out of the username if necessary and sets domain, username and
52 * password on the auth information object.
55 NS_SetAuthInfo(nsIAuthInformation
* aAuthInfo
, const nsString
& user
,
56 const nsString
& password
)
59 aAuthInfo
->GetFlags(&flags
);
60 if (flags
& nsIAuthInformation::NEED_DOMAIN
) {
61 // Domain is separated from username by a backslash
62 PRInt32 idx
= user
.FindChar(PRUnichar('\\'));
63 if (idx
== kNotFound
) {
64 aAuthInfo
->SetUsername(user
);
66 aAuthInfo
->SetDomain(Substring(user
, 0, idx
));
67 aAuthInfo
->SetUsername(Substring(user
, idx
+ 1));
70 aAuthInfo
->SetUsername(user
);
72 aAuthInfo
->SetPassword(password
);
76 * Gets the host and port from a channel and authentication info. This is the
77 * "logical" host and port for this authentication, i.e. for a proxy
78 * authentication it refers to the proxy, while for a host authentication it
81 * @param machineProcessing
82 * When this parameter is true, the host will be returned in ASCII
83 * (instead of UTF-8; this is relevant when IDN is used). In addition,
84 * the port will be returned as the real port even when it was not
85 * explicitly specified (when false, the port will be returned as -1 in
89 NS_GetAuthHostPort(nsIChannel
* aChannel
, nsIAuthInformation
* aAuthInfo
,
90 PRBool machineProcessing
, nsCString
& host
, PRInt32
* port
)
93 nsresult rv
= aChannel
->GetURI(getter_AddRefs(uri
));
97 // Have to distinguish proxy auth and host auth here...
99 aAuthInfo
->GetFlags(&flags
);
100 if (flags
& nsIAuthInformation::AUTH_PROXY
) {
101 nsCOMPtr
<nsIProxiedChannel
> proxied(do_QueryInterface(aChannel
));
102 NS_ASSERTION(proxied
, "proxy auth needs nsIProxiedChannel");
104 nsCOMPtr
<nsIProxyInfo
> info
;
105 proxied
->GetProxyInfo(getter_AddRefs(info
));
106 NS_ASSERTION(info
, "proxy auth needs nsIProxyInfo");
108 nsCAutoString idnhost
;
109 info
->GetHost(idnhost
);
112 if (machineProcessing
) {
113 nsCOMPtr
<nsIIDNService
> idnService
=
114 do_GetService(NS_IDNSERVICE_CONTRACTID
);
116 idnService
->ConvertUTF8toACE(idnhost
, host
);
118 // Not much we can do here...
125 if (machineProcessing
) {
126 uri
->GetAsciiHost(host
);
127 *port
= NS_GetRealPort(uri
);
136 * Creates the key for looking up passwords in the password manager. This
137 * function uses the same format that Gecko functions have always used, thus
138 * ensuring backwards compatibility.
141 NS_GetAuthKey(nsIChannel
* aChannel
, nsIAuthInformation
* aAuthInfo
,
144 // HTTP does this differently from other protocols
145 nsCOMPtr
<nsIHttpChannel
> http(do_QueryInterface(aChannel
));
147 nsCOMPtr
<nsIURI
> uri
;
148 aChannel
->GetURI(getter_AddRefs(uri
));
149 uri
->GetPrePath(key
);
153 // NOTE: For backwards-compatibility reasons, this must be the ASCII host.
157 NS_GetAuthHostPort(aChannel
, aAuthInfo
, PR_TRUE
, host
, &port
);
160 aAuthInfo
->GetRealm(realm
);
162 // Now assemble the key: host:port (realm)
166 key
.AppendLiteral(" (");
167 AppendUTF16toUTF8(realm
, key
);