User AuthModel::processAuthToken() { WApplication *app = WApplication::instance(); const WEnvironment& env = app->environment(); if (baseAuth()->authTokensEnabled()) { const std::string *token = env.getCookie(baseAuth()->authTokenCookieName()); if (token) { AuthTokenResult result = baseAuth()->processAuthToken(*token, users()); switch(result.state()) { case AuthTokenState::Valid: { if (!result.newToken().empty()) { /* * Only extend the validity from what we had currently. */ app->setCookie(baseAuth()->authTokenCookieName(), result.newToken(), result.newTokenValidity(), "", "", app->environment().urlScheme() == "https"); } return result.user(); } case AuthTokenState::Invalid: app->setCookie(baseAuth()->authTokenCookieName(),std::string(), 0, "", "", app->environment().urlScheme() == "https"); return User(); } } } return User(); }
/// Tries to log the user in, creates the session, and sets the session cookie. @return true if login was succesful bool tryLogin(const string& username, const string& password) { dbo::ptr<User> user = _doTryLogin(username, password); if (user) { cookieCache = WRandom::generateId(); WApplication* app = WApplication::instance(); app->setCookie(_cookieName, cookieCache, 60*60*24*365, "", "/", true); // TODO: set secure based on settings _userSessionStore.login(user.id(), cookieCache); // Record that they're logged in for other SessionHandles to find return true; } return false; }
void AuthModel::setRememberMeCookie(const User& user) { WApplication *app = WApplication::instance(); const AuthService *s = baseAuth(); app->setCookie(s->authTokenCookieName(), s->createAuthToken(user), s->authTokenValidity() * 60, s->authTokenCookieDomain(), "", app->environment().urlScheme() == "https"); }
bool AuthModel::login(Login& login) { if (valid()) { User user = users().findWithIdentity(Identity::LoginName, valueText(LoginNameField)); boost::any v = value(RememberMeField); const AuthService *s = baseAuth(); if (loginUser(login, user)) { reset(); if (!v.empty() && boost::any_cast<bool>(v) == true) { WApplication *app = WApplication::instance(); app->setCookie(s->authTokenCookieName(), s->createAuthToken(user), s->authTokenValidity() * 60); } return true; } else return false; } else return false; }