Toggle navigation
Toggle navigation
This project
Loading...
Sign in
tools
/
totp-key
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
lintry
2017-05-19 17:50:45 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
05329fc127d265e7f515690eb1648be78d612a7f
05329fc1
1 parent
be431fc9
增加算法和创建url的方法
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
25 additions
and
20 deletions
lib/authenticator.js
lib/totp.js
test/authenticator.test.js
test/img/qr.svg
test/totp.test.js
lib/authenticator.js
View file @
05329fc
...
...
@@ -20,49 +20,52 @@ const Authenticator = function (secret, options) {
const
TOTP_OPTIONS
=
this
.
totp_options
=
{
secret
:
this
.
secret
,
encoding
:
options
.
encoding
||
'base32'
,
step
:
options
.
step
||
30
step
:
options
.
step
||
30
,
algorithm
:
options
.
algorithm
||
'sha512'
};
/**
* 验证token有效性
* @
type {Authenticator.verify}
* @
param token
*/
const
verify
=
this
.
verify
=
function
(
token
)
{
this
.
verify
=
function
(
token
)
{
return
speakeasy
.
totp
.
verify
(
_
.
merge
({
token
:
token
},
TOTP_OPTIONS
));
};
/**
* 在options.window的范围内验证token的有效性
* @
type {Authenticator.verifyDelta}
* @
param token
*/
const
verifyDelta
=
this
.
verifyDelta
=
function
(
token
)
{
this
.
verifyDelta
=
function
(
token
)
{
return
speakeasy
.
totp
.
verifyDelta
(
_
.
merge
({
token
:
token
},
TOTP_OPTIONS
));
};
/**
* 获取QR显示值
* @type {Authenticator.getOtpAuth}
* @param title
* @param issuer
* @return {string}
*/
const
getOtpAuth
=
this
.
getOtpAuth
=
function
(
title
,
issuer
)
{
let
_title
=
title
||
'HowAreU'
;
let
_issuer
=
encodeURIComponent
(
issuer
||
'TOTP-KEY'
);
return
`otpauth://totp/
${
_title
}
?secret=
${
this
.
secret
}
&issuer=
${
_issuer
}
`
;
this
.
getOtpAuth
=
function
(
title
,
issuer
)
{
return
speakeasy
.
otpauthURL
(
_
.
merge
({
label
:
title
,
issuer
:
issuer
},
TOTP_OPTIONS
));
};
/**
* 生成svg的QR图片内容
* @type {Authenticator.getQR}
* @param title
* @param issuer
* @return {*}
*/
const
getQR
=
this
.
getQR
=
function
(
title
,
issuer
)
{
return
qr
.
imageSync
(
getOtpAuth
(
title
,
issuer
),
{
type
:
'svg'
});
this
.
getQR
=
function
(
title
,
issuer
)
{
return
qr
.
imageSync
(
this
.
getOtpAuth
(
title
,
issuer
),
{
type
:
'svg'
});
};
/**
* 生成新的token
* @
type {Authenticator.totp
}
* @
return {String
}
*/
const
totp
=
this
.
totp
=
function
()
{
this
.
totp
=
function
()
{
return
speakeasy
.
totp
(
_
.
merge
({},
TOTP_OPTIONS
));
};
...
...
lib/totp.js
View file @
05329fc
...
...
@@ -16,7 +16,8 @@ const TOTP = function (options) {
const
TOTP_OPTIONS
=
{
encoding
:
options
.
encoding
||
'base32'
,
step
:
options
.
step
||
30
step
:
options
.
step
||
30
,
algorithm
:
options
.
algorithm
||
'sha512'
};
/**
...
...
test/authenticator.test.js
View file @
05329fc
...
...
@@ -8,10 +8,9 @@ const fs = require('fs-extra');
const
path
=
require
(
'path'
);
let
secret
=
'vH6OdbUEjSukTqlDvW3TYdusjiOIkxRnAHNTjJewfZa5yNueG9wx1N9pJMFOmPAV'
;
let
authenticator
=
new
Authenticator
(
secret
);
let
authenticator
=
new
Authenticator
(
secret
,
{
algorithm
:
'sha512'
});
console
.
log
(
chalk
.
cyan
(
'totp的secret'
));
console
.
log
(
secret
);
console
.
log
(
secret
,
authenticator
.
totp_options
);
let
token
=
process
.
argv
[
2
];
if
(
!
token
)
{
...
...
test/img/qr.svg
0 → 100644
View file @
05329fc
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 51 51"><path d="M1 1h7v7h-7zM10 1h3v1h-1v1h-1v-1h-1zM15 1h1v1h-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1v-1h-1v-1h1zM13 41h3v2h-2v-1h-1zM19 41h1v1h-1zM21 41h1v1h-1zM49 41h1v1h-1zM9 42h1v1h-1zM11 42h2v1h-2zM24 42v3h3v-3zM38 42h1v4h-1v1h2v2h-1v-1h-2v1h2v1h-3v-1h-1v1h-1v-3h1v1h1v-1h1v-1h-1v-2h1v1h1zM42 42v3h3v-3zM48 42h1v1h1v1h-1v2h-2v-1h1v-1h-1v-1h1zM1 43h7v7h-7zM10 43h1v1h1v1h-1v1h-1v2h-1v-4h1zM25 43h1v1h-1zM32 43h1v1h-1zM35 43h1v1h-1zM43 43h1v1h-1zM2 44v5h5v-5zM14 44h2v1h-1v1h-1zM3 45h3v3h-3zM12 45h1v1h-1zM49 46h1v1h-1zM13 47h1v2h1v1h-2v-1h-1v-1h1zM15 47h1v1h-1zM30 47h1v1h-1zM10 48h1v1h1v1h-3v-1h1zM31 48h1v1h1v1h-3v-1h1zM49 48h1v2h-2v-1h1zM24 49h1v1h-1zM28 49h1v1h-1zM40 49h1v1h-1zM42 49h1v1h-1zM46 49h1v1h-1z"/></svg>
\ No newline at end of file
test/totp.test.js
View file @
05329fc
...
...
@@ -6,7 +6,8 @@ const TOTP = require('../lib/totp');
const
chalk
=
require
(
'chalk'
);
const
fs
=
require
(
'fs-extra'
);
let
totp
=
new
TOTP
();
let
algorithm
=
process
.
argv
[
2
]
||
'sha512'
;
let
totp
=
new
TOTP
({
algorithm
:
algorithm
});
let
secret
=
totp
.
createSecret
();
console
.
log
(
chalk
.
cyan
(
'创建totp的secret'
));
...
...
Please
register
or
login
to post a comment