@login512/nestjs 0.0.0-stage → 2.1.1
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 +38 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/dist/login512.guard.d.ts +7 -0
- package/dist/login512.guard.js +59 -0
- package/dist/login512.module.d.ts +4 -0
- package/dist/login512.module.js +29 -0
- package/package.json +20 -4
- package/src/index.ts +2 -0
- package/src/login512.guard.ts +52 -0
- package/src/login512.module.ts +18 -0
package/README.md
CHANGED
|
@@ -1,3 +1,38 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @login512/nestjs
|
|
2
|
+
|
|
3
|
+
Official NestJS module and `Login512Guard` for **Login512** Post-Quantum Biometrics and Passkeys.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @login512/nestjs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## `app.module.ts`
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Module } from '@nestjs/common';
|
|
15
|
+
import { Login512Module } from '@login512/nestjs';
|
|
16
|
+
|
|
17
|
+
@Module({
|
|
18
|
+
imports: [Login512Module.forRoot()],
|
|
19
|
+
})
|
|
20
|
+
export class AppModule {}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Controller Usage
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { Controller, Get, UseGuards, Req } from '@nestjs/common';
|
|
27
|
+
import { Login512Guard } from '@login512/nestjs';
|
|
28
|
+
|
|
29
|
+
@Controller('users')
|
|
30
|
+
export class UserController {
|
|
31
|
+
@Get('me')
|
|
32
|
+
@UseGuards(Login512Guard)
|
|
33
|
+
getProfile(@Req() req) {
|
|
34
|
+
return req.login512User;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Login512Module = exports.Login512Guard = void 0;
|
|
4
|
+
var login512_guard_1 = require("./login512.guard");
|
|
5
|
+
Object.defineProperty(exports, "Login512Guard", { enumerable: true, get: function () { return login512_guard_1.Login512Guard; } });
|
|
6
|
+
var login512_module_1 = require("./login512.module");
|
|
7
|
+
Object.defineProperty(exports, "Login512Module", { enumerable: true, get: function () { return login512_module_1.Login512Module; } });
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Login512Guard = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
let Login512Guard = class Login512Guard {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.secretKey = process.env.LOGIN512_SECRET_KEY || '';
|
|
17
|
+
this.endpoint = (process.env.LOGIN512_ENDPOINT || 'https://api.login512.com').replace(/\/+$/, '');
|
|
18
|
+
}
|
|
19
|
+
async canActivate(context) {
|
|
20
|
+
const req = context.switchToHttp().getRequest();
|
|
21
|
+
const authHeader = req.headers['authorization'];
|
|
22
|
+
if (!this.secretKey.startsWith('l512_sk_')) {
|
|
23
|
+
throw new common_1.UnauthorizedException('LOGIN512_SECRET_KEY is not configured');
|
|
24
|
+
}
|
|
25
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
26
|
+
throw new common_1.UnauthorizedException('Missing or invalid Authorization header');
|
|
27
|
+
}
|
|
28
|
+
const token = authHeader.substring(7);
|
|
29
|
+
try {
|
|
30
|
+
const res = await fetch(`${this.endpoint}/v1/token/verify`, {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
headers: {
|
|
33
|
+
'Content-Type': 'application/json',
|
|
34
|
+
'Authorization': `Bearer ${this.secretKey}`,
|
|
35
|
+
},
|
|
36
|
+
body: JSON.stringify({ token }),
|
|
37
|
+
});
|
|
38
|
+
const body = await res.json().catch(() => ({}));
|
|
39
|
+
// HTTP 200 alone is not success: rejected tokens come back as 200 {"valid": false, "error": "..."}
|
|
40
|
+
if (!res.ok || body.valid !== true) {
|
|
41
|
+
const e = body && body.error;
|
|
42
|
+
throw new common_1.UnauthorizedException((typeof e === 'string' ? e : e && (e.message || e.code)) || 'Token verification failed');
|
|
43
|
+
}
|
|
44
|
+
if (body.step_up_required === true) {
|
|
45
|
+
throw new common_1.UnauthorizedException('STEP_UP_REQUIRED');
|
|
46
|
+
}
|
|
47
|
+
req.login512User = body.claims;
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
throw new common_1.UnauthorizedException(err.message || 'Authentication error');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
exports.Login512Guard = Login512Guard;
|
|
56
|
+
exports.Login512Guard = Login512Guard = __decorate([
|
|
57
|
+
(0, common_1.Injectable)(),
|
|
58
|
+
__metadata("design:paramtypes", [])
|
|
59
|
+
], Login512Guard);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var Login512Module_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.Login512Module = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const login512_guard_1 = require("./login512.guard");
|
|
13
|
+
let Login512Module = Login512Module_1 = class Login512Module {
|
|
14
|
+
static forRoot() {
|
|
15
|
+
return {
|
|
16
|
+
module: Login512Module_1,
|
|
17
|
+
providers: [login512_guard_1.Login512Guard],
|
|
18
|
+
exports: [login512_guard_1.Login512Guard],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.Login512Module = Login512Module;
|
|
23
|
+
exports.Login512Module = Login512Module = Login512Module_1 = __decorate([
|
|
24
|
+
(0, common_1.Global)(),
|
|
25
|
+
(0, common_1.Module)({
|
|
26
|
+
providers: [login512_guard_1.Login512Guard],
|
|
27
|
+
exports: [login512_guard_1.Login512Guard],
|
|
28
|
+
})
|
|
29
|
+
], Login512Module);
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@login512/nestjs",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.1.1",
|
|
4
|
+
"description": "NestJS dynamic module and AuthGuard for Login512 Post-Quantum Biometrics and Passkeys",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"@nestjs/common": ">=9.0.0",
|
|
9
|
+
"@nestjs/core": ">=9.0.0",
|
|
10
|
+
"rxjs": ">=7.0.0"
|
|
11
|
+
},
|
|
12
|
+
"author": "Login512 Team",
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"src",
|
|
20
|
+
"README.md"
|
|
21
|
+
]
|
|
22
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
@Injectable()
|
|
4
|
+
export class Login512Guard implements CanActivate {
|
|
5
|
+
private readonly secretKey: string;
|
|
6
|
+
private readonly endpoint: string;
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
this.secretKey = process.env.LOGIN512_SECRET_KEY || '';
|
|
10
|
+
this.endpoint = (process.env.LOGIN512_ENDPOINT || 'https://api.login512.com').replace(/\/+$/, '');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
14
|
+
const req = context.switchToHttp().getRequest();
|
|
15
|
+
const authHeader = req.headers['authorization'];
|
|
16
|
+
|
|
17
|
+
if (!this.secretKey.startsWith('l512_sk_')) {
|
|
18
|
+
throw new UnauthorizedException('LOGIN512_SECRET_KEY is not configured');
|
|
19
|
+
}
|
|
20
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
21
|
+
throw new UnauthorizedException('Missing or invalid Authorization header');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const token = authHeader.substring(7);
|
|
25
|
+
try {
|
|
26
|
+
const res = await fetch(`${this.endpoint}/v1/token/verify`, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: {
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
'Authorization': `Bearer ${this.secretKey}`,
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify({ token }),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const body = await res.json().catch(() => ({}));
|
|
36
|
+
// HTTP 200 alone is not success: rejected tokens come back as 200 {"valid": false, "error": "..."}
|
|
37
|
+
if (!res.ok || body.valid !== true) {
|
|
38
|
+
const e = body && body.error;
|
|
39
|
+
throw new UnauthorizedException((typeof e === 'string' ? e : e && (e.message || e.code)) || 'Token verification failed');
|
|
40
|
+
}
|
|
41
|
+
if (body.step_up_required === true) {
|
|
42
|
+
throw new UnauthorizedException('STEP_UP_REQUIRED');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
req.login512User = body.claims;
|
|
46
|
+
return true;
|
|
47
|
+
} catch (err: any) {
|
|
48
|
+
throw new UnauthorizedException(err.message || 'Authentication error');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Module, DynamicModule, Global } from '@nestjs/common';
|
|
2
|
+
import { Login512Guard } from './login512.guard';
|
|
3
|
+
|
|
4
|
+
@Global()
|
|
5
|
+
@Module({
|
|
6
|
+
providers: [Login512Guard],
|
|
7
|
+
exports: [Login512Guard],
|
|
8
|
+
})
|
|
9
|
+
export class Login512Module {
|
|
10
|
+
static forRoot(): DynamicModule {
|
|
11
|
+
return {
|
|
12
|
+
module: Login512Module,
|
|
13
|
+
providers: [Login512Guard],
|
|
14
|
+
exports: [Login512Guard],
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|