Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return;
}
console.log(TAG, 'Device connected.', device_id);
// Update the socket on the device.
device.setGatewaySocket(socket, device_token);
// Return because the rest of this function is for connections to the client API.
return;
}
// Client API
const cookies = socket.handshake.headers.cookie ? cookie.parse(socket.handshake.headers.cookie) : {};
// TODO: Allow passing access_token through query params for non-browser clients (e.g. native apps).
if (!cookies.access_token) {
handleAuthenticationError('no access token');
return;
}
function verifyAccessToken () {
return new Promise((resolve, reject) => {
jwt.verify(cookies.access_token, jwt_secret, {issuer: config.api_token_issuer}, (error, claims) => {
if (error) {
reject();
handleAuthenticationError('invalid access token ' + error.name);
return;
}
// Add Set-Cookie headers before request ends
res.cookies = {};
res.before('headers', function() {
this.setHeader('Set-Cookie', Object.keys(res.cookies).map(function(name) {
return res.cookies[name].serialize();
}));
});
// No request cookies. Carry on.
if (!req.headers.cookie) {
req.cookies = {};
return next();
}
req.cookies = CookieCodec.parse(req.headers.cookie || '');
next();
});
};
var req = http.get(opts, function(resp){
var cookies = cookie.parse(resp.headers['set-cookie'].join('; '));
fn(null, cookies);
});
export function parse (
cookieHeader: string,
secretKey?: string,
): { signedCookies: { [key: string]: any }, plainCookies: { [key: string]: any } } {
const output = { signedCookies: {}, plainCookies: {} }
if (!cookieHeader) {
return output
}
const parsed = cookie.parse(cookieHeader)
return Object.keys(parsed).reduce((result, key: string) => {
const unpacked = unpack(parsed[key], secretKey)
if (unpacked === null) {
return result
}
if (unpacked.signed) {
result.signedCookies[key] = unpacked.value
} else {
result.plainCookies[key] = unpacked.value
}
return result
}, output)
}
function parseCookie(req: IncomingMessage): NowRequestCookies {
const header: undefined | string | string[] = req.headers.cookie;
if (!header) {
return {};
}
return parse(Array.isArray(header) ? header.join(';') : header);
}
export default () => {
const {
data: sharifyData,
data: { CURRENT_USER, CURRENT_URL },
} = sharify;
const token = CURRENT_USER && CURRENT_USER.authentication_token;
const currentRoute = { ...url.parse(CURRENT_URL || window.location.href) };
const isLoggedIn = !!(CURRENT_USER && CURRENT_USER.id);
const cookies = cookie.parse(document.cookie);
return {
token,
currentRoute,
isLoggedIn,
cookies,
serializedMe: serializedMe(CURRENT_USER),
sharifyData,
};
};
const setVtexIdAuthCookie = (ioContext: any, response: any, headers: any, authStatus: any) => {
if (authStatus === 'Success') {
const authAccount = `VtexIdclientAutCookie_${ioContext.account}`
const authCookie = parse(headers['set-cookie'].find((checkAuth: any) => {
return checkAuth.includes(authAccount)
}))
const VTEXID_EXPIRES = Math.round((new Date(authCookie.expires).getTime() - Date.now()) / 1000)
response.set('Set-Cookie', serialize(authAccount, authCookie[authAccount], {
httpOnly: true,
maxAge: VTEXID_EXPIRES,
path: '/',
secure: true
}))
}
return authStatus
}
/** Password must have at least eight characters with at least one number,
function parseCookie(cookieStr: string): { [key: string]: string }[] {
let options: { [s: string]: string } = {}
let cookies = []
let parsed = cookie.parse(cookieStr)
for (let attr in parsed) {
if (cookieAttributeNames.indexOf(attr) != -1) {
options[attr] = String(parsed[attr])
continue
}
cookies.push({ name: attr, value: parsed[attr] })
}
return cookies.map((c) => Object.assign(c, options))
}
function getMaxRefreshTime() {
var cookies = cookie.parse(document.cookie);
if (cookies['max_refresh_time']) {
return moment(cookies['max_refresh_time']).toDate();
} else {
return null;
}
}