- Basic Authentication - IP Filter
npm install express-simple-access-controlexpress-simple-access-control
---




This is a library for restricting access to applications implemented in express.
- Basic Authentication
- IP Filter
An example of Basic Authentication is as follows.
``typescript
import express from "express";
import useAccessControlMiddleware from "express-simple-access-control";
const app = express();
// apply access restrictions
useAccessControlMiddleware(app, {
basicAuthOption: {
users: [
{username: 'username', password: 'password'},
],
},
});
// ...
`
An example of IP Filter is as follows.
`typescript
import express from "express";
import useAccessControlMiddleware from "express-simple-access-control";
const app = express();
// apply access restrictions
useAccessControlMiddleware(app, {
ipFilterOption: {
allowsIPs: ['XXX.XXX.XXX.XXX'],
errStatusCode: 404,
errMessage: 'Not Found',
},
});
// ...
`
An example combination of IP Filter and Basic Authentication is as follows.
`typescript
import express from "express";
import useAccessControlMiddleware from "express-simple-access-control";
const app = express();
// apply access restrictions
useAccessControlMiddleware(app, {
basicAuthOption: {
users: [
{username: 'username', password: 'password'},
],
},
ipFilterOption: {
allowsIPs: ['XXX.XXX.XXX.XXX'],
errStatusCode: 404,
errMessage: 'Not Found',
},
});
// ...
`
In this case, if client IP is allowed, it is considered accessible, and if not allowed, it is shifted to Basic authentication.
`mermaid`
flowchart LR
p1(IP Filter) -- ok --> s1((Success))
p1 -- invalid --> p2
p2(Basic Auth) -- ok --> s1
p2 -- invalid --> s2((Unauthorized))
| field name | default | description |
|------------|---------|------------------------------------------------------------------|
| users | [] | List of objects with Basic authentication username and password. |
| field name | default | description |
|---------------|--------------|------------------------------------------------------------------------------------------|
| allowIPs | [] | List of accessible IP addresses. |
| errStatusCode | 401 | Response status when an access is received from an IP address not included in allowIPs. |
| errMessage | Unauthorized | Response message when an access is received from an IP address not included in allowIPs. |
Attempt to obtain an IP address in the following order.
1. x-client-ip in headerx-forwarded-for
2. in headercf-connecting-ip
3. in headerfastly-client-ip
4. in headertrue-client-ip
5. in headerx-real-ip
6. in headerx-cluster-client-ip
7. in headerx-forwarded
8. in headerforwarded-for
9. in headerforwarded
10. in headerremoteAddress` in socket
11.
The scripts and documentation in this repository are released under the MIT License.