1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/embedding/components/windowwatcher/public/nsPromptUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,139 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef NSPROMPTUTILS_H_ 1.9 +#define NSPROMPTUTILS_H_ 1.10 + 1.11 +#include "nsIHttpChannel.h" 1.12 + 1.13 +/** 1.14 + * @file 1.15 + * This file defines some helper functions that simplify interaction 1.16 + * with authentication prompts. 1.17 + */ 1.18 + 1.19 +/** 1.20 + * Given a username (possibly in DOMAIN\user form) and password, parses the 1.21 + * domain out of the username if necessary and sets domain, username and 1.22 + * password on the auth information object. 1.23 + */ 1.24 +inline void 1.25 +NS_SetAuthInfo(nsIAuthInformation* aAuthInfo, const nsString& user, 1.26 + const nsString& password) 1.27 +{ 1.28 + uint32_t flags; 1.29 + aAuthInfo->GetFlags(&flags); 1.30 + if (flags & nsIAuthInformation::NEED_DOMAIN) { 1.31 + // Domain is separated from username by a backslash 1.32 + int32_t idx = user.FindChar(char16_t('\\')); 1.33 + if (idx == kNotFound) { 1.34 + aAuthInfo->SetUsername(user); 1.35 + } else { 1.36 + aAuthInfo->SetDomain(Substring(user, 0, idx)); 1.37 + aAuthInfo->SetUsername(Substring(user, idx + 1)); 1.38 + } 1.39 + } else { 1.40 + aAuthInfo->SetUsername(user); 1.41 + } 1.42 + aAuthInfo->SetPassword(password); 1.43 +} 1.44 + 1.45 +/** 1.46 + * Gets the host and port from a channel and authentication info. This is the 1.47 + * "logical" host and port for this authentication, i.e. for a proxy 1.48 + * authentication it refers to the proxy, while for a host authentication it 1.49 + * is the actual host. 1.50 + * 1.51 + * @param machineProcessing 1.52 + * When this parameter is true, the host will be returned in ASCII 1.53 + * (instead of UTF-8; this is relevant when IDN is used). In addition, 1.54 + * the port will be returned as the real port even when it was not 1.55 + * explicitly specified (when false, the port will be returned as -1 in 1.56 + * this case) 1.57 + */ 1.58 +inline void 1.59 +NS_GetAuthHostPort(nsIChannel* aChannel, nsIAuthInformation* aAuthInfo, 1.60 + bool machineProcessing, nsCString& host, int32_t* port) 1.61 +{ 1.62 + nsCOMPtr<nsIURI> uri; 1.63 + nsresult rv = aChannel->GetURI(getter_AddRefs(uri)); 1.64 + if (NS_FAILED(rv)) 1.65 + return; 1.66 + 1.67 + // Have to distinguish proxy auth and host auth here... 1.68 + uint32_t flags; 1.69 + aAuthInfo->GetFlags(&flags); 1.70 + if (flags & nsIAuthInformation::AUTH_PROXY) { 1.71 + nsCOMPtr<nsIProxiedChannel> proxied(do_QueryInterface(aChannel)); 1.72 + NS_ASSERTION(proxied, "proxy auth needs nsIProxiedChannel"); 1.73 + 1.74 + nsCOMPtr<nsIProxyInfo> info; 1.75 + proxied->GetProxyInfo(getter_AddRefs(info)); 1.76 + NS_ASSERTION(info, "proxy auth needs nsIProxyInfo"); 1.77 + 1.78 + nsAutoCString idnhost; 1.79 + info->GetHost(idnhost); 1.80 + info->GetPort(port); 1.81 + 1.82 + if (machineProcessing) { 1.83 + nsCOMPtr<nsIIDNService> idnService = 1.84 + do_GetService(NS_IDNSERVICE_CONTRACTID); 1.85 + if (idnService) { 1.86 + idnService->ConvertUTF8toACE(idnhost, host); 1.87 + } else { 1.88 + // Not much we can do here... 1.89 + host = idnhost; 1.90 + } 1.91 + } else { 1.92 + host = idnhost; 1.93 + } 1.94 + } else { 1.95 + if (machineProcessing) { 1.96 + uri->GetAsciiHost(host); 1.97 + *port = NS_GetRealPort(uri); 1.98 + } else { 1.99 + uri->GetHost(host); 1.100 + uri->GetPort(port); 1.101 + } 1.102 + } 1.103 +} 1.104 + 1.105 +/** 1.106 + * Creates the key for looking up passwords in the password manager. This 1.107 + * function uses the same format that Gecko functions have always used, thus 1.108 + * ensuring backwards compatibility. 1.109 + */ 1.110 +inline void 1.111 +NS_GetAuthKey(nsIChannel* aChannel, nsIAuthInformation* aAuthInfo, 1.112 + nsCString& key) 1.113 +{ 1.114 + // HTTP does this differently from other protocols 1.115 + nsCOMPtr<nsIHttpChannel> http(do_QueryInterface(aChannel)); 1.116 + if (!http) { 1.117 + nsCOMPtr<nsIURI> uri; 1.118 + aChannel->GetURI(getter_AddRefs(uri)); 1.119 + uri->GetPrePath(key); 1.120 + return; 1.121 + } 1.122 + 1.123 + // NOTE: For backwards-compatibility reasons, this must be the ASCII host. 1.124 + nsCString host; 1.125 + int32_t port = -1; 1.126 + 1.127 + NS_GetAuthHostPort(aChannel, aAuthInfo, true, host, &port); 1.128 + 1.129 + nsAutoString realm; 1.130 + aAuthInfo->GetRealm(realm); 1.131 + 1.132 + // Now assemble the key: host:port (realm) 1.133 + key.Append(host); 1.134 + key.Append(':'); 1.135 + key.AppendInt(port); 1.136 + key.AppendLiteral(" ("); 1.137 + AppendUTF16toUTF8(realm, key); 1.138 + key.Append(')'); 1.139 +} 1.140 + 1.141 +#endif 1.142 +