1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
23 * Joshua Xia <joshua.xia@sun.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #include "nsNetUtil.h"
41 #include "nsJVMAuthTools.h"
42 #include "nsIHttpAuthManager.h"
44 static NS_DEFINE_CID(kHttpAuthManagerCID
, NS_HTTPAUTHMANAGER_CID
);
46 //---------------------------------------------------
47 // implementation of interface nsIAuthenticationInfo
48 // --------------------------------------------------
49 NS_IMPL_ISUPPORTS1(nsAuthenticationInfoImp
, nsIAuthenticationInfo
)
51 nsAuthenticationInfoImp::nsAuthenticationInfoImp(char* username
,
58 nsAuthenticationInfoImp::~nsAuthenticationInfoImp()
61 nsMemory::Free(mUserName
);
64 nsMemory::Free(mPassWord
);
67 /* readonly attribute string username; */
68 NS_IMETHODIMP
nsAuthenticationInfoImp::GetUsername(const char * *aUsername
)
70 *aUsername
= mUserName
;
74 /* readonly attribute string password; */
75 NS_IMETHODIMP
nsAuthenticationInfoImp::GetPassword(const char * *aPassword
)
77 *aPassword
= mPassWord
;
82 //---------------------------------------------------
83 // implementation of interface nsIJVMAuthTools
84 // --------------------------------------------------
85 NS_IMPL_AGGREGATED(nsJVMAuthTools
)
87 nsJVMAuthTools::nsJVMAuthTools(nsISupports
* outer
)
89 NS_INIT_AGGREGATED(outer
);
92 nsJVMAuthTools::~nsJVMAuthTools(void)
96 NS_INTERFACE_MAP_BEGIN_AGGREGATED(nsJVMAuthTools
)
97 NS_INTERFACE_MAP_ENTRY(nsIJVMAuthTools
)
101 nsJVMAuthTools::GetAuthenticationInfo(const char* protocol
,
106 nsIAuthenticationInfo
**_retval
)
108 if (!protocol
|| !host
|| !scheme
|| !realm
)
109 return NS_ERROR_INVALID_ARG
;
111 if (!PL_strcasecmp(protocol
, "HTTP") && !PL_strcasecmp(protocol
, "HTTPS"))
112 return NS_ERROR_INVALID_ARG
;
114 nsCOMPtr
<nsIHttpAuthManager
> authManager
= do_GetService(kHttpAuthManagerCID
);
116 return NS_ERROR_FAILURE
;
118 nsDependentCString
protocolString(protocol
);
119 nsDependentCString
hostString(host
);
120 nsDependentCString
schemeString(scheme
);
121 nsDependentCString
realmString(realm
);
122 nsAutoString domainString
, username
, password
;
124 nsresult rv
= authManager
->GetAuthIdentity(protocolString
,
134 return NS_ERROR_FAILURE
;
136 nsAuthenticationInfoImp
* authInfo
= new nsAuthenticationInfoImp(
137 ToNewUTF8String(username
),
138 ToNewUTF8String(password
));
139 NS_ENSURE_TRUE(authInfo
, NS_ERROR_OUT_OF_MEMORY
);
147 nsJVMAuthTools::SetAuthenticationInfo(const char* protocol
,
152 const char *username
,
153 const char *password
)
155 if (!protocol
|| !host
|| !scheme
|| !realm
)
156 return NS_ERROR_INVALID_ARG
;
158 if (!PL_strcasecmp(protocol
, "HTTP") && !PL_strcasecmp(protocol
, "HTTPS"))
159 return NS_ERROR_INVALID_ARG
;
161 nsCOMPtr
<nsIHttpAuthManager
> authManager
= do_GetService(kHttpAuthManagerCID
);
163 return NS_ERROR_FAILURE
;
165 nsDependentCString
protocolString(protocol
);
166 nsDependentCString
hostString(host
);
167 nsDependentCString
schemeString(scheme
);
168 nsDependentCString
realmString(realm
);
170 nsresult rv
= authManager
->SetAuthIdentity(protocolString
,
177 NS_ConvertUTF8toUTF16(username
),
178 NS_ConvertUTF8toUTF16(password
));