2024-10-30 17:59:44 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/multiwikiserver/auth/authentication.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: library
|
|
|
|
|
|
|
|
Handles authentication related operations
|
|
|
|
|
|
|
|
\*/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var crypto = require("crypto");
|
|
|
|
|
|
|
|
function Authenticator(database) {
|
|
|
|
if(!(this instanceof Authenticator)) {
|
|
|
|
return new Authenticator(database);
|
|
|
|
}
|
|
|
|
this.sqlTiddlerDatabase = database;
|
|
|
|
}
|
|
|
|
|
|
|
|
Authenticator.prototype.verifyPassword = function(inputPassword, storedHash) {
|
|
|
|
var hashedInput = this.hashPassword(inputPassword);
|
|
|
|
return hashedInput === storedHash;
|
|
|
|
};
|
|
|
|
|
|
|
|
Authenticator.prototype.hashPassword = function(password) {
|
|
|
|
return crypto.createHash("sha256").update(password).digest("hex");
|
|
|
|
};
|
|
|
|
|
|
|
|
Authenticator.prototype.createSession = function(userId) {
|
|
|
|
var sessionId = crypto.randomBytes(16).toString("hex");
|
|
|
|
// Store the session in your database or in-memory store
|
2024-12-22 20:59:01 +00:00
|
|
|
this.sqlTiddlerDatabase.createUserSession(userId, sessionId);
|
2024-10-30 17:59:44 +00:00
|
|
|
return sessionId;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.Authenticator = Authenticator;
|
|
|
|
|
|
|
|
})();
|