Skip to content

Commit

Permalink
Needs rework, blocked on AdminConsent application
Browse files Browse the repository at this point in the history
  • Loading branch information
robgruen committed Oct 31, 2024
1 parent 33ea716 commit 37aea5c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
2 changes: 2 additions & 0 deletions ts/packages/shell/src/main/azureSpeech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export class AzureSpeech {

this.token = result.token;

console.log(this.token);

return result;
};

Expand Down
5 changes: 3 additions & 2 deletions ts/packages/shell/src/renderer/src/auth/authConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const msalConfig = {
auth: {
// 'Application (client) ID' of app registration in Azure portal - this value is a GUID
clientId: "de5757b7-986f-4f02-aea1-395670da6da0",
//clientId: "04b07795-8ddb-461a-bbee-02f9e1bf7b46",
// Full directory URL, in the form of https://login.microsoftonline.com/<tenant-id>
authority: "https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47",
// Full redirect URL, in form of http://localhost:3000
Expand Down Expand Up @@ -53,14 +54,14 @@ export const msalConfig = {
* https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent#openid-connect-scopes
*/
export const loginRequest = {
scopes: ["User.Read"]
scopes: ["https://internal.cognitiveservices.azure.us/user_impersonation"],
};

/**
* Add here the scopes to request when obtaining an access token for MS Graph API. For more information, see:
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/resources-and-scopes.md
*/
export const tokenRequest = {
scopes: [],
scopes: ["https://cognitiveservices.azure.com/.default"],
forceRefresh: false // Set this to "true" to skip a cached token and go to the server to get a new token
};
44 changes: 30 additions & 14 deletions ts/packages/shell/src/renderer/src/auth/authPopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import * as msal from "@azure/msal-browser";
import { AuthResponseCallback } from "./authRedirect.js";
import { loginRequest, msalConfig } from "./authConfig.js";
import { loginRequest, msalConfig, tokenRequest } from "./authConfig.js";

export class SPAAuthPopup {

Expand Down Expand Up @@ -119,7 +119,7 @@ export class SPAAuthPopup {
*/

this.myMSALObj.loginPopup(loginRequest)
.then((response) => {
.then(async (response) => {
if (response !== null) {
this.username = response.account.username;
this.token = response.accessToken;
Expand All @@ -129,22 +129,28 @@ export class SPAAuthPopup {
} else {
this.selectAccount();

/**
* If you already have a session that exists with the authentication server, you can use the ssoSilent() API
* to make request for tokens without interaction, by providing a "login_hint" property. To try this, comment the
* line above and uncomment the section below.
*/
// /**
// * If you already have a session that exists with the authentication server, you can use the ssoSilent() API
// * to make request for tokens without interaction, by providing a "login_hint" property. To try this, comment the
// * line above and uncomment the section below.
// */
// this.myMSALObj.ssoSilent({loginHint: this.username})
// .then((response) => {
// this.username = response.account.username;
// this.token = response.accessToken;
// this.expires = response.expiresOn;

// myMSALObj.ssoSilent(silentRequest).
// then((response) => {
// welcomeUser(response.account.username);
// updateTable(response.account);
// // welcomeUser(response.account.username);
// // updateTable(response.account);
// }).catch(error => {
// console.error("Silent Error: " + error);
// if (error instanceof msal.InteractionRequiredAuthError) {
// signIn();
// this.signIn();
// }
// });

// let r = await this.myMSALObj.acquireTokenSilent(tokenRequest);
// console.log(r);
}
})
.catch(error => {
Expand All @@ -162,18 +168,28 @@ export class SPAAuthPopup {
// Choose which account to logout from by passing a username.
const logoutRequest = {
account: this.myMSALObj.getAccountByUsername(this.username),
mainWindowRedirectUri: '/signout'
mainWindowRedirectUri: '/'
};

this.myMSALObj.logoutPopup(logoutRequest);
}

getToken() { //: Promise<msal.AuthenticationResult | undefined | void> {
async getToken() { //: Promise<msal.AuthenticationResult | undefined | void> {

if (new Date() < this.expires! && this.token.length > 0) {
//return this.token;
}

try {
this.myMSALObj.setActiveAccount(this.myMSALObj.getAllAccounts()[0]);
let r = await this.myMSALObj.acquireTokenSilent(tokenRequest);
console.log(r);
} catch(error) {
if (error instanceof msal.InteractionRequiredAuthError) {
this.signIn();
}
};

return {
token: this.token,
expire: Number(this.expires),
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/shell/src/renderer/src/azureSpeech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ export class AzureSpeech {
// return { token: "", expire: Date.now(), region: this.region, endpoint: this.endpoint};


return new Promise<TokenResponse>((resolve) => {
resolve(SPAAuthPopup.getInstance().getToken());
return new Promise<TokenResponse>(async (resolve) => {
resolve(await SPAAuthPopup.getInstance().getToken());
});
};

Expand Down

0 comments on commit 37aea5c

Please sign in to comment.