Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added JWT verification endpoint #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ SENTRY_DSN=
# CORS
CORS_ALLOWED_ORIGINS=https://example.com
CORS_ALLOWED_HEADERS=Content-Type,Authorization,x-application-id
CORS_ALLOWED_METHODS=GET,PUT,POST,DELETE,PATCH,OPTIONS
CORS_ALLOWED_METHODS=GET,PUT,POST,DELETE,PATCH,OPTIONS

JWKS_URI=
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"flagsmith-nodejs": "^2.5.2",
"got": "^11.8.2",
"helmet": "^7.0.0",
"jwks-rsa": "^3.1.0",
"passport": "^0.5.2",
"passport-http": "^0.3.0",
"passport-jwt": "^4.0.1",
Expand Down
7 changes: 7 additions & 0 deletions src/api/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,11 @@ export class ApiController {
): Promise<any> {
return await this.apiService.loginWithOtp(user, authHeader);
}

@Get('verify-jwt')
async verifyJwt(
@Headers('authorization') token: string,
): Promise<{ authenticated: boolean; user?: any }> {
return this.apiService.verifyJwt(token);
}
}
20 changes: 19 additions & 1 deletion src/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const CryptoJS = require('crypto-js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const AES = require('crypto-js/aes');
import Flagsmith from 'flagsmith-nodejs';

const jwksClient = require('jwks-rsa');;
CryptoJS.lib.WordArray.words;

@Injectable()
Expand All @@ -44,6 +44,13 @@ export class ApiService {
private readonly configResolverService: ConfigResolverService,
) {}

private readonly jwksClient = jwksClient({
jwksUri: this.configService.get('JWKS_URI'),
cache: true,
cacheMaxEntries: 5,
cacheMaxAge: 86400000,
});

login(user: any, authHeader: string): Promise<SignupResponse> {
return this.fusionAuthService
.login(user, authHeader)
Expand Down Expand Up @@ -646,4 +653,15 @@ export class ApiService {
}
return registration;
}

async verifyJwt(token: string): Promise<{ authenticated: boolean; user?: any }> {
try {
const decodedToken = await this.jwksClient.getSigningKey(token);
const signingKey = decodedToken.getPublicKey();
return { authenticated: true, user: decodedToken.payload };
} catch (error) {
console.error('JWT Verification Error:', error);
return { authenticated: false };
}
}
}