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-06-22 15:24:26 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c97c5898a46d92bf460b60614a9ad796558cc031
c97c5898
1 parent
3075d553
二维码生成时返回图片内容和url
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
57 deletions
lib/authenticator.js
test/authenticator.test.js
lib/authenticator.js
View file @
c97c589
...
...
@@ -3,72 +3,76 @@
*/
const
Authenticator
=
function
(
secret
,
options
)
{
if
(
!
(
this
instanceof
Authenticator
))
{
return
new
Authenticator
(
secret
,
options
)
}
if
(
!
(
this
instanceof
Authenticator
))
{
return
new
Authenticator
(
secret
,
options
)
}
const
_
=
require
(
'lodash'
),
speakeasy
=
require
(
'speakeasy'
),
qr
=
require
(
'qr-image'
);
const
_
=
require
(
'lodash'
),
speakeasy
=
require
(
'speakeasy'
),
qr
=
require
(
'qr-image'
);
options
=
options
||
{};
options
=
options
||
{};
this
.
secret
=
secret
;
this
.
secret
=
secret
;
const
TOTP_OPTIONS
=
this
.
totp_options
=
{
secret
:
this
.
secret
,
encoding
:
options
.
encoding
||
'base32'
,
step
:
options
.
step
||
30
,
algorithm
:
options
.
algorithm
||
'sha512'
};
const
TOTP_OPTIONS
=
this
.
totp_options
=
{
secret
:
this
.
secret
,
encoding
:
options
.
encoding
||
'base32'
,
step
:
options
.
step
||
30
,
algorithm
:
options
.
algorithm
||
'sha512'
};
/**
* 验证token有效性
* @param token
*/
this
.
verify
=
function
(
token
)
{
return
speakeasy
.
totp
.
verify
(
_
.
merge
({},
TOTP_OPTIONS
,
{
token
:
token
}));
};
/**
* 验证token有效性
* @param token
*/
this
.
verify
=
function
(
token
)
{
return
speakeasy
.
totp
.
verify
(
_
.
merge
({},
TOTP_OPTIONS
,
{
token
:
token
}));
};
/**
* 在options.window的范围内验证token的有效性
* @param token
* @param window
*/
this
.
verifyDelta
=
function
(
token
,
window
)
{
return
speakeasy
.
totp
.
verifyDelta
(
_
.
merge
({},
TOTP_OPTIONS
,
{
token
:
token
,
window
:
window
}));
};
/**
* 在options.window的范围内验证token的有效性
* @param token
* @param window
*/
this
.
verifyDelta
=
function
(
token
,
window
)
{
return
speakeasy
.
totp
.
verifyDelta
(
_
.
merge
({},
TOTP_OPTIONS
,
{
token
:
token
,
window
:
window
}));
};
/**
* 获取授权定义地址
* @param label
* @param issuer
* @return {string}
*/
this
.
getOtpAuthURL
=
function
(
label
,
issuer
)
{
return
speakeasy
.
otpauthURL
(
_
.
merge
({},
TOTP_OPTIONS
,
{
label
:
title
,
issuer
:
issuer
}));
};
/**
* 获取授权定义地址
* @param label
* @param issuer
* @return {string}
*/
this
.
getOtpAuthURL
=
function
(
label
,
issuer
)
{
return
speakeasy
.
otpauthURL
(
_
.
merge
({},
TOTP_OPTIONS
,
{
label
:
title
,
issuer
:
issuer
}));
};
/**
* 生成svg的QR图片内容
* @param label
* @param issuer
* @return {*}
*/
this
.
getQR
=
function
(
label
,
issuer
)
{
return
qr
.
imageSync
(
this
.
getOtpAuthURL
(
label
,
issuer
),
{
type
:
'svg'
});
};
/**
* 生成svg的QR图片内容
* @param label
* @param issuer
* @return {{content: *, url: string}}
*/
this
.
getQR
=
function
(
label
,
issuer
)
{
let
url
=
this
.
getOtpAuthURL
(
label
,
issuer
);
return
{
content
:
qr
.
imageSync
(
url
,
{
type
:
'svg'
}),
url
:
url
};
};
/**
* 生成新的token
* @return {String}
*/
this
.
totp
=
function
()
{
return
speakeasy
.
totp
(
_
.
merge
({},
TOTP_OPTIONS
));
};
/**
* 生成新的token
* @return {String}
*/
this
.
totp
=
function
()
{
return
speakeasy
.
totp
(
_
.
merge
({},
TOTP_OPTIONS
));
};
return
this
;
return
this
;
};
module
.
exports
=
Authenticator
;
\ No newline at end of file
...
...
test/authenticator.test.js
View file @
c97c589
...
...
@@ -41,11 +41,12 @@ fs.ensureDir(img_path, function (err, added_root) {
}
added_root
&&
console
.
log
(
chalk
.
green
(
img_path
+
' is created'
));
let
qr
=
authenticator
.
getQR
(
'totp@gitlab.kmlab.com'
,
'通行密钥'
);
let
fd
=
fs
.
openSync
(
qr
,
'w'
);
fs
.
writeSync
(
fd
,
authenticator
.
getQR
(
'totp@gitlab.kmlab.com'
,
'通行密钥'
)
);
fs
.
writeSync
(
fd
,
qr
.
content
);
fs
.
closeSync
(
fd
);
console
.
log
(
chalk
.
green
(
'密钥字符串'
),
chalk
.
yellow
(
authenticator
.
getOtpAuthURL
(
'totp@gitlab.kmlab.com'
,
'通行密钥'
)
))
console
.
log
(
chalk
.
green
(
'密钥字符串'
),
chalk
.
yellow
(
qr
.
url
))
});
console
.
log
(
'QR SVG output is'
,
img_path
,
qr
);
\ No newline at end of file
...
...
Please
register
or
login
to post a comment