Giter Site home page Giter Site logo

net-passport's Introduction

Net Passport

net-passport is a all-strategy (Google, Facebook, etc...) server side authentication using Passport JS mechanism in just a single line of code. You don't need any more boilerplates, repetitive configurations, strategies installations and more. Just use net-passport middleware in your express application and manage your authenticated users! In addition, net-passport gives you a simple way for sign and verify messages using your NetPassport ID.

prerequisites

If you don't have one then go to NetPassport and create an account. After that, navigate to Console (on the left):

alt text

Then, click on "Generate key pair", scroll down and download the key-pair.

Alternatively, you could upload a private key you generated by yourself.

Installation

$ npm install net-passport

Usage

const { authenticate, signer } = require("net-passport");

Get your netPassportID in NetPassportHome on the top right:

alt text

you could click the "Copy" button to copy the id and paste it inside your code.

// Create an object with the relevant parameters
const message = {
  netPassportID: "112233", // **required** your NetPassport id (String type must be provided)
  domain: "http://localhost:5000", // **required** your domain
  initUri: "/auth", // Optional - your base auth path
  redirectUri: "/auth/callback", // **required** callback auth path so NetPassport could recieve authentication callback
  successRedirect: "/success", // **required** a success relative path in case user authenticated successfully
  failureRedirect: "/failed", // ***required** a failed relative path for failed authentication
  appName: "myAwesomeApp", // **required** application name
  provider: "google", // **optional** pass a provider name from the list of providers ("google" / "facebook" / "github") to skip NetPassport sign in screen
};

Add your private key

// Pass in the .pem file or a pth to the file
const pk = fs.readFileSync(
  path.join(__dirname, "lib", "keys", "privatekey.pem"),
  "utf-8"
);

// OR

const pk = path.join(__dirname, "lib", "keys", "privatekey.pem");

Define middlewares

// Use NetPassport in a top level middleware
app.use(authenticate(pk, message));

// Define success and failed routs
app.get("/success", (req, res) => {
  res.send(`Hello ${req.session.passport.user[0].identifier}`);
});

app.get("/failed", (req, res) => {
  res.send(`Failed authentication`);
});

Authenticate without configured initUri

app.get("/:company/auth", authenticate(pk, message, { initUri: true }));

Server to server authentication

Sign data

// Initiate your message object
const message = {
  netPassportID: "112233",
  myData: "Hi there",
};

// Pass in two parameters that includes your object message (as mentioned above) and a private key or path to your private key
const signature = signer.sign(message, pk);

Verify data

// Pass in two parameters that includes your original object message and the hashed signature of the message
signer.verify(message, signature).then((verifiedMessage) => verifiedMessage);

License

MIT

net-passport's People

Contributors

yaron14 avatar yaronler avatar

Watchers

Oren Zbeda 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.