Leosac  0.8.0
Open Source Access Control
Token.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "core/auth/AuthFwd.hpp"
24 #include "tools/db/database.hpp"
25 #include <boost/date_time/posix_time/posix_time.hpp>
26 #include <chrono>
27 
28 namespace Leosac
29 {
30 namespace Auth
31 {
41 #pragma db object optimistic
42 class Token
43 {
44  public:
45  Token();
46 
47  Token(const std::string &token, UserPtr owner);
48 
55  const std::string &id() const;
56 
61  const std::string &token() const;
62 
66  bool is_valid() const;
67 
72  UserPtr owner() const;
73 
78  boost::posix_time::ptime expiration() const;
79 
83  template <typename T>
84  void expire_in(const T &duration)
85  {
86  using namespace std::chrono;
87  auto duration_second = duration_cast<seconds>(duration);
88 
89  auto now = boost::posix_time::second_clock::local_time();
90  expiration_ = now + boost::posix_time::seconds(duration_second.count());
91  }
92 
93  private:
94  friend class odb::access;
95 
99 #pragma db id
100 #pragma db type("VARCHAR(128)")
101  std::string token_;
102 
106 #pragma db not_null
107 #pragma db on_delete(cascade)
109 
110 #pragma db not_null
111  boost::posix_time::ptime expiration_;
112 
113  public:
114 #pragma db version
115  ssize_t version_;
116 };
117 }
118 }
119 
120 #ifdef ODB_COMPILER
121 #include "core/auth/User.hpp"
122 #endif
Leosac::Auth::Token::access
friend class odb::access
Definition: Token.hpp:94
Leosac::Auth::Token::version_
ssize_t version_
Definition: Token.hpp:115
Leosac::Auth::Token
An authentication token used for authenticating a user against Leosac.
Definition: Token.hpp:42
database.hpp
AuthFwd.hpp
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
User.hpp
Leosac::Auth::Token::is_valid
bool is_valid() const
Check if the token is still active.
Definition: Token.cpp:45
Leosac::Auth::Token::id
const std::string & id() const
Return the token identifier.
Definition: Token.cpp:61
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
leosacexception.hpp
Exception class for LEOSAC Project related errors.
Leosac::Auth::Token::expiration
boost::posix_time::ptime expiration() const
Retrieve the unix timestamp at which the token will/has expire(d).
Definition: Token.cpp:56
Leosac::Auth::Token::expire_in
void expire_in(const T &duration)
Set the expiration point of the token to be now + duration.
Definition: Token.hpp:84
Leosac::Auth::Token::expiration_
boost::posix_time::ptime expiration_
Definition: Token.hpp:111
Leosac::Auth::Token::owner_
UserPtr owner_
The user owning the token.
Definition: Token.hpp:108
Leosac::Auth::Token::Token
Token()
Definition: Token.cpp:27
Leosac::Auth::Token::owner
UserPtr owner() const
Retrieve a shared_ptr to the user owning the token.
Definition: Token.cpp:51
Leosac::Auth::Token::token_
std::string token_
The string representation of the token.
Definition: Token.hpp:101
Leosac::Auth::Token::token
const std::string & token() const
Retrieve the string representation of the token.
Definition: Token.cpp:40