0

RFC 2617指定HTTP基本和摘要验证标准,并总结在Wikipedia's "Example with explanation" subsection工作:客户端JavaScript Facebook OAuth2实现?

  • 的客户端请求需要身份验证,但不提供用户名和密码的页面。通常这是因为用户只是简单地输入了地址或者跟随了该页面的链接。
  • 服务器响应401“客户端错误”响应代码,提供认证领域和随机生成的一次性值,称为随机数。
  • 此时,浏览器会向用户呈现身份验证领域(通常是正在访问的计算机或系统的描述)并提示输入用户名和密码。用户可能决定在这一点上取消。
  • 一旦提供了用户名和密码,客户端将重新发送相同的请求,但会添加包含响应代码的验证标头。
  • 在此示例中,服务器接受认证并返回页面。如果用户名无效和/或密码不正确,服务器可能会返回“401”响应码,客户端将再次提示用户。

注意:客户端可能已经具有所需的用户名和密码而不需要提示用户,例如,如果他们以前被网页浏览器存储过。

我正在执行登录使用OAuth2 draft-standardFacebook执行。

如果服务器具有公开的公开API(例如:JSONRPC或REST),那么如何编写客户端JavaScript前端以包含前面提到的RFC 2617示例的功能类型,但对于Facebook身份验证?

回答

1

我发现了一个正式的(但未维护的,现在正式废弃)的示例客户端JavaScript库实现示例的存储库,click here for most recent fork。下面是从页JQuery的例子:

<head> 
     <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
     <title>Connect JavaScript - jQuery Login Example</title> 
    </head> 
    <body> 
     <h1>Connect JavaScript - jQuery Login Example</h1> 
     <div> 
      <button id="login">Login</button> 
      <button id="logout">Logout</button> 
      <button id="disconnect">Disconnect</button> 
     </div> 
     <div id="user-info" style="display: none;"></div> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"> 
    </script> 
    <div id="fb-root"></div> 
    <script src="http://connect.facebook.net/en_US/all.js"></script> 
    <script> 
     // initialize the library with the API key 
     FB.init({ 
      apiKey: '48f06bc570aaf9ed454699ec4fe416df' 
     }); 


     // fetch the status on load 
     FB.getLoginStatus(handleSessionResponse); 

     $('#login').bind('click', function() { 
      FB.login(handleSessionResponse); 
     }); 


     $('#logout').bind('click', function() { 
      FB.logout(handleSessionResponse); 
     }); 

     $('#disconnect').bind('click', function() { 
      FB.api({ 
       method: 'Auth.revokeAuthorization' 
      }, function (response) { 
       clearDisplay(); 
      }); 
     }); 

     // no user, clear display 
     function clearDisplay() { 
      $('#user-info').hide('fast'); 
     } 

     // handle a session response from any of the auth related calls 
     function handleSessionResponse(response) { 
      // if we dont have a session, just hide the user info 
      if (!response.session) { 
       clearDisplay(); 
       return; 
      } 

      // if we have a session, query for the user's profile picture and name 
      FB.api({ 
       method: 'fql.query', 
       query: 'SELECT name, pic FROM profile WHERE id=' + FB.getSession().uid 
      }, 

      function (response) { 
       var user = response[0]; 
       $('#user-info').html('<img src="' + user.pic + '">' + user.name).show('fast'); 
      }); 
     } 
    </script> 

随意提出改进意见。最理想的我希望看到:

  • 香草JavaScript实现
  • JSONRPC电话,以方便登录个人服务器端
  • 路由(即:重定向到登录的成功的登录用户主页)