passport-jwt#ExtractJwt TypeScript Examples

The following examples show how to use passport-jwt#ExtractJwt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: jwt.stratergy.ts    From uniauth-backend with MIT License 6 votes vote down vote up
constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        (request: Request) => {
          return request?.cookies?.vitAuth;
        },
      ]),
      ignoreExpiration: false,
      secretOrKey: newJWTConstants.secret,
    });
  }
Example #2
Source File: firebase-admin-user-validate.strategy.ts    From mamori-i-japan-api with BSD 2-Clause "Simplified" License 6 votes vote down vote up
async validate(req: Request, done: VerifiedCallback): Promise<any> {
    const extractorFunction = ExtractJwt.fromAuthHeaderAsBearerToken()
    const token = extractorFunction(req)
    if (!token) {
      throw new UnauthorizedException('No bearer token found in the header')
    }

    let userDecodedToken: firebaseAdmin.auth.DecodedIdToken
    try {
      userDecodedToken = await firebaseAdmin.auth().verifyIdToken(token)
    } catch (error) {
      throw new UnauthorizedException(error.message)
    }

    // Expect all admin access tokens to have email and email_verified data.
    validateAdminTokenEmailPayload(userDecodedToken)
    // Check custom claims for isAdminUser, userAdminRole and userAccessKey.
    validateAdminTokenCustomClaims(userDecodedToken)

    const requestAdminUser: RequestAdminUser = {
      isAdminUser: userDecodedToken.isAdminUser,
      userAdminRole: userDecodedToken.userAdminRole,
      userAccessKey: userDecodedToken.userAccessKey,
      uid: userDecodedToken.uid,
      email: userDecodedToken.email,
    }

    done(null, requestAdminUser)
  }
Example #3
Source File: firebase-admin-user-login.strategy.ts    From mamori-i-japan-api with BSD 2-Clause "Simplified" License 6 votes vote down vote up
async validate(req: Request, done: VerifiedCallback): Promise<any> {
    const extractorFunction = ExtractJwt.fromAuthHeaderAsBearerToken()
    const token = extractorFunction(req)
    if (!token) {
      throw new UnauthorizedException('No bearer token found in the header')
    }

    let userDecodedToken: firebaseAdmin.auth.DecodedIdToken
    try {
      userDecodedToken = await firebaseAdmin.auth().verifyIdToken(token)
    } catch (error) {
      throw new UnauthorizedException(error.message)
    }

    // Expect all admin access tokens to have email and email_verified data.
    validateAdminTokenEmailPayload(userDecodedToken)

    const requestAdminUser: RequestAdminUser = {
      isAdminUser: userDecodedToken.isAdminUser,
      userAdminRole: userDecodedToken.userAdminRole,
      userAccessKey: userDecodedToken.userAccessKey,
      uid: userDecodedToken.uid,
      email: userDecodedToken.email,
    }

    // NOTE : Passport automatically creates a user object, based on the value we return here.
    done(null, requestAdminUser)
  }
Example #4
Source File: jwt.ts    From Deep-Lynx with MIT License 6 votes vote down vote up
export function SetJWTAuthMethod(app: express.Application) {
    passport.use(
        new JwtStrategy(
            {
                jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
                secretOrKey: Config.encryption_key_secret
            },
            (jwt, done) => {
                done(null, jwt);
            }
        )
    );
}
Example #5
Source File: jwt-reset-password.strategy.ts    From bank-server with MIT License 6 votes vote down vote up
constructor(
    private readonly _configService: ConfigService,
    private readonly _authenticationService: AuthService,
    private readonly _userAuthForgottenPasswordService: UserAuthForgottenPasswordService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: _configService.get('JWT_FORGOTTEN_PASSWORD_TOKEN_SECRET'),
      passReqToCallback: true,
    });
  }
Example #6
Source File: jwt.strategy.ts    From bank-server with MIT License 6 votes vote down vote up
constructor(
    private readonly _configService: ConfigService,
    private readonly _userService: UserService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: _configService.get('JWT_SECRET_KEY'),
    });
  }
Example #7
Source File: jwt.strategy.ts    From 42_checkIn with GNU General Public License v3.0 6 votes vote down vote up
constructor(
    private readonly configService: ConfigService,
    private readonly logger: MyLogger,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        (req: Request) => {
          return req?.cookies?.w_auth;
        },
      ]),
      ignoreExpiration: false,
      secretOrKey: configService.get('jwt.secret'),
    });
  }
Example #8
Source File: firebase-normal-user-login.strategy.ts    From mamori-i-japan-api with BSD 2-Clause "Simplified" License 6 votes vote down vote up
async validate(req: Request, done: VerifiedCallback): Promise<any> {
    const extractorFunction = ExtractJwt.fromAuthHeaderAsBearerToken()
    const token = extractorFunction(req)

    if (!token) {
      throw new UnauthorizedException('No bearer token found in the header')
    }

    let userDecodedToken: firebaseAdmin.auth.DecodedIdToken
    try {
      userDecodedToken = await firebaseAdmin.auth().verifyIdToken(token)
    } catch (error) {
      throw new UnauthorizedException(error.message)
    }

    // Expect all normal access tokens (FDT) to have provider id anonymous data.
    validateNormalTokenAnonymousPayload(userDecodedToken)

    // NOTE : Passport automatically creates a user object, based on the value we return here.
    done(null, userDecodedToken)
  }
Example #9
Source File: jwt.strategy.ts    From amplication with Apache License 2.0 6 votes vote down vote up
constructor(
    private readonly authService: AuthService,
    readonly configService: ConfigService
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      passReqToCallback: true,
      secretOrKey: configService.get('JWT_SECRET')
    });
  }
Example #10
Source File: jwt.strategy.ts    From amplication with Apache License 2.0 6 votes vote down vote up
async validate(req, payload: JwtDto): Promise<AuthUser> {
    if (payload.type === EnumTokenType.ApiToken) {
      const jwt = ExtractJwt.fromAuthHeaderAsBearerToken()(req);

      const isValid = await this.authService.validateApiToken({
        userId: payload.userId,
        tokenId: payload.tokenId,
        token: jwt
      });
      if (!isValid === true) {
        throw new UnauthorizedException();
      }
    }

    const user = await this.authService.getAuthUser({
      id: payload.userId
    });
    if (!user) {
      throw new UnauthorizedException();
    }
    return user;
  }
Example #11
Source File: jwt.strategy.base.ts    From amplication with Apache License 2.0 6 votes vote down vote up
constructor(
    protected readonly userService: UserService,
    protected readonly secretOrKey: string
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey,
    });
  }
Example #12
Source File: firebase-normal-user-validate.strategy.ts    From mamori-i-japan-api with BSD 2-Clause "Simplified" License 6 votes vote down vote up
async validate(req: Request, done: VerifiedCallback): Promise<any> {
    const extractorFunction = ExtractJwt.fromAuthHeaderAsBearerToken()
    const token = extractorFunction(req)
    if (!token) {
      throw new UnauthorizedException('No bearer token found in the header')
    }

    let userDecodedToken: firebaseAdmin.auth.DecodedIdToken
    try {
      userDecodedToken = await firebaseAdmin.auth().verifyIdToken(token)
    } catch (error) {
      throw new UnauthorizedException(error.message)
    }

    // Expect all normal access tokens (FDT) to have provider id anonymous data.
    validateNormalTokenAnonymousPayload(userDecodedToken)

    // Check isNormalUser custom claim.
    if (!userDecodedToken.isNormalUser) {
      throw new UnauthorizedException('Access token does not contain custom claim isNormalUser')
    }

    done(null, userDecodedToken)
  }
Example #13
Source File: jwt.strategy.ts    From svvs with MIT License 6 votes vote down vote up
/**
   * provide super
   */
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: environment.jwt.secret,
    })
  }
Example #14
Source File: cookie.stratergy.ts    From uniauth-backend with MIT License 6 votes vote down vote up
constructor(private readonly userService: UserService) {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        (request: Request) => {
          return request?.cookies?.Authentication;
        },
      ]),
      secretOrKey: newJWTConstants.secret,
    });
  }
Example #15
Source File: jwt.strategy.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
constructor(
    configService: ConfigService,
    private readonly userService: UserService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('SECRET_KEY'),
    });
  }
Example #16
Source File: jwt-ws-access.strategy.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
constructor(
    private readonly configService: ConfigService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        (client: any) => {
          const bearerToken = client?.handshake?.headers?.authorization;
          return bearerToken ? bearerToken.split(' ')[1] : null;
        },
      ]),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('ACCESS_TOKEN') || '<%= config.accessTokenSecret %>',
    });
  }
Example #17
Source File: jwt-ws-access.strategy.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        (client: any) => {
          const bearerToken = client?.handshake?.headers?.authorization;
          return bearerToken ? bearerToken.split(' ')[1] : null;
        },
      ]),
      ignoreExpiration: false,
      secretOrKey: this.configService.get<string>('ACCESS_TOKEN') || '<%= config.accessTokenSecret %>',
    });
  }
Example #18
Source File: jwt-ws-access.strategy.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
constructor(
    private readonly configService: ConfigService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([
        (client: any) => {
          const bearerToken = client?.handshake?.headers?.authorization;
          return bearerToken ? bearerToken.split(' ')[1] : null;
        },
      ]),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('ACCESS_TOKEN') || '<%= config.accessTokenSecret %>',
    });
  }
Example #19
Source File: jwt-refresh.strategy.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
constructor(
    private readonly configService: ConfigService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('REFRESH_TOKEN') || '<%= config.refreshTokenSecret %>',
    });
  }
Example #20
Source File: jwt-access.strategy.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
constructor(
    private readonly configService: ConfigService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('ACCESS_TOKEN') || '<%= config.accessTokenSecret %>',
    });
  }
Example #21
Source File: jwt-refresh.strategy.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
constructor(
    private readonly configService: ConfigService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('REFRESH_TOKEN') || '<%= config.refreshTokenSecret %>',
    });
  }
Example #22
Source File: jwt-access.strategy.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
constructor(
    private readonly configService: ConfigService,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>('ACCESS_TOKEN') || '<%= config.accessTokenSecret %>',
    });
  }
Example #23
Source File: jwt.strategy.ts    From pknote-backend with GNU General Public License v3.0 6 votes vote down vote up
constructor(
    @InjectRepository(UserRepository)
    private userRepository: UserRepository,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: process.env.JWT_SECRET || config.get('jwt.secret'),
    });
  }
Example #24
Source File: jwt.strategy.ts    From Phantom with MIT License 5 votes vote down vote up
constructor(private authService: AuthService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: process.env.SECRET_KEY,
    });
  }
Example #25
Source File: jwt.strategy.ts    From codeclannigeria-backend with MIT License 5 votes vote down vote up
constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configuration().jwtSecret
    });
  }
Example #26
Source File: jwt.strategy.ts    From MyAPI with MIT License 5 votes vote down vote up
constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: AUTH_SECRET_TOKEN,
    })
  }
Example #27
Source File: HttpJwtStrategy.ts    From typescript-clean-architecture with MIT License 5 votes vote down vote up
constructor(private authService: HttpAuthService) {
    super({
      jwtFromRequest: ExtractJwt.fromHeader(ApiServerConfig.ACCESS_TOKEN_HEADER),
      ignoreExpiration: false,
      secretOrKey: ApiServerConfig.ACCESS_TOKEN_SECRET,
    });
  }
Example #28
Source File: jwt.strategy.ts    From nestjs-starter with MIT License 5 votes vote down vote up
constructor(private readonly configService: ConfigService, private readonly userService: UsersService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>(CONFIG_SERVER_JWT_SECRET),
    });
  }
Example #29
Source File: jwt.strategy.ts    From nestjs-angular-starter with MIT License 5 votes vote down vote up
constructor(private authService: AuthService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: config.JWT.SECRET,
    });
  }