Skip to content

Commit

Permalink
modified to add a max-age value and a same-site value to cookie.
Browse files Browse the repository at this point in the history
  • Loading branch information
treefrogframework committed Feb 6, 2020
1 parent 7522bc4 commit 6b6e51b
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 16 deletions.
4 changes: 4 additions & 0 deletions defaults/application.ini
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ Session.CookieDomain=
# Specifies a path attribute to set in the session cookie. Defaults to /.
Session.CookiePath=/

# Specifies a value to assert that a cookie must not be sent with cross-origin
# requests; Strict, Lax or None.
Session.CookieSameSite=Lax

# Probability that the garbage collection starts.
# If 100 specified, the GC of sessions starts at the rate of once per 100
# accesses. If 0 specified, the GC never starts.
Expand Down
2 changes: 1 addition & 1 deletion include/TCookie
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#include "tcookiejar.h"
#include "tcookie.h"
2 changes: 1 addition & 1 deletion include/tcookie.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#include "../src/tcookiejar.h"
#include "../src/tcookie.h"
3 changes: 2 additions & 1 deletion src/corelib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ HEADERS += toption.h
SOURCES += toption.cpp
HEADERS += ttemporaryfile.h
SOURCES += ttemporaryfile.cpp
HEADERS += tcookie.h
SOURCES += tcookie.cpp
HEADERS += tcookiejar.h
SOURCES += tcookiejar.cpp
HEADERS += tsession.h
Expand Down Expand Up @@ -327,7 +329,6 @@ HEADERS += \
tfnamespace.h \
tfcore.h \
tfexception.h \
tcookie.h \
tdispatcher.h \
tloggerplugin.h \
tsessionobject.h \
Expand Down
9 changes: 3 additions & 6 deletions src/tactioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void TActionContext::execute(THttpRequest &request, int sid)
static const bool SessionAutoIdRegeneration = Tf::appSettings()->value(Tf::SessionAutoIdRegeneration).toBool();
static const QString SessionCookiePath = Tf::appSettings()->value(Tf::SessionCookiePath).toString().trimmed();
static const QString SessionCookieDomain = Tf::appSettings()->value(Tf::SessionCookieDomain).toString().trimmed();
static const QByteArray SessionCookieSameSite = Tf::appSettings()->value(Tf::SessionCookieSameSite).toByteArray().trimmed();

THttpResponseHeader responseHeader;

Expand Down Expand Up @@ -193,12 +194,8 @@ void TActionContext::execute(THttpRequest &request, int sid)
}
}());

QDateTime expire;
if (SessionCookieMaxAge > 0) {
expire = QDateTime::currentDateTime().addSecs(SessionCookieMaxAge);
}

currController->addCookie(TSession::sessionName(), currController->session().id(), expire, SessionCookiePath, SessionCookieDomain, false, true);
currController->addCookie(TSession::sessionName(), currController->session().id(), SessionCookieMaxAge,
SessionCookiePath, SessionCookieDomain, false, true, SessionCookieSameSite);

// Commits a transaction for session
commitTransactions();
Expand Down
21 changes: 19 additions & 2 deletions src/tactioncontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ bool TActionController::addCookie(const TCookie &cookie)
cookieJar.addCookie(cookie);
response.header().removeAllRawHeaders("Set-Cookie");
for (auto &ck : (const QList<TCookie>&)cookieJar.allCookies()) {
response.header().addRawHeader("Set-Cookie", ck.toRawForm());
response.header().addRawHeader("Set-Cookie", ck.toRawForm(QNetworkCookie::Full));
}
return true;
}
Expand All @@ -142,14 +142,31 @@ bool TActionController::addCookie(const TCookie &cookie)
Adds the cookie to the internal list of cookies.
*/
bool TActionController::addCookie(const QByteArray &name, const QByteArray &value, const QDateTime &expire,
const QString &path, const QString &domain, bool secure, bool httpOnly)
const QString &path, const QString &domain, bool secure, bool httpOnly,
const QByteArray &sameSite)
{
TCookie cookie(name, value);
cookie.setExpirationDate(expire);
cookie.setPath(path);
cookie.setDomain(domain);
cookie.setSecure(secure);
cookie.setHttpOnly(httpOnly);
cookie.setSameSite(sameSite);
return addCookie(cookie);
}


bool TActionController::addCookie(const QByteArray &name, const QByteArray &value, qint64 maxAge, const QString &path,
const QString &domain, bool secure, bool httpOnly, const QByteArray &sameSite)
{
TCookie cookie(name, value);
cookie.setMaxAge(maxAge);
cookie.setExpirationDate(QDateTime::currentDateTime().addSecs(maxAge)); // For IE11
cookie.setPath(path);
cookie.setDomain(domain);
cookie.setSecure(secure);
cookie.setHttpOnly(httpOnly);
cookie.setSameSite(sameSite);
return addCookie(cookie);
}

Expand Down
3 changes: 2 additions & 1 deletion src/tactioncontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class T_CORE_EXPORT TActionController : public QObject, public TAbstractControll
TSession &session() { return sessionStore; }
void setSession(const TSession &session);
bool addCookie(const TCookie &cookie);
bool addCookie(const QByteArray &name, const QByteArray &value, const QDateTime &expire = QDateTime(), const QString &path = QString(), const QString &domain = QString(), bool secure = false, bool httpOnly = false);
bool addCookie(const QByteArray &name, const QByteArray &value, const QDateTime &expire = QDateTime(), const QString &path = QString(), const QString &domain = QString(), bool secure = false, bool httpOnly = false, const QByteArray &sameSite = "Lax");
bool addCookie(const QByteArray &name, const QByteArray &value, qint64 maxAge, const QString &path = QString(), const QString &domain = QString(), bool secure = false, bool httpOnly = false, const QByteArray &sameSite = "Lax");
QByteArray contentType() const;
void setContentType(const QByteArray &type);
bool render(const QString &action = QString(), const QString &layout = QString());
Expand Down
1 change: 1 addition & 0 deletions src/tappsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class AttributeMap : public QMap<int, QString>
insert(Tf::SessionCookieMaxAge, "Session.CookieMaxAge");
insert(Tf::SessionCookieDomain, "Session.CookieDomain");
insert(Tf::SessionCookiePath, "Session.CookiePath");
insert(Tf::SessionCookieSameSite, "Session.CookieSameSite");
insert(Tf::SessionGcProbability, "Session.GcProbability");
insert(Tf::SessionGcMaxLifeTime, "Session.GcMaxLifeTime");
insert(Tf::SessionSecret, "Session.Secret");
Expand Down
35 changes: 34 additions & 1 deletion src/tcookie.h
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
#include "tcookiejar.h"
#ifndef TCOOKIE_H
#define TCOOKIE_H

#include <TGlobal>
#include <QNetworkCookie>


class TCookie : public QNetworkCookie
{
public:
TCookie(const QByteArray &name = QByteArray(), const QByteArray &value = QByteArray());
TCookie(const TCookie &other);
TCookie(const QNetworkCookie &other);
~TCookie() {}

TCookie &operator=(const TCookie &other);
qint64 maxAge() const { return _maxAge; }
void setMaxAge(qint64 maxAge) { _maxAge = maxAge; }
QByteArray sameSite() const { return _sameSite; }
bool setSameSite(const QByteArray &sameSite);

void swap(TCookie &other);
QByteArray toRawForm(QNetworkCookie::RawForm form = QNetworkCookie::Full) const;
bool operator!=(const TCookie &other) const;
bool operator==(const TCookie &other) const;

static QList<TCookie> parseCookies(const QByteArray &cookieString);

private:
qint64 _maxAge {INT64_MIN};
QByteArray _sameSite;
};

#endif // TCOOKIE_H
4 changes: 1 addition & 3 deletions src/tcookiejar.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

#include <QList>
#include <QString>
#include <QNetworkCookie>
#include <TCookie>
#include <TGlobal>

using TCookie = QNetworkCookie;

#ifdef Q_CC_MSVC
extern uint qHash(const TCookie &key);
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/tfnamespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ namespace Tf
CacheBackend,
CacheGcProbability,
CacheEnableCompression,
//
SessionCookieSameSite,
};

// Reason codes why a web socket has been closed
Expand Down

0 comments on commit 6b6e51b

Please sign in to comment.