Skip to content

Commit 735636a

Browse files
committed
Initial commit
0 parents  commit 735636a

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env node
2+
const cli = require('cli');
3+
const OTP = require('otp');
4+
const options = {
5+
counter: ['c', 'Counter for HTOP'],
6+
help: ['h', 'Help'],
7+
method: ['m', 'Method for OTP HOTP or TOTP.', 'string', 'TOTP'],
8+
otp: ['o', 'OTP value to be verified. Use with verify command', 'int'],
9+
secret: ['s', 'Secret to be used for OTP generation. Can be set using OTP_SECRET environment variable. parameter overrides environment variable', 'string']
10+
}
11+
const params = cli.parse(options, ['generate', 'verify']);
12+
const secret = params.secret ? params.secret : process.env.OTP_SECRET;
13+
if (secret === null || secret === undefined) {
14+
console.log('ERROR: Secret missing.\n\n');
15+
cli.getUsage(1);
16+
}
17+
18+
if (params.method === 'HOTP' && params.counter === null) {
19+
console.log('ERROR: Counter required. HOTP Method selected.\n\n');
20+
cli.getUsage(1);
21+
}
22+
var otp = OTP({
23+
secret: secret
24+
});
25+
26+
const generatedOTP = params.method === 'TOTP' ? otp.totp() : otp.hotp(params.counter);
27+
28+
switch (cli.command) {
29+
case 'generate':
30+
if (params.help) {
31+
cli.getUsage(1);
32+
} else {
33+
console.log(generatedOTP);
34+
}
35+
break;
36+
case 'verify':
37+
if (params.help) {
38+
cli.getUsage(1);
39+
} else {
40+
41+
if (params.otp === null) {
42+
console.log('ERROR: OTP required.\n\n');
43+
cli.getUsage(1);
44+
}
45+
if (generatedOTP === params.otp.toString()) {
46+
console.log('OK')
47+
process.exit(0);
48+
} else {
49+
console.log('FAIL');
50+
process.exit(1);
51+
52+
}
53+
}
54+
break;
55+
default:
56+
cli.getUsage(1);
57+
break;
58+
}
59+
60+

package-lock.json

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "otp-ci",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "./run_tests"
8+
},
9+
"bin": {
10+
"otpci": "index.js",
11+
"otp-ci": "index.js"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/phenixcoder/otp-ci.git"
16+
},
17+
"author": "",
18+
"license": "ISC",
19+
"bugs": {
20+
"url": "https://github.com/phenixcoder/otp-ci/issues"
21+
},
22+
"homepage": "https://github.com/phenixcoder/otp-ci#readme",
23+
"dependencies": {
24+
"cli": "^1.0.1",
25+
"otp": "^0.1.3"
26+
}
27+
}

readme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# OTP CI
2+
3+
Commandline utility to check OTP code.
4+
5+
## Usage
6+
```
7+
Usage:
8+
otpci [OPTIONS] <command> [ARGS]
9+
10+
Options:
11+
-c, --counter Counter for HTOP
12+
-h, --help Help
13+
-m, --method [STRING] Method for OTP HOTP or TOTP. (Default is TOTP)
14+
-o, --otp NUMBER OTP value to be verified. Use with verify command
15+
-s, --secret STRING Secret to be used for OTP generation. Can be set using
16+
OTP_SECRET environment variable. parameter
17+
overrides environment variable
18+
19+
Commands:
20+
generate, verify
21+
```
22+
## Examples
23+
24+
`otpci verify -m HTOP -c 1 -s SECRET -o 123456`
25+
26+
`otpci generate -m HTOP -c 1 -s SECRET`

run_tests

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
RED=$(echo -e '\e[31m');
4+
GREEN=$(echo -e '\e[32m');
5+
YELLOW=$(echo -e '\e[33m');
6+
RESET=$(echo -e '\e[0m');
7+
8+
9+
export SECRET=1234
10+
printf "\n\n$GREEN Implicit values$RESET\n"
11+
RESULT1=$(node index.js verify -s $SECRET -o $(node index.js generate -s $SECRET))
12+
RESULT2=$(node index.js verify --secret $SECRET -o $(node index.js generate --secret $SECRET))
13+
RESULT3=$(node index.js verify --secret=$SECRET -o $(node index.js generate --secret=$SECRET))
14+
RESULT4=$(OTP_SECRET=$SECRET node index.js verify -o $(OTP_SECRET=$SECRET node index.js generate))
15+
16+
printf "\n$YELLOW Verify Generated OTP when secret is passed with -s: $RESET$RESULT1"
17+
printf "\n$YELLOW Verify Generated OTP when secret is passed with --secret: $RESET$RESULT2"
18+
printf "\n$YELLOW Verify Generated OTP when secret is passed with ---secret= : $RESET$RESULT3"
19+
printf "\n$YELLOW Verify Generated OTP when secret is passed with environment variable: $RESET$RESULT4"
20+
21+
printf "\n\n$GREEN Explicit TOTP$RESET\n"
22+
23+
RESULT1=$(node index.js verify -m TOTP -s $SECRET -o $(node index.js generate -m TOTP -s $SECRET))
24+
25+
printf "\n$YELLOW Verify Generated OTP when explicit method TOTP is passed with -m: $RESET$RESULT1"
26+
27+
printf "\n\n$GREEN Explicit HTOP$RESET\n"
28+
29+
RESULT1=$(node index.js verify -m HTOP -c 1 -s $SECRET -o $(node index.js generate -m HTOP -c 1 -s $SECRET))
30+
31+
printf "\n$YELLOW Verify Generated OTP when explicit method HTOP -c 1 is passed with -m: $RESET$RESULT1"

0 commit comments

Comments
 (0)