Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Check IP address restrictions in API keys
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 8, 2020
1 parent 28bb668 commit e24b26d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/modules/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Request } from 'express';
import ipRangeCheck from 'ip-range-check';
import minimatch from 'minimatch';
import { Strategy } from 'passport-strategy';
import { getClientIp } from 'request-ip';
import { ApiKeysService } from '../api-keys/api-keys.service';
import { LOGIN_ACCESS_TOKEN } from '../tokens/tokens.constants';
import { TokensService } from '../tokens/tokens.service';
import { AccessTokenClaims, AccessTokenParsed } from './auth.interface';
import minimatch from 'minimatch';

class StaartStrategy extends Strategy {
name = 'jwt';
Expand Down Expand Up @@ -47,6 +49,16 @@ export class JwtStrategy extends PassportStrategy(StaartStrategy) {
if (!referrerRestrictionsMet)
return this.fail('Referrer restrictions not met', 401);
}
if (
Array.isArray(apiKeyDetails.ipRestrictions) &&
apiKeyDetails.ipRestrictions.length
) {
const ipAddress = getClientIp(request);
if (
!ipRangeCheck(ipAddress, apiKeyDetails.ipRestrictions as string[])
)
return this.fail('IP address restrictions not met', 401);
}
return this.safeSuccess({
type: 'api-key',
id: apiKeyDetails.id,
Expand All @@ -66,7 +78,7 @@ export class JwtStrategy extends PassportStrategy(StaartStrategy) {
LOGIN_ACCESS_TOKEN,
bearerToken,
) as AccessTokenClaims;
const { sub, id, scopes } = payload;
const { id, scopes } = payload;
return this.safeSuccess({ type: 'user', id, scopes });
} catch (error) {}

Expand Down

0 comments on commit e24b26d

Please sign in to comment.