Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
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);
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);
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);
});
});
};
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);
});
});
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);
@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;
+ '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
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');
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' ],