我正在使用 https://github.com/AzureAD/passport-azure-ad使用 Azure AD Graph API 的插件。
我的 package.json 中的依赖
"passport-azure-ad": "^3.0.12"
我引用了此处提供的示例:https://github.com/AzureADQuickStarts/WebApp-OpenIDConnect-NodeJS
我能够执行 auth
并获取 access_token
和 refresh_token
。但是最后一个参数是给出一个错误
TypeError: done is not a function
我的代码是使用 Typescript 编写的我的策略代码:
passport.use( new OIDCStrategy( {
identityMetadata: "https://login.microsoftonline.com/" + tenantName + "/v2.0/.well-known/openid-configuration",
clientID: clientID,
responseType: app_properties.responseType,
responseMode: app_properties.responseMode,
redirectUrl: app_properties.redirectUrl,
allowHttpForRedirectUrl: app_properties.allowHttpForRedirectUrl,
clientSecret: clientSecret,
validateIssuer: app_properties.validateIssuer,
isB2C: app_properties.isB2C,
issuer: app_properties.issuer,
passReqToCallback: app_properties.passReqToCallback,
scope: app_properties.scope,
loggingLevel: app_properties.loggingLevel,
nonceLifetime: app_properties.nonceLifetime,
nonceMaxAmount: app_properties.nonceMaxAmount,
useCookieInsteadOfSession: app_properties.useCookieInsteadOfSession,
cookieEncryptionKeys: app_properties.cookieEncryptionKeys,
clockSkew: app_properties.clockSkew
},
function ( iss: any, sub: any, profile: any, accessToken: any, refreshToken: any, done: any ) {
if ( iss ) {
console.log( 'iss' + JSON.stringify( iss ) );
}
if ( sub ) {
console.log( 'sub' + JSON.stringify( sub ) );
}
if ( accessToken ) {
console.log( 'Received accessToken ' + accessToken );
}
if ( refreshToken ) {
console.log( 'Received refreshToken ' + refreshToken );
}
if ( !profile.oid ) {
console.log( 'Received accessToken ' + accessToken );
return done( new Error( "No oid found" ), null );
}
if ( profile ) {
console.log( 'profile' + JSON.stringify( profile ) );
}
return done(null, profile);
} ) );
此外,根据文档,自定义状态参数不保留状态字符串
customState: if you want to use a custom state value instead of a randomly generated one
为什么 done
不起作用?另外,如何维护状态?
下面是我的应用服务器中的重定向 URL 调用端点函数
export function authAzureAd( req: Request, res: Response, next: any ) {
passport.authenticate( 'azuread-openidconnect' , function ( err: any, user: any, info: any ) {
if ( err ) {
return next( err );
}
if ( !user ) {
return res.send( { success: false, message: 'authentication failed' } );
}
console.info( 'user: ' + JSON.stringify( user ) );
return res.send( { success: true, message: 'authentication succeeded' } );
} )( req, res, next );
} catch ( err ) {
console.info( err );
res.status( 500 ).send( { message: err.message } );
}
}
Auth Callback POST API端点函数调用如下:
export function authCallback(req: Request, res: Response, next: any) {
console.info('In the AzureAD callback, next = '+next);
if (next == null) { // If we want something else to happen here, we can do it
next = () => {};
}
try {
passport.authenticate('azuread-openidconnect', { failureRedirect: '/login', customState: 'my_state', successRedirect: '/dashboard' })(req, res, next)
} catch (err) {
console.info(err);
res.status(500).send({ message: err.message });
}
}
更新
1] 关于 TypeError: done is not a function
我更新了 package.json 以具有依赖性 "passport-azure-ad": "4.0.0"
而不是 "passport-azure-ad": "^3.0.12"
然后 done
开始使用 process.nextTick()
中定义的the documentation. 中的示例验证回调
2] 关于customState
我在req.body.state
中获取状态参数,而不是在req.query.state
中。 See this question. azure-ad 的 auth 回调的原因恰好是 HTTP POST,而不是像其他 OAuth 流程中的 HTTP GET。因此,状态是 body 的一部分。
最佳答案
更新
1] 关于 TypeError: done is not a function
我更新了 package.json 以具有依赖性 "passport-azure-ad": "4.0.0"
而不是 "passport-azure-ad": "^3.0.12"
然后 done
开始使用 process.nextTick()
中定义的the documentation. 中的示例验证回调
2] 关于customState
我在req.body.state
中获取状态参数,而不是在req.query.state
中。 See this question. azure-ad 的 auth 回调的原因恰好是 HTTP POST,而不是像其他 OAuth 流程中的 HTTP GET。因此,状态是 body 的一部分。
关于node.js - OIDCStrategy passport-azure-ad passport js 有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57921742/