@aitianyu.cn/types 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** @format */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Environment = void 0;
|
|
5
|
+
class Environment {
|
|
6
|
+
static runtimeEnv() {
|
|
7
|
+
const isNode = !!(typeof process !== "undefined" && process.versions && process.versions.node);
|
|
8
|
+
return isNode ? "node" : "browser";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.Environment = Environment;
|
package/dist/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**@format */
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.SHA = exports.RSA = exports.QRCode = exports.hash = exports.guid = exports.Base32 = exports.Json = exports.Integer = exports.DataView = exports.Bytes = exports.StringHelper = exports.ArrayHelper = exports.ObjectHelper = exports.ObjectCalculater = exports.getBoolean = exports.Performance = exports.Log = exports.parseAreaString = exports.parseAreaCode = exports.ObjectDiffMergeFailedException = exports.ObjectDiffApplyInvalidStatusException = exports.ObjectMergeStatusCheckFailedException = exports.ObjectCloneFunctionNotSupportException = exports.ArgumentNullOrEmptyException = exports.Path = exports.PathDirAndFileConvertInvaild = exports.PathDirectoryValidationFailException = exports.PathProcessorSourceLostException = exports.TMap = exports.EncryptOption = exports.PathBase = exports.LogLevel = exports.Exception = exports.AreaCode = void 0;
|
|
4
|
+
exports.TOTP = exports.SHA = exports.RSA = exports.QRCode = exports.hash = exports.guid = exports.Base32 = exports.Json = exports.Integer = exports.DataView = exports.Bytes = exports.StringHelper = exports.ArrayHelper = exports.ObjectHelper = exports.ObjectCalculater = exports.Environment = exports.getBoolean = exports.Performance = exports.Log = exports.parseAreaString = exports.parseAreaCode = exports.ObjectDiffMergeFailedException = exports.ObjectDiffApplyInvalidStatusException = exports.ObjectMergeStatusCheckFailedException = exports.ObjectCloneFunctionNotSupportException = exports.ArgumentNullOrEmptyException = exports.Path = exports.PathDirAndFileConvertInvaild = exports.PathDirectoryValidationFailException = exports.PathProcessorSourceLostException = exports.TMap = exports.EncryptOption = exports.PathBase = exports.LogLevel = exports.Exception = exports.AreaCode = void 0;
|
|
5
5
|
// src/types
|
|
6
6
|
var AreaCode_1 = require("./types/AreaCode");
|
|
7
7
|
Object.defineProperty(exports, "AreaCode", { enumerable: true, get: function () { return AreaCode_1.AreaCode; } });
|
|
@@ -38,6 +38,8 @@ Object.defineProperty(exports, "Log", { enumerable: true, get: function () { ret
|
|
|
38
38
|
Object.defineProperty(exports, "Performance", { enumerable: true, get: function () { return Log_1.Performance; } });
|
|
39
39
|
var TypeConvertion_1 = require("./core/TypeConvertion");
|
|
40
40
|
Object.defineProperty(exports, "getBoolean", { enumerable: true, get: function () { return TypeConvertion_1.getBoolean; } });
|
|
41
|
+
var Environment_1 = require("./core/Environment");
|
|
42
|
+
Object.defineProperty(exports, "Environment", { enumerable: true, get: function () { return Environment_1.Environment; } });
|
|
41
43
|
//// object
|
|
42
44
|
var Calculater_1 = require("./core/object/Calculater");
|
|
43
45
|
Object.defineProperty(exports, "ObjectCalculater", { enumerable: true, get: function () { return Calculater_1.ObjectCalculater; } });
|
|
@@ -68,3 +70,5 @@ var RSA_1 = require("./security/RSA");
|
|
|
68
70
|
Object.defineProperty(exports, "RSA", { enumerable: true, get: function () { return RSA_1.RSA; } });
|
|
69
71
|
var SHA_1 = require("./security/SHA");
|
|
70
72
|
Object.defineProperty(exports, "SHA", { enumerable: true, get: function () { return SHA_1.SHA; } });
|
|
73
|
+
var TOTP_1 = require("./security/TOTP");
|
|
74
|
+
Object.defineProperty(exports, "TOTP", { enumerable: true, get: function () { return TOTP_1.TOTP; } });
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** @format */
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.TOTP = void 0;
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
+
const Integer_1 = require("../core/object/Integer");
|
|
10
|
+
const DataView_1 = require("../core/object/DataView");
|
|
11
|
+
const Base32_1 = require("./Base32");
|
|
12
|
+
/** TOTP Lib */
|
|
13
|
+
class TOTP {
|
|
14
|
+
/**
|
|
15
|
+
* To generate a new TOTP secret key
|
|
16
|
+
*
|
|
17
|
+
* @returns TOTP secret key
|
|
18
|
+
*/
|
|
19
|
+
static generate() {
|
|
20
|
+
return Base32_1.Base32.random(20);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Generate TOTP random code
|
|
24
|
+
*
|
|
25
|
+
* @param key TOTP secret key
|
|
26
|
+
* @returns return TOTP random code
|
|
27
|
+
*/
|
|
28
|
+
static code(key, time) {
|
|
29
|
+
const K = Base32_1.Base32.decode(key.toUpperCase(), "RFC4648");
|
|
30
|
+
const T = Math.floor((time || Date.now()) / 1000 / 30);
|
|
31
|
+
const hmac = crypto_1.default.createHmac("sha1", DataView_1.DataView.parse(K));
|
|
32
|
+
const T1 = Buffer.from(Integer_1.Integer.toBytes(T));
|
|
33
|
+
const HS = hmac.update(T1).digest();
|
|
34
|
+
const offset = Integer_1.Integer.and(HS[19], 0xf);
|
|
35
|
+
const bytes = Integer_1.Integer.or(Integer_1.Integer.left(Integer_1.Integer.and(HS[offset], 0x7f) /** 这里是为了忽略符号位 */, 24), Integer_1.Integer.left(Integer_1.Integer.and(HS[offset + 1], 0xff), 16), Integer_1.Integer.left(Integer_1.Integer.and(HS[offset + 2], 0xff), 8), Integer_1.Integer.and(HS[offset + 3], 0xff));
|
|
36
|
+
let code = bytes.toString().slice(-6);
|
|
37
|
+
/* istanbul ignore next */
|
|
38
|
+
for (let i = 0; i > 6 - code.length; i++) {
|
|
39
|
+
code = `0${code}`;
|
|
40
|
+
}
|
|
41
|
+
return code;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Generate a key and TOTP register URL from given user
|
|
45
|
+
*
|
|
46
|
+
* @param user user name
|
|
47
|
+
* @returns return the secret key and url
|
|
48
|
+
*/
|
|
49
|
+
static getUrl(user, project = "tianyu-template-project") {
|
|
50
|
+
const key = TOTP.generate();
|
|
51
|
+
const url = `otpauth://totp/${user}?secret=${key}&issuer=${project}`;
|
|
52
|
+
return { key, url };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.TOTP = TOTP;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export { ArgumentNullOrEmptyException, ObjectCloneFunctionNotSupportException, O
|
|
|
13
13
|
export { parseAreaCode, parseAreaString } from "./core/Language";
|
|
14
14
|
export { Log, Performance } from "./core/Log";
|
|
15
15
|
export { getBoolean } from "./core/TypeConvertion";
|
|
16
|
+
export { Environment } from "./core/Environment";
|
|
16
17
|
export { ObjectCalculater } from "./core/object/Calculater";
|
|
17
18
|
export { ObjectHelper } from "./core/object/ObjectHelper";
|
|
18
19
|
export { ArrayHelper } from "./core/object/ArrayHelper";
|
|
@@ -27,3 +28,4 @@ export { hash } from "./security/Hash";
|
|
|
27
28
|
export { QRCode } from "./security/QRCode";
|
|
28
29
|
export { RSA } from "./security/RSA";
|
|
29
30
|
export { SHA } from "./security/SHA";
|
|
31
|
+
export { TOTP } from "./security/TOTP";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
/** TOTP Lib */
|
|
3
|
+
export declare class TOTP {
|
|
4
|
+
/**
|
|
5
|
+
* To generate a new TOTP secret key
|
|
6
|
+
*
|
|
7
|
+
* @returns TOTP secret key
|
|
8
|
+
*/
|
|
9
|
+
static generate(): string;
|
|
10
|
+
/**
|
|
11
|
+
* Generate TOTP random code
|
|
12
|
+
*
|
|
13
|
+
* @param key TOTP secret key
|
|
14
|
+
* @returns return TOTP random code
|
|
15
|
+
*/
|
|
16
|
+
static code(key: string, time?: number): string;
|
|
17
|
+
/**
|
|
18
|
+
* Generate a key and TOTP register URL from given user
|
|
19
|
+
*
|
|
20
|
+
* @param user user name
|
|
21
|
+
* @returns return the secret key and url
|
|
22
|
+
*/
|
|
23
|
+
static getUrl(user: string, project?: string): {
|
|
24
|
+
key: string;
|
|
25
|
+
url: string;
|
|
26
|
+
};
|
|
27
|
+
}
|