You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/active-directory/develop/quickstart-v2-nodejs-desktop.md
+19-84Lines changed: 19 additions & 84 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -108,94 +108,29 @@ ms.custom: mode-api
108
108
>
109
109
> ### Requesting tokens
110
110
>
111
-
> In the first leg of authorization code flow with PKCE, prepare and send an authorization code request with the appropriate parameters. Then, in the second leg of the flow, listen for the authorization code response. Once the code is obtained, exchange it to obtain a token.
111
+
> You can use MSAL Node's acquireTokenInteractive public API to acquire tokens via an external user-agent such as the default system browser.
112
112
>
113
113
> ```javascript
114
-
> // The redirect URI you setup during app registration with a custom file protocol "msal"
115
-
> const redirectUri = "msal://redirect";
116
-
>
117
-
> const cryptoProvider = new CryptoProvider();
118
-
>
119
-
> const pkceCodes = {
120
-
> challengeMethod: "S256", // Use SHA256 Algorithm
121
-
> verifier: "", // Generate a code verifier for the Auth Code Request first
122
-
> challenge: "" // Generate a code challenge from the previously generated code verifier
123
-
> };
124
-
>
125
-
> /**
126
-
> * Starts an interactive token request
127
-
> * @param {object} authWindow: Electron window object
128
-
> * @param {object} tokenRequest: token request object with scopes
> successTemplate: '<h1>Successfully signed in!</h1> <p>You can close this window now.</p>',
125
+
> failureTemplate: '<h1>Oops! Something went wrong</h1> <p>Check the console for more information.</p>',
126
+
> });
127
+
>
128
+
> return authResponse;
129
+
> } catch (error) {
130
+
> throw error;
192
131
> }
193
132
> ```
194
-
>
195
-
> >|Where:| Description |
196
-
> >|---------|---------|
197
-
> >|`authWindow`| Current Electron window in process. |
198
-
> >|`tokenRequest`| Contains the scopes being requested, such as `"User.Read"`for Microsoft Graph or `"api://<Application ID>/access_as_user"`for custom web APIs. |
Copy file name to clipboardExpand all lines: articles/active-directory/develop/tutorial-v2-nodejs-desktop.md
+17-25Lines changed: 17 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,9 +40,9 @@ First, complete the steps in [Register an application with the Microsoft identit
40
40
Use the following settings for your app registration:
41
41
42
42
- Name: `ElectronDesktopApp` (suggested)
43
-
- Supported account types: **Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)**
43
+
- Supported account types: **Accounts in my organizational directory only (single tenant)**
44
44
- Platform type: **Mobile and desktop applications**
This preload script exposes a renderer methods to give the renderer process controlled access to some `Node APIs` by applying IPC channels that have been configured for communication between the main and renderer processes.
80
-
81
-
*CustomProtocolListener* class can be instantiated in order to register and unregister a custom typed protocol on which MSAL Node can listen for Auth Code responses.
79
+
This preload script exposes a renderer API to give the renderer process controlled access to some `Node APIs` by applying IPC channels that have been configured for communication between the main and renderer processes.
82
80
83
81
6. Finally, create a file named *constants.js* that will store the strings constants for describing the application **events**:
84
82
@@ -91,13 +89,11 @@ ElectronDesktopApp/
91
89
├── App
92
90
│ ├── AuthProvider.js
93
91
│ ├── constants.js
94
-
│ ├── CustomProtocolListener.js
95
-
│ ├── fetch.js
92
+
│ ├── graph.js
96
93
│ ├── index.html
97
94
| ├── main.js
98
95
| ├── preload.js
99
96
| ├── renderer.js
100
-
│ ├── UIManager.js
101
97
│ ├── authConfig.js
102
98
├── package.json
103
99
```
@@ -108,11 +104,17 @@ In *App* folder, create a file named *AuthProvider.js*. The *AuthProvider.js* fi
In the code snippet above, we first initialized MSAL Node `PublicClientApplication` by passing a configuration object (`msalConfig`). We then exposed `login`, `logout` and `getToken` methods to be called by main module (*main.js*). In `login` and `getToken`, we acquire ID and access tokens, respectively, by first requesting an authorization code and then exchanging this with a token using MSAL Node `acquireTokenByCode` public API.
107
+
In the code snippet above, we first initialized MSAL Node `PublicClientApplication` by passing a configuration object (`msalConfig`). We then exposed `login`, `logout` and `getToken` methods to be called by main module (*main.js*). In `login` and `getToken`, we acquire ID and access tokens using MSAL Node `acquireTokenInteractive` public API.
108
+
109
+
## Add Microsoft Graph SDK
110
+
111
+
Create a file named *graph.js*. The *graph.js* file will contain an instance of the Microsoft Graph SDK Client to facilitate accessing data on the Microsoft Graph API, using the access token obtained by MSAL Node:
Finally, create an environment file to store the app registration details that will be used when acquiring tokens. To do so, create a file named *authConfig.js* inside the root folder of the sample (*ElectronDesktopApp*), and add the following code:
117
+
Create an environment file to store the app registration details that will be used when acquiring tokens. To do so, create a file named *authConfig.js* inside the root folder of the sample (*ElectronDesktopApp*), and add the following code:
@@ -127,7 +129,7 @@ Fill in these details with the values you obtain from Azure app registration por
127
129
- `Enter_the_Cloud_Instance_Id_Here`: The Azure cloud instance in which your application is registered.
128
130
- For the main (or *global*) Azure cloud, enter `https://login.microsoftonline.com/`.
129
131
- For **national** clouds (for example, China), you can find appropriate values in [National clouds](authentication-national-cloud.md).
130
-
- `Enter_the_Redirect_Uri_Here`: The Redirect Uri of the application you registered `msal{Your_Application/Client_Id}:///auth`.
132
+
- `Enter_the_Redirect_Uri_Here`: The Redirect Uri of the application you registered `http://localhost`.
131
133
- `Enter_the_Graph_Endpoint_Here` is the instance of the Microsoft Graph API the application should communicate with.
132
134
- For the **global** Microsoft Graph API endpoint, replace both instances of this string with `https://graph.microsoft.com/`.
133
135
- For endpoints in **national** cloud deployments, see [National cloud deployments](/graph/deployments) in the Microsoft Graph documentation.
@@ -156,23 +158,13 @@ If you consent to the requested permissions, the web applications displays your
156
158
157
159
## Test web API call
158
160
159
-
After you sign in, select **See Profile** to view the user profile information returned in the response from the call to the Microsoft Graph API:
161
+
After you sign in, select **See Profile** to view the user profile information returned in the response from the call to the Microsoft Graph API. After consent, you'll view the messages returned in the response from the call to the Microsoft Graph API:
160
162
161
163
:::image type="content" source="media/tutorial-v2-nodejs-desktop/desktop-04-profile.png" alt-text="profile information from Microsoft Graph":::
162
164
163
-
Select **Read Mails** to view the messages in user's account. You'll be presented with a consent screen:
164
-
165
-
:::image type="content" source="media/tutorial-v2-nodejs-desktop/desktop-05-consent-mail.png" alt-text="consent screen for read.mail permission":::
166
-
167
-
After consent, you'll view the messages returned in the response from the call to the Microsoft Graph API:
168
-
169
-
:::image type="content" source="media/tutorial-v2-nodejs-desktop/desktop-06-mails.png" alt-text="mail information from Microsoft Graph":::
170
-
171
165
## How the application works
172
166
173
-
When a user selects the **Sign In** button for the first time, get `getTokenInteractive` method of *AuthProvider.js* is called. This method redirects the user to sign-in with the Microsoft identity platform endpoint and validates the user's credentials, and then obtains an **authorization code**. This code is then exchanged for an access token using `acquireTokenByCode` public API of MSAL Node.
174
-
175
-
At this point, a PKCE-protected authorization code is sent to the CORS-protected token endpoint and is exchanged for tokens. An ID token, access token, and refresh token are received by your application and processed by MSAL Node, and the information contained in the tokens is cached.
167
+
When a user selects the **Sign In** button for the first time, get `getTokenInteractive` method of *AuthProvider.js* is called. This method redirects the user to sign-in with the Microsoft identity platform endpoint and validates the user's credentials, obtains an **authorization code** and then exchanges that code for an ID token, access token, and refresh token. MSAL Node also caches these tokens for future use.
176
168
177
169
The ID token contains basic information about the user, like their display name. The access token has a limited lifetime and expires after 24 hours. If you plan to use these tokens for accessing protected resource, your back-end server *must* validate it to guarantee the token was issued to a valid user for your application.
0 commit comments