Leosac  0.8.0
Open Source Access Control
WSAPICall.cpp
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 #include "WSAPICall.hpp"
21 #include "core/audit/WSAPICall_odb.h"
22 #include "core/auth/User.hpp"
24 #include "tools/log.hpp"
25 
26 using namespace Leosac;
27 using namespace Leosac::Audit;
28 
30  : status_code_(APIStatusCode::UNKNOWN)
31  , database_operations_(0)
32 {
33 }
34 
36 {
37  ASSERT_LOG(database, "Database cannot be null.");
38 
40  WSAPICallPtr audit(new Audit::WSAPICall());
41  database->persist(audit);
42  t.commit();
43  audit->database_ = database;
44  return audit;
45 }
46 
47 void WSAPICall::method(const std::string &str)
48 {
49  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
50  api_method_ = str;
51 }
52 
53 void WSAPICall::uuid(const std::string &str)
54 {
55  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
56  uuid_ = str;
57 }
58 
60 {
61  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
62  status_code_ = code;
63 }
64 
65 void WSAPICall::status_string(const std::string &str)
66 {
67  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
68  status_string_ = str;
69 }
70 
71 void WSAPICall::source_endpoint(const std::string &str)
72 {
73  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
74  source_endpoint_ = str;
75 }
76 
77 void WSAPICall::request_content(const std::string &str)
78 {
79  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
80  request_content_ = str;
81 }
82 
83 void WSAPICall::response_content(const std::string &str)
84 {
85  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
86  response_content_ = str;
87 }
88 
89 void WSAPICall::database_operations(uint16_t nb_operation)
90 {
91  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
92  database_operations_ = nb_operation;
93 }
94 
95 const std::string &WSAPICall::method() const
96 {
97  return api_method_;
98 }
99 
100 const std::string &WSAPICall::uuid() const
101 {
102  return uuid_;
103 }
104 
106 {
107  return status_code_;
108 }
109 
110 const std::string &WSAPICall::status_string() const
111 {
112  return status_string_;
113 }
114 
115 const std::string &WSAPICall::source_endpoint() const
116 {
117  return source_endpoint_;
118 }
119 
121 {
122  std::stringstream ss;
123 
124  auto author = author_.load();
125  if (author)
126  {
127  ss << "Websocket API call by user " << author->username() << " to "
128  << method() << " took " << duration_ << " milliseconds.";
129  }
130  else
131  {
132  ss << "Websocket API call by anonymous to " << method() << " took "
133  << duration_ << " milliseconds.";
134  }
135  return ss.str();
136 }
Leosac::Audit::AuditEntry::author
virtual void author(Auth::UserPtr user) override
Set the author of the entry.
Definition: AuditEntry.cpp:88
Leosac::Audit::WSAPICall::source_endpoint
virtual const std::string & source_endpoint() const override
Definition: WSAPICall.cpp:115
Leosac::Audit::WSAPICall::database_operations_
uint16_t database_operations_
Count the number of database operation that happened while processing this API call.
Definition: WSAPICall.hpp:115
Leosac::Audit::WSAPICall::request_content_
std::string request_content_
Copy of the JSON content of the request.
Definition: WSAPICall.hpp:104
Leosac::Audit::WSAPICall::WSAPICall
WSAPICall()
Definition: WSAPICall.cpp:29
Leosac::Audit::WSAPICall::create
static WSAPICallPtr create(const DBPtr &database)
Definition: WSAPICall.cpp:35
Leosac::Audit::WSAPICall::source_endpoint_
std::string source_endpoint_
The source IP:Port of the client who made the request.
Definition: WSAPICall.hpp:99
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Audit::AuditEntry::database
void database(DBPtr db)
Set the database pointer.
Definition: AuditEntry.cpp:144
User.hpp
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::WSAPICall::status_string_
std::string status_string_
The status string of the response.
Definition: WSAPICall.hpp:92
Leosac::Audit::WSAPICall::status_code_
APIStatusCode status_code_
Status code of the response.
Definition: WSAPICall.hpp:86
Leosac::Audit::WSAPICall::method
virtual const std::string & method() const override
Definition: WSAPICall.cpp:95
Leosac::Audit::WSAPICall::database_operations
virtual void database_operations(uint16_t nb_operation) override
The number of database queries.
Definition: WSAPICall.cpp:89
WSAPICall.hpp
Leosac::Audit::WSAPICall::generate_description
virtual std::string generate_description() const override
Generate a description for this event.
Definition: WSAPICall.cpp:120
Leosac::Audit
The Audit namespace provides classes and facilities to keep track of what's happening on the Leosac d...
Definition: AccessPointEvent.hpp:27
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Audit::WSAPICall::response_content
virtual void response_content(const std::string &string) override
Definition: WSAPICall.cpp:83
Leosac::Audit::WSAPICall::status_string
virtual const std::string & status_string() const override
Definition: WSAPICall.cpp:110
Leosac::Audit::AuditEntry::duration_
size_t duration_
How long did it take for the Audit object to be finalized.
Definition: AuditEntry.hpp:124
Leosac::Audit::AuditEntry::finalized
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:72
Leosac::Audit::WSAPICall::uuid_
std::string uuid_
The UUID of the request.
Definition: WSAPICall.hpp:80
Leosac::db::MultiplexedTransaction::commit
void commit()
Commit the transaction.
Definition: MultiplexedTransaction.cpp:47
Leosac::Audit::WSAPICall::uuid
virtual const std::string & uuid() const override
Definition: WSAPICall.cpp:100
MultiplexedTransaction.hpp
Leosac::db::MultiplexedTransaction
Acts like an odb::transaction, with the exception that it will becomes the active transaction at cons...
Definition: MultiplexedTransaction.hpp:35
Leosac::Audit::WSAPICall::request_content
virtual void request_content(const std::string &string) override
The (JSON) content of the request.
Definition: WSAPICall.cpp:77
Leosac::Audit::WSAPICall::api_method_
std::string api_method_
Definition: WSAPICall.hpp:74
Leosac::APIStatusCode::UNKNOWN
@ UNKNOWN
Unknown status.
Leosac::APIStatusCode
APIStatusCode
Those are the Leosac API status code.
Definition: APIStatusCode.hpp:31
log.hpp
Leosac::Audit::WSAPICallPtr
std::shared_ptr< WSAPICall > WSAPICallPtr
Definition: AuditFwd.hpp:90
Leosac::Audit::WSAPICall::status_code
virtual APIStatusCode status_code() const override
Definition: WSAPICall.cpp:105
Leosac::Audit::WSAPICall::response_content_
std::string response_content_
Copy of the JSON content of the response.
Definition: WSAPICall.hpp:109
Leosac::Audit::WSAPICall
Provides the implementation of IWSAPICall.
Definition: WSAPICall.hpp:33
Leosac::Audit::AuditEntry::author_
Auth::UserLPtr author_
The user at the source of the entry.
Definition: AuditEntry.hpp:116