From 7918e1f2a38129fa403e78cc413eefd43d863d02 Mon Sep 17 00:00:00 2001 From: Hugo Bernier Date: Wed, 20 Sep 2023 23:36:56 -0400 Subject: [PATCH] Added sample.json, fixed readme, container, nvmrc --- .../.devcontainer/devcontainer.json | 39 ++++++++ .../.devcontainer/spfx-startup.sh | 33 +++++++ samples/react-apim-tablestorage/.nvmrc | 1 + samples/react-apim-tablestorage/README.md | 93 ++++++++++++------- .../assets/sample.json | 50 ++++++++++ 5 files changed, 181 insertions(+), 35 deletions(-) create mode 100644 samples/react-apim-tablestorage/.devcontainer/devcontainer.json create mode 100644 samples/react-apim-tablestorage/.devcontainer/spfx-startup.sh create mode 100644 samples/react-apim-tablestorage/.nvmrc create mode 100644 samples/react-apim-tablestorage/assets/sample.json diff --git a/samples/react-apim-tablestorage/.devcontainer/devcontainer.json b/samples/react-apim-tablestorage/.devcontainer/devcontainer.json new file mode 100644 index 000000000..a34946b01 --- /dev/null +++ b/samples/react-apim-tablestorage/.devcontainer/devcontainer.json @@ -0,0 +1,39 @@ +// For more information on how to run this SPFx project in a VS Code Remote Container, please visit https://aka.ms/spfx-devcontainer +{ + "name": "SPFx 1.17.4", + "image": "docker.io/m365pnp/spfx:1.17.4", + // Set *default* container specific settings.json values on container create. + "settings": {}, + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "editorconfig.editorconfig", + "dbaeumer.vscode-eslint" + ], + // Use 'forwardPorts' to make a list of ports inside the container available locally. + "forwardPorts": [ + 4321, + 35729 + ], + "portsAttributes": { + "4321": { + "protocol": "https", + "label": "Manifest", + "onAutoForward": "silent", + "requireLocalPort": true + }, + // Not needed for SPFx>= 1.12.1 + // "5432": { + // "protocol": "https", + // "label": "Workbench", + // "onAutoForward": "silent" + // }, + "35729": { + "protocol": "https", + "label": "LiveReload", + "onAutoForward": "silent", + "requireLocalPort": true + } + }, + "postCreateCommand": "bash .devcontainer/spfx-startup.sh", + "remoteUser": "node" +} \ No newline at end of file diff --git a/samples/react-apim-tablestorage/.devcontainer/spfx-startup.sh b/samples/react-apim-tablestorage/.devcontainer/spfx-startup.sh new file mode 100644 index 000000000..456d6aea8 --- /dev/null +++ b/samples/react-apim-tablestorage/.devcontainer/spfx-startup.sh @@ -0,0 +1,33 @@ +echo +echo -e "\e[1;94mInstalling Node dependencies\e[0m" +npm install + +## commands to create dev certificate and copy it to the root folder of the project +echo +echo -e "\e[1;94mGenerating dev certificate\e[0m" +gulp trust-dev-cert + +# Convert the generated PEM certificate to a CER certificate +openssl x509 -inform PEM -in ~/.rushstack/rushstack-serve.pem -outform DER -out ./spfx-dev-cert.cer + +# Copy the PEM ecrtificate for non-Windows hosts +cp ~/.rushstack/rushstack-serve.pem ./spfx-dev-cert.pem + +## add *.cer to .gitignore to prevent certificates from being saved in repo +if ! grep -Fxq '*.cer' ./.gitignore + then + echo "# .CER Certificates" >> .gitignore + echo "*.cer" >> .gitignore +fi + +## add *.pem to .gitignore to prevent certificates from being saved in repo +if ! grep -Fxq '*.pem' ./.gitignore + then + echo "# .PEM Certificates" >> .gitignore + echo "*.pem" >> .gitignore +fi + +echo +echo -e "\e[1;92mReady!\e[0m" + +echo -e "\n\e[1;94m**********\nOptional: if you plan on using gulp serve, don't forget to add the container certificate to your local machine. Please visit https://aka.ms/spfx-devcontainer for more information\n**********" \ No newline at end of file diff --git a/samples/react-apim-tablestorage/.nvmrc b/samples/react-apim-tablestorage/.nvmrc new file mode 100644 index 000000000..ff650592a --- /dev/null +++ b/samples/react-apim-tablestorage/.nvmrc @@ -0,0 +1 @@ +v16.13.0 diff --git a/samples/react-apim-tablestorage/README.md b/samples/react-apim-tablestorage/README.md index 3dfa0cb84..53351938a 100644 --- a/samples/react-apim-tablestorage/README.md +++ b/samples/react-apim-tablestorage/README.md @@ -2,13 +2,13 @@ ## Summary -This SharePoint Framework (SPFx) web part allows you to access securely Azure stroage table data directly from a SharePoint Framework (SPFx) web part using Azure API Management (APIM). This scenario is useful when you want to provide a seamless user experience for your application without exposing your backend services or credentials. This is a great way to simplify and secure your web app’s communication with the cloud. +This SharePoint Framework (SPFx) web part allows you to access securely Azure storage table data directly from a SharePoint Framework (SPFx) web part using Azure API Management (APIM). This scenario is useful when you want to provide a seamless user experience for your application without exposing your backend services or credentials. This is a great way to simplify and secure your web app’s communication with the cloud. ## Solution Architecture The basic flow is as follows: -- The SharePoint Framework (SPFx) webpart authenticates with Azure Active Directory (AAD) and obtains an access token. +- The SharePoint Framework (SPFx) web part authenticates with Azure Active Directory (AAD) and obtains an access token. - The SharePoint Framework (SPFx) makes a request to APIM with the access token in the header. - APIM validates the access token using AAD token validation and CORS policies. - APIM accesses the Azure resource via Azure managed identities and returns the response to the SharePoint Framework (SPFx) webpart. @@ -56,13 +56,13 @@ Let's go through each step in detail. ### 1. Create an Azure API Management resource -Create an Azure API Management resource [Click here for more detail](https://learn.microsoft.com/en-us/azure/api-management/get-started-create-service-instance) +Create an Azure API Management resource [Click here for more detail](https://learn.microsoft.com/azure/api-management/get-started-create-service-instance) -### 2. Create a Azure storge account +### 2. Create a Azure storage account -Create a Azure storge account [Click here for more detail](https://learn.microsoft.com/en-us/azure/storage/common/storage-account-create?tabs=azure-portal). +Create a Azure storage account [Click here for more detail](https://learn.microsoft.com/azure/storage/common/storage-account-create?tabs=azure-portal). -### 3. Configure Azure Managed Identities. +### 3. Configure Azure Managed Identities The next step is to configure Azure Managed Identities. This will allow our API to access our Azure resources without storing any credentials or secrets in our code. To do this, follow these steps: @@ -76,22 +76,22 @@ The next step is to configure Azure Managed Identities. This will allow our API The next step is to create an Azure AD app. This will allow us to authenticate our users with Azure AD and get an access token that we can use to call our Azure API management endpoint. To do this, follow these steps: -1. In the Azure portal, search for and select App registrations. -2. Select New Registration. -3. When the Register an application page appears, enter your application's registration information: +1. In the Azure portal, search for and select App registrations. +2. Select New Registration. +3. When the Register an application page appears, enter your application's registration information: • In the Name section, enter a meaningful application name that will be displayed to users of the app, such as the backend-app. • In the Supported Account Types section, select an option that suits your scenario. -4. Leave the Redirect URI section empty. Later, you'll add a redirect URI generated in the OAuth 2.0 configuration in API Management. -5. Select Register to create the application. -6. On the app Overview page, find the Application (client) ID value and record it for later. -7. Under the Manage section of the side menu, select Expose an API and set the Application ID URI as below. Record this value for later. +4. Leave the Redirect URI section empty. Later, you'll add a redirect URI generated in the OAuth 2.0 configuration in API Management. +5. Select Register to create the application. +6. On the app Overview page, find the Application (client) ID value and record it for later. +7. Under the Manage section of the side menu, select Expose an API and set the Application ID URI as below. Record this value for later. `api://[Client ID]/[Tenant Name].sharepoint.com` -8. Select the Add a Scope button to display the Add a Scope page: +8. Select the Add a Scope button to display the Add a Scope page: a. Enter a Scope name for a scope that's supported by the API (for example, **user_impersonation**). b. In Who can consent? Make a selection for your scenario, such as Admins and users. Select Admins only for higher privileged scenarios. c. Enter the Admin consent display name and Admin consent description. d. Make sure the Enabled scope state is selected. -9. Select the Add Scope button to create the scope. +9. Select the Add Scope button to create the scope. 10. Repeat the previous two steps to add all scopes supported by your API. 11. Once the scopes are created, make a note of them for use in a subsequent step. @@ -103,9 +103,9 @@ The next step is to create an API for our Azure Storage account in our API Manag 2. From the left menu, select + Add API. 3. Select HTTP from the list. -![Manually define HTTP API](./assets/blank-api-1.png "Manually define HTTP API") + ![Manually define HTTP API](./assets/blank-api-1.png "Manually define HTTP API") -4. Enter the backend Web service URL (In our case, Azure storage table URI, `https://[storageaccountname].table.core.windows.net/`) and other settings for the API. The settings are explained in the [Import and publish your first API](https://learn.microsoft.com/en-us/azure/api-management/import-and-publish#import-and-publish-a-backend-api) tutorial. +4. Enter the backend Web service URL (In our case, Azure storage table URI, `https://[storageaccountname].table.core.windows.net/`) and other settings for the API. The settings are explained in the [Import and publish your first API](https://learn.microsoft.com/azure/api-management/import-and-publish#import-and-publish-a-backend-api) tutorial. 5. Select Create. At this point, you have no operations in API Management that map to the operations in your backend API. If you call an operation that is exposed through the back end but not through the API Management, you get a 404. @@ -223,37 +223,60 @@ return String.Format("https://[AzureStorageName].table.core.windows.net/{0}()", 2. From your command line, change your current directory to the directory containing this sample (`react-apim-tablestroage`, located under `samples`) 3. In the command line run: -```cmd - `npm install` - `gulp bundle` - `gulp package-solution` -``` + ```cmd + npm install + gulp bundle + gulp package-solution + ``` 4. Deploy the package to your app catalog 5. Approve the following API permission request from the SharePoint admin -```JSON - { - "resource": "o3c-apim-sp", //name of the Azure AD app - "scope": "user_impersonation" - } -``` + ```JSON + { + "resource": "o3c-apim-sp", //name of the Azure AD app + "scope": "user_impersonation" + } + ``` -7. In the command-line run: +6. In the command-line run: -```cmd - gulp serve --nobrowser -``` + ```cmd + gulp serve --nobrowser + ``` -8. Open the hosted workbench on a SharePoint site - i.e. https://_tenant_.sharepoint.com/site/_sitename_/_layouts/workbench.aspx +7. Open the hosted workbench on a SharePoint site - i.e. https://_tenant_.sharepoint.com/site/_sitename_/_layouts/workbench.aspx - Add the [O3C] Azure Connect web part to the page. - In the web part properties, configure the following properties 1. Add Subscription Key (e.g. `2a80a80cf8f7878485588ba887ad85`) 2. Add AAD App Scope URL (e.g. `api://88784ee-44eee-4b8e-ad72-9918e7777/tenantname.sharepoint.com`) - 3. Azure Table Storage Endpoint (e.g. https://myapim.azure-api.net/tablestorage)- + 3. Azure Table Storage Endpoint (e.g. - Close the web part properties pane and save and reload the page ## Features -This SharePoint Framework (SPFx) web part allows you to access securely Azure stroage table data directly from a SharePoint Framework (SPFx) web part using Azure API Management (APIM). This scenario is useful when you want to provide a seamless user experience for your application without exposing your backend services or credentials +This SharePoint Framework (SPFx) web part allows you to access securely Azure storage table data directly from a SharePoint Framework (SPFx) web part using Azure API Management (APIM). This scenario is useful when you want to provide a seamless user experience for your application without exposing your backend services or credentials + +## Help + + +We do not support samples, but this community is always willing to help, and we want to improve these samples. We use GitHub to track issues, which makes it easy for community members to volunteer their time and help resolve issues. + +If you're having issues building the solution, please run [spfx doctor](https://pnp.github.io/cli-microsoft365/cmd/spfx/spfx-doctor/) from within the solution folder to diagnose incompatibility issues with your environment. + +You can try looking at [issues related to this sample](https://github.com/pnp/sp-dev-fx-webparts/issues?q=label%3A%22sample%3A%20react-apim-tablestorage%22) to see if anybody else is having the same issues. + +You can also try looking at [discussions related to this sample](https://github.com/pnp/sp-dev-fx-webparts/discussions?discussions_q=react-apim-tablestorage) and see what the community is saying. + +If you encounter any issues using this sample, [create a new issue](https://github.com/pnp/sp-dev-fx-webparts/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected%2Csample%3A%20react-apim-tablestorage&template=bug-report.yml&sample=react-apim-tablestorage&authors=@ejazhussain&title=react-apim-tablestorage%20-%20). + +For questions regarding this sample, [create a new question](https://github.com/pnp/sp-dev-fx-webparts/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Aquestion%2Csample%3A%20react-apim-tablestorage&template=question.yml&sample=react-apim-tablestorage&authors=@ejazhussain&title=react-apim-tablestorage%20-%20). + +Finally, if you have an idea for improvement, [make a suggestion](https://github.com/pnp/sp-dev-fx-webparts/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Aenhancement%2Csample%3A%20react-apim-tablestorage&template=suggestion.yml&sample=react-apim-tablestorage&authors=@ejazhussain&title=react-apim-tablestorage%20-%20). + +## Disclaimer + +**THIS CODE IS PROVIDED _AS IS_ WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.** + + diff --git a/samples/react-apim-tablestorage/assets/sample.json b/samples/react-apim-tablestorage/assets/sample.json new file mode 100644 index 000000000..278ef5c14 --- /dev/null +++ b/samples/react-apim-tablestorage/assets/sample.json @@ -0,0 +1,50 @@ +[ + { + "name": "pnp-sp-dev-spfx-web-parts-react-apim-tablestorage", + "source": "pnp", + "title": "Accessing Azure table storage data using Azure API Management", + "shortDescription": "Allows you to access securely Azure storage table data using Azure API Management (APIM)", + "url": "https://github.com/pnp/sp-dev-fx-webparts/tree/main/samples/react-apim-tablestorage", + "downloadUrl": "https://pnp.github.io/download-partial/?url=https://github.com/pnp/sp-dev-fx-webparts/tree/main/samples/react-apim-tablestorage", + "longDescription": [ + "This SharePoint Framework (SPFx) web part allows you to access securely Azure storage table data directly from a SharePoint Framework (SPFx) web part using Azure API Management (APIM). This scenario is useful when you want to provide a seamless user experience for your application without exposing your backend services or credentials. This is a great way to simplify and secure your web app’s communication with the cloud." + ], + "creationDateTime": "2023-09-11", + "updateDateTime": "2023-09-11", + "products": [ + "SharePoint" + ], + "metadata": [ + { + "key": "CLIENT-SIDE-DEV", + "value": "React" + }, + { + "key": "SPFX-VERSION", + "value": "1.17.4" + } + ], + "thumbnails": [ + { + "type": "image", + "order": 100, + "url": "https://github.com/pnp/sp-dev-fx-webparts/raw/main/samples/react-apim-tablestorage/assets/demo.gif", + "alt": "Web Part Preview" + } + ], + "authors": [ + { + "gitHubAccount": "ejazhussain", + "pictureUrl": "https://github.com/ejazhussain.png", + "name": "Ejaz Hussain" + } + ], + "references": [ + { + "name": "Build your first SharePoint client-side web part", + "description": "Client-side web parts are client-side components that run in the context of a SharePoint page. Client-side web parts can be deployed to SharePoint environments that support the SharePoint Framework. You can also use modern JavaScript web frameworks, tools, and libraries to build them.", + "url": "https://docs.microsoft.com/sharepoint/dev/spfx/web-parts/get-started/build-a-hello-world-web-part" + } + ] + } +]