Add sdk signer client interface
This commit is contained in:
parent
7b9d58545b
commit
7e679ae33f
170
src/server.js
170
src/server.js
@ -606,6 +606,176 @@ app.post('/api/verify-code', validatePhoneNumber, (req, res) => {
|
||||
|
||||
//------------------------------------ End of SMS Section ------------------------------------
|
||||
|
||||
//------------------------------------ Signer Client Integration -----------------------------------------
|
||||
|
||||
const { SDKSignerClient } = require('sdk-signer-client');
|
||||
|
||||
// Signer client configuration
|
||||
const signerConfig = {
|
||||
url: process.env.SIGNER_WS_URL || 'ws://localhost:9090',
|
||||
apiKey: process.env.SIGNER_API_KEY || 'your-api-key-change-this',
|
||||
timeout: 30000,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 3
|
||||
};
|
||||
|
||||
// Initialize signer client
|
||||
const signerClient = new SDKSignerClient(signerConfig);
|
||||
|
||||
// Session storage for verified users
|
||||
const verifiedSessions = new Map();
|
||||
|
||||
// Session management
|
||||
class SessionManager {
|
||||
static generateSessionId() {
|
||||
return uuidv4();
|
||||
}
|
||||
|
||||
static createSession(phoneNumber, userData = {}) {
|
||||
const sessionId = this.generateSessionId();
|
||||
const session = {
|
||||
id: sessionId,
|
||||
phoneNumber,
|
||||
userData,
|
||||
createdAt: Date.now(),
|
||||
expiresAt: Date.now() + (1 * 60 * 1000) // 1 minute
|
||||
};
|
||||
|
||||
verifiedSessions.set(sessionId, session);
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
static getSession(sessionId) {
|
||||
const session = verifiedSessions.get(sessionId);
|
||||
if (!session) return null;
|
||||
|
||||
if (Date.now() > session.expiresAt) {
|
||||
verifiedSessions.delete(sessionId);
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
static deleteSession(sessionId) {
|
||||
verifiedSessions.delete(sessionId);
|
||||
}
|
||||
|
||||
static cleanupExpiredSessions() {
|
||||
const now = Date.now();
|
||||
for (const [sessionId, session] of verifiedSessions) {
|
||||
if (now > session.expiresAt) {
|
||||
verifiedSessions.delete(sessionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Middleware to validate session
|
||||
const validateSession = (req, res, next) => {
|
||||
const sessionId = req.headers['x-session-id'] || req.body.sessionId;
|
||||
|
||||
if (!sessionId) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: 'Session ID requis'
|
||||
});
|
||||
}
|
||||
|
||||
const session = SessionManager.getSession(sessionId);
|
||||
if (!session) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: 'Session invalide ou expirée'
|
||||
});
|
||||
}
|
||||
|
||||
req.session = session;
|
||||
next();
|
||||
};
|
||||
|
||||
// Cleanup expired sessions every 5 minutes
|
||||
setInterval(() => {
|
||||
SessionManager.cleanupExpiredSessions();
|
||||
}, 5 * 60 * 1000);
|
||||
|
||||
// Connect to signer on startup
|
||||
(async () => {
|
||||
try {
|
||||
await signerClient.connect();
|
||||
console.log('Connected to signer service');
|
||||
const serverResponse = await signerClient.get_owned_processes();
|
||||
console.log('Server response:', serverResponse);
|
||||
|
||||
for (const data of Object.values(serverResponse.data)) {
|
||||
console.log('Process data:', data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to connect to signer:', error);
|
||||
}
|
||||
})();
|
||||
|
||||
//------------------------------------ End of Signer Client Integration ------------------------------------
|
||||
|
||||
/// client auth endpoint
|
||||
/// client sends its pairing process id, we add it to the customer process
|
||||
app.post('/api/v1/customer/auth/client-auth', validateSession, async (req, res) => {
|
||||
const { pairingId } = req.body;
|
||||
|
||||
if (!pairingId) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Missing pairingId'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await signerClient.updateProcess(processId, newData, privateFields || [], roles || null);
|
||||
|
||||
// Clean up the session after successful update
|
||||
SessionManager.deleteSession(req.session.id);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Client authentication successful',
|
||||
data: result
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Client authentication error:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Error during client authentication',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/v1/customer/auth/get-phone-number-for-email', validateSession, async (req, res) => {
|
||||
const { email } = req.body;
|
||||
|
||||
if (!email) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'Missing email'
|
||||
});
|
||||
}
|
||||
|
||||
const phoneNumber = await signerClient.getPhoneNumberForEmail(email);
|
||||
|
||||
if (!phoneNumber) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: 'No phone number found for this email'
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Phone number retrieved successfully',
|
||||
phoneNumber: phoneNumber
|
||||
});
|
||||
});
|
||||
|
||||
//------------------------------------ Email Section -----------------------------------------
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user