Skip to content

Commit ef526ba

Browse files
committed
update tutorial
1 parent f6265f1 commit ef526ba

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

docs/examples/tutorials/mock-ldap-group-permission.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ icon: bi-shield-lock
88

99
## Introduction
1010

11-
In this tutorial, you'll learn how to use Mokapi to mock an LDAP server for authentication and group-based access
12-
control in a Node.js backend. This is useful for testing without relying on a real LDAP
13-
server.
11+
LDAP authentication is widely used for managing user access in enterprise applications. However, setting up a real LDAP
12+
server for testing can be complex and time-consuming. In this tutorial, you will learn how to use Mokapi to mock an
13+
LDAP server and implement group-based authentication in a Node.js backend.
1414

1515
### What You'll Learn
1616

17-
- Set up a mock LDAP server using Mokapi
18-
- Authenticate users via LDAP in a Node.js backend
19-
- Implement group-based permissions
17+
- ✅ Setting up a mock LDAP server with Mokapi
18+
- ✅ Authenticating users via LDAP in a Node.js backend
19+
- ✅ Implementing group-based permissions
20+
- ✅ Testing authentication using cURL
21+
22+
## Prerequisites
23+
24+
Before starting, ensure you have the following:
25+
26+
- Node.js installed
27+
- Mokapi installed [Installation Guide](/docs/guides/get-started/installation.md)
28+
- Basic knowledge of LDAP authentication
2029

2130
## 1. Create an LDAP Mock Configuration
2231

@@ -66,9 +75,15 @@ Using [LDAP Browser](https://marketplace.visualstudio.com/items?itemName=fengtan
6675

6776
<img src="./vscode-ldap-browse-example.png" alt="Screenshot of VSCode displaying an LDAP directory structure." />
6877

69-
## 2. Implementing Backend in Node.js
78+
## 2. Implement LDAP Authentication in Node.js
79+
80+
Run the following command in install required dependencies:
81+
82+
```shell
83+
npm install ldapjs express
84+
```
7085

71-
Create a file `auth.js` and add:
86+
Create a file `auth.js` and add the following code:
7287

7388
```javascript
7489
const ldap = require('ldapjs');
@@ -101,6 +116,9 @@ function checkGroup(username, groupName, callback) {
101116
module.exports = { authenticate, checkGroup };
102117
```
103118

119+
- **authenticate**: This function binds the provided username and password to the mock LDAP server to verify authentication.
120+
- **checkGroup**: This function searches for the user to determine if they are a member of the Admins group.
121+
104122
In 'server.js' we use the LDAP authentication and group-based access control:
105123

106124
```javascript tab=server.js
@@ -129,6 +147,8 @@ app.post('/login', (req, res) => {
129147
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
130148
```
131149

150+
This Express.js server handles authentication and ensures only users in the Admins group can access protected resources.
151+
132152
## 3. Testing the Mock LDAP Authentication
133153

134154
Start Mokapi LDAP server:
@@ -170,4 +190,6 @@ If the username and/or password is not correct, the response will be:
170190
## Conclusion
171191

172192
You’ve successfully mocked an LDAP server with Mokapi, authenticated users, and
173-
implemented group-based permissions in a Node.js backend!
193+
implemented group-based permissions in a Node.js backend!
194+
195+
This setup allows you to test LDAP authentication and group permissions without needing a real LDAP server.

0 commit comments

Comments
 (0)