-
Notifications
You must be signed in to change notification settings - Fork 2
/
Authentication.cpp
61 lines (46 loc) · 1.15 KB
/
Authentication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <poll.h>
#include <netinet/in.h>
#include <signal.h>
#include <arpa/inet.h>
#include <map>
#include <string>
using namespace std;
#include "Mutex.h"
#include "Address.h"
#include "Authentication.h"
#include "Tempo.h"
// This is a map from ip address in binary form to the time at which updateClientAuthentication()
// was last called.
map<string, uint64_t> authTimes;
Mutex authMutex;
bool clientIsAuthenticated(sockaddr_in address)
{
MutexLocker m(authMutex);
string strAddress = addressToBinaryString(address);
uint64_t now = CurrentTimeSeconds();
if (authTimes.count(strAddress) < 1)
return false;
return authTimes[strAddress] + AUTH_TIMEOUT > now;
}
void updateClientAuthentication(sockaddr_in address, bool authed)
{
MutexLocker m(authMutex);
string strAddress = addressToBinaryString(address);
uint64_t now = CurrentTimeSeconds();
if (authed)
authTimes[strAddress] = now;
else
authTimes.erase(strAddress);
}
string authPassword;
void setPassword(string password)
{
authPassword = password;
}
bool checkPassword(string password)
{
return authPassword == password;
}