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);
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);
});
});
.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;
}
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;
function copyCookies(obj) {
if ( !obj || !obj.headers ) {
return;
}
const headers = obj.headers.get('set-cookie');
if ( headers ) {
setCookieParser(headers).forEach((opts) => {
const name = opts.name;
const value = opts.value;
delete opts.name;
delete opts.value
opts.raw = true;
cookieSvc.write(name, value, opts);
});
}
}
},
function copyCookies(obj) {
if ( !obj || !obj.headers ) {
return;
}
const headers = obj.headers.get('set-cookie');
if ( headers ) {
setCookieParser(headers).forEach((opts) => {
const name = opts.name;
const value = opts.value;
delete opts.name;
delete opts.value
opts.raw = true;
cookieSvc.write(name, value, opts);
});
}
}
},