Active directory basic auth middleware for express.
npm install express-ad-basicauth
!npm (tag) !npm  !Libraries.io dependency status for GitHub repo !npm 
Active directory basic auth middleware for express.
``javascript
const app = require("express")();
const AdAuth = require("express-ad-basicauth");
const port = 8080;
AdAuth.initAd(
{
url: "ldap://ad.local:389",
baseDN: "dc=ad,dc=local",
username: "",
password: ""
},
null
);
app.use(AdAuth.app);
app.get("/", (req, res) => {
res.send("Secret...!");
});
app.listen(port, () => console.log(App is listening on port ${port}!));`
`javascript
const app = require("express")();
const AdAuth = require("express-ad-basicauth");
const port = 8080;
AdAuth.initAd(
{
url: "ldap://ad.local:389",
baseDN: "dc=ad,dc=local",
username: "",
password: ""
},
"domain-admins"
);
app.use(AdAuth.app);
app.get("/", (req, res) => {
res.send("Only for domain admins!");
});
app.listen(port, () => console.log(App is listening on port ${port}!));`
`javascript${user}@ad.local
...
AdAuth.editName(user => {
if (!user.includes("@")) user = ;
return user;
});
AdAuth.editPass(password => {
password = "Replace the password or do something else here";
return password;
});
...
`
To cancel the authentication process with one of the functions simply return false.
Then the user will get a 401 code.
`javascript`
{
url: "ldap://ad.local:389",
baseDN: "dc=ad,dc=local",
username: "",
password: ""
}
`javascript`
{
url: "ldap://ad.local:389",
baseDN: "dc=ad,dc=local",
cn: "Exchange",
username: "",
password: ""
}
For more options visit the activedirectory2` package.
You can find it here. All options are available.