diff options
Diffstat (limited to 'files/es/uso_de_nsiloginmanager/index.html')
| -rw-r--r-- | files/es/uso_de_nsiloginmanager/index.html | 177 |
1 files changed, 0 insertions, 177 deletions
diff --git a/files/es/uso_de_nsiloginmanager/index.html b/files/es/uso_de_nsiloginmanager/index.html deleted file mode 100644 index 785f9e8681..0000000000 --- a/files/es/uso_de_nsiloginmanager/index.html +++ /dev/null @@ -1,177 +0,0 @@ ---- -title: Uso de nsILoginManager -slug: Uso_de_nsILoginManager -tags: - - Firefox 3 - - Interfaces - - Todas_las_Categorías - - páginas_a_traducir -translation_of: Mozilla/Tech/XPCOM/Reference/Interface/nsILoginManager/Using_nsILoginManager ---- -<p></p> - -<h3 id="Working_with_the_Login_Manager" name="Working_with_the_Login_Manager">Working with the Login Manager</h3> - -<p>Extensions often need to securely store passwords to external sites, web applications, and so on. To do so securely, they can use <code><a href="es/NsILoginManager">nsILoginManager</a></code>, which provides for secure storage of sensitive password information and <code><a href="es/NsILoginInfo">nsILoginInfo</a></code>, which provides a way of storing login information.</p> - -<h3 id="Getting_nsILoginManager" name="Getting_nsILoginManager">Getting <code>nsILoginManager</code></h3> - -<p>To get a component implementing <code>nsILoginManager</code>, use the following:</p> - -<pre>var passwordManager = Components.classes["@mozilla.org/login-manager;1"] - .getService(Components.interfaces.nsILoginManager); -</pre> - -<p>Most Login Manager functions take an <code><a href="es/NsILoginInfo">nsILoginInfo</a></code> object as a parameter. An <code><a href="es/NsILoginInfo">nsILoginInfo</a></code> object contains the following attributes: hostname, form submit URL, HTTP realm, username, username field, password, and password field. The hostname, username and password attributes are mandatory, while the other fields are set based on whether the login is for a web page form or an HTTP/FTP authentication site login. See the <code><a href="es/NsILoginInfo">nsILoginInfo</a></code> attribute definitions for more details. Defining an <code><a href="es/NsILoginInfo">nsILoginInfo</a></code> object is simple:</p> - -<pre>var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", - Components.interfaces.nsILoginInfo, - "init"); - -var loginInfo = new nsLoginInfo(hostname, formSubmitURL, httprealm, username, password, - usernameField, passwordField); -</pre> - -<h3 id="Examples" name="Examples">Examples</h3> - -<h4 id="Creating_a_login_for_a_web_page" name="Creating_a_login_for_a_web_page">Creating a login for a web page</h4> - -<pre class="eval"> <span class="nowiki">var formLoginInfo = new nsLoginInfo('http://www.example.com', - 'http://login.example.com', null, - 'joe', 'SeCrEt123', 'uname', 'pword');</span> -</pre> - -<p>This login would correspond to a HTML form such as:</p> - -<pre class="eval"> <span class="nowiki"> - <form action="http://login.example.com/foo/authenticate.cgi"> - Please log in. - Username: <input type="text" name="uname"> - Password: <input type="password" name="pword"> - </form> - </span> -</pre> - -<h4 id="Creating_a_site_authentication_login" name="Creating_a_site_authentication_login">Creating a site authentication login</h4> - -<pre class="eval"> <span class="nowiki">var authLoginInfo = new nsLoginInfo('http://www.example.com', - null, 'ExampleCo Login', - 'alice', 'SeCrEt321', null, null);</span> -</pre> - -<p>This would correspond to a login on <span class="nowiki">http://www.example.com</span> when the server sends a reply such as:</p> - -<pre class="eval"> HTTP/1.0 401 Authorization Required - Server: Apache/1.3.27 - WWW-Authenticate: Basic realm="ExampleCo Login" -</pre> - -<h4 id="Creating_a_local_extension_login" name="Creating_a_local_extension_login">Creating a local extension login</h4> - -<pre class="eval"> <span class="nowiki">var extLoginInfo = new nsLoginInfo('chrome://firefoo', - 'User Registration', null, - 'bob', '123sEcReT', null, null);</span> -</pre> - -<p>The Login Manager treats this as if it was a web site login. You should use your extension's <a class="external" rel="freelink">chrome://</a> URL to prevent conflicts with other extensions, and a realm string which briefly denotes the login's purpose.</p> - -<h3 id="Almacenar_una_contrase.C3.B1a" name="Almacenar_una_contrase.C3.B1a">Almacenar una contraseña</h3> - -<p>Para almacenar una contraseña en el Gestor de Accesos, primero necesitas crear un objeto <code><a href="es/NsILoginInfo">nsILoginInfo</a></code> como se define arriba. Entonces simplemente necesitas llamar al método <code><a href="es/NsILoginManager">nsILoginManager</a></code> <code><a href="es/NsILoginManager#addLogin.28.29">addLogin()</a></code>.</p> - -<pre class="eval"> myLoginManager.addLogin(loginInfo); -</pre> - -<p>Esto provocará una excepción si tanto el parámetro httprealm como el parámetro formSubmitURL son <code>NULL</code>. Uno debe ser especificado cuando se almacena una contraseña. Los parámetros hostname, username y password también son obligatorios.</p> - -<h3 id="Retrieving_a_password" name="Retrieving_a_password">Retrieving a password</h3> - -<p>Retrieving a password from the Login Manager is slightly more difficult. In order to locate a password, the hostname, formSubmitURL and httprealm must <strong>match exactly</strong> what is stored for the password to be found. The only exception is that if the stored formSubmitURL is blank, in which case the formSubmitURL parameter is ignored. Note that the hostname and formSubmitURL arguments should not include the path from the full URL. The example below should serve as a starting point for matching form logins:</p> - -<pre>var hostname = 'http://www.example.com'; -var formSubmitURL = 'http://www.example.com'; // not http://www.example.com/foo/auth.cgi -var httprealm = null; -var username = 'user'; -var password; - -try { - // Get Login Manager - var myLoginManager = Components.classes["@mozilla.org/login-manager;1"] - .getService(Components.interfaces.nsILoginManager); - - // Find users for the given parameters - var logins = myLoginManager.findLogins({}, hostname, formSubmitURL, httprealm); - - // Find user from returned array of nsILoginInfo objects - for (var i = 0; i < logins.length; i++) { - if (logins[i].username == username) { - password = logins[i].password; - break; - } - } -} -catch(ex) { - // This will only happen if there is no nsILoginManager component class -} -</pre> - -<p>Note that the user will be prompted for their master password if they have chosen to set one to secure their passwords.</p> - -<h3 id="Removing_a_password" name="Removing_a_password">Removing a password</h3> - -<p>Removing a password is simple:</p> - -<pre class="eval"> myLoginManager.removeLogin(loginInfo); -</pre> - -<p>When removing a password the specified <code><a href="es/NsILoginInfo">nsILoginInfo</a></code> object must <strong>exactly match</strong> what was stored or an exception will be thrown. This includes the password attribute. Here's an example on how to remove the password without actually knowing what the password is:</p> - -<pre>// example values -var hostname = 'http://www.example.com'; -var formSubmitURL = 'http://www.example.com'; -var httprealm = null; -var username = 'user'; - -try { - // Get Login Manager - var passwordManager = Components.classes["@mozilla.org/login-manager;1"] - .getService(Components.interfaces.nsILoginManager); - - // Find users for this extension - var logins = passwordManager.findLogins({}, hostname, formSubmitURL, httprealm); - - for (var i = 0; i < logins.length; i++) { - if (logins[i].username == username) { - passwordManager.removeLogin(logins[i]); - break; - } - } -} -catch(ex) { - // This will only happen if there is no nsILoginManager component class -} -</pre> - -<h3 id="Changing_stored_login_information" name="Changing_stored_login_information">Changing stored login information</h3> - -<p>Changing a password is rather simple. Since all this does is make a <code><a href="es/NsILoginManager#removeLogin.28.29">removeLogin()</a></code> call followed by an <code><a href="es/NsILoginManager#addLogin.28.29">addLogin()</a></code> call, it has the same caveats as both of them: namely that the oldLogin must match an existing login exactly (see above) and that the newLogin attributes must be set correctly.:</p> - -<pre>myLoginManager.modifyLogin(oldLogin, newLogin);</pre> - -<h3 id="Depuraci.C3.B3n" name="Depuraci.C3.B3n">Depuración</h3> - -<p>La implementación del gestor de accesos tiene la capacidad de enviar mensajes de depuración a la Consola de Errores, lo que puede provocar algo de visibilidad en lo que se está haciendo. Para habilitar la depuración de accesos, véase <a class="external" href="http://wiki.mozilla.org/Firefox:Password_Manager_Debugging" rel="freelink">http://wiki.mozilla.org/Firefox:Pass...ager_Debugging</a>.</p> - -<h3 id="Soporte_de_versiones_anteriores_de_Firefox" name="Soporte_de_versiones_anteriores_de_Firefox">Soporte de versiones anteriores de Firefox</h3> - -<p>Si quieres implementar tu extensión para que soporte Firefox 3 y versiones anteriores será necesario implementar tanto el componente <code><a href="es/NsILoginManager">nsILoginManager</a></code> como el componente <code><a href="es/NsIPasswordManager">nsIPasswordManager</a></code>. Un método simple para hacer ésto se expone a continuación:</p> - -<pre>if ("@mozilla.org/passwordmanager;1" in Components.classes) { - // El gestor de contraseñas existe así que no es Firefox 3 (puede ser Firefox 2, Netscape, SeaMonkey, etc). - // Código del gestor de contraseñas -} -else if ("@mozilla.org/login-manager;1" in Components.classes) { - // El gestor de accesos existe así que es Firefox 3 - // Código del gestor de accesos -} -</pre> |
