Commit 1e3fe909 authored by Matija Obreza's avatar Matija Obreza
Browse files

Fix: set correct Auth cookie expiry times

parent 6cecf9fb
Pipeline #7417 passed with stages
in 8 minutes and 31 seconds
...@@ -24,10 +24,10 @@ export default function checkAuthToken(req, res, dispatch) { ...@@ -24,10 +24,10 @@ export default function checkAuthToken(req, res, dispatch) {
console.log('Init checkAuthToken method'); console.log('Init checkAuthToken method');
return checkAuthTokenRequest(req, dispatch) return checkAuthTokenRequest(req, dispatch)
.then((data) => { .then((data) => {
console.log(`Setting cookie to expire in ${data.exp}ms from`, data); console.log(`Setting cookie to expire in ${(data.exp || data.expires_in) / 60}min from`, data);
res.cookie('access_token', data.access_token, { path: '/', maxAge: data.exp || data.expires_in || /* 1hr */ 1000 * 60 * 60 }); res.cookie('access_token', data.access_token, { path: '/', expires: new Date(data.exp * 1000 || new Date().getTime() + ((data.expires_in * 1000) || (/* 1hr */ 1000 * 60 * 60))) });
if (data.authorities) { if (data.authorities) {
res.cookie('authorities', JSON.stringify(data.authorities), { path: '/', maxAge: data.exp || data.expires_in || /* 1hr */ 1000 * 60 * 60 }); res.cookie('authorities', JSON.stringify(data.authorities), { path: '/', expires: new Date(data.exp * 1000 || new Date().getTime() + ((data.expires_in * 1000) || (/* 1hr */ 1000 * 60 * 60))) });
} else { } else {
res.clearCookie('authorities'); res.clearCookie('authorities');
} }
......
...@@ -12,7 +12,8 @@ export function checkAccessTokens(dispatch) { ...@@ -12,7 +12,8 @@ export function checkAccessTokens(dispatch) {
const applicationLogin = () => const applicationLogin = () =>
LoginService.loginApp() LoginService.loginApp()
.then((data) => { .then((data) => {
saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp); // console.log('loginApp token', data);
saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
dispatch(loginApp(data)); dispatch(loginApp(data));
}) })
.catch((error) => { .catch((error) => {
...@@ -75,7 +76,8 @@ export const loginRequest = (username, password) => (dispatch) => { ...@@ -75,7 +76,8 @@ export const loginRequest = (username, password) => (dispatch) => {
return LoginService.login(username, password) return LoginService.login(username, password)
.then((data) => { .then((data) => {
saveCookies(data, data.exp); // console.log('User login token', data);
saveCookies(data, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
return dispatch(loginUser(data)); return dispatch(loginUser(data));
}); });
}; };
...@@ -103,7 +105,8 @@ export const verifyGoogleTokenRequest = (accessToken) => (dispatch, getState) => ...@@ -103,7 +105,8 @@ export const verifyGoogleTokenRequest = (accessToken) => (dispatch, getState) =>
return LoginService.verifyGoogleToken(token, accessToken) return LoginService.verifyGoogleToken(token, accessToken)
.then((data) => { .then((data) => {
saveCookies(data, data.exp); // console.log('Verified Google token', data);
saveCookies(data, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
return dispatch(loginApp(data)); return dispatch(loginApp(data));
}); });
}; };
......
...@@ -124,7 +124,8 @@ class Header extends React.Component<IHeaderProps | any, any> { ...@@ -124,7 +124,8 @@ class Header extends React.Component<IHeaderProps | any, any> {
this.props.logoutRequest() this.props.logoutRequest()
.then(() => this.props.loginAppRequest()) .then(() => this.props.loginAppRequest())
.then((data) => { .then((data) => {
saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp); // console.log('Logout', data);
saveCookies({access_token: data.access_token, authorities: [ROLE_CLIENT]}, data.exp * 1000 || new Date().getTime() + data.expires_in * 1000);
this.props.history.push('/login'); this.props.history.push('/login');
}); });
} }
......
import * as React from 'react'; import * as React from 'react';
import {connect} from 'react-redux'; import { connect } from 'react-redux';
import {bindActionCreators} from 'redux'; import { bindActionCreators } from 'redux';
import {saveCookies} from 'utilities';
import * as _ from 'lodash'; import * as _ from 'lodash';
import {log} from 'utilities/debug'; import { log } from 'utilities/debug';
import {loginRequest, checkTokenRequest} from 'actions/login'; import { loginRequest, checkTokenRequest } from 'actions/login';
import ContentHeader from 'ui/common/heading/ContentHeader'; import ContentHeader from 'ui/common/heading/ContentHeader';
...@@ -33,7 +32,6 @@ class LoginContainer extends React.Component<ILoginContainerProps, void> { ...@@ -33,7 +32,6 @@ class LoginContainer extends React.Component<ILoginContainerProps, void> {
return checkTokenRequest(access_token); return checkTokenRequest(access_token);
}) })
.then((data) => { .then((data) => {
saveCookies(data, data.exp);
history.push('/dashboard'); history.push('/dashboard');
return false; return false;
}).catch((e) => { }).catch((e) => {
......
...@@ -96,10 +96,11 @@ export function cleanFilters(filter, keysToSkip?): string { ...@@ -96,10 +96,11 @@ export function cleanFilters(filter, keysToSkip?): string {
return result; return result;
} }
export function saveCookies(resp, exp: number) { export function saveCookies(resp, expireOn: number) {
log(`Saving cookies to expire after ${exp}ms`); const expDate = new Date(expireOn);
cookies.set('access_token', resp.access_token, { path: '/', expires: new Date(new Date().getTime() + exp) }); log(`Saving cookies to expire on ${expDate}`);
cookies.set('authorities', JSON.stringify(resp.authorities), { path: '/', expires: new Date(new Date().getTime() + exp) }); cookies.set('access_token', resp.access_token, { path: '/', expires: expDate });
cookies.set('authorities', JSON.stringify(resp.authorities), { path: '/', expires: expDate });
} }
export function clearCookies() { export function clearCookies() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment