@backstage/plugin-permission-react 0.1.1 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # @backstage/plugin-permission-react
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 70281a475b: Breaking Changes:
8
+
9
+ - Remove "api" suffixes from constructor parameters in IdentityPermissionApi.create
10
+
11
+ ```diff
12
+ const { config, discovery, identity } = options;
13
+ - const permissionApi = IdentityPermissionApi.create({
14
+ - configApi: config,
15
+ - discoveryApi: discovery,
16
+ - identityApi: identity
17
+ - });
18
+ + const permissionApi = IdentityPermissionApi.create({ config, discovery, identity });
19
+ ```
20
+
21
+ ### Patch Changes
22
+
23
+ - Updated dependencies
24
+ - @backstage/core-plugin-api@0.4.0
25
+ - @backstage/plugin-permission-common@0.3.0
26
+
3
27
  ## 0.1.1
4
28
 
5
29
  ### Patch Changes
package/dist/index.cjs.js CHANGED
@@ -22,9 +22,9 @@ class IdentityPermissionApi {
22
22
  this.identityApi = identityApi;
23
23
  }
24
24
  static create(options) {
25
- const { configApi, discoveryApi, identityApi } = options;
26
- const permissionClient = new pluginPermissionCommon.PermissionClient({ discoveryApi, configApi });
27
- return new IdentityPermissionApi(permissionClient, identityApi);
25
+ const { config, discovery, identity } = options;
26
+ const permissionClient = new pluginPermissionCommon.PermissionClient({ discovery, config });
27
+ return new IdentityPermissionApi(permissionClient, identity);
28
28
  }
29
29
  async authorize(request) {
30
30
  const response = await this.permissionClient.authorize([request], {
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":["../src/apis/PermissionApi.ts","../src/apis/IdentityPermissionApi.ts","../src/hooks/usePermission.ts","../src/components/PermissionedRoute.tsx"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AuthorizeRequest,\n AuthorizeResponse,\n} from '@backstage/plugin-permission-common';\nimport { ApiRef, createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * This API is used by various frontend utilities that allow developers to implement authorization wihtin their frontend\n * plugins. A plugin developer will likely not have to interact with this API or its implementations directly, but\n * rather with the aforementioned utility components/hooks.\n * @public\n */\nexport type PermissionApi = {\n authorize(request: AuthorizeRequest): Promise<AuthorizeResponse>;\n};\n\n/**\n * A Backstage ApiRef for the Permission API. See https://backstage.io/docs/api/utility-apis for more information on\n * Backstage ApiRefs.\n * @public\n */\nexport const permissionApiRef: ApiRef<PermissionApi> = createApiRef({\n id: 'plugin.permission.api',\n});\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizeRequest,\n AuthorizeResponse,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which simply calls the authorize method of the given\n * {@link @backstage/plugin-permission-common#PermissionClient}.\n * @public\n */\nexport class IdentityPermissionApi implements PermissionApi {\n private constructor(\n private readonly permissionClient: PermissionClient,\n private readonly identityApi: IdentityApi,\n ) {}\n\n static create(options: {\n configApi: Config;\n discoveryApi: DiscoveryApi;\n identityApi: IdentityApi;\n }) {\n const { configApi, discoveryApi, identityApi } = options;\n const permissionClient = new PermissionClient({ discoveryApi, configApi });\n return new IdentityPermissionApi(permissionClient, identityApi);\n }\n\n async authorize(request: AuthorizeRequest): Promise<AuthorizeResponse> {\n const response = await this.permissionClient.authorize([request], {\n token: await this.identityApi.getIdToken(),\n });\n return response[0];\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useAsync } from 'react-use';\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n Permission,\n} from '@backstage/plugin-permission-common';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utlity for authorization. Given a {@link @backstage/plugin-permission-common#Permission} and an optional\n * resourceRef, it will return whether or not access is allowed (for the given resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for more details.\n * @public\n */\nexport const usePermission = (\n permission: Permission,\n resourceRef?: string,\n): AsyncPermissionResult => {\n const permissionApi = useApi(permissionApiRef);\n\n const { loading, error, value } = useAsync(async () => {\n const { result } = await permissionApi.authorize({\n permission,\n resourceRef,\n });\n\n return result;\n }, [permissionApi, permission, resourceRef]);\n\n if (loading) {\n return { loading: true, allowed: false };\n }\n if (error) {\n return { error, loading: false, allowed: false };\n }\n return { loading: false, allowed: value === AuthorizeResult.ALLOW };\n};\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ComponentProps, ReactElement } from 'react';\nimport { Route } from 'react-router';\nimport { useApp } from '@backstage/core-plugin-api';\nimport { usePermission } from '../hooks';\nimport { Permission } from '@backstage/plugin-permission-common';\n\n/**\n * Returns a React Router Route which only renders the element when authorized. If unauthorized, the Route will render a\n * NotFoundErrorPage (see {@link @backstage/core-app-api#AppComponents}).\n *\n * @public\n */\nexport const PermissionedRoute = (\n props: ComponentProps<typeof Route> & {\n permission: Permission;\n resourceRef?: string;\n errorComponent?: ReactElement | null;\n },\n) => {\n const { permission, resourceRef, errorComponent, ...otherProps } = props;\n const permissionResult = usePermission(permission, resourceRef);\n const app = useApp();\n const { NotFoundErrorPage } = app.getComponents();\n\n let shownElement: ReactElement | null | undefined =\n errorComponent === undefined ? <NotFoundErrorPage /> : errorComponent;\n\n if (permissionResult.loading) {\n shownElement = null;\n } else if (permissionResult.allowed) {\n shownElement = props.element;\n }\n\n return <Route {...otherProps} element={shownElement} />;\n};\n"],"names":["createApiRef","PermissionClient","useApi","useAsync","AuthorizeResult","useApp","Route"],"mappings":";;;;;;;;;;;;;;MAqCa,mBAA0CA,2BAAa;AAAA,EAClE,IAAI;AAAA;;4BCRsD;AAAA,EAClD,YACW,kBACA,aACjB;AAFiB;AACA;AAAA;AAAA,SAGZ,OAAO,SAIX;AACD,UAAM,EAAE,WAAW,cAAc,gBAAgB;AACjD,UAAM,mBAAmB,IAAIC,wCAAiB,EAAE,cAAc;AAC9D,WAAO,IAAI,sBAAsB,kBAAkB;AAAA;AAAA,QAG/C,UAAU,SAAuD;AACrE,UAAM,WAAW,MAAM,KAAK,iBAAiB,UAAU,CAAC,UAAU;AAAA,MAChE,OAAO,MAAM,KAAK,YAAY;AAAA;AAEhC,WAAO,SAAS;AAAA;AAAA;;MCbP,gBAAgB,CAC3B,YACA,gBAC0B;AAC1B,QAAM,gBAAgBC,qBAAO;AAE7B,QAAM,EAAE,SAAS,OAAO,UAAUC,kBAAS,YAAY;AACrD,UAAM,EAAE,WAAW,MAAM,cAAc,UAAU;AAAA,MAC/C;AAAA,MACA;AAAA;AAGF,WAAO;AAAA,KACN,CAAC,eAAe,YAAY;AAE/B,MAAI,SAAS;AACX,WAAO,EAAE,SAAS,MAAM,SAAS;AAAA;AAEnC,MAAI,OAAO;AACT,WAAO,EAAE,OAAO,SAAS,OAAO,SAAS;AAAA;AAE3C,SAAO,EAAE,SAAS,OAAO,SAAS,UAAUC,uCAAgB;AAAA;;MC9BjD,oBAAoB,CAC/B,UAKG;AACH,QAAM,EAAE,YAAY,aAAa,mBAAmB,eAAe;AACnE,QAAM,mBAAmB,cAAc,YAAY;AACnD,QAAM,MAAMC;AACZ,QAAM,EAAE,sBAAsB,IAAI;AAElC,MAAI,eACF,mBAAmB,iEAAa,mBAAD,QAAwB;AAEzD,MAAI,iBAAiB,SAAS;AAC5B,mBAAe;AAAA,aACN,iBAAiB,SAAS;AACnC,mBAAe,MAAM;AAAA;AAGvB,iEAAQC,mBAAD;AAAA,OAAW;AAAA,IAAY,SAAS;AAAA;AAAA;;;;;;;"}
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/apis/PermissionApi.ts","../src/apis/IdentityPermissionApi.ts","../src/hooks/usePermission.ts","../src/components/PermissionedRoute.tsx"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AuthorizeRequest,\n AuthorizeResponse,\n} from '@backstage/plugin-permission-common';\nimport { ApiRef, createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * This API is used by various frontend utilities that allow developers to implement authorization wihtin their frontend\n * plugins. A plugin developer will likely not have to interact with this API or its implementations directly, but\n * rather with the aforementioned utility components/hooks.\n * @public\n */\nexport type PermissionApi = {\n authorize(request: AuthorizeRequest): Promise<AuthorizeResponse>;\n};\n\n/**\n * A Backstage ApiRef for the Permission API. See https://backstage.io/docs/api/utility-apis for more information on\n * Backstage ApiRefs.\n * @public\n */\nexport const permissionApiRef: ApiRef<PermissionApi> = createApiRef({\n id: 'plugin.permission.api',\n});\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizeRequest,\n AuthorizeResponse,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which simply calls the authorize method of the given\n * {@link @backstage/plugin-permission-common#PermissionClient}.\n * @public\n */\nexport class IdentityPermissionApi implements PermissionApi {\n private constructor(\n private readonly permissionClient: PermissionClient,\n private readonly identityApi: IdentityApi,\n ) {}\n\n static create(options: {\n config: Config;\n discovery: DiscoveryApi;\n identity: IdentityApi;\n }) {\n const { config, discovery, identity } = options;\n const permissionClient = new PermissionClient({ discovery, config });\n return new IdentityPermissionApi(permissionClient, identity);\n }\n\n async authorize(request: AuthorizeRequest): Promise<AuthorizeResponse> {\n const response = await this.permissionClient.authorize([request], {\n token: await this.identityApi.getIdToken(),\n });\n return response[0];\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useAsync } from 'react-use';\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n Permission,\n} from '@backstage/plugin-permission-common';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utlity for authorization. Given a {@link @backstage/plugin-permission-common#Permission} and an optional\n * resourceRef, it will return whether or not access is allowed (for the given resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for more details.\n * @public\n */\nexport const usePermission = (\n permission: Permission,\n resourceRef?: string,\n): AsyncPermissionResult => {\n const permissionApi = useApi(permissionApiRef);\n\n const { loading, error, value } = useAsync(async () => {\n const { result } = await permissionApi.authorize({\n permission,\n resourceRef,\n });\n\n return result;\n }, [permissionApi, permission, resourceRef]);\n\n if (loading) {\n return { loading: true, allowed: false };\n }\n if (error) {\n return { error, loading: false, allowed: false };\n }\n return { loading: false, allowed: value === AuthorizeResult.ALLOW };\n};\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ComponentProps, ReactElement } from 'react';\nimport { Route } from 'react-router';\nimport { useApp } from '@backstage/core-plugin-api';\nimport { usePermission } from '../hooks';\nimport { Permission } from '@backstage/plugin-permission-common';\n\n/**\n * Returns a React Router Route which only renders the element when authorized. If unauthorized, the Route will render a\n * NotFoundErrorPage (see {@link @backstage/core-app-api#AppComponents}).\n *\n * @public\n */\nexport const PermissionedRoute = (\n props: ComponentProps<typeof Route> & {\n permission: Permission;\n resourceRef?: string;\n errorComponent?: ReactElement | null;\n },\n) => {\n const { permission, resourceRef, errorComponent, ...otherProps } = props;\n const permissionResult = usePermission(permission, resourceRef);\n const app = useApp();\n const { NotFoundErrorPage } = app.getComponents();\n\n let shownElement: ReactElement | null | undefined =\n errorComponent === undefined ? <NotFoundErrorPage /> : errorComponent;\n\n if (permissionResult.loading) {\n shownElement = null;\n } else if (permissionResult.allowed) {\n shownElement = props.element;\n }\n\n return <Route {...otherProps} element={shownElement} />;\n};\n"],"names":["createApiRef","PermissionClient","useApi","useAsync","AuthorizeResult","useApp","Route"],"mappings":";;;;;;;;;;;;;;MAqCa,mBAA0CA,2BAAa;AAAA,EAClE,IAAI;AAAA;;4BCRsD;AAAA,EAClD,YACW,kBACA,aACjB;AAFiB;AACA;AAAA;AAAA,SAGZ,OAAO,SAIX;AACD,UAAM,EAAE,QAAQ,WAAW,aAAa;AACxC,UAAM,mBAAmB,IAAIC,wCAAiB,EAAE,WAAW;AAC3D,WAAO,IAAI,sBAAsB,kBAAkB;AAAA;AAAA,QAG/C,UAAU,SAAuD;AACrE,UAAM,WAAW,MAAM,KAAK,iBAAiB,UAAU,CAAC,UAAU;AAAA,MAChE,OAAO,MAAM,KAAK,YAAY;AAAA;AAEhC,WAAO,SAAS;AAAA;AAAA;;MCbP,gBAAgB,CAC3B,YACA,gBAC0B;AAC1B,QAAM,gBAAgBC,qBAAO;AAE7B,QAAM,EAAE,SAAS,OAAO,UAAUC,kBAAS,YAAY;AACrD,UAAM,EAAE,WAAW,MAAM,cAAc,UAAU;AAAA,MAC/C;AAAA,MACA;AAAA;AAGF,WAAO;AAAA,KACN,CAAC,eAAe,YAAY;AAE/B,MAAI,SAAS;AACX,WAAO,EAAE,SAAS,MAAM,SAAS;AAAA;AAEnC,MAAI,OAAO;AACT,WAAO,EAAE,OAAO,SAAS,OAAO,SAAS;AAAA;AAE3C,SAAO,EAAE,SAAS,OAAO,SAAS,UAAUC,uCAAgB;AAAA;;MC9BjD,oBAAoB,CAC/B,UAKG;AACH,QAAM,EAAE,YAAY,aAAa,mBAAmB,eAAe;AACnE,QAAM,mBAAmB,cAAc,YAAY;AACnD,QAAM,MAAMC;AACZ,QAAM,EAAE,sBAAsB,IAAI;AAElC,MAAI,eACF,mBAAmB,iEAAa,mBAAD,QAAwB;AAEzD,MAAI,iBAAiB,SAAS;AAC5B,mBAAe;AAAA,aACN,iBAAiB,SAAS;AACnC,mBAAe,MAAM;AAAA;AAGvB,iEAAQC,mBAAD;AAAA,OAAW;AAAA,IAAY,SAAS;AAAA;AAAA;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -56,9 +56,9 @@ declare class IdentityPermissionApi implements PermissionApi {
56
56
  private readonly identityApi;
57
57
  private constructor();
58
58
  static create(options: {
59
- configApi: Config;
60
- discoveryApi: DiscoveryApi;
61
- identityApi: IdentityApi;
59
+ config: Config;
60
+ discovery: DiscoveryApi;
61
+ identity: IdentityApi;
62
62
  }): IdentityPermissionApi;
63
63
  authorize(request: AuthorizeRequest): Promise<AuthorizeResponse>;
64
64
  }
package/dist/index.esm.js CHANGED
@@ -14,9 +14,9 @@ class IdentityPermissionApi {
14
14
  this.identityApi = identityApi;
15
15
  }
16
16
  static create(options) {
17
- const { configApi, discoveryApi, identityApi } = options;
18
- const permissionClient = new PermissionClient({ discoveryApi, configApi });
19
- return new IdentityPermissionApi(permissionClient, identityApi);
17
+ const { config, discovery, identity } = options;
18
+ const permissionClient = new PermissionClient({ discovery, config });
19
+ return new IdentityPermissionApi(permissionClient, identity);
20
20
  }
21
21
  async authorize(request) {
22
22
  const response = await this.permissionClient.authorize([request], {
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/apis/PermissionApi.ts","../src/apis/IdentityPermissionApi.ts","../src/hooks/usePermission.ts","../src/components/PermissionedRoute.tsx"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AuthorizeRequest,\n AuthorizeResponse,\n} from '@backstage/plugin-permission-common';\nimport { ApiRef, createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * This API is used by various frontend utilities that allow developers to implement authorization wihtin their frontend\n * plugins. A plugin developer will likely not have to interact with this API or its implementations directly, but\n * rather with the aforementioned utility components/hooks.\n * @public\n */\nexport type PermissionApi = {\n authorize(request: AuthorizeRequest): Promise<AuthorizeResponse>;\n};\n\n/**\n * A Backstage ApiRef for the Permission API. See https://backstage.io/docs/api/utility-apis for more information on\n * Backstage ApiRefs.\n * @public\n */\nexport const permissionApiRef: ApiRef<PermissionApi> = createApiRef({\n id: 'plugin.permission.api',\n});\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizeRequest,\n AuthorizeResponse,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which simply calls the authorize method of the given\n * {@link @backstage/plugin-permission-common#PermissionClient}.\n * @public\n */\nexport class IdentityPermissionApi implements PermissionApi {\n private constructor(\n private readonly permissionClient: PermissionClient,\n private readonly identityApi: IdentityApi,\n ) {}\n\n static create(options: {\n configApi: Config;\n discoveryApi: DiscoveryApi;\n identityApi: IdentityApi;\n }) {\n const { configApi, discoveryApi, identityApi } = options;\n const permissionClient = new PermissionClient({ discoveryApi, configApi });\n return new IdentityPermissionApi(permissionClient, identityApi);\n }\n\n async authorize(request: AuthorizeRequest): Promise<AuthorizeResponse> {\n const response = await this.permissionClient.authorize([request], {\n token: await this.identityApi.getIdToken(),\n });\n return response[0];\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useAsync } from 'react-use';\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n Permission,\n} from '@backstage/plugin-permission-common';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utlity for authorization. Given a {@link @backstage/plugin-permission-common#Permission} and an optional\n * resourceRef, it will return whether or not access is allowed (for the given resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for more details.\n * @public\n */\nexport const usePermission = (\n permission: Permission,\n resourceRef?: string,\n): AsyncPermissionResult => {\n const permissionApi = useApi(permissionApiRef);\n\n const { loading, error, value } = useAsync(async () => {\n const { result } = await permissionApi.authorize({\n permission,\n resourceRef,\n });\n\n return result;\n }, [permissionApi, permission, resourceRef]);\n\n if (loading) {\n return { loading: true, allowed: false };\n }\n if (error) {\n return { error, loading: false, allowed: false };\n }\n return { loading: false, allowed: value === AuthorizeResult.ALLOW };\n};\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ComponentProps, ReactElement } from 'react';\nimport { Route } from 'react-router';\nimport { useApp } from '@backstage/core-plugin-api';\nimport { usePermission } from '../hooks';\nimport { Permission } from '@backstage/plugin-permission-common';\n\n/**\n * Returns a React Router Route which only renders the element when authorized. If unauthorized, the Route will render a\n * NotFoundErrorPage (see {@link @backstage/core-app-api#AppComponents}).\n *\n * @public\n */\nexport const PermissionedRoute = (\n props: ComponentProps<typeof Route> & {\n permission: Permission;\n resourceRef?: string;\n errorComponent?: ReactElement | null;\n },\n) => {\n const { permission, resourceRef, errorComponent, ...otherProps } = props;\n const permissionResult = usePermission(permission, resourceRef);\n const app = useApp();\n const { NotFoundErrorPage } = app.getComponents();\n\n let shownElement: ReactElement | null | undefined =\n errorComponent === undefined ? <NotFoundErrorPage /> : errorComponent;\n\n if (permissionResult.loading) {\n shownElement = null;\n } else if (permissionResult.allowed) {\n shownElement = props.element;\n }\n\n return <Route {...otherProps} element={shownElement} />;\n};\n"],"names":[],"mappings":";;;;;;MAqCa,mBAA0C,aAAa;AAAA,EAClE,IAAI;AAAA;;4BCRsD;AAAA,EAClD,YACW,kBACA,aACjB;AAFiB;AACA;AAAA;AAAA,SAGZ,OAAO,SAIX;AACD,UAAM,EAAE,WAAW,cAAc,gBAAgB;AACjD,UAAM,mBAAmB,IAAI,iBAAiB,EAAE,cAAc;AAC9D,WAAO,IAAI,sBAAsB,kBAAkB;AAAA;AAAA,QAG/C,UAAU,SAAuD;AACrE,UAAM,WAAW,MAAM,KAAK,iBAAiB,UAAU,CAAC,UAAU;AAAA,MAChE,OAAO,MAAM,KAAK,YAAY;AAAA;AAEhC,WAAO,SAAS;AAAA;AAAA;;MCbP,gBAAgB,CAC3B,YACA,gBAC0B;AAC1B,QAAM,gBAAgB,OAAO;AAE7B,QAAM,EAAE,SAAS,OAAO,UAAU,SAAS,YAAY;AACrD,UAAM,EAAE,WAAW,MAAM,cAAc,UAAU;AAAA,MAC/C;AAAA,MACA;AAAA;AAGF,WAAO;AAAA,KACN,CAAC,eAAe,YAAY;AAE/B,MAAI,SAAS;AACX,WAAO,EAAE,SAAS,MAAM,SAAS;AAAA;AAEnC,MAAI,OAAO;AACT,WAAO,EAAE,OAAO,SAAS,OAAO,SAAS;AAAA;AAE3C,SAAO,EAAE,SAAS,OAAO,SAAS,UAAU,gBAAgB;AAAA;;MC9BjD,oBAAoB,CAC/B,UAKG;AACH,QAAM,EAAE,YAAY,aAAa,mBAAmB,eAAe;AACnE,QAAM,mBAAmB,cAAc,YAAY;AACnD,QAAM,MAAM;AACZ,QAAM,EAAE,sBAAsB,IAAI;AAElC,MAAI,eACF,mBAAmB,6CAAa,mBAAD,QAAwB;AAEzD,MAAI,iBAAiB,SAAS;AAC5B,mBAAe;AAAA,aACN,iBAAiB,SAAS;AACnC,mBAAe,MAAM;AAAA;AAGvB,6CAAQ,OAAD;AAAA,OAAW;AAAA,IAAY,SAAS;AAAA;AAAA;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/apis/PermissionApi.ts","../src/apis/IdentityPermissionApi.ts","../src/hooks/usePermission.ts","../src/components/PermissionedRoute.tsx"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AuthorizeRequest,\n AuthorizeResponse,\n} from '@backstage/plugin-permission-common';\nimport { ApiRef, createApiRef } from '@backstage/core-plugin-api';\n\n/**\n * This API is used by various frontend utilities that allow developers to implement authorization wihtin their frontend\n * plugins. A plugin developer will likely not have to interact with this API or its implementations directly, but\n * rather with the aforementioned utility components/hooks.\n * @public\n */\nexport type PermissionApi = {\n authorize(request: AuthorizeRequest): Promise<AuthorizeResponse>;\n};\n\n/**\n * A Backstage ApiRef for the Permission API. See https://backstage.io/docs/api/utility-apis for more information on\n * Backstage ApiRefs.\n * @public\n */\nexport const permissionApiRef: ApiRef<PermissionApi> = createApiRef({\n id: 'plugin.permission.api',\n});\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizeRequest,\n AuthorizeResponse,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which simply calls the authorize method of the given\n * {@link @backstage/plugin-permission-common#PermissionClient}.\n * @public\n */\nexport class IdentityPermissionApi implements PermissionApi {\n private constructor(\n private readonly permissionClient: PermissionClient,\n private readonly identityApi: IdentityApi,\n ) {}\n\n static create(options: {\n config: Config;\n discovery: DiscoveryApi;\n identity: IdentityApi;\n }) {\n const { config, discovery, identity } = options;\n const permissionClient = new PermissionClient({ discovery, config });\n return new IdentityPermissionApi(permissionClient, identity);\n }\n\n async authorize(request: AuthorizeRequest): Promise<AuthorizeResponse> {\n const response = await this.permissionClient.authorize([request], {\n token: await this.identityApi.getIdToken(),\n });\n return response[0];\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useAsync } from 'react-use';\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n Permission,\n} from '@backstage/plugin-permission-common';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utlity for authorization. Given a {@link @backstage/plugin-permission-common#Permission} and an optional\n * resourceRef, it will return whether or not access is allowed (for the given resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for more details.\n * @public\n */\nexport const usePermission = (\n permission: Permission,\n resourceRef?: string,\n): AsyncPermissionResult => {\n const permissionApi = useApi(permissionApiRef);\n\n const { loading, error, value } = useAsync(async () => {\n const { result } = await permissionApi.authorize({\n permission,\n resourceRef,\n });\n\n return result;\n }, [permissionApi, permission, resourceRef]);\n\n if (loading) {\n return { loading: true, allowed: false };\n }\n if (error) {\n return { error, loading: false, allowed: false };\n }\n return { loading: false, allowed: value === AuthorizeResult.ALLOW };\n};\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ComponentProps, ReactElement } from 'react';\nimport { Route } from 'react-router';\nimport { useApp } from '@backstage/core-plugin-api';\nimport { usePermission } from '../hooks';\nimport { Permission } from '@backstage/plugin-permission-common';\n\n/**\n * Returns a React Router Route which only renders the element when authorized. If unauthorized, the Route will render a\n * NotFoundErrorPage (see {@link @backstage/core-app-api#AppComponents}).\n *\n * @public\n */\nexport const PermissionedRoute = (\n props: ComponentProps<typeof Route> & {\n permission: Permission;\n resourceRef?: string;\n errorComponent?: ReactElement | null;\n },\n) => {\n const { permission, resourceRef, errorComponent, ...otherProps } = props;\n const permissionResult = usePermission(permission, resourceRef);\n const app = useApp();\n const { NotFoundErrorPage } = app.getComponents();\n\n let shownElement: ReactElement | null | undefined =\n errorComponent === undefined ? <NotFoundErrorPage /> : errorComponent;\n\n if (permissionResult.loading) {\n shownElement = null;\n } else if (permissionResult.allowed) {\n shownElement = props.element;\n }\n\n return <Route {...otherProps} element={shownElement} />;\n};\n"],"names":[],"mappings":";;;;;;MAqCa,mBAA0C,aAAa;AAAA,EAClE,IAAI;AAAA;;4BCRsD;AAAA,EAClD,YACW,kBACA,aACjB;AAFiB;AACA;AAAA;AAAA,SAGZ,OAAO,SAIX;AACD,UAAM,EAAE,QAAQ,WAAW,aAAa;AACxC,UAAM,mBAAmB,IAAI,iBAAiB,EAAE,WAAW;AAC3D,WAAO,IAAI,sBAAsB,kBAAkB;AAAA;AAAA,QAG/C,UAAU,SAAuD;AACrE,UAAM,WAAW,MAAM,KAAK,iBAAiB,UAAU,CAAC,UAAU;AAAA,MAChE,OAAO,MAAM,KAAK,YAAY;AAAA;AAEhC,WAAO,SAAS;AAAA;AAAA;;MCbP,gBAAgB,CAC3B,YACA,gBAC0B;AAC1B,QAAM,gBAAgB,OAAO;AAE7B,QAAM,EAAE,SAAS,OAAO,UAAU,SAAS,YAAY;AACrD,UAAM,EAAE,WAAW,MAAM,cAAc,UAAU;AAAA,MAC/C;AAAA,MACA;AAAA;AAGF,WAAO;AAAA,KACN,CAAC,eAAe,YAAY;AAE/B,MAAI,SAAS;AACX,WAAO,EAAE,SAAS,MAAM,SAAS;AAAA;AAEnC,MAAI,OAAO;AACT,WAAO,EAAE,OAAO,SAAS,OAAO,SAAS;AAAA;AAE3C,SAAO,EAAE,SAAS,OAAO,SAAS,UAAU,gBAAgB;AAAA;;MC9BjD,oBAAoB,CAC/B,UAKG;AACH,QAAM,EAAE,YAAY,aAAa,mBAAmB,eAAe;AACnE,QAAM,mBAAmB,cAAc,YAAY;AACnD,QAAM,MAAM;AACZ,QAAM,EAAE,sBAAsB,IAAI;AAElC,MAAI,eACF,mBAAmB,6CAAa,mBAAD,QAAwB;AAEzD,MAAI,iBAAiB,SAAS;AAC5B,mBAAe;AAAA,aACN,iBAAiB,SAAS;AACnC,mBAAe,MAAM;AAAA;AAGvB,6CAAQ,OAAD;AAAA,OAAW;AAAA,IAAY,SAAS;AAAA;AAAA;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/plugin-permission-react",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "main": "dist/index.esm.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "Apache-2.0",
@@ -28,8 +28,8 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@backstage/config": "^0.1.11",
31
- "@backstage/core-plugin-api": "^0.3.0",
32
- "@backstage/plugin-permission-common": "^0.2.0",
31
+ "@backstage/core-plugin-api": "^0.4.0",
32
+ "@backstage/plugin-permission-common": "^0.3.0",
33
33
  "cross-fetch": "^3.0.6",
34
34
  "react-router": "6.0.0-beta.0",
35
35
  "react-use": "^17.2.4"
@@ -39,8 +39,8 @@
39
39
  "react": "^16.13.1 || ^17.0.0"
40
40
  },
41
41
  "devDependencies": {
42
- "@backstage/cli": "^0.10.1",
43
- "@backstage/test-utils": "^0.1.24",
42
+ "@backstage/cli": "^0.10.3",
43
+ "@backstage/test-utils": "^0.2.0",
44
44
  "@testing-library/jest-dom": "^5.10.1",
45
45
  "@testing-library/react": "^11.2.5",
46
46
  "@types/jest": "^26.0.7"
@@ -48,5 +48,5 @@
48
48
  "files": [
49
49
  "dist"
50
50
  ],
51
- "gitHead": "562be0b43016294e27af3ad024191bb86b13b1c1"
51
+ "gitHead": "b315430f9dfcfa19ab0dd90f5b4ac6904938fba7"
52
52
  }