-
Notifications
You must be signed in to change notification settings - Fork 1
/
oAuth2Interface.cpp
768 lines (682 loc) · 21.2 KB
/
oAuth2Interface.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// File: oAuth2Interface.cpp
// Date: 4/15/2013
// Auth: K. Loux
// Desc: Handles interface to a server using OAuth 2.0.
// Standard C++ headers
#include <cstdlib>
#include <cassert>
#include <sstream>
#include <ctime>
#include <algorithm>
#include <chrono>
// Standard C headers
#include <string.h>
// cURL headers
#include <curl/curl.h>
// utilities headers
#include "utilities/cppSocket.h"
// OS headers
#ifdef _WIN32
#include <Windows.h>
#include <shellapi.h>
#else
#include <unistd.h>
#endif
// Local headers
#include "oAuth2Interface.h"
//==========================================================================
// Class: OAuth2Interface
// Function: OAuth2Interface
//
// Description: Constant declarations
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// None
//
//==========================================================================
OAuth2Interface *OAuth2Interface::singleton = nullptr;
//==========================================================================
// Class: OAuth2Interface
// Function: OAuth2Interface
//
// Description: Constructor for OAuth2Interface class.
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// None
//
//==========================================================================
OAuth2Interface::OAuth2Interface()
{
verbose = false;
}
//==========================================================================
// Class: OAuth2Interface
// Function: Get (static)
//
// Description: Access method for singleton pattern.
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// None
//
//==========================================================================
OAuth2Interface& OAuth2Interface::Get()
{
if (!singleton)
singleton = new OAuth2Interface;
return *singleton;
}
//==========================================================================
// Class: OAuth2Interface
// Function: Destroy (static)
//
// Description: Cleans up memory associated with singleton pattern.
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// None
//
//==========================================================================
void OAuth2Interface::Destroy()
{
if (singleton)
delete singleton;
singleton = nullptr;
}
//==========================================================================
// Class: OAuth2Interface
// Function: SetRefreshToken
//
// Description: Sets the refresh token. If the token is not valid, request
// information from the user to obtain a valid token.
//
// Input Arguments:
// refreshToken = const UString::String&
//
// Output Arguments:
// None
//
// Return Value:
// None
//
//==========================================================================
void OAuth2Interface::SetRefreshToken(const UString::String &refreshTokenIn)
{
// If the token isn't valid, request one, otherwise, use it as-is
if (refreshTokenIn.length() < 2)// TODO: Better way to tell if it's valid?
refreshToken = RequestRefreshToken();// TODO: Check for errors (returned empty UString::?)
else
refreshToken = refreshTokenIn;
}
//==========================================================================
// Class: OAuth2Interface
// Function: RequestRefreshToken
//
// Description: Requests a refresh token from the server.
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// UString::String containing refresh token, or emtpy UString::String on error
//
//==========================================================================
UString::String OAuth2Interface::RequestRefreshToken()
{
assert(!authURL.empty() && !tokenURL.empty());
if (IsLimitedInput())
{
std::string rawReadBuffer;
if (!DoCURLPost(authURL, UString::ToNarrowString(AssembleRefreshRequestQueryString()), rawReadBuffer))
return UString::String();
UString::String readBuffer(UString::ToStringType(rawReadBuffer));
if (ResponseContainsError(readBuffer))
return UString::String();
AuthorizationResponse authResponse;
if (!HandleAuthorizationRequestResponse(readBuffer, authResponse))
return UString::String();
UString::String queryString = AssembleAccessRequestQueryString(authResponse.deviceCode, true);
time_t startTime = time(nullptr);
time_t now = startTime;
while (!HandleRefreshRequestResponse(readBuffer, true))
{
std::this_thread::sleep_for(std::chrono::seconds(authResponse.interval));
now = time(nullptr);
if (difftime(now, startTime) > authResponse.expiresIn)
{
*log << "Request timed out - restart application to start again" << std::endl;
return UString::String();
}
if (!DoCURLPost(authPollURL, UString::ToNarrowString(queryString), rawReadBuffer))
return UString::String();
readBuffer = UString::ToStringType(rawReadBuffer);
if (ResponseContainsError(readBuffer))
return UString::String();
}
}
else
{
assert(!responseType.empty());
UString::String stateKey;// = GenerateSecurityStateKey();// Not sure why it doesn't work with the state key...
// TODO: Appropriate to use code challenge here (without user specifying anything?)
const UString::String assembledAuthURL(authURL + UString::Char('?') + AssembleRefreshRequestQueryString(stateKey));
CPPSocket webSocket(CPPSocket::SocketType::SocketTCPServer, *log);
if (RedirectURIIsLocal())
{
if (!webSocket.Create(StripPortFromLocalRedirectURI(), UString::ToNarrowString(StripAddressFromLocalRedirectURI()).c_str()))
return UString::String();
#ifdef _WIN32
ShellExecute(nullptr, _T("open"), assembledAuthURL.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
#else
system(UString::ToNarrowString(UString::String(_T("xdg-open '")) + assembledAuthURL + UString::String(_T("'"))).c_str());
#endif
}
else// (for example, with redirect URI set to "oob")
{
// The benefit of doing it the way we're doing it now, though, is
// that the browser used to authenticate does not need to be on the
// same machine that is running this application.
Cout << "Enter this address in your browser:" << std::endl << assembledAuthURL << std::endl;
}
UString::String authorizationCode;
if (RedirectURIIsLocal())
{
if (!webSocket.WaitForClientData(60000))
{
*log << "No response... aborting" << std::endl;
return UString::String();
}
std::string message;
{
std::lock_guard<std::mutex> lock(webSocket.GetMutex());
const auto messageSize(webSocket.Receive());
if (messageSize <= 0)
return UString::String();
message.assign(reinterpret_cast<char*>(webSocket.GetLastMessage()), messageSize);
}
if (message.empty())
return UString::String();
authorizationCode = ExtractAuthCodeFromGETRequest(message);
const auto successResponse(BuildHTTPSuccessResponse(successMessage));
assert(successResponse.length() < std::numeric_limits<unsigned int>::max());
if (!webSocket.TCPSend(reinterpret_cast<const CPPSocket::DataType*>(successResponse.c_str()), static_cast<int>(successResponse.length())))
*log << "Warning: Authorization code response failed to send" << std::endl;
}
else
{
Cout << "Enter verification code:" << std::endl;
Cin >> authorizationCode;
}
std::string readBuffer;
if (!DoCURLPost(tokenURL, UString::ToNarrowString(AssembleAccessRequestQueryString(authorizationCode)), readBuffer) ||
ResponseContainsError(UString::ToStringType(readBuffer)) ||
!HandleRefreshRequestResponse(UString::ToStringType(readBuffer)))
{
*log << "Failed to obtain refresh token" << std::endl;
return UString::String();
}
}
*log << "Successfully obtained refresh token" << std::endl;
return refreshToken;
}
UString::String OAuth2Interface::ExtractAuthCodeFromGETRequest(const std::string& rawRequest)
{
UString::String request(UString::ToStringType(rawRequest));
const UString::String startKey(_T("?code="));
const auto start(request.find(startKey));
if (start == UString::String::npos)
return UString::String();
const UString::String endKey(_T(" HTTP/1.1"));
const auto end(request.find(endKey, start));
if (end == UString::String::npos)
return UString::String();
return request.substr(start + startKey.length(), end - start - startKey.length());
}
std::string OAuth2Interface::BuildHTTPSuccessResponse(const UString::String& successMessage)
{
std::string body("<html><body><h1>Success!</h1><p>" + UString::ToNarrowString(successMessage) + "</p></body></html>");
std::ostringstream headerStream;
headerStream << "HTTP/1.1 200 OK\n"
<< "Date: Sun, 18 Oct 2009 08:56:53 GMT\n"
<< "Server: eBirdDataProcessor\n"
<< "Last-Modified: Sat, 20 Nov 2004 07:16:26 GMT\n"
<< "Accept-Ranges: bytes\n"
<< "Content-Length: " << body.length() << '\n'
<< "Connection: close\n"
<< "Content-Type: text/html\n\n";
return headerStream.str() + body;
}
//==========================================================================
// Class: OAuth2Interface
// Function: ResponseContainsError
//
// Description: Checks JSON array to see if there is an error entry.
// "Authorization pending" errors are not considered errors.
//
// Input Arguments:
// buffer = const UString::String & containing JSON UString::
//
// Output Arguments:
// None
//
// Return Value:
// bool, true for error, false otherwise
//
//==========================================================================
bool OAuth2Interface::ResponseContainsError(const UString::String &buffer)
{
cJSON *root(cJSON_Parse(UString::ToNarrowString(buffer).c_str()));
if (!root)
{
*log << "Failed to parse returned string (ResponseContainsError())" << std::endl;
if (verbose)
Cerr << buffer << '\n';
return true;
}
UString::String error;
if (ReadJSON(root, _T("error"), error))
{
if (error != _T("authorization_pending"))
{
UString::OStringStream ss;
ss << "Recieved error from OAuth server: " << error;
UString::String description;
if (ReadJSON(root, _T("error_description"), description))
ss << " - " << description;
*log << ss.str() << std::endl;
cJSON_Delete(root);
return true;
}
cJSON_Delete(root);
}
return false;
}
//==========================================================================
// Class: OAuth2Interface
// Function: HandleAuthorizationRequestResponse
//
// Description: Processes JSON responses from server. Used for input-limited
// devices only.
//
// Input Arguments:
// buffer = const UString::String & containing JSON UString::
//
// Output Arguments:
// response = AuthorizationResponse&
//
// Return Value:
// bool, true for success, false otherwise
//
//==========================================================================
bool OAuth2Interface::HandleAuthorizationRequestResponse(
const UString::String &buffer, AuthorizationResponse &response)
{
assert(IsLimitedInput());
cJSON *root(cJSON_Parse(UString::ToNarrowString(buffer).c_str()));
if (!root)
{
*log << "Failed to parse returned string (HandleAuthorizationRequestResponse())" << std::endl;
return false;
}
UString::String userCode, verificationURL;
// TODO: Check state key?
if (!ReadJSON(root, _T("device_code"), response.deviceCode) ||
!ReadJSON(root, _T("user_code"), userCode) ||
!ReadJSON(root, _T("verification_url"), verificationURL) ||
!ReadJSON(root, _T("expires_in"), response.expiresIn) ||
!ReadJSON(root, _T("interval"), response.interval))
{
cJSON_Delete(root);
return false;
}
Cout << "Please visit this URL: " << std::endl << verificationURL << std::endl;
Cout << "And enter this code (case sensitive):" << std::endl << userCode << std::endl;
cJSON_Delete(root);
return true;
}
//==========================================================================
// Class: OAuth2Interface
// Function: HandleRefreshRequestResponse
//
// Description: Processes JSON responses from server.
//
// Input Arguments:
// buffer = const UString::String& containing JSON
//
// Output Arguments:
// None
//
// Return Value:
// bool, true for success, false otherwise
//
//==========================================================================
bool OAuth2Interface::HandleRefreshRequestResponse(const UString::String &buffer, const bool &silent)
{
cJSON *root = cJSON_Parse(UString::ToNarrowString(buffer).c_str());
if (!root)
{
if (!silent)
*log << "Failed to parse returned string (HandleRefreshRequsetResponse())" << std::endl;
return false;
}
UString::String tokenType;
if (!ReadJSON(root, _T("refresh_token"), refreshToken))
{
if (!silent)
*log << "Failed to read refresh token field from server" << std::endl;
cJSON_Delete(root);
return false;
}
cJSON_Delete(root);
return HandleAccessRequestResponse(buffer);
}
//==========================================================================
// Class: OAuth2Interface
// Function: HandleAccessRequestResponse
//
// Description: Processes JSON responses from server.
//
// Input Arguments:
// buffer = const UString::String & containing JSON UString::
//
// Output Arguments:
// None
//
// Return Value:
// bool, true for success, false otherwise
//
//==========================================================================
bool OAuth2Interface::HandleAccessRequestResponse(const UString::String &buffer)
{
cJSON *root = cJSON_Parse(UString::ToNarrowString(buffer).c_str());
if (!root)
{
*log << "Failed to parse returned string (HandleAccessRequestResponse())" << std::endl;
return false;
}
UString::String tokenType;
UString::String scopes;
unsigned int tokenValidDuration;// [sec]
if (!ReadJSON(root, _T("access_token"), accessToken) ||
!ReadJSON(root, _T("token_type"), tokenType) ||
!ReadJSON(root, _T("expires_in"), tokenValidDuration) ||
!ReadJSON(root, _T("scope"), scopes))
{
*log << "Failed to read all required fields from server" << std::endl;
cJSON_Delete(root);
return false;
}
*log << "Received token for the following scopes: " << scopes << std::endl;
if (tokenType.compare(_T("Bearer")) != 0)
{
*log << "Expected token type 'Bearer', received '" << tokenType << "'" << std::endl;
cJSON_Delete(root);
return false;
}
accessTokenValidUntilTime = std::chrono::system_clock::now() + std::chrono::seconds(tokenValidDuration);
cJSON_Delete(root);
return true;
}
//==========================================================================
// Class: OAuth2Interface
// Function: GetAccessToken
//
// Description: Returns a valid access token. This method generates new
// access tokens as necessary (as they expire).
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// UString::String containing access token (or empty UString::String on error)
//
//==========================================================================
UString::String OAuth2Interface::GetAccessToken()
{
// TODO: Better way to check if access token is valid? It would be good to be able
// to request a new one after an API response with a 401 error.
if (!accessToken.empty() && std::chrono::system_clock::now() < accessTokenValidUntilTime)
return accessToken;
*log << "Access token is invalid - requesting a new one" << std::endl;
std::string readBuffer;
if (!DoCURLPost(tokenURL, UString::ToNarrowString(AssembleAccessRequestQueryString()), readBuffer) ||
ResponseContainsError(UString::ToStringType(readBuffer)) ||
!HandleAccessRequestResponse(UString::ToStringType(readBuffer)))
{
*log << "Failed to obtain access token" << std::endl;
return UString::String();
}
*log << "Successfully obtained new access token" << std::endl;
return accessToken;
}
//==========================================================================
// Class: OAuth2Interface
// Function: AssembleRefreshRequestQueryString
//
// Description: Assembles the proper request query UString::String for obtaining a
// refresh token.
//
// Input Arguments:
// state = const UString::String&, anti-forgery state key
//
// Output Arguments:
// None
//
// Return Value:
// UString::String containing access token (or empty UString::String on error)
//
//==========================================================================
UString::String OAuth2Interface::AssembleRefreshRequestQueryString(const UString::String& state) const
{
assert(!clientID.empty() &&
!scope.empty());
// Required fields
UString::String queryString;
queryString.append(_T("client_id=") + clientID);
queryString.append(_T("&scope=") + scope);
// Optional fields
if (!loginHint.empty())
queryString.append(_T("&login_hint=") + loginHint);
if (!responseType.empty())
queryString.append(_T("&response_type=") + responseType);
if (!redirectURI.empty())
queryString.append(_T("&redirect_uri=") + redirectURI);
if (!state.empty())
queryString.append(_T("&state=") + state);
return queryString;
}
//==========================================================================
// Class: OAuth2Interface
// Function: AssembleAccessRequestQueryString
//
// Description: Assembles the proper request query UString::String for obtaining an access
// token.
//
// Input Arguments:
// code = const UString::String&
// usePollGrantType = const bool&
//
// Output Arguments:
// None
//
// Return Value:
// String containing access token (or empty UString::String on error)
//
//==========================================================================
UString::String OAuth2Interface::AssembleAccessRequestQueryString(const UString::String &code, const bool& usePollGrantType) const
{
assert((!refreshToken.empty() || !code.empty()) &&
!clientID.empty() &&
!clientSecret.empty()/* &&
!grantType.empty()*/);
// Required fields
UString::String queryString;
queryString.append(_T("client_id=") + clientID);
queryString.append(_T("&client_secret=") + clientSecret);
if (code.empty())
{
queryString.append(_T("&refresh_token=") + refreshToken);
queryString.append(_T("&grant_type=refresh_token"));
}
else
{
if (IsLimitedInput())
queryString.append(_T("&device_code=") + code);
else
queryString.append(_T("&code=") + code);
if (usePollGrantType)
{
assert(!pollGrantType.empty());
queryString.append(_T("&grant_type=") + pollGrantType);
}
else
queryString.append(_T("&grant_type=") + grantType);
if (!redirectURI.empty())
queryString.append(_T("&redirect_uri=") + redirectURI);
}
return queryString;
}
//==========================================================================
// Class: OAuth2Interface
// Function: RedirectURIIsLocal
//
// Description: Checks to see if the URI indicates we should be listening
// on a local port.
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// bool
//
//==========================================================================
bool OAuth2Interface::RedirectURIIsLocal() const
{
assert(!redirectURI.empty());
const UString::String localURL1(_T("http://localhost"));
const UString::String localURL2(_T("http://127.0.0.1"));
return redirectURI.substr(0, localURL1.length()).compare(localURL1) == 0 ||
redirectURI.substr(0, localURL2.length()).compare(localURL2) == 0;
}
//==========================================================================
// Class: OAuth2Interface
// Function: StripPortFromLocalRedirectURI
//
// Description: Parses the redirect URI UString::String to obtain the local port number.
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// unsigned short, contains port number or zero for error
//
//==========================================================================
unsigned short OAuth2Interface::StripPortFromLocalRedirectURI() const
{
assert(RedirectURIIsLocal());
const size_t colon(redirectURI.find_last_of(':'));
if (colon == UString::String::npos)
return 0;
UString::IStringStream s(redirectURI.substr(colon + 1));
unsigned short port;
s >> port;
return port;
}
UString::String OAuth2Interface::StripAddressFromLocalRedirectURI() const
{
assert(RedirectURIIsLocal());
const size_t colon(redirectURI.find_last_of(':'));
if (colon == UString::String::npos)
return redirectURI;
return redirectURI.substr(0, colon);
}
//==========================================================================
// Class: OAuth2Interface
// Function: GenerateSecurityStateKey
//
// Description: Generates a random UString::String of characters to use as a
// security state key.
//
// Input Arguments:
// None
//
// Output Arguments:
// None
//
// Return Value:
// UString::String
//
//==========================================================================
UString::String OAuth2Interface::GenerateSecurityStateKey() const
{
UString::String stateKey;
while (stateKey.length() < 30)
stateKey.append(Base36Encode((int64_t)rand()
* (int64_t)rand() * (int64_t)rand() * (int64_t)rand()));
return stateKey;
}
//==========================================================================
// Class: OAuth2Interface
// Function: Base36Encode
//
// Description: Encodes the specified value in base36.
//
// Input Arguments:
// value = const int64_t&
//
// Output Arguments:
// None
//
// Return Value:
// UString::String
//
//==========================================================================
UString::String OAuth2Interface::Base36Encode(const int64_t &value)
{
const unsigned int maxDigits(35);
const char* charset = "abcdefghijklmnopqrstuvwxyz0123456789";
UString::String buf;
buf.reserve(maxDigits);
int64_t v(value);
do
{
buf += charset[std::abs(v % 36)];
v /= 36;
} while (v);
std::reverse(buf.begin(), buf.end());
return buf;
}