2018-05-21 12:35:11 +00:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2017
* @ license Apache - 2.0
* /
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
2025-01-23 11:28:13 +00:00
import * as OTPAuth from "otpauth" ;
2018-05-21 12:35:11 +00:00
/ * *
* Generate HOTP operation
* /
class GenerateHOTP extends Operation {
/ * *
2025-01-23 11:28:13 +00:00
*
2018-05-21 12:35:11 +00:00
* /
constructor ( ) {
super ( ) ;
this . name = "Generate HOTP" ;
this . module = "Default" ;
2020-12-14 15:32:12 +00:00
this . description = "The HMAC-based One-Time Password algorithm (HOTP) is an algorithm that computes a one-time password from a shared secret key and an incrementing counter. It has been adopted as Internet Engineering Task Force standard RFC 4226, is the cornerstone of Initiative For Open Authentication (OAUTH), and is used in a number of two-factor authentication systems.<br><br>Enter the secret as the input or leave it blank for a random secret to be generated." ;
2018-08-21 19:07:13 +01:00
this . infoURL = "https://wikipedia.org/wiki/HMAC-based_One-time_Password_algorithm" ;
2019-07-29 17:09:46 +01:00
this . inputType = "ArrayBuffer" ;
2018-05-21 12:35:11 +00:00
this . outputType = "string" ;
this . args = [
{
"name" : "Name" ,
"type" : "string" ,
"value" : ""
} ,
{
"name" : "Code length" ,
"type" : "number" ,
"value" : 6
} ,
{
"name" : "Counter" ,
"type" : "number" ,
"value" : 0
}
] ;
}
/ * *
2025-01-23 11:28:13 +00:00
*
2018-05-21 12:35:11 +00:00
* /
run ( input , args ) {
2025-01-23 11:28:13 +00:00
const secretStr = new TextDecoder ( "utf-8" ) . decode ( input ) . trim ( ) ;
const secret = secretStr ? secretStr . toUpperCase ( ) . replace ( /\s+/g , "" ) : "" ;
const hotp = new OTPAuth . HOTP ( {
issuer : "" ,
label : args [ 0 ] ,
algorithm : "SHA1" ,
digits : args [ 1 ] ,
counter : args [ 2 ] ,
secret : OTPAuth . Secret . fromBase32 ( secret )
2018-05-21 12:35:11 +00:00
} ) ;
2025-01-23 11:28:13 +00:00
const uri = hotp . toString ( ) ;
const code = hotp . generate ( ) ;
return ` URI: ${ uri } \n \n Password: ${ code } ` ;
}
2018-05-21 12:35:11 +00:00
}
export default GenerateHOTP ;