@riao/iam 1.0.0 → 3.0.0
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.
- package/README.md +67 -1
- package/auth/auth-migrations.d.ts +6 -0
- package/auth/auth-migrations.js +15 -0
- package/auth/auth-migrations.js.map +1 -0
- package/auth/auth.d.ts +9 -0
- package/auth/auth.js +15 -0
- package/auth/auth.js.map +1 -0
- package/auth/index.d.ts +2 -0
- package/auth/index.js +6 -0
- package/auth/index.js.map +1 -0
- package/auth/migrations/001-create-principals-table.d.ts +5 -0
- package/auth/migrations/001-create-principals-table.js +39 -0
- package/auth/migrations/001-create-principals-table.js.map +1 -0
- package/auth/principal.d.ts +8 -0
- package/{token.js → auth/principal.js} +3 -3
- package/auth/principal.js.map +1 -0
- package/authentication/authentication.d.ts +9 -0
- package/authentication/authentication.js +41 -0
- package/authentication/authentication.js.map +1 -0
- package/authentication/index.d.ts +1 -0
- package/authentication/index.js +6 -0
- package/authentication/index.js.map +1 -0
- package/crypto.d.ts +16 -0
- package/crypto.js +35 -0
- package/crypto.js.map +1 -0
- package/hash.d.ts +5 -0
- package/hash.js +15 -0
- package/hash.js.map +1 -0
- package/index.d.ts +4 -2
- package/index.js +13 -18
- package/index.js.map +1 -1
- package/jwt.d.ts +26 -28
- package/jwt.js +48 -83
- package/jwt.js.map +1 -1
- package/keypair.d.ts +24 -24
- package/keypair.js +70 -70
- package/keypair.js.map +1 -1
- package/package.json +7 -6
- package/secret.d.ts +5 -5
- package/secret.js +2 -2
- package/auth.d.ts +0 -11
- package/auth.js +0 -16
- package/auth.js.map +0 -1
- package/authentication-base.d.ts +0 -12
- package/authentication-base.js +0 -48
- package/authentication-base.js.map +0 -1
- package/authentication-password.d.ts +0 -11
- package/authentication-password.js +0 -43
- package/authentication-password.js.map +0 -1
- package/authentication-passwordless.d.ts +0 -24
- package/authentication-passwordless.js +0 -55
- package/authentication-passwordless.js.map +0 -1
- package/authorization-abac.d.ts +0 -8
- package/authorization-abac.js +0 -33
- package/authorization-abac.js.map +0 -1
- package/authorization-base.d.ts +0 -17
- package/authorization-base.js +0 -30
- package/authorization-base.js.map +0 -1
- package/authorization-hmm.d.ts +0 -0
- package/authorization-hmm.js +0 -173
- package/authorization-hmm.js.map +0 -1
- package/authorization-rbac.d.ts +0 -14
- package/authorization-rbac.js +0 -61
- package/authorization-rbac.js.map +0 -1
- package/errors/authentication-error.d.ts +0 -3
- package/errors/authentication-error.js +0 -8
- package/errors/authentication-error.js.map +0 -1
- package/errors/authorization-error.d.ts +0 -3
- package/errors/authorization-error.js +0 -8
- package/errors/authorization-error.js.map +0 -1
- package/errors/error.d.ts +0 -2
- package/errors/error.js +0 -7
- package/errors/error.js.map +0 -1
- package/iam.d.ts +0 -40
- package/iam.js +0 -73
- package/iam.js.map +0 -1
- package/migrations/1725221752756-create-users-table.d.ts +0 -7
- package/migrations/1725221752756-create-users-table.js +0 -36
- package/migrations/1725221752756-create-users-table.js.map +0 -1
- package/migrations/1725221859886-create-roles-table.d.ts +0 -7
- package/migrations/1725221859886-create-roles-table.js +0 -36
- package/migrations/1725221859886-create-roles-table.js.map +0 -1
- package/migrations/1725221896876-create-permissions-table.d.ts +0 -7
- package/migrations/1725221896876-create-permissions-table.js +0 -41
- package/migrations/1725221896876-create-permissions-table.js.map +0 -1
- package/migrations/1725221935064-create-role-permissions-table.d.ts +0 -7
- package/migrations/1725221935064-create-role-permissions-table.js +0 -56
- package/migrations/1725221935064-create-role-permissions-table.js.map +0 -1
- package/migrations/1725221951873-create-user-roles-table.d.ts +0 -7
- package/migrations/1725221951873-create-user-roles-table.js +0 -56
- package/migrations/1725221951873-create-user-roles-table.js.map +0 -1
- package/migrations/index.d.ts +0 -5
- package/migrations/index.js +0 -22
- package/migrations/index.js.map +0 -1
- package/token.d.ts +0 -13
- package/token.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,4 +1,70 @@
|
|
|
1
|
-
# riao
|
|
1
|
+
# @riao/iam - Readme
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
`npm i @riao/iam`
|
|
6
|
+
|
|
7
|
+
## Encryption & Key Pairs
|
|
8
|
+
|
|
9
|
+
To use encryption and signing features, you need to generate a key pair. The `KeyPairGenerator` class supports generating RSA and Elliptic Curve key pairs.
|
|
10
|
+
|
|
11
|
+
### Quick Start with Encryptor/Decryptor (Recommended)
|
|
12
|
+
|
|
13
|
+
The `Encryptor` and `Decryptor` classes provide a simple, object-oriented interface for managing encryption and decryption:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Encryptor, Decryptor, KeyPairGenerator } from '@riao/iam';
|
|
17
|
+
|
|
18
|
+
// Generate a key pair
|
|
19
|
+
const generator = new KeyPairGenerator({ algorithm: 'RS256' });
|
|
20
|
+
const keypair = generator.generate();
|
|
21
|
+
|
|
22
|
+
// Create Encryptor and Decryptor instances
|
|
23
|
+
const encryptor = new Encryptor(keypair.publicKey);
|
|
24
|
+
const decryptor = new Decryptor(keypair.privateKey);
|
|
25
|
+
|
|
26
|
+
// Encrypt and decrypt
|
|
27
|
+
const encrypted = encryptor.encrypt('Secret message');
|
|
28
|
+
const decrypted = decryptor.decrypt(encrypted);
|
|
29
|
+
console.log(decrypted.toString()); // 'Secret message'
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## JSON Web Tokens (JWT)
|
|
33
|
+
|
|
34
|
+
The `Jwt` class provides a secure way to generate and verify JSON Web Tokens for authentication and authorization. It supports both symmetric signing (using a shared secret) and asymmetric signing (using public/private key pairs).
|
|
35
|
+
|
|
36
|
+
### Quick Start
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Jwt } from '@riao/iam';
|
|
40
|
+
|
|
41
|
+
// Create a JWT manager with a shared secret
|
|
42
|
+
const jwtManager = new Jwt({
|
|
43
|
+
secret: 'your-secret-key',
|
|
44
|
+
algorithm: 'HS512',
|
|
45
|
+
expiresIn: '1h',
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Generate a token
|
|
49
|
+
const { token } = await jwtManager.generateToken({
|
|
50
|
+
userId: 'user-123',
|
|
51
|
+
role: 'admin',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Verify and decode a token
|
|
55
|
+
const payload = await jwtManager.decodeToken(token);
|
|
56
|
+
console.log(payload.userId); // 'user-123'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
For detailed usage, algorithms, security best practices, and advanced patterns, see the [JWT Guide](docs/jwt-guide.md).
|
|
60
|
+
|
|
61
|
+
### Documentation
|
|
62
|
+
|
|
63
|
+
- [Encryptor & Decryptor Guide](docs/encryptor-decryptor-guide.md) - **Recommended** - Object-oriented encryption/decryption interface
|
|
64
|
+
- [KeyPair Usage Guide](docs/keypair-guide.md) - Learn how to use the KeyPairGenerator for cryptographic operations
|
|
65
|
+
- [Encryption & Decryption Guide](docs/crypto-guide.md) - Lower-level `encrypt` and `decrypt` functions (for advanced use cases)
|
|
66
|
+
- [Hash Guide](docs/hash-guide.md) - Password hashing and verification with bcrypt
|
|
67
|
+
- [JWT Guide](docs/jwt-guide.md) - Generate and verify JSON Web Tokens for authentication and authorization
|
|
2
68
|
|
|
3
69
|
## Contributing & Development
|
|
4
70
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthMigrations = void 0;
|
|
4
|
+
const dbal_1 = require("@riao/dbal");
|
|
5
|
+
// eslint-disable-next-line max-len
|
|
6
|
+
const _001_create_principals_table_1 = require("./migrations/001-create-principals-table");
|
|
7
|
+
class AuthMigrations extends dbal_1.MigrationPackage {
|
|
8
|
+
name = '@riao/iam';
|
|
9
|
+
package = '@riao/iam';
|
|
10
|
+
async getMigrations() {
|
|
11
|
+
return { 'create-principals-table': _001_create_principals_table_1.CreatePrincipalsTableMigration };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.AuthMigrations = AuthMigrations;
|
|
15
|
+
//# sourceMappingURL=auth-migrations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-migrations.js","sourceRoot":"","sources":["../../../src/auth/auth-migrations.ts"],"names":[],"mappings":";;;AAAA,qCAAyD;AACzD,mCAAmC;AACnC,2FAA0F;AAE1F,MAAa,cAAe,SAAQ,uBAAgB;IAC1C,IAAI,GAAG,WAAW,CAAC;IACnB,OAAO,GAAG,WAAW,CAAC;IAExB,KAAK,CAAC,aAAa;QAIzB,OAAO,EAAE,yBAAyB,EAAE,6DAA8B,EAAE,CAAC;IACtE,CAAC;CACD;AAVD,wCAUC"}
|
package/auth/auth.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Database, DatabaseRecord, QueryRepository } from '@riao/dbal';
|
|
2
|
+
export interface AuthOptions {
|
|
3
|
+
db: Database;
|
|
4
|
+
}
|
|
5
|
+
export declare abstract class Auth<TPrincipal extends DatabaseRecord> {
|
|
6
|
+
principalRepo: QueryRepository<TPrincipal>;
|
|
7
|
+
protected loginColumn: string;
|
|
8
|
+
constructor(options: AuthOptions);
|
|
9
|
+
}
|
package/auth/auth.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Auth = void 0;
|
|
4
|
+
class Auth {
|
|
5
|
+
principalRepo;
|
|
6
|
+
loginColumn = 'login';
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.principalRepo = options.db.getQueryRepository({
|
|
9
|
+
table: 'iam_principals',
|
|
10
|
+
identifiedBy: 'id',
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.Auth = Auth;
|
|
15
|
+
//# sourceMappingURL=auth.js.map
|
package/auth/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../../src/auth/auth.ts"],"names":[],"mappings":";;;AAMA,MAAsB,IAAI;IAClB,aAAa,CAA8B;IACxC,WAAW,GAAG,OAAO,CAAC;IAEhC,YAAmB,OAAoB;QACtC,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,EAAE,CAAC,kBAAkB,CAAa;YAC9D,KAAK,EAAE,gBAAgB;YACvB,YAAY,EAAE,IAAI;SAClB,CAAC,CAAC;IACJ,CAAC;CACD;AAVD,oBAUC"}
|
package/auth/index.d.ts
ADDED
package/auth/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Auth = void 0;
|
|
4
|
+
var auth_1 = require("./auth");
|
|
5
|
+
Object.defineProperty(exports, "Auth", { enumerable: true, get: function () { return auth_1.Auth; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/auth/index.ts"],"names":[],"mappings":";;;AAAA,+BAA8B;AAArB,4FAAA,IAAI,OAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CreatePrincipalsTableMigration = void 0;
|
|
4
|
+
const column_pack_1 = require("@riao/dbal/column-pack");
|
|
5
|
+
const dbal_1 = require("@riao/dbal");
|
|
6
|
+
class CreatePrincipalsTableMigration extends dbal_1.Migration {
|
|
7
|
+
async up() {
|
|
8
|
+
await this.ddl.createTable({
|
|
9
|
+
name: 'iam_principals',
|
|
10
|
+
columns: [
|
|
11
|
+
column_pack_1.UUIDKeyColumn,
|
|
12
|
+
{
|
|
13
|
+
name: 'type',
|
|
14
|
+
type: dbal_1.ColumnType.VARCHAR,
|
|
15
|
+
length: 20,
|
|
16
|
+
required: true,
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
...column_pack_1.UsernameColumn,
|
|
20
|
+
name: 'login',
|
|
21
|
+
required: true,
|
|
22
|
+
},
|
|
23
|
+
{ ...column_pack_1.NameColumn, required: true },
|
|
24
|
+
column_pack_1.CreateTimestampColumn,
|
|
25
|
+
{
|
|
26
|
+
name: 'deactivate_timestamp',
|
|
27
|
+
type: dbal_1.ColumnType.TIMESTAMP,
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
async down() {
|
|
33
|
+
await this.ddl.dropTable({
|
|
34
|
+
tables: ['iam_principals'],
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.CreatePrincipalsTableMigration = CreatePrincipalsTableMigration;
|
|
39
|
+
//# sourceMappingURL=001-create-principals-table.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"001-create-principals-table.js","sourceRoot":"","sources":["../../../../src/auth/migrations/001-create-principals-table.ts"],"names":[],"mappings":";;;AAAA,wDAKgC;AAChC,qCAAmD;AAEnD,MAAa,8BAA+B,SAAQ,gBAAS;IACnD,KAAK,CAAC,EAAE;QAChB,MAAM,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC;YAC1B,IAAI,EAAE,gBAAgB;YACtB,OAAO,EAAE;gBACR,2BAAa;gBACb;oBACC,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,iBAAU,CAAC,OAAO;oBACxB,MAAM,EAAE,EAAE;oBACV,QAAQ,EAAE,IAAI;iBACd;gBACD;oBACC,GAAG,4BAAc;oBACjB,IAAI,EAAE,OAAO;oBACb,QAAQ,EAAE,IAAI;iBACd;gBACD,EAAE,GAAG,wBAAU,EAAE,QAAQ,EAAE,IAAI,EAAE;gBACjC,mCAAqB;gBACrB;oBACC,IAAI,EAAE,sBAAsB;oBAC5B,IAAI,EAAE,iBAAU,CAAC,SAAS;iBAC1B;aACD;SACD,CAAC,CAAC;IACJ,CAAC;IAEQ,KAAK,CAAC,IAAI;QAClB,MAAM,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC;YACxB,MAAM,EAAE,CAAC,gBAAgB,CAAC;SAC1B,CAAC,CAAC;IACJ,CAAC;CACD;AAhCD,wEAgCC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
//# sourceMappingURL=
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
//# sourceMappingURL=principal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principal.js","sourceRoot":"","sources":["../../../src/auth/principal.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DatabaseRecordId, Expression, SelectQuery } from '@riao/dbal';
|
|
2
|
+
import { Auth, Principal } from '../auth';
|
|
3
|
+
export declare abstract class Authentication<TPrincipal extends Principal> extends Auth<TPrincipal> {
|
|
4
|
+
createPrincipal(principal: Omit<TPrincipal, 'id' | 'create_timestamp'>): Promise<DatabaseRecordId>;
|
|
5
|
+
abstract authenticate(credentials: any): Promise<TPrincipal | null>;
|
|
6
|
+
findActivePrincipal(query: SelectQuery<TPrincipal>): Promise<TPrincipal | null>;
|
|
7
|
+
deactivatePrincipal(principalId: DatabaseRecordId): Promise<void>;
|
|
8
|
+
protected isActiveQuery(): undefined | Expression<TPrincipal>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Authentication = void 0;
|
|
4
|
+
const dbal_1 = require("@riao/dbal");
|
|
5
|
+
const auth_1 = require("../auth");
|
|
6
|
+
class Authentication extends auth_1.Auth {
|
|
7
|
+
async createPrincipal(principal) {
|
|
8
|
+
const inserted = await this.principalRepo.insertOne({
|
|
9
|
+
record: principal,
|
|
10
|
+
});
|
|
11
|
+
return inserted.id;
|
|
12
|
+
}
|
|
13
|
+
async findActivePrincipal(query) {
|
|
14
|
+
const isActiveQuery = this.isActiveQuery();
|
|
15
|
+
if (query.where && isActiveQuery !== undefined) {
|
|
16
|
+
query.where = [isActiveQuery, dbal_1.and, query.where];
|
|
17
|
+
}
|
|
18
|
+
else if (isActiveQuery !== undefined) {
|
|
19
|
+
query.where = isActiveQuery;
|
|
20
|
+
}
|
|
21
|
+
const principal = await this.principalRepo.findOne({
|
|
22
|
+
...query,
|
|
23
|
+
});
|
|
24
|
+
return principal;
|
|
25
|
+
}
|
|
26
|
+
async deactivatePrincipal(principalId) {
|
|
27
|
+
return this.principalRepo.update({
|
|
28
|
+
set: {
|
|
29
|
+
deactivate_timestamp: new Date(),
|
|
30
|
+
},
|
|
31
|
+
where: { id: principalId },
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
isActiveQuery() {
|
|
35
|
+
return {
|
|
36
|
+
deactivate_timestamp: null,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.Authentication = Authentication;
|
|
41
|
+
//# sourceMappingURL=authentication.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authentication.js","sourceRoot":"","sources":["../../../src/authentication/authentication.ts"],"names":[],"mappings":";;;AAAA,qCAA4E;AAC5E,kCAA0C;AAG1C,MAAsB,cAEpB,SAAQ,WAAgB;IAClB,KAAK,CAAC,eAAe,CAC3B,SAAsD;QAEtD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC;YACnD,MAAM,EAAE,SAAuB;SAC/B,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC,EAAsB,CAAC;IACxC,CAAC;IAKM,KAAK,CAAC,mBAAmB,CAC/B,KAA8B;QAE9B,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;QAE3C,IAAI,KAAK,CAAC,KAAK,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAChD,KAAK,CAAC,KAAK,GAAG,CAAC,aAAa,EAAE,UAAG,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;QACjD,CAAC;aACI,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YACtC,KAAK,CAAC,KAAK,GAAG,aAAa,CAAC;QAC7B,CAAC;QAED,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC;YAClD,GAAG,KAAK;SACR,CAAC,CAAC;QAEH,OAAO,SAAS,CAAC;IAClB,CAAC;IAEM,KAAK,CAAC,mBAAmB,CAC/B,WAA6B;QAE7B,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;YAChC,GAAG,EAAE;gBACJ,oBAAoB,EAAE,IAAI,IAAI,EAAE;aACT;YACxB,KAAK,EAAE,EAAE,EAAE,EAAE,WAAW,EAAkC;SAC1D,CAAC,CAAC;IACJ,CAAC;IAES,aAAa;QACtB,OAAO;YACN,oBAAoB,EAAE,IAAI;SACM,CAAC;IACnC,CAAC;CACD;AAnDD,wCAmDC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Authentication } from './authentication';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Authentication = void 0;
|
|
4
|
+
var authentication_1 = require("./authentication");
|
|
5
|
+
Object.defineProperty(exports, "Authentication", { enumerable: true, get: function () { return authentication_1.Authentication; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/authentication/index.ts"],"names":[],"mappings":";;;AAAA,mDAAkD;AAAzC,gHAAA,cAAc,OAAA"}
|
package/crypto.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
import * as crypto from 'crypto';
|
|
5
|
+
export declare function encrypt(publicKey: crypto.KeyObject, data: string | Buffer): Buffer;
|
|
6
|
+
export declare function decrypt(privateKey: crypto.KeyObject, encryptedData: Buffer): Buffer;
|
|
7
|
+
export declare class Encryptor {
|
|
8
|
+
private publicKey;
|
|
9
|
+
constructor(publicKey: crypto.KeyObject);
|
|
10
|
+
encrypt(data: string | Buffer): Buffer;
|
|
11
|
+
}
|
|
12
|
+
export declare class Decryptor {
|
|
13
|
+
private privateKey;
|
|
14
|
+
constructor(privateKey: crypto.KeyObject);
|
|
15
|
+
decrypt(encryptedData: Buffer): Buffer;
|
|
16
|
+
}
|
package/crypto.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Decryptor = exports.Encryptor = exports.decrypt = exports.encrypt = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const crypto = tslib_1.__importStar(require("crypto"));
|
|
6
|
+
function encrypt(publicKey, data) {
|
|
7
|
+
data = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
8
|
+
return crypto.publicEncrypt(publicKey, data);
|
|
9
|
+
}
|
|
10
|
+
exports.encrypt = encrypt;
|
|
11
|
+
function decrypt(privateKey, encryptedData) {
|
|
12
|
+
return crypto.privateDecrypt(privateKey, encryptedData);
|
|
13
|
+
}
|
|
14
|
+
exports.decrypt = decrypt;
|
|
15
|
+
class Encryptor {
|
|
16
|
+
publicKey;
|
|
17
|
+
constructor(publicKey) {
|
|
18
|
+
this.publicKey = publicKey;
|
|
19
|
+
}
|
|
20
|
+
encrypt(data) {
|
|
21
|
+
return encrypt(this.publicKey, data);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.Encryptor = Encryptor;
|
|
25
|
+
class Decryptor {
|
|
26
|
+
privateKey;
|
|
27
|
+
constructor(privateKey) {
|
|
28
|
+
this.privateKey = privateKey;
|
|
29
|
+
}
|
|
30
|
+
decrypt(encryptedData) {
|
|
31
|
+
return decrypt(this.privateKey, encryptedData);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.Decryptor = Decryptor;
|
|
35
|
+
//# sourceMappingURL=crypto.js.map
|
package/crypto.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":";;;;AAAA,uDAAiC;AAEjC,SAAgB,OAAO,CACtB,SAA2B,EAC3B,IAAqB;IAErB,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxD,OAAO,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAC9C,CAAC;AAND,0BAMC;AAED,SAAgB,OAAO,CACtB,UAA4B,EAC5B,aAAqB;IAErB,OAAO,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;AACzD,CAAC;AALD,0BAKC;AAED,MAAa,SAAS;IACb,SAAS,CAAmB;IAEpC,YAAY,SAA2B;QACtC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC5B,CAAC;IAED,OAAO,CAAC,IAAqB;QAC5B,OAAO,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACtC,CAAC;CACD;AAVD,8BAUC;AAED,MAAa,SAAS;IACb,UAAU,CAAmB;IAErC,YAAY,UAA4B;QACvC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC9B,CAAC;IAED,OAAO,CAAC,aAAqB;QAC5B,OAAO,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;IAChD,CAAC;CACD;AAVD,8BAUC"}
|
package/hash.d.ts
ADDED
package/hash.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Hash = void 0;
|
|
4
|
+
const bcrypt_1 = require("bcrypt");
|
|
5
|
+
class Hash {
|
|
6
|
+
rounds = 12;
|
|
7
|
+
async make(input, rounds) {
|
|
8
|
+
return await (0, bcrypt_1.hash)(input, rounds || this.rounds);
|
|
9
|
+
}
|
|
10
|
+
async check(input, hashed) {
|
|
11
|
+
return await (0, bcrypt_1.compare)(input, hashed);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.Hash = Hash;
|
|
15
|
+
//# sourceMappingURL=hash.js.map
|
package/hash.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/hash.ts"],"names":[],"mappings":";;;AAAA,mCAAuC;AAEvC,MAAa,IAAI;IACT,MAAM,GAAW,EAAE,CAAC;IAEpB,KAAK,CAAC,IAAI,CAAC,KAAa,EAAE,MAAe;QAC/C,OAAO,MAAM,IAAA,aAAI,EAAC,KAAK,EAAE,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC;IACjD,CAAC;IAEM,KAAK,CAAC,KAAK,CAAC,KAAa,EAAE,MAAc;QAC/C,OAAO,MAAM,IAAA,gBAAO,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACrC,CAAC;CACD;AAVD,oBAUC"}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./iam"), exports);
|
|
18
|
-
__exportStar(require("./jwt"), exports);
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KeyPairGenerator = exports.Decryptor = exports.Encryptor = exports.decrypt = exports.encrypt = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
var crypto_1 = require("./crypto");
|
|
6
|
+
Object.defineProperty(exports, "encrypt", { enumerable: true, get: function () { return crypto_1.encrypt; } });
|
|
7
|
+
Object.defineProperty(exports, "decrypt", { enumerable: true, get: function () { return crypto_1.decrypt; } });
|
|
8
|
+
Object.defineProperty(exports, "Encryptor", { enumerable: true, get: function () { return crypto_1.Encryptor; } });
|
|
9
|
+
Object.defineProperty(exports, "Decryptor", { enumerable: true, get: function () { return crypto_1.Decryptor; } });
|
|
10
|
+
var keypair_1 = require("./keypair");
|
|
11
|
+
Object.defineProperty(exports, "KeyPairGenerator", { enumerable: true, get: function () { return keypair_1.KeyPairGenerator; } });
|
|
12
|
+
tslib_1.__exportStar(require("./auth"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./authentication"), exports);
|
|
19
14
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;AAAA,mCAAkE;AAAzD,iGAAA,OAAO,OAAA;AAAE,iGAAA,OAAO,OAAA;AAAE,mGAAA,SAAS,OAAA;AAAE,mGAAA,SAAS,OAAA;AAC/C,qCAA6C;AAApC,2GAAA,gBAAgB,OAAA;AAEzB,iDAAuB;AACvB,2DAAiC"}
|
package/jwt.d.ts
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import * as jwt from 'jsonwebtoken';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
export type
|
|
8
|
-
export type
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
export declare class Jwt<TPayload extends JwtPayload = JwtPayload> {
|
|
17
|
-
protected publicKey: KeyObject;
|
|
18
|
-
protected privateKey: KeyObject;
|
|
19
|
-
protected expiresIn?:
|
|
20
|
-
protected notBefore?:
|
|
21
|
-
protected algorithm?: SecretAlgorithm | KeyPairAlgorithm;
|
|
22
|
-
constructor(options: JwtOptions);
|
|
23
|
-
protected tokenOptions(): jwt.SignOptions;
|
|
24
|
-
generateToken(data: TPayload, options?: jwt.SignOptions): Promise<Token>;
|
|
25
|
-
decodeToken(token: string): Promise<TPayload>;
|
|
26
|
-
|
|
27
|
-
verifyRefreshToken(token: string): Promise<TPayload>;
|
|
28
|
-
}
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import * as jwt from 'jsonwebtoken';
|
|
3
|
+
import ms from 'ms';
|
|
4
|
+
import { Secret, SecretAlgorithm } from './secret';
|
|
5
|
+
import { KeyPair, KeyPairAlgorithm } from './keypair';
|
|
6
|
+
import { KeyObject } from 'crypto';
|
|
7
|
+
export type JwtPayload = Record<string, unknown>;
|
|
8
|
+
export type JwtSigningOptions = Secret | KeyPair;
|
|
9
|
+
export type JwtOptions = JwtSigningOptions & {
|
|
10
|
+
expiresIn?: ms.StringValue;
|
|
11
|
+
notBefore?: ms.StringValue;
|
|
12
|
+
};
|
|
13
|
+
export interface Token {
|
|
14
|
+
token: string;
|
|
15
|
+
}
|
|
16
|
+
export declare class Jwt<TPayload extends JwtPayload = JwtPayload> {
|
|
17
|
+
protected publicKey: KeyObject;
|
|
18
|
+
protected privateKey: KeyObject;
|
|
19
|
+
protected expiresIn?: ms.StringValue;
|
|
20
|
+
protected notBefore?: ms.StringValue;
|
|
21
|
+
protected algorithm?: SecretAlgorithm | KeyPairAlgorithm;
|
|
22
|
+
constructor(options: JwtOptions);
|
|
23
|
+
protected tokenOptions(): jwt.SignOptions;
|
|
24
|
+
generateToken(data: TPayload, options?: jwt.SignOptions): Promise<Token>;
|
|
25
|
+
decodeToken(token: string): Promise<TPayload>;
|
|
26
|
+
}
|
package/jwt.js
CHANGED
|
@@ -1,84 +1,49 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const token = yield jwt.sign(data, this.privateKey, options);
|
|
50
|
-
return { token, expiration };
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
decodeToken(token) {
|
|
54
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
-
const result = yield jwt.verify(token, this.publicKey);
|
|
56
|
-
delete result.iat;
|
|
57
|
-
delete result.nbf;
|
|
58
|
-
delete result.exp;
|
|
59
|
-
return result;
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
verifyAccessToken(token) {
|
|
63
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
const data = yield this.decodeToken(token);
|
|
65
|
-
if (data.type !== 'access') {
|
|
66
|
-
throw new error_1.IamError('Expected access token, received ' + data.type);
|
|
67
|
-
}
|
|
68
|
-
delete data.type;
|
|
69
|
-
return data;
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
verifyRefreshToken(token) {
|
|
73
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
-
const data = yield this.decodeToken(token);
|
|
75
|
-
if (data.type !== 'refresh') {
|
|
76
|
-
throw new error_1.IamError('Expected refresh token, received ' + data.type);
|
|
77
|
-
}
|
|
78
|
-
delete data.type;
|
|
79
|
-
return data;
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
exports.Jwt = Jwt;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Jwt = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const jwt = tslib_1.__importStar(require("jsonwebtoken"));
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
7
|
+
class Jwt {
|
|
8
|
+
publicKey;
|
|
9
|
+
privateKey;
|
|
10
|
+
expiresIn;
|
|
11
|
+
notBefore;
|
|
12
|
+
algorithm;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.expiresIn = options.expiresIn ?? this.expiresIn;
|
|
15
|
+
this.notBefore = options.notBefore ?? this.notBefore;
|
|
16
|
+
this.algorithm = options.algorithm;
|
|
17
|
+
if ('secret' in options && options.secret) {
|
|
18
|
+
this.privateKey = this.publicKey = (0, crypto_1.createSecretKey)(Buffer.from(options.secret));
|
|
19
|
+
}
|
|
20
|
+
else if ('privateKey' in options && options.privateKey) {
|
|
21
|
+
this.publicKey = options.publicKey;
|
|
22
|
+
this.privateKey = options.privateKey;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
tokenOptions() {
|
|
26
|
+
return {
|
|
27
|
+
expiresIn: this.expiresIn ?? '15m',
|
|
28
|
+
notBefore: this.notBefore ?? 1,
|
|
29
|
+
algorithm: this.algorithm,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
async generateToken(data, options = {}) {
|
|
33
|
+
options = {
|
|
34
|
+
...this.tokenOptions(),
|
|
35
|
+
...options,
|
|
36
|
+
};
|
|
37
|
+
const token = await jwt.sign(data, this.privateKey, options);
|
|
38
|
+
return { token };
|
|
39
|
+
}
|
|
40
|
+
async decodeToken(token) {
|
|
41
|
+
const result = await jwt.verify(token, this.publicKey);
|
|
42
|
+
delete result['iat'];
|
|
43
|
+
delete result['nbf'];
|
|
44
|
+
delete result['exp'];
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.Jwt = Jwt;
|
|
84
49
|
//# sourceMappingURL=jwt.js.map
|
package/jwt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../../src/jwt.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../../src/jwt.ts"],"names":[],"mappings":";;;;AAAA,0DAAoC;AAKpC,mCAAoD;AAepD,MAAa,GAAG;IACL,SAAS,CAAY;IACrB,UAAU,CAAY;IAEtB,SAAS,CAAkB;IAC3B,SAAS,CAAkB;IAC3B,SAAS,CAAsC;IAEzD,YAAmB,OAAmB;QACrC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC;QACrD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC;QACrD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QAEnC,IAAI,QAAQ,IAAI,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YAC3C,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,SAAS,GAAG,IAAA,wBAAe,EACjD,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAC3B,CAAC;QACH,CAAC;aACI,IAAI,YAAY,IAAI,OAAO,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACxD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;YACnC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QACtC,CAAC;IACF,CAAC;IAES,YAAY;QACrB,OAAO;YACN,SAAS,EAAE,IAAI,CAAC,SAAS,IAAI,KAAK;YAClC,SAAS,EAAE,IAAI,CAAC,SAAS,IAAI,CAAC;YAC9B,SAAS,EAAE,IAAI,CAAC,SAAS;SACzB,CAAC;IACH,CAAC;IAEM,KAAK,CAAC,aAAa,CACzB,IAAc,EACd,UAA2B,EAAE;QAE7B,OAAO,GAAG;YACT,GAAG,IAAI,CAAC,YAAY,EAAE;YACtB,GAAG,OAAO;SACV,CAAC;QAEF,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAE7D,OAAO,EAAE,KAAK,EAAE,CAAC;IAClB,CAAC;IAEM,KAAK,CAAC,WAAW,CAAC,KAAa;QACrC,MAAM,MAAM,GAAa,MAAM,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;QAEjE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;QACrB,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;QACrB,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;QAErB,OAAO,MAAM,CAAC;IACf,CAAC;CACD;AAvDD,kBAuDC"}
|