forked from Novators/libsqrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrlClient.cpp
More file actions
270 lines (240 loc) · 8.02 KB
/
SqrlClient.cpp
File metadata and controls
270 lines (240 loc) · 8.02 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/** \file SqrlClient.cpp
*
* \author Adam Comley
*
* This file is part of libsqrl. It is released under the MIT license.
* For more details, see the LICENSE file included with this package.
**/
#include "sqrl_internal.h"
#include "SqrlClient.h"
#include "SqrlAction.h"
#include "SqrlUser.h"
#include "SqrlEntropy.h"
#include "gcm.h"
namespace libsqrl
{
template class SqrlDeque<SqrlClient::CallbackInfo *>;
#define SQRL_CALLBACK_SAVE_SUGGESTED 0
#define SQRL_CALLBACK_SELECT_USER 1
#define SQRL_CALLBACK_SELECT_ALT 2
#define SQRL_CALLBACK_ACTION_COMPLETE 3
#define SQRL_CALLBACK_AUTH_REQUIRED 4
#define SQRL_CALLBACK_SEND 5
#define SQRL_CALLBACK_ASK 6
#define SQRL_CALLBACK_PROGRESS 7
SqrlClient *SqrlClient::client = NULL;
#if defined(WITH_THREADS)
std::mutex *SqrlClient::clientMutex = NULL;
#endif
SqrlClient::SqrlClient() :
rapid(false)
{
#if defined(WITH_THREADS)
if( SqrlClient::clientMutex == nullptr ) {
SqrlClient::clientMutex = new std::mutex();
}
#endif
SQRL_MUTEX_LOCK( SqrlClient::clientMutex )
if( SqrlClient::client != NULL ) {
// Enforce a single SqrlClient object
exit( 1 );
}
SqrlInit();
SqrlEntropy::start();
SqrlClient::client = this;
SQRL_MUTEX_UNLOCK( SqrlClient::clientMutex )
}
SqrlClient::~SqrlClient() {
this->onClientIsStopping();
SQRL_MUTEX_LOCK( SqrlClient::clientMutex )
SqrlClient::client = NULL;
SQRL_MUTEX_UNLOCK( SqrlClient::clientMutex )
SqrlEntropy::stop();
SqrlAction *action;
SqrlUser *user;
do {
SQRL_MUTEX_LOCK( &this->actionMutex );
action = this->actions.pop();
SQRL_MUTEX_UNLOCK( &this->actionMutex );
if( action ) {
delete action;
}
} while( action );
do {
SQRL_MUTEX_LOCK( &this->userMutex );
user = this->users.pop();
SQRL_MUTEX_UNLOCK( &this->userMutex );
if( user ) {
delete user;
}
} while( user );
}
SqrlClient *SqrlClient::getClient() {
SqrlClient *client = nullptr;
SQRL_MUTEX_LOCK( SqrlClient::clientMutex );
client = SqrlClient::client;
SQRL_MUTEX_UNLOCK( SqrlClient::clientMutex );
return client;
}
int SqrlClient::getUserIdleSeconds() {
return 0;
}
bool SqrlClient::isScreenLocked() {
return false;
}
bool SqrlClient::isUserChanged() {
return false;
}
void SqrlClient::onLoop() {
}
bool SqrlClient::loop() {
if( SqrlClient::getClient() != this ) return false;
this->onLoop();
SqrlAction *action;
while( !this->callbackQueue.empty() ) {
struct CallbackInfo *info = this->callbackQueue.pop();
switch( info->cbType ) {
case SQRL_CALLBACK_SAVE_SUGGESTED:
this->onSaveSuggested( (SqrlUser*)info->ptr );
break;
case SQRL_CALLBACK_SELECT_USER:
action = (SqrlAction*)info->ptr;
this->onSelectUser( action );
break;
case SQRL_CALLBACK_SELECT_ALT:
action = (SqrlAction*)info->ptr;
this->onSelectAlternateIdentity( action );
break;
case SQRL_CALLBACK_ACTION_COMPLETE:
action = (SqrlAction*)info->ptr;
this->onActionComplete( action );
break;
case SQRL_CALLBACK_AUTH_REQUIRED:
action = (SqrlAction*)info->ptr;
this->onAuthenticationRequired( action, info->credentialType );
break;
case SQRL_CALLBACK_SEND:
action = (SqrlAction*)info->ptr;
this->onSend( action, *info->str[0], *info->str[1] );
break;
case SQRL_CALLBACK_ASK:
action = (SqrlAction*)info->ptr;
this->onAsk( action, *info->str[0], *info->str[1], *info->str[2] );
break;
case SQRL_CALLBACK_PROGRESS:
action = (SqrlAction*)info->ptr;
this->onProgress( action, info->progress );
break;
}
delete info;
}
SQRL_MUTEX_LOCK( &this->actionMutex );
action = this->actions.pop();
SQRL_MUTEX_UNLOCK( &this->actionMutex );
if( action ) action->exec();
if( this->actions.empty() && this->callbackQueue.empty() ) {
return false;
}
return true;
}
SqrlUser * SqrlClient::getUser( const SqrlString * uniqueId ) {
char testId[SQRL_UNIQUE_ID_LENGTH + 1];
SQRL_MUTEX_LOCK( &this->userMutex );
size_t end = this->users.count();
for( size_t i = 0; i < end; i++ ) {
SqrlUser *cur = this->users.peek( i );
if( cur->getUniqueId( testId ) ) {
if( 0 == uniqueId->compare( testId ) ) {
SQRL_MUTEX_UNLOCK( &this->userMutex );
return cur;
}
}
}
SQRL_MUTEX_UNLOCK( &this->userMutex );
return nullptr;
}
SqrlUser * SqrlClient::getUser( void * tag ) {
SQRL_MUTEX_LOCK( &this->userMutex );
size_t end = this->users.count();
for( size_t i = 0; i < end; i++ ) {
SqrlUser *cur = this->users.peek( i );
if( cur->getTag() == tag ) {
SQRL_MUTEX_UNLOCK( &this->userMutex );
return cur;
}
}
SQRL_MUTEX_UNLOCK( &this->userMutex );
return nullptr;
}
void SqrlClient::callSaveSuggested( SqrlUser * user ) {
struct CallbackInfo *info = new struct CallbackInfo();
info->cbType = SQRL_CALLBACK_SAVE_SUGGESTED;
info->ptr = user;
this->callbackQueue.push( info );
}
void SqrlClient::callSelectUser( SqrlAction * action ) {
struct CallbackInfo *info = new struct CallbackInfo();
info->cbType = SQRL_CALLBACK_SELECT_USER;
info->ptr = action;
this->callbackQueue.push( info );
}
void SqrlClient::callSelectAlternateIdentity( SqrlAction * action ) {
struct CallbackInfo *info = new struct CallbackInfo();
info->cbType = SQRL_CALLBACK_SELECT_ALT;
info->ptr = action;
this->callbackQueue.push( info );
}
void SqrlClient::callActionComplete( SqrlAction * action ) {
struct CallbackInfo *info = new struct CallbackInfo();
info->cbType = SQRL_CALLBACK_ACTION_COMPLETE;
info->ptr = action;
this->callbackQueue.push( info );
}
void SqrlClient::callProgress( SqrlAction * action, int progress ) {
struct CallbackInfo *info = new struct CallbackInfo();
info->cbType = SQRL_CALLBACK_PROGRESS;
info->ptr = action;
info->progress = progress;
this->callbackQueue.push_back( info );
}
void SqrlClient::callAuthenticationRequired( SqrlAction * action, Sqrl_Credential_Type credentialType ) {
struct CallbackInfo *info = new struct CallbackInfo();
info->cbType = SQRL_CALLBACK_AUTH_REQUIRED;
info->ptr = action;
info->credentialType = credentialType;
this->callbackQueue.push( info );
}
void SqrlClient::callSend( SqrlAction * action, SqrlString *url, SqrlString * payload ) {
struct CallbackInfo *info = new struct CallbackInfo();
info->cbType = SQRL_CALLBACK_SEND;
info->ptr = action;
info->str[0] = new SqrlString( *url );
info->str[1] = new SqrlString( *payload );
this->callbackQueue.push( info );
}
void SqrlClient::callAsk( SqrlAction * action, SqrlString * message, SqrlString * firstButton, SqrlString * secondButton ) {
struct CallbackInfo *info = new struct CallbackInfo();
info->cbType = SQRL_CALLBACK_ASK;
info->ptr = action;
info->str[0] = new SqrlString( *message );
info->str[1] = new SqrlString( *firstButton );
info->str[2] = new SqrlString( *secondButton );
this->callbackQueue.push( info );
}
SqrlClient::CallbackInfo::CallbackInfo() {
this->cbType = 0;
this->progress = 0;
this->credentialType = SQRL_CREDENTIAL_PASSWORD;
this->ptr = NULL;
this->str[0] = NULL;
this->str[1] = NULL;
this->str[2] = NULL;
}
SqrlClient::CallbackInfo::~CallbackInfo() {
for( int i = 0; i < 3; i++ ) {
if( this->str[i] ) {
delete this->str[i];
}
}
}
}