这个中间层主要是为了登出功能所设计,外部使用非常简单authPaypal.logout(options, (err, actor) => {}),即可完成登出效果,这里简要的分析下该中间层做了什么事,整段代码自上而下分为三段(取➡️删➡️发),下面逐一介绍下
- 取
由getTokenPayload函数完成,主要负责构建tokenPayload(object)和tokenCookies(array)这两个重要属性,tokenCookies记录了所有的cookieid,之后会被直接删除,具体主要和以下几个cookie有关,tokenPayload则记载着cookie的详细信息用于之后调用服务消去token
const userSessionCookie = req.cookies[CookieIds.WEBSCR_LOGIN_COOKIE_NAME];
const accessTokenCookie = getULATCookie(req) || req.cookies[CookieIds.ACCESS_TOKEN_COOKIE_NAME];
const idTokenCookie = req.cookies[CookieIds.ID_TOKEN_COOKIE_NAME];
const refreshTokenCookie = req.cookies[CookieIds.REFRESH_TOKEN_COOKIE_NAME];
const rememberedUserCookie = req.cookies[CookieIds.REMEMBERED_USER_COOKIE];
const loginEmailCookie = req.cookies[CookieIds.LOGIN_EMAIL_COOKIE];
const udtCookie = req.cookies[CookieIds.UDT];
- 删
这里非常简单,直接foreach循环挨个删就完事
if (tokenCookies) {
tokenCookies.forEach(function(cookieName) {
utils.removeCookie(req, req.res, cookieName);
});
}
- 发
指的是发送http请求去消除登陆的token,主要是根据tokenPayload(上面已经提到)以及fptiHeaderValue(根据tokenPayload中的某些特定项目以及trackingData获取,这两者是可能不存在的,但是不影响流程),之后便是发送http请求了,然后该中间层会重写req.securityContext,说说是重写,实际上是直接将securityContext清空了,毕竟登出操作,相当于init了,之后就简单了,主要是对一些返回值进行处理和一些日志的抛出
const actor = {
id: req.securityContext.actor.id,
party_id: req.securityContext.actor.party_id,
account_number: req.securityContext.actor.account_number,
auth_state: 'ANONYMOUS',
auth_claims: undefined,
};
// Reset SecurityContext
req.securityContext = {
actor,
subjects: [
{
subject: actor,
},
],
auth_token: undefined,
auth_token_type: 'SESSION_TOKEN',
global_session_id: req.securityContext.actor.actor_session_id,
last_validated: req.securityContext.last_validated,
};
Object.defineProperty(req.securityContext, 'consumer', {
enumerable: false,
configurable: false,
get() {
return actor;
},
});