热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Nodeopenid基于Node.js实现的OpenID协议

OpenID的创建基于这样一个概念:我们可以通过URI(又叫URL或网站地址)来认证一个网站的唯一身份,同理

OpenID 的创建基于这样一个概念:我们可以通过 URI (又叫 URL 或网站地址)来认证一个网站的唯一身份,同理我们也可以通过这种方式来作为用户的身份认证。由于 URI 是整个网络世界的核心,它为基于 URI 的用户身份认证提供了广泛的、坚实的基础。

Node-openid 是一个基于 Node.js 实现的 OpenID 协议。

特点

  • 全面支持 OpenID 1.1/OpenID 2.0 协议的第三方扩展库
  • 非常简单的 API 接口
  • 易于扩展和使用

安装

如果你使用 npm 你可以运行下面的代码安装 Node-openid

npm install openid

如果你喜欢自己动手安装,你可以访问 Node-openid 的 Github 地址,然后下载到您的本地项目目录中,解压下载的 Node-openid 源文件,将 lib 文件夹和 openid.js 文件移动到你的项目目录中,使用 require('openid') 语句引入插件并使用,记得在你的目录把文件做 require.paths.unshift 除非它已经在你的 require.paths。

简单示例

这是使用 Node.js 认证 OpenID 的一个非常简单的服务器端示例:

var relyingParty = new openid.RelyingParty(
    'http://example.com/verify', // Verification URL (yours)
    null, // Realm (optional, specifies realm for OpenID authentication)
    false, // Use stateless verification
    false, // Strict mode
    []); // Optional list of extensions to enable and include
var server = require('http').createServer(
    function(req, res){
        var parsedUrl = url.parse(req.url);
        if(parsedUrl.pathname == '/authenticate'){
          // User supplied identifier
          var query = querystring.parse(parsedUrl.query);
          var identifier = query.openid_identifier;
          // Resolve identifier, associate, and build authentication URL
          relyingParty.authenticate(identifier, false, function(error, authUrl){
            if(error){
              res.writeHead(200, { 'Content-Type' : 'text/plain; charset=utf-8' });
              res.end('Authentication failed: ' + error.message);
            }else if (!authUrl){
              res.writeHead(200, { 'Content-Type' : 'text/plain; charset=utf-8' });
              res.end('Authentication failed');
            }else{
              res.writeHead(302, { Location: authUrl });
              res.end();
            }
          });
        }else if(parsedUrl.pathname == '/verify'){
          // Verify identity assertion
          // NOTE: Passing just the URL is also possible
          relyingParty.verifyAssertion(req, function(error, result){
            res.writeHead(200, { 'Content-Type' : 'text/plain; charset=utf-8' });
            if(error){
              res.end('Authentication failed: ' + error.message);
            }else{
              res.end((result.authenticated ? 'Success :)' : 'Failure :(') +
                '\n\n' + JSON.stringify(result));
            }
          });
        }else{
            // Deliver an OpenID form on all other URLs
            res.writeHead(200, { 'Content-Type' : 'text/html; charset=utf-8' });
            res.end(''
                + ''
                + '

Login using OpenID

'
                + ''
                + ''
                + '');
        }
    });
server.listen(80);

一个更复杂的例子包括扩展可以在 GitHub 库 sample.js 发现。

支持扩展

这个库是为下面的内置支持 OpenID 的扩展:

  • The Simple Registration (SREG) 1.1 extension is implemented as openid.SimpleRegistration.
  • The Attribute Exchange (AX) 1.0 extension is implemented as openid.AttributeExchange.
  • The OAuth 1.0 extension is implemented as openid.OAuthHybrid.
  • The User Interface 1.0 extension is implemented as openid.UserInterface.
  • The Provider Authentication Policy Extension 1.0 (PAPE) is implemented as openid.pape.

存储关联状态

为保存/加载关联状态提供了一种方法,你需要混合在 OpenID 的模块功能:

  • saveAssociation(provider, type, handle, secret, expiry_time_in_seconds, callback) is called when a new association is established during authentication. The callback should be called with any error as its first argument (or null if no error occured).
  • loadAssociation(handle, callback) is used to retrieve the association identified by handle when verification happens. The callback should be called with any error as its first argument (and null as the second argument), or an object with the keys provider, type, secret if the association was loaded successfully.

OpenID 模块包括默认实现这些函数使用一个简单的对象存储在内存中的关联。

缓存信息

验证的一个积极的断言(即一个身份验证的用户),可以加快速度显着,避免需要额外的供应商发现时,可能。为了达到这个速度,节点,OpenID 需要缓存的发现者。可以用2个函数组合来重写默认的缓存,这是一个在内存中使用一个简单对象存储的缓存:

  • saveDiscoveredInformation(key, provider, callback) is used when saving a discovered provider. The following behavior is required:
    • The key parameter should be uses as a key for storing the provider - it will be used as the lookup key when loading the provider. (Currently, the key is either a claimed identifier or an OP-local identifier, depending on the OpenID context.)
    • When saving fails for some reason, callback(error) is called with error being an error object specifying what failed.
    • When saving succeeds, callback(null) is called.
  • loadDiscoveredInformation(key, callback) is used to load any previously discovered information about the provider for an identifier. The following behavior is required:
    • When no provider is found for the identifier, callback(null, null) is called (i.e. it is not an error to not have any data to return).
    • When loading fails for some reason, callback(error, null) is called with error being an error string specifying why loading failed.
    • When loading succeeds, callback(null, provider) is called with the exact provider object that was previously stored using saveDiscoveredInformation.

相关链接

  • Github地址:https://github.com/havard/node-openid
  • 项目地址:http://ox.no/software/node-openid

推荐阅读
  • nvmw安装,用于控制node版本;
    之前一直使用的是nodev2.2.0版本,挺说新版本的node解决了npm安装插件产生文件夹结构过深的问题,所以就想更新试试;上网一看才发现,尼玛的node已经到了6.+版本了,好 ... [详细]
  • 前端微服务二
    为了解决庞大的一整块后端服务带来的变更与扩展方面的限制,出现了微服务架构(Microservices):微服务是面向服务架构(SOA)的一种变体,把应用程序设计成一系列松耦合的细粒 ... [详细]
  • 这篇文章将为大家详细讲解有关C#开发技巧有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。C#开发技 ... [详细]
  • PHP 扩展编译的通用方法
    2019独角兽企业重金招聘Python工程师标准以memcache扩展为例子首先需要到软件的官方(如memcached的地址http:pecl.php.netp ... [详细]
  • 本文分析和介绍了GLo ... [详细]
  • Git(1)
    安装Git完毕(在开始菜单打开的话,打开的不是你想要的路径,切换路径很麻烦)1.D盘新建GitTest文件夹2.打开GitTest,在空白的地方右键,3.单击GitBashHere ... [详细]
  • MyBatis模糊查询和多条件查询一、ISmbmsUserDao层根据姓名模糊查询publicListgetUser();多条件查询publicList ... [详细]
  • 导读:今天编程笔记来给各位分享关于php动态扩展怎么加载的相关内容,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览: ... [详细]
  • 系统osx10.11用的是brew下的php56brew下的nginx下了一个项目,在安装过程中提示缺少,intl和apc扩展,就用下面的语句下载了,也装上了,但php还是没有加载 ... [详细]
  • Action参数绑定功能提供了URL变量和操作方法的参数绑定支持,这一功能可以使得你的操作方法定义和参数获取更加清晰,也便于跨模块调用操作方法了。这一新特性对以往的操作方法使用没有任何影响,你也可以用 ... [详细]
  • FluxCD、ArgoCD或Jenkins X,哪个才是适合你的GitOps工具?
    GitOps是一种使用基于Git的工作流程来全面管理应用和基础设施的想法,其在最近获得了极大关注。新一代的部署工具更能说明这一点,它们将GitOps作为 ... [详细]
  • 本文目录一览:1、数据库有哪几种2、数据库软件 ... [详细]
  • Day 5 20190120 老男孩python学习第5天 内容整理
    今天继续看MasteringPycharm的视频,一个半小时看git的教学视频:视频1小时44分钟,看了2个半小时以上https:www.youtube ... [详细]
  • nacos的github的链接:https:github.comalibabanacosreleasestag1.4.1nacos的ZIP的链接:htt ... [详细]
  • ARToolKitunity
    ARToolKit为开源的AR库,相对于高通和easyAr有几点特点:1)开源2)识别项目可以动态添加(详细在后)3)识别文件可以本地生成4)目前只能识别图片(目前为.jpg格式) ... [详细]
author-avatar
飞飞飞070801
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有