Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then((res) => {
if (res.headers['set-cookie'] && profileId) { // TODO: I think I only implemented this because of SoFurry. Might be worth removing this logic.
const _cookies = setCookie.parse(res, {
decodeValues: false
});
const cookieSession = session.fromPartition(`persist:${profileId}`).cookies;
_cookies.forEach((c) => {
c.domain = c.domain || res.request.gotOptions.host;
const converted = _convertCookie(c);
const now = new Date();
converted.expirationDate = now.setMonth(now.getMonth() + 4); // add 4 months
cookieSession.set(converted, function(err) {
if (err) {
console.warn(err, this);
}
}.bind(converted));
});
}
resolve(res);
parse (setCookieStr = '', domain) {
// parse
var cookies = cookieParser.parse(cookieParser.splitCookiesString(setCookieStr))
// 转换为 Cookie 对象
return cookies.map((item) => {
if (!item.domain) item.domain = domain
return new Cookie(item)
})
}
set (setCookieStr = '') {
var cookie = cookieParser.parse(setCookieStr)[0]
if (cookie) {
Object.assign(this, cookie)
// 更新设置时间
this.dateTime = new Date()
}
return this
}
let success = true
try {
await this.http.post('http://sso.codoon.com/login', Object.assign({
forever: 'on',
app_id: 'www',
next: '/'
}, this.config), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
// 禁止 302,不然拿不到 cookie
maxRedirects: 0
})
this.log('咕咚登录成功')
} catch (err) {
const cookies = setCookie.parse(err.response) || []
const sessionIdCookie = cookies.find(c => c.name === 'sessionid')
if (sessionIdCookie) {
const sessionId = sessionIdCookie.value
this.log('咕咚登录成功,sessionid=' + sessionId)
await this.cache({ sessionId })
} else {
success = false
this.log('咕咚登录失败')
}
}
return success
}
it('should return status 401', async function () {
const res = await agent.get('/login');
let setCookieHeader = res.headers['set-cookie'];
if (setCookieHeader.length === 1) {
setCookieHeader = setCookieParser.splitCookiesString(setCookieHeader[0]);
}
const cookies = setCookieParser.parse(setCookieHeader, { map: true });
const cookie = Object.values(cookies).map(c => c.name + '=' + c.value).join('; ');
const xsrfToken = cookies['XSRF-TOKEN'].value;
return request(app).post('/api/login')
.set('X-XSRF-TOKEN', xsrfToken)
.set('Cookie', cookie)
.send({ userName: 'administrator', password: 'invalidpassword' })
.expect(401);
});
});
it('should return a user object', async function () {
let res = await request(app).get('/login');
let setCookieHeader = res.headers['set-cookie'];
if (setCookieHeader.length === 1) {
setCookieHeader = setCookieParser.splitCookiesString(setCookieHeader[0]);
}
const cookies = setCookieParser.parse(setCookieHeader, { map: true });
const cookie = Object.values(cookies).map(c => c.name + '=' + c.value).join('; ');
const xsrfToken = cookies['XSRF-TOKEN'].value;
res = await request(app).post('/api/login')
.set('X-XSRF-TOKEN', xsrfToken)
.set('Cookie', cookie)
.send({ userName: 'administrator', password: 'urungi' })
.expect(200);
expect(res.body).toHaveProperty('user');
expect(res.body.user).toHaveProperty('roles');
expect(res.body.user.roles).toContain('ADMIN');
});
});
.expect(function (response) {
let cookies = cookieParser.parse(response);
anotherSessionCookie = cookies.find(cookie => cookie.name === 'session');
});
});
async function login (agent, username = 'administrator', password = 'urungi') {
const res = await agent.get('/login');
const cookies = setCookieParser.parse(res, { map: true });
const xsrfToken = cookies['XSRF-TOKEN'].value;
await agent.post('/api/login')
.set('X-XSRF-TOKEN', xsrfToken)
.send({ userName: username, password: password });
return xsrfToken;
}
async function login (app, username = 'administrator', password = 'urungi') {
const res = await request(app).get('/login');
let setCookieHeader = res.headers['set-cookie'];
if (setCookieHeader.length === 1) {
setCookieHeader = setCookieParser.splitCookiesString(setCookieHeader[0]);
}
const cookies = setCookieParser.parse(setCookieHeader, { map: true });
const cookie = Object.values(cookies).map(c => c.name + '=' + c.value).join('; ');
const xsrfToken = cookies['XSRF-TOKEN'].value;
const headers = {
'X-XSRF-TOKEN': xsrfToken,
Cookie: cookie,
};
await request(app).post('/api/login')
.set(headers)
.send({ userName: username, password: password });
return headers;
}
setCookies.forEach(headerValue => {
let parsed;
try {
parsed = setCookie.parse(headerValue);
} catch (err) {
return;
}
parsed.forEach(cookie => {
const { name, value, path, domain, expires, httpOnly, secure } = cookie;
const harCookie = {
name,
value,
httpOnly: httpOnly || false,
secure: secure || false
};
if (path) {
harCookie.path = path;
}
if (domain) {
harCookie.domain = domain;