|
1 | 1 | # Javascript Web App Sample
|
2 | 2 |
|
3 |
| -For javascript web applications that want access to resources like Azure DevOps REST API's, they will have to support an authentication flow for their users. The [Azure Active Directory Authentication Library (ADAL) for JS](https://github.com/AzureAD/azure-activedirectory-library-for-js) enables javascript application developers to setup inerative authentication flows and obtain access tokens for API usage. |
| 3 | +For javascript web applications that want access to resources like Azure DevOps REST API, they will have to support an authentication flow for their users. The [Microsoft Authentication Library for JavaScript](https://github.com/AzureAD/microsoft-authentication-library-for-js) (MSAL.js) enables javascript application developers to setup interactive authentication flows and obtain access tokens for API usage. |
4 | 4 |
|
5 | 5 | ## Sample Application
|
6 | 6 |
|
7 |
| -This buildable sample will walk you through the steps to create a single page javascript application which uses ADAL to authenticate a user via an interactive prompt and display all projects contained in a Azure DevOps account/TFS collection. |
| 7 | +This sample will walk you through the steps to create a single-page javascript application which uses **MSAL.js** to authenticate a user via an interactive prompt and display all projects contained in an Azure DevOps account/TFS collection. |
8 | 8 |
|
9 | 9 | To run this sample you will need:
|
10 |
| -* Http-server. You can download [NPM http server](https://www.npmjs.com/package/http-server) if you need one. |
11 |
| -* An Azure Active Directory (AAD) tenant. If you do not have one, follow these [steps to set up an AAD](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-howto-tenant) |
12 |
| -* A user account in your AAD tenant |
13 |
| -* A Azure DevOps account backed by your AAD tenant where your user account has access. If you have an existing Azure DevOps account not connected to your AAD tenant follow these [steps to connect your AAD tenant to your Azure DevOps account](https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/manage-azure-active-directory-groups-vsts?view=vsts&tabs=new-nav) |
14 | 10 |
|
15 |
| -## Step 1: Clone or download vsts-auth-samples repository |
| 11 | +- [Node.js](https://nodejs.org/en/download/) must be installed to run this sample. |
| 12 | +- A modern web browser. This sample uses **ES6** conventions and will not run on **Internet Explorer**. |
| 13 | +- [Visual Studio Code](https://code.visualstudio.com/download) is recommended for running and editing this sample. |
| 14 | +- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see: [How to get an Azure AD tenant](https://azure.microsoft.com/documentation/articles/active-directory-howto-tenant/) |
| 15 | +- A user account in your Azure AD tenant. |
| 16 | +- An Azure DevOps account backed by your AAD tenant where your user account has access. If you have an existing Azure DevOps account not connected to your AAD tenant follow these [steps to connect your AAD tenant to your Azure DevOps account](https://docs.microsoft.com/azure/devops/organizations/accounts/manage-azure-active-directory-groups-vsts?view=vsts&tabs=new-nav) |
16 | 17 |
|
17 |
| -From a shell or command line: |
18 |
| -```no-highlight |
19 |
| -git clone https://github.com/Microsoft/vsts-auth-samples.git |
| 18 | +## Step 1: Clone or download azure-devops-auth-samples repository |
| 19 | + |
| 20 | +From a shell or command line: |
| 21 | + |
| 22 | +```console |
| 23 | +git clone https://github.com/microsoft/azure-devops-auth-samples.git |
| 24 | +``` |
| 25 | + |
| 26 | +Then locate the sample folder, where `package.json` file resides. Once you do, type: |
| 27 | + |
| 28 | +```console |
| 29 | +npm install |
20 | 30 | ```
|
21 | 31 |
|
22 |
| -## Step 2: Register the sample application with you Azure Active Directory tenant |
| 32 | +## Step 2: Register the app |
| 33 | + |
| 34 | +1. Navigate to the Microsoft identity platform for developers [App registrations](https://go.microsoft.com/fwlink/?linkid=2083908) page. |
| 35 | +1. Select **New registration**. |
| 36 | +1. In the **Register an application page** that appears, enter your application's registration information: |
| 37 | + - In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `devops-js-app`. |
| 38 | + - Under **Supported account types**, select **Accounts in your organizational directory only**. |
| 39 | + - In the **Redirect URI (optional)** section, select **Single-Page Application** in the combo-box and enter the following redirect URI: `http://localhost:8081/`. |
| 40 | +1. Select **Register** to create the application. |
| 41 | +1. In the app's registration screen, find and note the **Application (client) ID**. You use this value in your app's configuration file(s) later in your code. |
| 42 | +1. Select **Save** to save your changes. |
23 | 43 |
|
24 |
| -1. Sign in to the [Azure Portal](https://portal.azure.com). |
25 |
| -2. On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application. |
26 |
| -3. On the left hand navigation menu, select `Azure Active Directory`. |
27 |
| -4. Click on `App registrations` and select `New application registration` from the top bar. |
28 |
| -5. Enter a `name` for you application, ex. "Adal JS sample", choose `Web app/API` for `application type`, and enter `http://localhost:8081` for the `Sign-on URL`. Finally click `create` at the bottom of the screen. |
29 |
| -6. Save the `Application ID` from your new application registration. You will need it later in this sample. |
30 |
| -7. Grant permissions for Azure DevOps. Click `Settings` -> `Required permissions` -> `add` -> `1 Select an API` -> type in and select `Azure DevOps (Microsoft Visual Studio Team Services)` -> check the box for `Delegated Permissions` -> click `Select` -> click `Done` -> click `Grant Permissions` -> click `Yes`. |
31 |
| -8. Click on `Manifest` -> set the value for `oauth2AllowImplicitFlow` to `true` -> click `Save`. |
| 44 | +## Step 3: Configure the app to use your app registration |
32 | 45 |
|
33 |
| -## Step 3: Run the sample |
| 46 | +Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code. |
34 | 47 |
|
35 |
| -1. Open `index.html` in [Visual Studio Code](https://code.visualstudio.com/download) or another text editor or IDE. |
36 |
| -2. Inside `index.html` there is a section called `Input Vars` you will need to update to run the sample: |
37 |
| - * `clientId` - (Required) update this with the `application id` you saved from step 2.6 above |
38 |
| - * `replyUri` - (optional) In single page apps this should be the app URL itself. We have set this to `http://localhost:8081`(where we will host our app), but if you are hosting your app at another URL please change this value and the reply URI in `portal.azure.com` |
39 |
| - * `logoutredirectUri` - (optional) update if you are hosting your app at a location other than `http://localhost:8081` |
40 |
| - * `azureDevOpsApi` - (Required) update this with your Azure DevOps/TFS collection, e.g. http://dev.azure.com/organization/_apis/projects?api-version=4.0 for Azure DevOps or http://myserver:8080/tfs/DefaultCollection/_apis/projects?api-version=4.0 for TFS. If you would like the sample to run a different Azure DevOps API please change the entire string. [Learn more about other Azure DevOps REST API's](https://docs.microsoft.com/en-us/rest/api/vsts/?view=vsts-rest-4.1&viewFallbackFrom=vsts) |
41 |
| - * `azureDevOpsResourceId` - Do not change this value. It is used to receive Azure DevOps ADAL authentication tokens |
42 |
| -3. Navigate to the ADAL JS sample in cloned repo `vsts-auth-samples/JavascriptWebAppSample/` and start your http-server and set it to serve pages at `localhost:8081` which will by default serve `index.html` at `http://localhost:8081`. |
43 |
| -4. Navigate to `http://localhost:8081`. Sign in with a user account from your AAD tenant which has access to the VSTS account specified in the `azureDevOpsApi`. All projects contained in the account should be displayed. |
| 48 | +> In the steps below, "ClientID" is the same as "Application ID" or "AppId". |
44 | 49 |
|
| 50 | +1. Open the `config.js` file |
| 51 | +1. Find the key `Enter_the_Application_Id_Here` and replace the existing value with the application ID (clientId) of the `devops-js-app` application copied from the Azure portal. |
| 52 | +1. Find the key `https://login.microsoftonline.com/Enter_the_Tenant_Info_Here` and replace the existing value with `https://login.microsoftonline.com/<your-tenant-id>`. |
| 53 | +1. Find the key `Enter_the_Redirect_Uri_Here` and replace the existing value with the base address of the `devops-js-app` project (by default `http://localhost:8081`). |
45 | 54 |
|
| 55 | +## Running the sample |
| 56 | + |
| 57 | +```console |
| 58 | + npm start |
| 59 | +``` |
46 | 60 |
|
| 61 | +## Explore the sample |
47 | 62 |
|
| 63 | +1. Open your browser and navigate to `http://localhost:8081`. |
| 64 | +1. Click the **sign-in** button on the top right corner. |
| 65 | +1. Once signed-in, select the **Call DevOps Rest API** button at the center. |
0 commit comments