Hi,
I have a azure function with Azure AD authentication. I am not sure on how to trigger the function from PCF component. I tried using msal.js, but that did not work. Azure function is hosted in the same tenant as that of CRM in which I've deployed the PCF. PFB the code I tried. Could you guys help me to authenticate to call the function app.
Here I try to get the token and then planning to use the token with header in the call to Azure Function. I also like to aquire a token without a login popup if possible.
import { AxiosRequestConfig } from 'axios';
import * as Msal from "msal";
var appConfig = {
genScope: ["https://url.azurewebsites.net/user_impersonation"]
};
export const msalConfig: Msal.Configuration = {
auth: {
clientId: 'bd73****-****-****-****-****74179bb',
authority: "https://login.microsoftonline.com/0417****-****-****-****-****0dfff06c",
validateAuthority: false,
redirectUri: "https://url.azurewebsites.net/.auth/login/aad/callback"
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: true
}
};
const msalInstance = new Msal.UserAgentApplication(msalConfig);
const loginRequest = {
scopes: appConfig.genScope
};
const tokenRequest = {
scopes: appConfig.genScope
};
function signIn() {
msalInstance.loginPopup(loginRequest).then(function (loginResponse) {
getToken(tokenRequest).then(tk => {
console.log(tk);
});
}).catch(function (error) {
console.log(error);
});
}
//acquire a token silently
function getToken(tokenRequest: any) {
return msalInstance.acquireTokenSilent(tokenRequest).catch(function (error) {
console.log("aquire token popup");
// fallback to interaction when silent call fails
return msalInstance.acquireTokenPopup(tokenRequest).then(function (tokenResponse) {
}).catch(function (error) {
console.log("Failed token acquisition", error);
});
});
}
msalInstance.handleRedirectCallback((error, response) => {
// handle redirect response or error
});
export const getTokens = (): Promise<any> => {
return getToken(tokenRequest);
}
export const runFunction = (): Promise<any> => {
const requestConfig: AxiosRequestConfig = {
url: "https://url.azurewebsites.net",
method: "GET"
};
const token = getTokens();
// use token and call Azure Function APP
return Promise.resolve(true);
}