How to use @foal/jwt - 10 common examples

To help you get started, we’ve selected a few @foal/jwt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github FoalTS / foal / packages / acceptance-tests / src / openapi.hooks.spec.ts View on Github external
deepStrictEqual(actualDocument, expectedDocument);

    // Test hook conflicts (Two calls of @JWT).

    @ApiInfo({
      title: 'My API',
      version: '1.0.0'
    })
    class ApiController2 {
      @Get('/products')
      @JWTOptional()
      readProducts() {}

      @Post('/products')
      @JWTRequired()
      @ValidateBody({
        properties: {
          name: { type: 'string' }
        },
        type: 'object',
      })
      createProduct() {}
    }

    const yamlDocument2 = readFileSync(join(__dirname, './assets/openapi.hooks2.yml'), 'utf8');
    const expectedDocument2 = parse(yamlDocument2);

    const actualDocument2 = createOpenApiDocument(ApiController2);

    deepStrictEqual(actualDocument2, expectedDocument2);
github FoalTS / foal / packages / acceptance-tests / src / openapi.hooks.spec.ts View on Github external
const yamlDocument = readFileSync(join(__dirname, './assets/openapi.hooks.yml'), 'utf8');
    const expectedDocument = parse(yamlDocument);

    const actualDocument = createOpenApiDocument(ApiController);

    deepStrictEqual(actualDocument, expectedDocument);

    // Test hook conflicts (Two calls of @JWT).

    @ApiInfo({
      title: 'My API',
      version: '1.0.0'
    })
    class ApiController2 {
      @Get('/products')
      @JWTOptional()
      readProducts() {}

      @Post('/products')
      @JWTRequired()
      @ValidateBody({
        properties: {
          name: { type: 'string' }
        },
        type: 'object',
      })
      createProduct() {}
    }

    const yamlDocument2 = readFileSync(join(__dirname, './assets/openapi.hooks2.yml'), 'utf8');
    const expectedDocument2 = parse(yamlDocument2);
github FoalTS / foal / packages / jwks-rsa / src / get-rsa-public-key-from-jwks.ts View on Github external
return async ({ alg, kid }) => {
    if (alg !== 'RS256') {
      throw new InvalidTokenError('invalid algorithm');
    }
    if (kid === undefined) {
      throw new InvalidTokenError('missing kid');
    }

    const client = jwksClient(options);

    return new Promise((resolve, reject) => {
      client.getSigningKey(kid, (err, key) => {
        if (err) {
          return reject(err.name === 'SigningKeyNotFoundError' ? new InvalidTokenError('invalid kid') : err);
        }
        // "key.publicKey || key.rsaPublicKey" because of
        // https://github.com/auth0/node-jwks-rsa/blob/master/src/integrations/express.js#L36
        // The " || key.rsaPublicKey" part is currently not tested.
        resolve(key.publicKey || key.rsaPublicKey);
github FoalTS / foal / packages / jwks-rsa / src / get-rsa-public-key-from-jwks.ts View on Github external
return async ({ alg, kid }) => {
    if (alg !== 'RS256') {
      throw new InvalidTokenError('invalid algorithm');
    }
    if (kid === undefined) {
      throw new InvalidTokenError('missing kid');
    }

    const client = jwksClient(options);

    return new Promise((resolve, reject) => {
      client.getSigningKey(kid, (err, key) => {
        if (err) {
          return reject(err.name === 'SigningKeyNotFoundError' ? new InvalidTokenError('invalid kid') : err);
        }
        // "key.publicKey || key.rsaPublicKey" because of
        // https://github.com/auth0/node-jwks-rsa/blob/master/src/integrations/express.js#L36
        // The " || key.rsaPublicKey" part is currently not tested.
        resolve(key.publicKey || key.rsaPublicKey);
      });
    });
  };
github FoalTS / foal / packages / jwks-rsa / src / get-rsa-public-key-from-jwks.ts View on Github external
client.getSigningKey(kid, (err, key) => {
        if (err) {
          return reject(err.name === 'SigningKeyNotFoundError' ? new InvalidTokenError('invalid kid') : err);
        }
        // "key.publicKey || key.rsaPublicKey" because of
        // https://github.com/auth0/node-jwks-rsa/blob/master/src/integrations/express.js#L36
        // The " || key.rsaPublicKey" part is currently not tested.
        resolve(key.publicKey || key.rsaPublicKey);
      });
    });
github FoalTS / foal / packages / acceptance-tests / src / openapi.hooks.spec.ts View on Github external
it('should generate OpenAPI spec from hooks.', () => {

    @ApiInfo({
      title: 'My API',
      version: '1.0.0'
    })
    @JWTRequired()
    class ApiController {
      @Post('/products')
      @ValidateBody({
        properties: {
          name: { type: 'string' }
        },
        type: 'object',
      })
      createProduct(ctx) {

      }
    }

    const yamlDocument = readFileSync(join(__dirname, './assets/openapi.hooks.yml'), 'utf8');
    const expectedDocument = parse(yamlDocument);
github FoalTS / foal / packages / acceptance-tests / src / authentication / jwt.token.spec.ts View on Github external
@Column()
    password: string;
  }

  const credentialsSchema = {
    additionalProperties: false,
    properties: {
      email: { type: 'string', format: 'email' },
      password: { type: 'string' }
    },
    required: [ 'email', 'password' ],
    type: 'object',
  };

  @JWTRequired({ user: fetchUser(User), blackList: isBlackListed })
  class ApiController {
    @Get('/products')
    readProducts(ctx: Context) {
      return new HttpResponseOK({
        email: ctx.user.email
      });
    }
  }

  class AuthController {

    @Post('/signup')
    @ValidateBody(credentialsSchema)
    async signup(ctx: Context) {
      const user = new User();
      user.email = ctx.request.body.email;
github FoalTS / foal / packages / acceptance-tests / src / authentication / jwt.jwks.spec.ts View on Github external
+ 'YL7H1Q8NiK9LGEN6-JSWfgckQCs6UUBOXSZdreNN9zbQCwyzee7bOJqXUDAuLcFARzPw1EsZAyjVt'
            + 'GCKIQ0_btqK-jFunT2NBC8RItanDZpptQ',
          use: 'sig',
        }
      ]
    };

    class AppController {

      @Get('/.well-known/jwks.json')
      getJWKS() {
        return new HttpResponseOK(jwks);
      }

      @Get('/api/users/me')
      @JWTRequired({
        secretOrPublicKey: getRSAPublicKeyFromJWKS({
          jwksUri: 'http://localhost:3000/.well-known/jwks.json'
        })
      })
      getUser() {
        return new HttpResponseOK({
          name: 'Alix'
        });
      }

    }

    server = createApp(AppController).listen(3000);

    try {
      const response = await superagent
github FoalTS / foal / packages / acceptance-tests / src / authentication / jwt.cookie.spec.ts View on Github external
async login(ctx: Context) {
      const user = await getRepository(User).findOne({ email: ctx.request.body.email });

      if (!user) {
        return new HttpResponseUnauthorized();
      }

      if (!await verifyPassword(ctx.request.body.password, user.password)) {
        return new HttpResponseUnauthorized();
      }

      return this.generateLoginResponse(user);
    }

    @Post('/logout')
    @JWTRequired({ cookie: true })
    async logout() {
      return new HttpResponseNoContent()
        .setCookie(
          Config.get('settings.jwt.cookieName', 'auth'),
          '',
          { ...cookieOptions, maxAge: 0 }
        );
    }

    private async generateLoginResponse(user: User): Promise {
      const payload = {
        email: user.email,
        id: user.id,
      };

      const secret = Config.get('settings.jwt.secretOrPublicKey');
github FoalTS / foal / packages / acceptance-tests / src / authentication / jwt.cookie.spec.ts View on Github external
return blackList.includes(token);
  }

  @Entity()
  class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ unique: true })
    email: string;

    @Column()
    password: string;
  }

  @JWTRequired({ user: fetchUser(User), blackList: isBlackListed, cookie: true })
  class ApiController {
    @Get('/products')
    readProducts(ctx: Context) {
      return new HttpResponseOK({
        email: ctx.user.email
      });
    }
  }

  const credentialsSchema = {
    additionalProperties: false,
    properties: {
      email: { type: 'string', format: 'email' },
      password: { type: 'string' }
    },
    required: [ 'email', 'password' ],

@foal/jwt

Authentication with JWT for FoalTS

MIT
Latest version published 2 months ago

Package Health Score

76 / 100
Full package analysis