@universis/janitor 2.1.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,13 +1,12 @@
1
1
  import * as _themost_common from '@themost/common';
2
- import { ApplicationService, ApplicationBase, ConfigurationStrategy, ConfigurationBase, HttpForbiddenError, HttpError } from '@themost/common';
2
+ import { ApplicationService, ApplicationBase, ConfigurationStrategy, ConfigurationBase, HttpForbiddenError } from '@themost/common';
3
3
  import { Router, Application, Handler, Request as Request$1 } from 'express';
4
- import { Store, Options } from 'express-rate-limit';
4
+ import { Options, Store } from 'express-rate-limit';
5
5
  import { BehaviorSubject } from 'rxjs';
6
- import { Store as Store$1, Options as Options$1 } from 'express-slow-down';
6
+ import { Options as Options$1, Store as Store$1 } from 'express-slow-down';
7
7
  import RedisStore from 'rate-limit-redis';
8
- import { DataContext } from '@themost/data';
9
- import BearerStrategy from 'passport-http-bearer';
10
- import { Authenticator } from 'passport';
8
+ import { DataContext, UserService } from '@themost/data';
9
+ import { Strategy, Authenticator } from 'passport';
11
10
 
12
11
  declare type RateLimitStoreConstructor = new (...arg: unknown[]) => Store;
13
12
 
@@ -139,6 +138,7 @@ declare class ExtendScopeAccessConfiguration extends ApplicationService {
139
138
  }
140
139
 
141
140
  declare function validateScope(): Handler;
141
+ declare function validateClientScope(): Handler;
142
142
 
143
143
  declare interface OAuth2MethodOptions {
144
144
  access_token: string;
@@ -237,13 +237,17 @@ declare class OAuth2ClientService extends ApplicationService {
237
237
  }
238
238
 
239
239
  declare class RemoteAddressValidator extends ApplicationService {
240
-
240
+
241
241
  constructor(app: ApplicationService);
242
242
  validateRemoteAddress(request: Request$1): Promise<boolean>;
243
243
  getRemoteAddress(request: Request$1): string;
244
244
 
245
245
  }
246
246
 
247
+ declare class HttpRemoteAddrForbiddenError extends HttpForbiddenError {
248
+ constructor();
249
+ }
250
+
247
251
  declare class AppRateLimitService extends RateLimitService {
248
252
  constructor(app: _themost_common.ApplicationBase);
249
253
  }
@@ -252,133 +256,106 @@ declare class AppSpeedLimitService extends SpeedLimitService {
252
256
  constructor(app: _themost_common.ApplicationBase);
253
257
  }
254
258
 
255
- declare class HttpBearerTokenRequired extends HttpError {
256
- constructor() {
257
- super(499, 'A token is required to fulfill the request.');
258
- this.code = 'E_TOKEN_REQUIRED';
259
- this.title = 'Token Required';
260
- }
259
+ declare class HttpBearerStrategy extends Strategy {
260
+ constructor();
261
261
  }
262
262
 
263
- declare class HttpBearerTokenNotFound extends HttpError {
264
- constructor() {
265
- super(498, 'Token was not found.');
266
- this.code = 'E_TOKEN_NOT_FOUND';
267
- this.title = 'Invalid token';
268
- }
263
+ declare class PassportService extends ApplicationService {
264
+ getInstance(): Authenticator
269
265
  }
270
266
 
271
- declare class HttpBearerTokenExpired extends HttpError {
272
- constructor() {
273
- super(498, 'Token was expired or is in invalid state.');
274
- this.code = 'E_TOKEN_EXPIRED';
275
- this.title = 'Invalid token';
276
- }
267
+ declare interface OutputCachingProfile {
268
+ /**
269
+ * Defines the location where the output cache will be stored. Possible values are: client, server, clientServer, none. The default value is client.
270
+ */
271
+ location: 'any' | 'none' | 'server' | 'client' | 'serverAndClient';
272
+ /**
273
+ * Defines the duration in seconds for which the output cache will be stored. The default value is 60 seconds.
274
+ */
275
+ duration?: number;
276
+ /**
277
+ * Defines the content encoding that will be used to vary the output cache. The default value is null, which means that the output cache will not be varied by content encoding.
278
+ */
279
+ varyByContentEncoding?: string;
280
+ /**
281
+ * Defines the headers that will be used to vary the output cache. The default value is an empty array.
282
+ */
283
+ varyByHeader?: string[];
284
+ /**
285
+ * Defines the query string parameters that will be used to vary the output cache. The default value is a "*" character, which means that the output cache will be varied by all query string parameters.
286
+ */
287
+ varyByParam?: string[];
288
+ /**
289
+ * Defines the custom expression that will be used to vary the output cache. The default value is null.
290
+ * An example of a custom expression is: { "$md5": { "$user": 1 } } where the output cache will be varied by the MD5 hash of the user ID.
291
+ */
292
+ varyByExpr?: any;
277
293
  }
278
294
 
279
- declare class HttpAccountDisabled extends HttpForbiddenError {
280
- constructor() {
281
- super('Access is denied. User account is disabled.');
282
- this.code = 'E_ACCOUNT_DISABLED';
283
- this.statusCode = 403.2;
284
- this.title = 'Disabled account';
295
+ declare module 'express' {
296
+ interface Request {
297
+ outputCaching?: OutputCachingProfile
285
298
  }
286
299
  }
287
300
 
288
- declare class HttpBearerStrategy extends BearerStrategy {
289
- constructor() {
290
- super({
291
- passReqToCallback: true
292
- },
293
- /**
294
- * @param {Request} req
295
- * @param {string} token
296
- * @param {Function} done
297
- */
298
- function(req, token, done) {
299
- /**
300
- * Gets OAuth2 client services
301
- * @type {import('./OAuth2ClientService').OAuth2ClientService}
302
- */
303
- let client = req.context.getApplication().getStrategy(function OAuth2ClientService() {});
304
- // if client cannot be found
305
- if (client == null) {
306
- // throw configuration error
307
- return done(new Error('Invalid application configuration. OAuth2 client service cannot be found.'))
308
- }
309
- if (token == null) {
310
- // throw 499 Token Required error
311
- return done(new HttpBearerTokenRequired());
312
- }
313
- // get token info
314
- client.getTokenInfo(req.context, token).then(info => {
315
- if (info == null) {
316
- // the specified token cannot be found - 498 invalid token with specific code
317
- return done(new HttpBearerTokenNotFound());
318
- }
319
- // if the given token is not active throw token expired - 498 invalid token with specific code
320
- if (!info.active) {
321
- return done(new HttpBearerTokenExpired());
322
- }
323
- // find user from token info
324
- return (function () {
325
- /**
326
- * @type {import('./services/user-provisioning-mapper-service').UserProvisioningMapperService}
327
- */
328
- const mapper = req.context.getApplication().getService(function UserProvisioningMapperService() {});
329
- if (mapper == null) {
330
- return req.context.model('User').where('name').equal(info.username).silent().getItem();
331
- }
332
- return mapper.getUser(req.context, info);
333
- })().then((user) => {
334
- // check if userProvisioning service is installed and try to find related user only if user not found
335
- if (user == null) {
336
- /**
337
- * @type {import('./services/user-provisioning-service').UserProvisioningService}
338
- */
339
- const service = req.context.getApplication().getService(function UserProvisioningService() {});
340
- if (service == null) {
341
- return user;
342
- }
343
- return service.validateUser(req.context, info);
344
- }
345
- return user;
346
- }).then( user => {
347
- // user cannot be found and of course cannot be authenticated (throw forbidden error)
348
- if (user == null) {
349
- // write access log for forbidden
350
- return done(new HttpForbiddenError());
351
- }
352
- // check if user has enabled attribute
353
- if (Object.prototype.hasOwnProperty.call(user, 'enabled') && !user.enabled) {
354
- //if user.enabled is off throw forbidden error
355
- return done(new HttpAccountDisabled('Access is denied. User account is disabled.'));
356
- }
357
- // otherwise return user data
358
- return done(null, {
359
- 'name': user.name,
360
- 'authenticationProviderKey': user.id,
361
- 'authenticationType':'Bearer',
362
- 'authenticationToken': token,
363
- 'authenticationScope': info.scope
364
- });
365
- });
366
- }).catch(err => {
367
- // end log token info request with error
368
- if (err && err.statusCode === 404) {
369
- // revert 404 not found returned by auth server to 498 invalid token
370
- return done(new HttpBearerTokenNotFound());
371
- }
372
- // otherwise continue with error
373
- return done(err);
374
- });
375
- });
376
- }
301
+ declare interface OutputCachingElement {
302
+ /**
303
+ * Defines the URL pattern for which the output caching profile will be applied. The URL pattern can include wildcards, such as /api/*, which means that the output caching profile will be applied to all URLs that start with /api/.
304
+ */
305
+ resource: string;
306
+ /**
307
+ * Defines the regular expression that will be used to match the URL pattern. The regular expression should be defined as a string and will be converted to a RegExp object internally. If the regular expression is not defined, the URL pattern will be used as a simple string match.
308
+ */
309
+ expr?: RegExp;
310
+ /**
311
+ * Defines the output caching profile that will be applied to the URL pattern. The output caching profile can be defined as an object of type OutputCachingProfile or as a string that references a predefined output caching profile.
312
+ */
313
+ profile: (OutputCachingProfile | string);
377
314
  }
378
315
 
379
- declare class PassportService extends ApplicationService {
380
- getInstance(): Authenticator
316
+ declare function outputCache(options: {
317
+ profiles: Map<string, OutputCachingProfile>,
318
+ elements: OutputCachingElement[]
319
+ }): Router;
320
+
321
+ declare class OutputCachingService extends ApplicationService{
322
+ profiles: Map<string, OutputCachingProfile>;
323
+ elements: OutputCachingElement[];
324
+ }
325
+
326
+ declare class InvalidClientScopeError extends HttpForbiddenError {
327
+ constructor();
328
+ }
329
+
330
+ declare class VerifyUserClientScope extends ApplicationService {
331
+ constructor(app: ApplicationBase);
332
+ options: { whitelist: string[] };
333
+ verify(context: DataContext): Promise<boolean>;
334
+ }
335
+
336
+ declare class DisableUserClientScopeVerification extends VerifyUserClientScope {
337
+ }
338
+
339
+ declare class UserWithNetworkAddress extends UserService {
340
+ constructor(app: ApplicationBase);
341
+
342
+ protected queryUser(context: DataContext, username: string): Promise<any>;
343
+ protected queryGroup(context: DataContext, username: string): Promise<any>;
381
344
  }
382
345
 
383
- export { AppRateLimitService, AppSpeedLimitService, DefaultScopeAccessConfiguration, EnableScopeAccessConfiguration, ExtendScopeAccessConfiguration, HttpAccountDisabled, HttpBearerStrategy, HttpBearerTokenExpired, HttpBearerTokenNotFound, HttpBearerTokenRequired, OAuth2ClientService, PassportService, RateLimitService, RedisClientStore, RemoteAddressValidator, ScopeAccessConfiguration, ScopeString, SpeedLimitService, validateScope };
384
- export type { GenericUser, OAuth2AuthorizeUser, OAuth2MethodOptions, OAuth2ServiceSettings, OAuth2User, OAuth2UserProfile, RateLimitServiceConfiguration, RateLimitStoreConstructor, ScopeAccessConfigurationElement, ScopeAccessConfigurationSection, SpeedLimitServiceConfiguration, SpeedLimitStoreConstructor, UniversisConfigurationSection };
346
+ declare class NetworkRestrictAccess extends ApplicationService {
347
+ constructor(app: ApplicationBase);
348
+ }
349
+
350
+ declare class EnableNetworkRestrictAccess extends NetworkRestrictAccess {
351
+ constructor(app: ApplicationBase);
352
+ }
353
+
354
+ declare class DisableNetworkRestrictAccess extends NetworkRestrictAccess {
355
+ constructor(app: ApplicationBase);
356
+ }
357
+
358
+ declare function validateNetworkAccess(): Handler;
359
+
360
+ export { AppRateLimitService, AppSpeedLimitService, DefaultScopeAccessConfiguration, DisableNetworkRestrictAccess, DisableUserClientScopeVerification, EnableNetworkRestrictAccess, EnableScopeAccessConfiguration, ExtendScopeAccessConfiguration, HttpBearerStrategy, HttpRemoteAddrForbiddenError, InvalidClientScopeError, NetworkRestrictAccess, OAuth2ClientService, OutputCachingService, PassportService, RateLimitService, RedisClientStore, RemoteAddressValidator, ScopeAccessConfiguration, ScopeString, SpeedLimitService, UserWithNetworkAddress, VerifyUserClientScope, outputCache, validateClientScope, validateNetworkAccess, validateScope };
361
+ export type { GenericUser, OAuth2AuthorizeUser, OAuth2MethodOptions, OAuth2ServiceSettings, OAuth2User, OAuth2UserProfile, OutputCachingElement, OutputCachingProfile, RateLimitServiceConfiguration, RateLimitStoreConstructor, ScopeAccessConfigurationElement, ScopeAccessConfigurationSection, SpeedLimitServiceConfiguration, SpeedLimitStoreConstructor, UniversisConfigurationSection };