У меня есть Node.js API, который защищен с помощью BearerStrategy Passport-Azure-Ad.
У меня есть еще одно клиентское приложение, которое предоставляет мне токен для доступа к Microsoft Graph.
Я использовал этот токен для доступа к Microsoft Graph и его правильной работы.
Но когда я использую тот же токен для аутентификации в своем API, я получаю эту ошибку.
Тело ответа: Несанкционированный
Статус ответа: 401
Это моя конфигурация паспорта для защиты API
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const passport = require('passport');
const OIDCBearerStrategy = require('passport-azure-ad').BearerStrategy;
const app = express();
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan(':method :url :date :remote-addr :status :response-time'));
app.use(passport.initialize());
app.use(passport.session());
const passportConfiguration = {
clientID: <<<Application ID>>>,
identityMetadata: 'https://login.microsoftonline.com/<<<Tenant ID>>>/.well-known/openid-configuration',
audience: 'https://graph.microsoft.com', // I've even tried setting this to my App ID URI of Application registration
validateIssuer: true, // if you have validation on, you cannot have users from multiple tenants sign in
passReqToCallback: false,
loggingLevel: 'info',
};
const OIDCStrategy = new OIDCBearerStrategy(passportConfiguration, (token, done) => {
console.log('Verifying the User...');
console.log('Access Token:', token);
done(null, token);
});
passport.use(OIDCStrategy);
app.get('/', (req, res) => {
res.status(200).send('Welcome!');
});
app.get('/users', passport.authenticate('oauth-bearer', {
session: false,
}), (req, res) => {
res.status(200).send('Take users...');
});
app.listen(8082, () => {
console.log('App is running on http://localhost:8082');
});
Поэтому, когда я попадаю на домашнюю страницу '/', отображается правильный ответ, поскольку он не покрыт слоем защиты паспорта.
Но когда я нажму http://localhost:8082/users
с заголовком:
Авторизация: Носитель << >>
Возвращается неавторизованным со статусом 401.
Но я попытался получить доступ к Microsoft Graph с помощью того же токена, и я получил ответ.
Это журнал паспорта:
{"name":"AzureAD: Metadata Parser","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"Parsing JSON retreived from the endpoint","time":"2019-01-15T07:54:36.374Z","v":0}
{"name":"AzureAD: Metadata Parser","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"Request to update the Open ID Connect Metadata","time":"2019-01-15T07:54:36.378Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"In Strategy.prototype.authenticate: received metadata","time":"2019-01-15T07:54:36.637Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"In Strategy.prototype.authenticate: we will validate the options","time":"2019-01-15T07:54:36.640Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"In Strategy.prototype.authenticate: access_token is received from request header","time":"2019-01-15T07:54:36.643Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"In Strategy.prototype.jwtVerify: token is decoded","time":"2019-01-15T07:54:36.649Z","v":0}
{"name":"AzureAD: Metadata Parser","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"working on key","time":"2019-01-15T07:54:36.655Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"PEMkey generated","time":"2019-01-15T07:54:36.666Z","v":0}
{"name":"AzureAD: Bearer Strategy","hostname":"Deadman-INC","pid":5036,"level":30,"msg":"authentication failed due to: In Strategy.prototype.jwtVerify: cannot verify token","time":"2019-01-15T07:54:36.675Z","v":0}
Может ли кто-нибудь помочь мне в этом?