welsonjs/lib/totp.js
Namhyeon, Go dfc18cfd7d Refactor TFA tool to TwoFactorAuth and update TOTP client
Renamed ResourceTools/Tfa.cs to ResourceTools/TwoFactorAuth.cs and updated all references accordingly. Enhanced TwoFactorAuth with improved key handling, error responses, and form parsing. Updated totp.js to use the new local HTTP API endpoints for key generation and OTP calculation, removing legacy JSON-RPC code.
2025-08-22 17:21:31 +09:00

48 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// totp.js
// Copyright 2019-2025, Namhyeon Go <gnh1201@catswords.re.kr> and the WelsonJS contributors.
// SPDX-License-Identifier: GPL-3.0-or-later
// https://github.com/gnh1201/welsonjs
//
// To use this feature, youll need software that can generate Time-based OTPs (HMACSHA1 algorithm, supporting 16- or 32-character keys).
// If the WelsonJS Launcher is running, you can access this feature right away.
//
var HTTP = require("lib/http");
var TFA_API_BASE_URL = "http://localhost:3000/tfa";
function getPubKey() {
var response = HTTP.create()
.open("GET", TFA_API_BASE_URL + "/pubkey")
.send()
.responseBody
;
return response.trim();
}
function getOtp(pubkey) {
var response = HTTP.create()
.setRequestBody({
"secret": pubkey
})
.open("POST", TFA_API_BASE_URL + "/tfa/otp")
.send()
.responseBody
;
return response.trim();
}
exports.getPubKey = getPubKey;
exports.getOtp = getOtp;
exports.VERSIONINFO = "Time-based OTP client (totp.js) version 1.0";
exports.AUTHOR = "gnh1201@catswords.re.kr";
exports.global = global;
exports.require = global.require;
/*
// Example:
var TOTP = require("lib/totp");
console.log(TOTP.getPubKey()); // get public key. e.g. 6Y4R 3AQN 4TTV CEQT
console.log(TOTP.getOtp('6Y4R 3AQN 4TTV CEQT')); // get OTP code. e.g. 317884
*/