@riao/iam 2.0.0 → 3.1.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 +5 -3
- package/auth/auth-migrations.js +8 -5
- package/auth/auth-migrations.js.map +1 -1
- package/auth/auth.d.ts +6 -8
- package/auth/auth.js +5 -8
- package/auth/auth.js.map +1 -1
- package/auth/index.d.ts +1 -0
- package/auth/migrations/{001-create-accounts-table.d.ts → 001-create-principals-table.d.ts} +1 -1
- 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/auth/principal.js +3 -0
- 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/authorization/authorization.d.ts +58 -0
- package/authorization/authorization.js +32 -0
- package/authorization/authorization.js.map +1 -0
- package/authorization/index.d.ts +1 -0
- package/authorization/index.js +6 -0
- package/authorization/index.js.map +1 -0
- package/crypto.d.ts +16 -0
- package/crypto.js +35 -0
- package/crypto.js.map +1 -0
- package/index.d.ts +5 -1
- package/index.js +12 -2
- package/index.js.map +1 -1
- package/jwt.d.ts +1 -1
- package/jwt.js +3 -9
- package/jwt.js.map +1 -1
- package/keypair.d.ts +5 -5
- package/keypair.js +21 -23
- package/keypair.js.map +1 -1
- package/package.json +9 -29
- package/auth/migrations/001-create-accounts-table.js +0 -27
- package/auth/migrations/001-create-accounts-table.js.map +0 -1
- package/authentication/authentication-base/authentication-base.d.ts +0 -10
- package/authentication/authentication-base/authentication-base.js +0 -33
- package/authentication/authentication-base/authentication-base.js.map +0 -1
- package/authentication/authentication-base/index.d.ts +0 -1
- package/authentication/authentication-base/index.js +0 -6
- package/authentication/authentication-base/index.js.map +0 -1
- package/authentication/authentication-fido2/authentication-fido2-migrations.d.ts +0 -5
- package/authentication/authentication-fido2/authentication-fido2-migrations.js +0 -17
- package/authentication/authentication-fido2/authentication-fido2-migrations.js.map +0 -1
- package/authentication/authentication-fido2/authentication-fido2.d.ts +0 -58
- package/authentication/authentication-fido2/authentication-fido2.js +0 -260
- package/authentication/authentication-fido2/authentication-fido2.js.map +0 -1
- package/authentication/authentication-fido2/index.d.ts +0 -1
- package/authentication/authentication-fido2/index.js +0 -6
- package/authentication/authentication-fido2/index.js.map +0 -1
- package/authentication/authentication-fido2/migrations/create-fido-challenges-table.d.ts +0 -5
- package/authentication/authentication-fido2/migrations/create-fido-challenges-table.js +0 -61
- package/authentication/authentication-fido2/migrations/create-fido-challenges-table.js.map +0 -1
- package/authentication/authentication-fido2/migrations/create-fido-credentials-table.d.ts +0 -5
- package/authentication/authentication-fido2/migrations/create-fido-credentials-table.js +0 -68
- package/authentication/authentication-fido2/migrations/create-fido-credentials-table.js.map +0 -1
- package/authentication/authentication-fido2/migrations/index.d.ts +0 -2
- package/authentication/authentication-fido2/migrations/index.js +0 -6
- package/authentication/authentication-fido2/migrations/index.js.map +0 -1
- package/authentication/authentication-magic-token/authentication-magic-token-migrations.d.ts +0 -5
- package/authentication/authentication-magic-token/authentication-magic-token-migrations.js +0 -16
- package/authentication/authentication-magic-token/authentication-magic-token-migrations.js.map +0 -1
- package/authentication/authentication-magic-token/authentication-magic-token.d.ts +0 -25
- package/authentication/authentication-magic-token/authentication-magic-token.js +0 -76
- package/authentication/authentication-magic-token/authentication-magic-token.js.map +0 -1
- package/authentication/authentication-magic-token/index.d.ts +0 -1
- package/authentication/authentication-magic-token/index.js +0 -6
- package/authentication/authentication-magic-token/index.js.map +0 -1
- package/authentication/authentication-magic-token/magic-token.d.ts +0 -17
- package/authentication/authentication-magic-token/magic-token.js +0 -7
- package/authentication/authentication-magic-token/magic-token.js.map +0 -1
- package/authentication/authentication-magic-token/migrations/001-create-magic-token-table.d.ts +0 -5
- package/authentication/authentication-magic-token/migrations/001-create-magic-token-table.js +0 -44
- package/authentication/authentication-magic-token/migrations/001-create-magic-token-table.js.map +0 -1
- package/authentication/authentication-password/authentication-password-migrations.d.ts +0 -5
- package/authentication/authentication-password/authentication-password-migrations.js +0 -16
- package/authentication/authentication-password/authentication-password-migrations.js.map +0 -1
- package/authentication/authentication-password/authentication-password.d.ts +0 -8
- package/authentication/authentication-password/authentication-password.js +0 -28
- package/authentication/authentication-password/authentication-password.js.map +0 -1
- package/authentication/authentication-password/index.d.ts +0 -1
- package/authentication/authentication-password/index.js +0 -6
- package/authentication/authentication-password/index.js.map +0 -1
- package/authentication/authentication-password/migrations/001-add-password-column.d.ts +0 -5
- package/authentication/authentication-password/migrations/001-add-password-column.js +0 -21
- package/authentication/authentication-password/migrations/001-add-password-column.js.map +0 -1
- package/encrypt.d.ts +0 -6
- package/encrypt.js +0 -18
- package/encrypt.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
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { Migration } from '@riao/dbal';
|
|
2
|
-
export declare class AuthMigrations {
|
|
3
|
-
|
|
1
|
+
import { Migration, MigrationPackage } from '@riao/dbal';
|
|
2
|
+
export declare class AuthMigrations extends MigrationPackage {
|
|
3
|
+
name: string;
|
|
4
|
+
package: string;
|
|
5
|
+
getMigrations(): Promise<Record<string, typeof Migration<any>>>;
|
|
4
6
|
}
|
package/auth/auth-migrations.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AuthMigrations = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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 };
|
|
9
12
|
}
|
|
10
13
|
}
|
|
11
14
|
exports.AuthMigrations = AuthMigrations;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-migrations.js","sourceRoot":"","sources":["../../../src/auth/auth-migrations.ts"],"names":[],"mappings":";;;
|
|
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
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { DatabaseRecord, QueryRepository } from '@riao/dbal';
|
|
2
|
-
export interface AuthOptions
|
|
3
|
-
|
|
1
|
+
import { Database, DatabaseRecord, QueryRepository } from '@riao/dbal';
|
|
2
|
+
export interface AuthOptions {
|
|
3
|
+
db: Database;
|
|
4
4
|
}
|
|
5
|
-
export declare abstract class Auth<
|
|
6
|
-
|
|
7
|
-
protected accountTable: string;
|
|
8
|
-
protected accountIdColumn: string;
|
|
5
|
+
export declare abstract class Auth<TPrincipal extends DatabaseRecord> {
|
|
6
|
+
principalRepo: QueryRepository<TPrincipal>;
|
|
9
7
|
protected loginColumn: string;
|
|
10
|
-
constructor(options: AuthOptions
|
|
8
|
+
constructor(options: AuthOptions);
|
|
11
9
|
}
|
package/auth/auth.js
CHANGED
|
@@ -2,16 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Auth = void 0;
|
|
4
4
|
class Auth {
|
|
5
|
-
|
|
6
|
-
accountTable = 'iam_accounts';
|
|
7
|
-
accountIdColumn = 'id';
|
|
5
|
+
principalRepo;
|
|
8
6
|
loginColumn = 'login';
|
|
9
7
|
constructor(options) {
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.accountRepo.getIdentifier() || this.accountIdColumn;
|
|
8
|
+
this.principalRepo = options.db.getQueryRepository({
|
|
9
|
+
table: 'iam_principals',
|
|
10
|
+
identifiedBy: 'id',
|
|
11
|
+
});
|
|
15
12
|
}
|
|
16
13
|
}
|
|
17
14
|
exports.Auth = Auth;
|
package/auth/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../../src/auth/auth.ts"],"names":[],"mappings":";;;
|
|
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
CHANGED
|
@@ -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"}
|
|
@@ -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"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { DatabaseRecordId } from '@riao/dbal';
|
|
2
|
+
import { Auth, Principal } from '../auth';
|
|
3
|
+
export interface AuthorizationContext<TPrincipal extends Principal = Principal> {
|
|
4
|
+
principal: TPrincipal;
|
|
5
|
+
resource?: string | Record<string, unknown>;
|
|
6
|
+
action: string;
|
|
7
|
+
metadata?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
export interface AuthorizationResult {
|
|
10
|
+
allowed: boolean;
|
|
11
|
+
reason?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface GrantPermissionOptions {
|
|
14
|
+
principalId: DatabaseRecordId;
|
|
15
|
+
action: string;
|
|
16
|
+
resource?: string;
|
|
17
|
+
metadata?: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
export interface RevokePermissionOptions {
|
|
20
|
+
principalId: DatabaseRecordId;
|
|
21
|
+
action: string;
|
|
22
|
+
resource?: string;
|
|
23
|
+
metadata?: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
export declare abstract class Authorization<TPrincipal extends Principal> extends Auth<TPrincipal> {
|
|
26
|
+
/**
|
|
27
|
+
* Evaluate authorization for a given context
|
|
28
|
+
* @param context The authorization context containing principal,
|
|
29
|
+
* resource, action, and optional metadata
|
|
30
|
+
* @returns Authorization result with allowed status and optional reason
|
|
31
|
+
*/
|
|
32
|
+
abstract evaluate(context: AuthorizationContext<TPrincipal>): Promise<AuthorizationResult>;
|
|
33
|
+
/**
|
|
34
|
+
* Check if a principal is authorized for a specific action on a resource
|
|
35
|
+
* @param context The authorization context
|
|
36
|
+
* @returns True if authorized, false otherwise
|
|
37
|
+
*/
|
|
38
|
+
isAuthorized(context: AuthorizationContext<TPrincipal>): Promise<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* Check if a principal has permission for an action
|
|
41
|
+
* Subclasses should override this for specific authorization models
|
|
42
|
+
* @param context The authorization context
|
|
43
|
+
* @returns Authorization result
|
|
44
|
+
*/
|
|
45
|
+
protected checkPermission(context: AuthorizationContext<TPrincipal>): Promise<AuthorizationResult>;
|
|
46
|
+
/**
|
|
47
|
+
* Grant a permission to a principal
|
|
48
|
+
* Subclasses should implement this based on their model
|
|
49
|
+
* @param options The grant permission options
|
|
50
|
+
*/
|
|
51
|
+
abstract grantPermission(options: GrantPermissionOptions): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Revoke a permission from a principal
|
|
54
|
+
* Subclasses should implement this based on their model
|
|
55
|
+
* @param options The revoke permission options
|
|
56
|
+
*/
|
|
57
|
+
abstract revokePermission(options: RevokePermissionOptions): Promise<void>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Authorization = void 0;
|
|
4
|
+
const auth_1 = require("../auth");
|
|
5
|
+
class Authorization extends auth_1.Auth {
|
|
6
|
+
/**
|
|
7
|
+
* Check if a principal is authorized for a specific action on a resource
|
|
8
|
+
* @param context The authorization context
|
|
9
|
+
* @returns True if authorized, false otherwise
|
|
10
|
+
*/
|
|
11
|
+
async isAuthorized(context) {
|
|
12
|
+
const result = await this.evaluate(context);
|
|
13
|
+
return result.allowed;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Check if a principal has permission for an action
|
|
17
|
+
* Subclasses should override this for specific authorization models
|
|
18
|
+
* @param context The authorization context
|
|
19
|
+
* @returns Authorization result
|
|
20
|
+
*/
|
|
21
|
+
async checkPermission(
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
+
context) {
|
|
24
|
+
// Default implementation returns false
|
|
25
|
+
return {
|
|
26
|
+
allowed: false,
|
|
27
|
+
reason: 'No authorization model configured',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.Authorization = Authorization;
|
|
32
|
+
//# sourceMappingURL=authorization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authorization.js","sourceRoot":"","sources":["../../../src/authorization/authorization.ts"],"names":[],"mappings":";;;AACA,kCAA0C;AA8B1C,MAAsB,aAEpB,SAAQ,WAAgB;IAWzB;;;;OAIG;IACI,KAAK,CAAC,YAAY,CACxB,OAAyC;QAEzC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC5C,OAAO,MAAM,CAAC,OAAO,CAAC;IACvB,CAAC;IAED;;;;;OAKG;IACO,KAAK,CAAC,eAAe;IAC9B,6DAA6D;IAC7D,OAAyC;QAEzC,uCAAuC;QACvC,OAAO;YACN,OAAO,EAAE,KAAK;YACd,MAAM,EAAE,mCAAmC;SAC3C,CAAC;IACH,CAAC;CAmBD;AA3DD,sCA2DC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Authorization, AuthorizationContext, AuthorizationResult, GrantPermissionOptions, RevokePermissionOptions, } from './authorization';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Authorization = void 0;
|
|
4
|
+
var authorization_1 = require("./authorization");
|
|
5
|
+
Object.defineProperty(exports, "Authorization", { enumerable: true, get: function () { return authorization_1.Authorization; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/authorization/index.ts"],"names":[],"mappings":";;;AAAA,iDAMyB;AALxB,8GAAA,aAAa,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/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
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);
|
|
14
|
+
tslib_1.__exportStar(require("./authorization"), exports);
|
|
5
15
|
//# 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;AACjC,0DAAgC"}
|
package/jwt.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import ms from 'ms';
|
|
|
4
4
|
import { Secret, SecretAlgorithm } from './secret';
|
|
5
5
|
import { KeyPair, KeyPairAlgorithm } from './keypair';
|
|
6
6
|
import { KeyObject } from 'crypto';
|
|
7
|
-
export type JwtPayload = Record<string,
|
|
7
|
+
export type JwtPayload = Record<string, unknown>;
|
|
8
8
|
export type JwtSigningOptions = Secret | KeyPair;
|
|
9
9
|
export type JwtOptions = JwtSigningOptions & {
|
|
10
10
|
expiresIn?: ms.StringValue;
|
package/jwt.js
CHANGED
|
@@ -13,19 +13,13 @@ class Jwt {
|
|
|
13
13
|
constructor(options) {
|
|
14
14
|
this.expiresIn = options.expiresIn ?? this.expiresIn;
|
|
15
15
|
this.notBefore = options.notBefore ?? this.notBefore;
|
|
16
|
-
this.algorithm = options.algorithm
|
|
16
|
+
this.algorithm = options.algorithm;
|
|
17
17
|
if ('secret' in options && options.secret) {
|
|
18
18
|
this.privateKey = this.publicKey = (0, crypto_1.createSecretKey)(Buffer.from(options.secret));
|
|
19
19
|
}
|
|
20
20
|
else if ('privateKey' in options && options.privateKey) {
|
|
21
|
-
this.publicKey =
|
|
22
|
-
|
|
23
|
-
? (0, crypto_1.createPublicKey)(options.publicKey)
|
|
24
|
-
: options.publicKey;
|
|
25
|
-
this.privateKey =
|
|
26
|
-
typeof options.privateKey === 'string'
|
|
27
|
-
? (0, crypto_1.createPrivateKey)(options.privateKey)
|
|
28
|
-
: options.privateKey;
|
|
21
|
+
this.publicKey = options.publicKey;
|
|
22
|
+
this.privateKey = options.privateKey;
|
|
29
23
|
}
|
|
30
24
|
}
|
|
31
25
|
tokenOptions() {
|
package/jwt.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.js","sourceRoot":"","sources":["../../src/jwt.ts"],"names":[],"mappings":";;;;AAAA,0DAAoC;AAKpC,
|
|
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"}
|
package/keypair.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import * as crypto from 'crypto';
|
|
3
|
-
export type KeyPairAlgorithm = 'RS256' | 'RS384' | 'RS512' | 'ES256' | 'ES384' | 'ES512';
|
|
3
|
+
export type KeyPairAlgorithm = 'RS256' | 'RS384' | 'RS512' | 'HS256' | 'ES256' | 'ES384' | 'ES512';
|
|
4
4
|
export interface KeyPair {
|
|
5
|
-
publicKey:
|
|
6
|
-
privateKey:
|
|
5
|
+
publicKey: crypto.KeyObject;
|
|
6
|
+
privateKey: crypto.KeyObject;
|
|
7
7
|
algorithm: KeyPairAlgorithm;
|
|
8
8
|
}
|
|
9
9
|
export declare class KeyPairGenerator {
|
|
@@ -15,10 +15,10 @@ export declare class KeyPairGenerator {
|
|
|
15
15
|
load(options: {
|
|
16
16
|
publicKeyPath: string;
|
|
17
17
|
privateKeyPath: string;
|
|
18
|
-
}): KeyPair
|
|
18
|
+
}): Promise<KeyPair>;
|
|
19
19
|
save(options: {
|
|
20
20
|
keys: KeyPair;
|
|
21
21
|
publicKeyPath: string;
|
|
22
22
|
privateKeyPath: string;
|
|
23
|
-
}): void
|
|
23
|
+
}): Promise<void>;
|
|
24
24
|
}
|