npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

l-passport

v1.1.5

Published

koa middleware and api sdk for wechat oauth,qq oauth, baidu oauth and weibo oauth

Downloads

3

Readme

l-passport

集成微信(wechat)、QQ(qq)、百度(baidu)和微博(weibo)于一体的koa中间件与API SDK

功能列表

  • OAuth授权
  • 获取用户基本信息

koa2中间件,开发者可以通过此中间件获取用户的基本信息(包括用户编号、昵称、头像)

交流群

QQ群:157964097,使用疑问,开发,贡献代码请加群。

Installation

npm install l-passport -S

Usage

Authentication

引入l-passport并配置

const passport = require('l-passport');

// 微信登录:设置appId与app secret
passport.initialize({
  provider: 'wechat'
  appId: 'your_app_id',
  appSecret: 'your_app_secret'
});

router.get('/login/wechat', passport.authorization('wechat'), async (ctx) => {
  ctx.body = ctx.state.passport;
});

如果需要配置多个平台(如web、ios、android),建议参考如下代码

const passport = require('l-passport');

passport.initialize({
  provider: 'wechat', 
  clients: [
    { platform: 'web', appId: 'your_app_id', appSecret: 'your_app_secret' },
    { platform: 'ios', appId: 'your_app_id', appSecret: 'your_app_secret' },
    { platform: 'android', appId: 'your_app_id', appSecret: 'your_app_secret' },
  ]
});

router.get('/login/wechat_web', passport.authorization('wechat', { platform: 'web' }), async (ctx) => {
  ctx.body = ctx.state.passport;
});

router.get('/login/wechat_ios', passport.authorization('wechat', { platform: 'ios' }), async (ctx) => {
  ctx.body = ctx.state.passport;
});

router.get('/login/wechat_android', passport.authorization('wechat', { platform: 'android' }), async (ctx) => {
  ctx.body = ctx.state.passport;
});

如果需要配置多个服务提供商与多个平台,建议参考如下代码

const passport = require('l-passport');

passport.initialize([
  {
    provider: 'wechat', 
    clients: [
      { platform: 'web', appId: 'your_app_id', appSecret: 'your_app_secret' },
      { platform: 'ios', appId: 'your_app_id', appSecret: 'your_app_secret' },
      { platform: 'android', appId: 'your_app_id', appSecret: 'your_app_secret' },
    ]
  },
  {
    provider: 'baidu', 
    clients: [
      { platform: 'web', appId: 'your_app_id', appSecret: 'your_app_secret', redirect: 'your_baidu_redirect' },
      { platform: 'ios', appId: 'your_app_id', appSecret: 'your_app_secret', redirect: 'your_baidu_redirect'},
      { platform: 'android', appId: 'your_app_id', appSecret: 'your_app_secret', redirect: 'your_baidu_redirect' },
    ]
  }
]);

router.get('/login/wechat_web', passport.authorization('wechat', { platform: 'web' }), async (ctx) => {
  ctx.body = ctx.state.passport;
});

router.get('/login/baidu_ios', passport.authorization('baidu', { platform: 'ios' }), async (ctx) => {
  ctx.body = ctx.state.passport;
});

配置参数说明:

  • provider: - 服务提供商(必选)
    • 当前可选:qq、baidu、weibo、wechat
  • appId: - 应用编号(必填)
  • appSecret: - 应用秘钥(必填)
  • platform: - 服务平台(选填)
  • redirect: - 应用回调地址(选填)
  • scope: - 申请的权限范围(选填)
  • state: - 应用当前状态,可以指定任意值,服务提供商会原封不动地返回这个值(选填)

Authentication Url

注意:不同的服务提供商之间,在认证时对回调地址的处理方式各不相同,如微信不会检查回调函数,微博和QQ只需核查回调函数的域名,而百度则需要核查包括Query参数在内的整个回调地址

l-passport支持两种方式设置回调函数

1.配置设置

const passport = require('l-passport');

passport.initialize({
  provider: 'baidu'
  appId: 'your_app_id',
  appSecret: 'your_app_secret',
  redirect: 'your_app_redirect',
  state: 'your_app_state',
  scope: 'your_app_scope'
});

router.get('/login/baidu', passport.authorization('baidu'), async (ctx) => {
  ctx.body = ctx.state.passport;
});

2.动态设置

将redirect、state、scope放在路由的Query参数中,如/login/baidu?redirect=your_redirect&state=your_state&scope=your_scope

拓展登录策略

l-passport已经集成如下:

  • 1.qq:QQ登录
  • 2.baidu:百度登录
  • 3.weibo:微博登录
  • 4.wechat:微信登录

如果开发者觉得当前集成的登录策略无法满足需求时,可以自行拓展,其基本形式如下:

class YourStragety {
  // 服务提供商提供的授权地址  
  getAuthorizeUrl(redirect, state, scope) {}

  // 用户通过授权后的认证过程
  authorize(code) {}
}

passport.use('your_stagety_name', YourStragety);

用户信息格式

认证完成后用户信息将挂载ctx.state.passport中,其基本格式如下:

{
  "provider": "服务提供商",
  "uid"     : "用户编号",
  "nickname": "用户昵称",
  "avatar"  : "用户头像",
  "body"    : {}
}