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', user: 'admin', digits: 6, step: 30 }); //
Each request consumes the code of its step, so make one per step const
users = await client.get('/api/v1/ldap/users'); const userData = await
users.json();
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!'); }