code
Usage Examples
Installation
npm install ldap-rest # or yarn add ldap-rest
Import the TOTP Client
import { generateTotp, TotpAuthClient } from
'ldap-rest/browser-shared-utils-totp';
Generate TOTP Code
// Generate a TOTP code const code = await generateTotp({ secret:
'JBSWY3DPEHPK3PXP', digits: 6, step: 30 }); console.log('Current TOTP
code:', code);
Use TotpAuthClient for API Requests
// Create authenticated client const client = new TotpAuthClient({
secret: 'JBSWY3DPEHPK3PXP', digits: 6, step: 30 }); // Make
authenticated requests const users = await
client.get('/api/v1/ldap/users'); const userData = await users.json();
// POST with automatic authentication await
client.post('/api/v1/ldap/users', { uid: 'user1', mail:
'user1@example.com' }); // Other methods: put, patch, delete await
client.put('/api/v1/ldap/users/user1', { mail: 'new@example.com' });
await client.delete('/api/v1/ldap/users/user1');
Helper Functions
import { getRemainingSeconds, isValidBase32 } from
'ldap-rest/browser-shared-utils-totp'; // Check how much time until
next code const remaining = getRemainingSeconds(30); console.log(`Code
expires in ${remaining} seconds`); // Validate Base32 secret if
(isValidBase32('JBSWY3DPEHPK3PXP')) { console.log('Valid secret!'); }