Funciones Avanzadas

Hubo errores de script en esta página. Mientras los editores del sitio lo solucionan, puedes ver un contenido parcial debajo.

In all example code in this page, gotAssertion is the callback argument to navigator.id.get().

Persistent Login

The navigator.id.get() API takes a set of options as an optional parameter. By using two of these options, allowPersistent and silent, you can enable the user to stay logged into your web site until the certificate signed by their identity provider expires.

allowPersistent

You use allowPersistent like this:

{{ syntax.javascript{code: "navigator.id.get(gotAssertion, {allowPersistent: true});"} }}

This option causes a new checkbox to appear in the BrowserID dialog asking the user if they want to stay signed in. If the user checks the box to stay signed in, then the browser creates an assertion and passes it to gotAssertion as normal, but remembers the user selection.

silent

You use silent like this:

{{ syntax.javascript{code: "navigator.id.get(gotAssertion, {silent: true});"} }}

With this option the browser will not display any dialog to the user, but will call gotAssertion without any user intervention. If the user had checked the box to stay signed in, and the user's certificate has not yet expired, then the browser will silently generate a new assertion and pass it into gotAssertion. Otherwise, the browser will pass null into gotAssertion.

Supporting Persistent Login

First, on page load, call navigator.id.get({silent: true}):

{{ syntax.javascript{code: "$(function() {\n  navigator.id.get(gotAssertion, {silent: true});\n});"} }}

Next, inside gotAssertion, if the assertion is not null and validates, log the user in, otherwise call loggedOut():

{{ syntax.javascript{code: "function gotAssertion(assertion) {\n  if (assertion !== null) {\n    // Send assertion to the server for validation\n    if (!validateAssertionOnServer(assertion) loggedOut();\n    else loggedIn();\n  }\n  else {\n    loggedOut();\n  }\n}"} }}

Finally, loggedOut() updates the page to indicate that the user is logged out. In particular, the page needs to display a "Sign In" button whose click handler will call navigator.id.get({allowPersistent: true}):

{{ syntax.javascript{code: "$('#browserid' }}).click(function() {\n  navigator.id.get(gotAssertion, {allowPersistent: true});\n  return false;\n});"}

Example Code

You can see a sample implementation of this at http://myfavoritebooze.org/, whose source code is available on GitHub.

Requiring a Specific Email

BrowserID enables a user to have multiple identities, and choose which one to sign in with. If a user has multiple email addresses which can be verified using BrowserID, then when a web site calls navigator.id.get(), the user will be presented with all their addresses, and asked which one to use.

Sometimes, though, a web site knows which email the user needs to choose. For example, if Alice shares a photo with Bob using a BrowserID-enabled photo-sharing site, the web site needs to authenticate Bob against the exact email address Alice used to invite him. If Bob chooses the wrong email from multiple choices the invitation will fail.

In this situation the web site can require that the user use a particular email address by setting the requiredEmail option as an argument to navigator.id.get(). For example:

{{ syntax.javascript{code: "navigator.id.get(gotAssertion, {requiredEmail: \"bob@example.com\"});"} }}

Even if Bob has multiple email addresses, he'll only be presented with "bob@example.com". The web site's gotAssertion() function is guaranteed to be called with either an assertion for "bob@example.com", or with a null assertion.

Etiquetas y colaboradores del documento

Colaboradores de esta página: maedca
Última actualización por: maedca,