I have a project application created using NodeJS + ReactJS, and all I want to do is use Azure AD to authenticate the user and retrieve their data such as name, groups, image, profession, etc. using the Graph API from Azure.
I already have Azure AD and the application is correctly configured on the Azure Portal. Including delegations of permits and all these employees.
I am trying to figure out what is the best way to do this, but to no avail. I tried to find in Google documents, StackOverflow, Microsoft Documentations, even sample projects.
Some samples worked, but none of them could understand and include work as a production application in my project.
I used this to authenticate the user, but the returned AccessToken is not valid for calling the Graph API:
passport.use(new OIDCStrategy({
redirectUrl: config.creds.returnURL,
realm: config.creds.realm,
clientID: config.creds.clientID,
clientSecret: config.creds.clientSecret,
oidcIssuer: config.creds.issuer,
identityMetadata: config.creds.identityMetadata,
skipUserProfile: config.creds.skipUserProfile,
responseType: config.creds.responseType,
responseMode: config.creds.responseMode,
allowHttpForRedirectUrl: config.creds.allowHttpForRedirectUrl
},
function(iss, sub, profile, accessToken, refreshToken, done) {
console.log(accessToken);
profile = profile._json;
if (!profile.email) {
return done(new Error("No email found"), null);
}
process.nextTick(function () {
findByEmail(profile.email, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
users.push(profile);
return done(null, profile);
}
return done(null, user);
});
});
}
));
And this gives me the AccessToken right to use the Graph API, but I cannot figure out how to use it instead of Passport:
function createAuthorizationUrl(state) {
var authorizationUrl = templateAuthzUrl.replace('<client_id>', sampleParameters.clientId);
authorizationUrl = authorizationUrl.replace('<redirect_uri>',redirectUri);
authorizationUrl = authorizationUrl.replace('<state>', state);
authorizationUrl = authorizationUrl.replace('<resource>', resource);
return authorizationUrl;
}
app.get('/auth', function(req, res) {
crypto.randomBytes(48, function(ex, buf) {
var token = buf.toString('base64').replace(/\//g,'_').replace(/\+/g,'-');
res.cookie('authstate', token);
var authorizationUrl = createAuthorizationUrl(token);
res.redirect(authorizationUrl);
});
});
app.get('/getAToken', function(req, res) {
if (req.cookies.authstate !== req.query.state) {
res.send('error: state does not match');
}
var authenticationContext = new AuthenticationContext(authorityUrl);
authenticationContext.acquireTokenWithAuthorizationCode(req.query.code, redirectUri, resource, sampleParameters.clientId, sampleParameters.clientSecret, function(err, response) {
var message = '';
if (err) {
message = 'error: ' + err.message + '\n';
}
message += 'response: ' + JSON.stringify(response);
if (err) {
res.send(message);
return;
}
authenticationContext.acquireTokenWithRefreshToken(response.refreshToken, sampleParameters.clientId, sampleParameters.clientSecret, resource, function(refreshErr, refreshResponse) {
if (refreshErr) {
message += 'refreshError: ' + refreshErr.message + '\n';
}
message += 'refreshResponse: ' + JSON.stringify(refreshResponse);
res.send(message);
});
});
});
If anyone could help me with some real-world example application, video, or something else, would be very good. I'm losing my mind to figure this out.
Thank!