How to use the italia-ts-commons/lib/strings.NonEmptyString.decode function in italia-ts-commons

To help you get started, we’ve selected a few italia-ts-commons 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 teamdigitale / io-functions / lib / utils / middlewares / azure_api_auth.ts View on Github external
return resolve(
          left(
            ResponseErrorForbiddenAnonymousUser
          )
        );
      }

      const userId = errorOrUserId.value;
      const subscriptionId = errorOrSubscriptionId.value;

      // to correctly process the request, we must associate the correct
      // authorizations to the user that made the request; to do so, we
      // need to extract the groups associated to the authenticated user
      // from the x-user-groups header, generated by the Azure API Management
      // proxy.
      const errorOrGroupsHeader = NonEmptyString.decode(
        request.header("x-user-groups")
      );

      // extract the groups from the header
      const maybeGroups = errorOrGroupsHeader.map(getGroupsFromHeader);

      if (isLeft(maybeGroups) || maybeGroups.value.size === 0) {
        // the user as no valid authorization groups assigned
        return resolve(
          left<
            | IResponseErrorForbiddenNotAuthorized
            | IResponseErrorForbiddenAnonymousUser
            | IResponseErrorForbiddenNoAuthorizationGroups,
            IAzureApiAuthorization
          >(ResponseErrorForbiddenNoAuthorizationGroups)
        );
github teamdigitale / io-functions / lib / utils / middlewares / azure_api_auth.ts View on Github external
new Promise(resolve => {
      // get Azure userId and subscriptionId from the headers
      // these headers get added by the Azure API Manager gateway
      const errorOrUserId = NonEmptyString.decode(request.header("x-user-id"));

      const errorOrSubscriptionId = NonEmptyString.decode(
        request.header("x-subscription-id")
      );

      if (isLeft(errorOrUserId) || isLeft(errorOrSubscriptionId)) {
        // we cannot proceed unless we cannot associate the request to a
        // valid user and a subscription
        return resolve(
          left(
            ResponseErrorForbiddenAnonymousUser
          )
        );
      }

      const userId = errorOrUserId.value;
github teamdigitale / io-functions / lib / utils / middlewares / azure_api_auth.ts View on Github external
new Promise(resolve => {
      // get Azure userId and subscriptionId from the headers
      // these headers get added by the Azure API Manager gateway
      const errorOrUserId = NonEmptyString.decode(request.header("x-user-id"));

      const errorOrSubscriptionId = NonEmptyString.decode(
        request.header("x-subscription-id")
      );

      if (isLeft(errorOrUserId) || isLeft(errorOrSubscriptionId)) {
        // we cannot proceed unless we cannot associate the request to a
        // valid user and a subscription
        return resolve(
          left(
            ResponseErrorForbiddenAnonymousUser
          )
        );
      }

      const userId = errorOrUserId.value;
      const subscriptionId = errorOrSubscriptionId.value;
github teamdigitale / io-functions / lib / utils / middlewares / azure_user_attributes.ts View on Github external
return async request => {
    const errorOrUserEmail = EmailString.decode(
      request.header(HEADER_USER_EMAIL)
    );

    if (isLeft(errorOrUserEmail)) {
      return left, IAzureUserAttributes>(
        ResponseErrorInternal(
          `Missing, empty or invalid ${HEADER_USER_EMAIL} header`
        )
      );
    }

    const userEmail = errorOrUserEmail.value;

    const errorOrUserSubscriptionId = NonEmptyString.decode(
      request.header(HEADER_USER_SUBSCRIPTION_KEY)
    );

    if (isLeft(errorOrUserSubscriptionId)) {
      return left, IAzureUserAttributes>(
        ResponseErrorInternal(
          `Missing or empty ${HEADER_USER_SUBSCRIPTION_KEY} header`
        )
      );
    }

    const subscriptionId = errorOrUserSubscriptionId.value;

    // serviceId equals subscriptionId
    const errorOrMaybeService = await serviceModel.findOneByServiceId(
      subscriptionId
github teamdigitale / io-functions / lib / models / sender_service.ts View on Github external
export function makeSenderServiceId(
  recipientFiscalCode: FiscalCode,
  serviceId: ServiceId
): NonEmptyString {
  return NonEmptyString.decode(
    `${recipientFiscalCode}:${serviceId}`
  ).getOrElseL(() => {
    throw new Error("Invalid sender service id");
  });
}
github teamdigitale / io-functions / lib / utils / env.ts View on Github external
export function getRequiredStringEnv(k: string): NonEmptyString {
  const maybeValue = NonEmptyString.decode(process.env[k]);

  if (isLeft(maybeValue)) {
    throw new Error(`${k} must be defined and non-empty`);
  }

  return maybeValue.value;
}