net-passport
v3.0.32
Published
Easily create OAuth2 keys in one line of code
Downloads
93
Maintainers
Readme
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):
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:
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);