我在https://github.com/AzureAD/passport-azure-ad/issues/427上问过这个问题,但没有得到回应。我觉得这是一个阻碍我完成我的工作的错误,所以我越来越广泛地去寻找答案。是我在做什么还是虫子?
我写的问题与以前不同,因为我已经对这个问题做了更多的调查(我会指出差异从哪里开始)。
护照-Azure-AD版本4.1.0 - https://www.npmjs.com/package/passport-azure-ad#52-bearerstrategy
我已经从文档中设置了这一点:
setup() {
const findById = (id, fn) => {
for (let i = 0, len = this.users.length; i < len; i++) {
const user = this.users[i];
if (user.sub === id) {
logger.info('Found user: ', user);
return fn(null, user);
}
}
return fn(null, null);
};
this.bearerStrategy = new BearerStrategy(jwtOptions,
(token: ITokenPayload, done: VerifyCallback) => {
findById(token.oid, (err, user) => {
if (err) {
return done(err);
}
if (!user) {
// 'Auto-registration'
logger.info('User was added automatically as they were new. Their oid is: ', token.oid);
this.users.push(token);
this.owner = token.oid;
return done(null, token);
}
this.owner = token.oid;
return done(null, user, token);
});
}
);
console.log(`setup bearerStrategy`);
}我使用的jwtOptions是:
备选方案如下:
const jwtOptions = {
identityMetadata: 'https://login.microsoftonline.com/xyz/v2.0/.well-known/openid-configuration',
clientID: '0123456789',
loggingLevel: 'info',
loggingNoPII: false,
passReqToCallback: false
};并使用以下方法运行身份验证(从middlewhere):
authenticate(request: express.Request) {
this.bearerStrategy.authenticate(request, {session: false});
}注意到,这与doco不同,因为他们所拥有的不起作用。
它在线路上失败了:
return done(null, token);通过以下方式:
[2019-05-29T13:49:33.479] [INFO ] [AUTHSERVICE_LOGGER] - User was added automatically as they were new. Their oid is: 123
.../translateboard/node_modules/passport-azure-ad/lib/bearerstrategy.js:565
return self.success(user, info);
^
TypeError: self.success is not a function
at verified (/Users/bbos/dev/dhs/translate/translateboard/node_modules/passport-azure-ad/lib/bearerstrategy.js:565:21)
at findById (/Users/bbos/dev/dhs/translate/translateboard/server/src/services/AuthService.ts:106:32)
at findById (/Users/bbos/dev/dhs/translate/translateboard/server/src/services/AuthService.ts:87:20)
at Strategy.bearerStrategy.passport_azure_ad_1.BearerStrategy [as _verify] (/Users/bbos/dev/dhs/translate/translateboard/server/src/services/AuthService.ts:97:17)
at jwt.verify (/Users/bbos/dev/dhs/translate/translateboard/node_modules/passport-azure-ad/lib/bearerstrategy.js:363:19)
at /Users/bbos/dev/dhs/translate/translateboard/node_modules/passport-azure-ad/lib/jsonWebToken.js:80:16
at process._tickCallback (internal/process/next_tick.js:61:11)这里的与最初的post不同
如果我在代码中放置了一个断点,那么BearerStrategy.js中的self对象就是:
{
"name": "oauth-bearer",
"_options": {
"identityMetadata": "https://login.microsoftonline.com/xyz/v2.0/.well-known/openid-configuration",
"clientID": "0123456789",
"loggingLevel": "info",
"loggingNoPII": false,
"passReqToCallback": false,
"clockSkew": 300,
"validateIssuer": true,
"allowMultiAudiencesInToken": false,
"audience": [
"1234",
"spn:1234"
],
"isB2C": false,
"_isCommonEndpoint": false,
"_verify" = (token, done) => {...},
"__proto__" = Strategy(...,
}
}在__proto__下有:
authenticate = function authenticateStrategy(req, options) {
constructor = function Strategy(options, verifyFn) {
failWithLog = function(message) {
jwtVerify = function jwtVerifyFunc(req, token, metadata, optionsToValidate, done) {
loadMetadata = function(params, next) {你可以看到在护照-Azure-广告中没有success。它确实定义了一个failWithLog https://github.com/AzureAD/passport-azure-ad/blob/e9684341920ac8ac41c55a1e7150d1765dced809/lib/bearerstrategy.js#L600 --他们忘记添加其他的了吗?
护照定义了这些其他人(https://github.com/jaredhanson/passport/blob/1c8ede35a334d672024e14234f023a87bdccaac2/lib/middleware/authenticate.js#L230),但是他们处于封闭状态,从未暴露出来。定义它们的父策略对象也不是。与外部的唯一连接是通过公开的身份验证方法https://github.com/jaredhanson/passport/blob/1c8ede35a334d672024e14234f023a87bdccaac2/lib/middleware/authenticate.js#L70。
然而,正如所见,Passport定义了它自己的身份验证方法(https://github.com/AzureAD/passport-azure-ad/blob/e9684341920ac8ac41c55a1e7150d1765dced809/lib/bearerstrategy.js#L372),并且从不调用pass吻合方法。
在我看来,它似乎从未起作用。
有人能证实或不同意吗?
我将更新在https://github.com/AzureAD/passport-azure-ad/issues/427的帖子,以参考这一点。
接下来,我将访问存储库git bisect,看看是否能够找到那些丢失的方法曾经被定义的地方的更改,或者其他一些突出的东西。
发布于 2019-05-31 06:31:29
我可以确认,当我的代码被编写时,它将永远不会起作用。有两个主要问题:
通径参数
根据我对这个问题的评论,我忽略了在问题中提供信息,因为我不认为它是相关的。但它是。
我使用的是TSED - TypeScript快捷解码器(https://tsed.io),它取代了以下表示中间件代码:
server.get('/api/tasks', passport.authenticate('oauth-bearer', { session: false }), listTasks);使用带注释的中间件类- https://tsed.io/docs/middlewares.html
因此,现在对passport.authenticate()的调用是在use()方法中进行的,就像我前面展示的那样(,这是不正确的):
@OverrideMiddleware(AuthenticatedMiddleware)
export class UserAuthMiddleware implements IMiddleware {
constructor(@Inject() private authService: AuthService) {
}
public use(
@EndpointInfo() endpoint: EndpointMetadata,
@Request() request: express.Request,
@Response() response: express.Response,
@Next() next: express.NextFunction
) {
const options = endpoint.get(AuthenticatedMiddleware) || {};
Passport.authenticate('oauth-bearer', {session: false}); // <-- WRONG
if (!request.isAuthenticated()) {
throw new Forbidden('Forbidden');
}
next();
}
}我忽略考虑的是,快递中间件被传递给请求对象。所以我真正需要的是:
Passport.authenticate('oauth-bearer', {session: false})(request, response, next); // <-- CORRECT必须使用Passport.use()
这些文件有误导性。考虑到我对护照并不过分挑剔,我对此没怎么想。
doco (http://www.passportjs.org/packages/passport-azure-ad/) (at 5.2.1.1 Sample using the BearerStrategy)说要使用:
var bearerStrategy = new BearerStrategy(options,
function(token, done) {
log.info('verifying the user');
log.info(token, 'was the token retreived');
findById(token.oid, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
// "Auto-registration"
log.info('User was added automatically as they were new. Their oid is: ', token.oid);
users.push(token);
owner = token.oid;
return done(null, token);
}
owner = token.oid;
return done(null, user, token);
});
}
);我知道,当描述其他策略时(例如同一页上的5.1 OIDCStrategy ):
passport.use(new OIDCStrategy({
identityMetadata: config.creds.identityMetadata,
clientID: config.creds.clientID,
...
},
function(iss, sub, profile, accessToken, refreshToken, done) {
...
}
));他们使用passport.use。我考虑了(当我第一次看到它的时候)每秒1/2的区别,并得出结论,如果登录是由Azure使用他们的msal.js库完成的,那么msal.js处理事情的方式是不同的。直到上面的Fix #1没有解决这个问题,我才重新讨论这个问题。
我的结论是,TSED项目需要更新他们的文档/样本(我会为他们做这件事);Passport Azure AD项目需要更新他们的文档/样本。
还有一些问题,我不知道是谁的错。我在Passport-Azure-Ad in TSED framework seems to run asynchronously上写过这些。
https://stackoverflow.com/questions/56372349
复制相似问题