How to use the http-status.FORBIDDEN function in http-status

To help you get started, we’ve selected a few http-status 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 zalando-incubator / authmosphere / integration-test / oauth-tooling / middlewares.spec.ts View on Github external
addAuthenticationEndpointWithoutRequiredScopes();

    // when
    const promise = fetch('http://127.0.0.1:30002/resource/user', {
      method: 'GET',
      headers: {
        authorization: authHeader
      }
    })
    .then((res) => {

      return res.status;
    });

    // then
    return expect(promise).to.become(HttpStatus.FORBIDDEN);
  });
github anandundavia / express-api-structure / src / config / cors.config.js View on Github external
origin: (origin, callback) => {
		// In dev, allow these origins to access the API
		const whiteList = ['localhost', 'chrome-extension'];
		// We are doing string matching here.
		// For advanced use-case, use regex
		const index = whiteList.findIndex((aWhiteListedOrigin) => origin.includes(aWhiteListedOrigin));
		if (!origin || index !== -1) {
			callback(null, true);
		} else {
			const error = {
				message: `'${origin}' is not allowed to access the specified route/resource`,
				status: httpStatus.FORBIDDEN,
			};
			callback(new APIError(error), false);
		}
	},
	credentials: false,
github deepstreamIO / deepstream.io / src / connection-endpoint / http / node-server.ts View on Github external
const requestHostUrl = request.headers.host
    if (this.config.hostUrl && requestHostUrl !== this.config.hostUrl) {
      Server.terminateResponse(response, HTTPStatus.FORBIDDEN, 'Forbidden Host.')
      return false
    }
    if (this.origins.indexOf(requestOriginUrl) === -1) {
      if (!requestOriginUrl) {
        Server.terminateResponse(
          response,
          HTTPStatus.FORBIDDEN,
          'CORS is configured for this server. All requests must set a valid "Origin" header.'
        )
      } else {
        Server.terminateResponse(
          response,
          HTTPStatus.FORBIDDEN,
          `Origin "${requestOriginUrl}" is forbidden.`
        )
      }
      return false
    }

    // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
    response.setHeader('Access-Control-Allow-Origin', requestOriginUrl)
    response.setHeader('Access-Control-Allow-Credentials', 'true')
    response.setHeader('Vary', 'Origin')

    return true
  }
github deepstreamIO / deepstream.io / src / services / http / node / node-http.ts View on Github external
private verifyOrigin (
    request: http.IncomingMessage,
    response: http.ServerResponse
  ): boolean {
    const requestOriginUrl = request.headers.origin as string || request.headers.referer as string
    const requestHostUrl = request.headers.host
    if (this.pluginOptions.hostUrl && requestHostUrl !== this.pluginOptions.hostUrl) {
      this.terminateResponse(response, HTTPStatus.FORBIDDEN, 'Forbidden Host.')
      return false
    }
    if (this.origins!.indexOf(requestOriginUrl) === -1) {
      if (!requestOriginUrl) {
        this.terminateResponse(
          response,
          HTTPStatus.FORBIDDEN,
          'CORS is configured for this this. All requests must set a valid "Origin" header.'
        )
      } else {
        this.terminateResponse(
          response,
          HTTPStatus.FORBIDDEN,
          `Origin "${requestOriginUrl}" is forbidden.`
        )
      }
      return false
    }

    // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
    response.setHeader('Access-Control-Allow-Origin', requestOriginUrl)
    response.setHeader('Access-Control-Allow-Credentials', 'true')
    response.setHeader('Vary', 'Origin')
github roman-sachenko / boilerplate-express-entity-based / app / utils / apiErrors / Forbidden.js View on Github external
constructor(message) {
    super(message || 'Forbidden', httpStatus.FORBIDDEN);
  }
};
github garage-it / SmartHouse-backend / src / shared / auth / auth.service.js View on Github external
(req, res, next) => {
            if (config.userRoles.indexOf(req.user.role) >= config.userRoles.indexOf(roleRequired)) {
                next();
            } else {
                res.status(httpStatus.FORBIDDEN).json({
                    code: 'ACCESS_FORBIDDEN',
                    error: 'Access forbidden'
                });
            }
        }
    );
github linitix / nzero-push / lib / common / requests_manager.js View on Github external
request(requestOptions, function (err, res, body) {
    if ( err )
      return callback(err);

    debug("Response body: " + JSON.stringify(body));
    debug("Response headers: " + JSON.stringify(res.headers));

    switch ( res.statusCode ) {
      case HTTPStatus.OK:
        callback(null, body, res.headers);
        break;
      case HTTPStatus.UNAUTHORIZED:
        callback(new UnauthorizedAccessError("Resource access denied"));
        break;
      case HTTPStatus.FORBIDDEN:
        callback(new ForbiddenAccessError(body.message));
        break;
      case HTTPStatus.NOT_FOUND:
        callback(new ResourceNotFoundError("Resource not found"));
        break;
      case HTTPStatus.PRECONDITION_FAILED:
        callback(new PreconditionFailedError(body.message));
        break;
      default:
        callback(new Error("Expected status code " + HTTPStatus.OK + " and received " + res.statusCode));
    }
  });
}
github danielfsousa / express-rest-es2017-boilerplate / src / api / middlewares / auth.js View on Github external
try {
    if (error || !user) throw error;
    await logIn(user, { session: false });
  } catch (e) {
    return next(apiError);
  }

  if (roles === LOGGED_USER) {
    if (user.role !== 'admin' && req.params.userId !== user._id.toString()) {
      apiError.status = httpStatus.FORBIDDEN;
      apiError.message = 'Forbidden';
      return next(apiError);
    }
  } else if (!roles.includes(user.role)) {
    apiError.status = httpStatus.FORBIDDEN;
    apiError.message = 'Forbidden';
    return next(apiError);
  } else if (err || !user) {
    return next(apiError);
  }

  req.user = user;

  return next();
};
github zalando-incubator / authmosphere / src / express-tooling.ts View on Github external
(req, res, next, _scopes, _logger) =>
            rejectRequest(res, _logger, HttpStatus.FORBIDDEN);
github IBM / taxinomitis / src / lib / utils / download.ts View on Github external
function recognizeCommonProblems(response: request.Response, url: string): Error | undefined
{
    if (response.statusCode >= 400) {
        if (response.statusCode === httpstatus.FORBIDDEN ||
            response.statusCode === httpstatus.UNAUTHORIZED)
        {
            return new Error(safeGetHost(url) + ERRORS.DOWNLOAD_FORBIDDEN);
        }

        numErrors += 1;
        log.error({ statusCode : response.statusCode, url, numDownloads, numErrors }, 'Failed to request url');
        return new Error(ERRORS.DOWNLOAD_FAIL + url);
    }

    if (downloadTooBig(response.headers)) {
        return new Error(ERRORS.DOWNLOAD_TOO_BIG);
    }

    if (response.headers['content-type'] &&
        response.headers['content-type'].startsWith('text/html') &&