GithubHelp home page GithubHelp logo

aedes-authorization-plugin's Introduction

aedes-authorization-plugin  Build Status

A semi-opinionated authorizer for aedes MQTT broker loosely inspired by expressjs.

Note: This library is written in ES6, so be careful when mixing with aedes!!

Install

npm i -S aedes-authorization-plugin

Example

const aedes = require("aedes")({
  persistence: new require("aedes-persistence")()
});
const server = require("net").createServer(aedes.handle);
const port = 1883;
const {
  authorizePublish,
  authorizeSubscribe,
  addTopic
} = require("aedes-authorization-plugin");

// add topics and authorizer functionality (promises supported, too)
addTopic("users/+userId", (client, sub) => {
  if (sub.params.userId === "12345") {
    return true; // allowed!
  } else {
    return false; // not allowed!
  }
});

// hook it up
aedes.authorizeSubscribe = authorizeSubscribe;
aedes.authorizePublish = authorizePublish;

server.listen(port, function() {
  console.log("server listening on port", port);
});

API

authorizePublish ( client, sub, callback )

aedes publish authorizer handle. Once set, clients can only publish to topics that have been added via addTopic.

authorizeSubscribe ( client, sub, callback )

aedes subscribe authorizer handle. Once set, clients can only subscribe to topics that have been added via addTopic.

addTopic ( topic, authorizer, [...opts] )

Add a topic for validation. topic is specified according to the MQTT spec. A good guide on this can be found here.

authorizer is a function of the form

function (client, sub){
	//sub.params holds the topic params
	return true
}

where sub has the property params which holds the mapped + values from subscribed/published topic. Should return true or false depending on desired auth pattern.

opts takes the object form {isSubscriptionTopic: true, isPublishTopic: false}. If no opts is given, authorizer will be run on both subscriptions and publications. If only isSubscriptionTopic or isPublishTopic is specified, then the other will not be included in the running of authorizer for that topic.

clearTopics ()

Clears all the topics that were added via addTopic.

Testing

Pull requests accepted.

npm install -D
npm test

There is also an aedes server written for full integration testing in test/server.js.

License

MIT licensed, so have your way with it.

aedes-authorization-plugin's People

Contributors

bjyurkovich avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.