Skip to content

Commit

Permalink
refactor: update permission group name regex
Browse files Browse the repository at this point in the history
  • Loading branch information
kark committed Nov 22, 2022
1 parent a0c4efc commit 2e06827
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
6 changes: 0 additions & 6 deletions .changeset/dirty-queens-obey.md

This file was deleted.

2 changes: 0 additions & 2 deletions .changeset/fresh-coins-shout.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
'@commercetools-frontend/cypress': minor
---

WIP:

Enable configuring additional permissions in Custom Applications.

Additional permissions are defined with `additionalOAuthScopes` field in the Custom Application. [See docs](https://docs.commercetools.com/custom-applications/concepts/oauth-scopes-and-user-permissions#additional-oauth-scopes).
7 changes: 7 additions & 0 deletions application-templates/starter/custom-application-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const config = {
view: ['view_products'],
manage: ['manage_products'],
},
additionalOAuthScopes: [
{
name: 'other-one',
view: ['view_products'],
manage: ['manage_products'],
},
],
icon: '${path:@commercetools-frontend/assets/application-icons/rocket.svg}',
mainMenuLink: {
defaultLabel: 'Template starter',
Expand Down
6 changes: 6 additions & 0 deletions packages/application-config/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
export const ENTRY_POINT_URI_PATH_REGEX =
/^[^-_#]([0-9a-z]|[-_](?![-_])){0,62}[^-_#]$/g;

/**
* The permission group name may be between 2 and 64 characters and only contain alphanumeric lowercase characters and non-consecutive hyphens. Leading and trailing hyphens are also not allowed.
*/
export const PERMISSION_GROUP_NAME_REGEX =
/^[^-#]([a-z]|[-](?![-])){0,62}[^-#]$/g;

export const CLOUD_IDENTIFIERS = {
GCP_AU: 'gcp-au',
GCP_EU: 'gcp-eu',
Expand Down
13 changes: 8 additions & 5 deletions packages/application-config/src/validations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Ajv, { type ErrorObject } from 'ajv';
import schemaJson from '../schema.json';
import { ENTRY_POINT_URI_PATH_REGEX } from './constants';
import {
ENTRY_POINT_URI_PATH_REGEX,
PERMISSION_GROUP_NAME_REGEX,
} from './constants';
import type { JSONSchemaForCustomApplicationConfigurationFiles } from './schema';

type ErrorAdditionalProperty = ErrorObject<
Expand Down Expand Up @@ -52,7 +55,7 @@ export const validateEntryPointUriPath = (
) => {
if (!config.entryPointUriPath.match(ENTRY_POINT_URI_PATH_REGEX)) {
throw new Error(
'Invalid "entryPointUriPath". The value may be between 2 and 64 characters and only contain alphanumeric lowercase characters, non-consecutive underscores and hyphens. Leading and trailing underscore and hyphens are also not allowed.'
'Invalid "entryPointUriPath". The value may be between 2 and 64 characters and only contain alphanumeric lowercase characters, non-consecutive underscores and hyphens. Leading and trailing underscores and hyphens are also not allowed.'
);
}
};
Expand All @@ -78,12 +81,12 @@ export const validateAdditionalOAuthScopes = (
config.additionalOAuthScopes?.forEach(({ name }) => {
if (additionalPermissionNames.has(name)) {
throw new Error(
`Duplicate additional permission "${name}". Every additional permission must have a unique name`
`Duplicate additional permission group name "${name}". Every additional permission must have a unique name`
);
}
if (!name.match(ENTRY_POINT_URI_PATH_REGEX)) {
if (!name.match(PERMISSION_GROUP_NAME_REGEX)) {
throw new Error(
`Additional permission name "${name}" is invalid. The value may be between 2 and 64 characters and only contain alphanumeric lowercase characters, non-consecutive underscores and hyphens. Leading and trailing underscore and hyphens are also not allowed`
`Additional permission group name "${name}" is invalid. The value may be between 2 and 64 characters and only contain alphanumeric lowercase characters and non-consecutive hyphens. Leading and trailing hyphens are also not allowed`
);
}
additionalPermissionNames.add(name);
Expand Down

0 comments on commit 2e06827

Please sign in to comment.