forked from WiiLink24/AccountLinker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.cpp
More file actions
114 lines (91 loc) · 3.64 KB
/
oauth.cpp
File metadata and controls
114 lines (91 loc) · 3.64 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
#include "oauth.h"
#include "utils.h"
#include "tinyjson.h"
#include <curl/curl.h>
#include <sys/unistd.h>
#include "config.h"
bool OAuth::StartDeviceFlow() {
const std::string post_data = std::format("client_id={}&scope=openid+profile+email+goauthentik.io/api", CLIENT_ID);
std::vector<std::string> headers = {
std::format("User-Agent: WiiLink Account Linker {}", version),
"Content-Type: application/x-www-form-urlencoded"
};
for (int tries = 0; tries < 5; tries++) {
// We do multiple tries here, as sometimes SSO returns 502 for whatever reason
m_response = http_post(DEVICE_PATH, post_data, headers);
if (m_response.status_code == 200 && m_response.curl_code == CURLE_OK) {
m_device_code = m_response.j["device_code"];
m_user_code = m_response.j["user_code"];
m_interval = m_response.j["interval"];
return true;
}
}
// JSON will probably be empty. Return and display message.
return false;
}
std::string OAuth::GetErrorMessage() const{
// First check if we have a JSON error.
if (!m_response.json_error.empty()) {
return m_response.json_error;
}
// Server will respond with 400 Bad Request when the user has not completed the flow.
// Only check if Curl succeeded.
if (m_response.status_code != 200 && m_response.curl_code == CURLE_OK) {
std::string err = m_response.j["error"];
return std::format("failed with HTTP response code: {}\nError: {}", m_response.status_code, err);
}
// If we got here Curl failed.
return std::format("failed with Curl code: {}", static_cast<int>(m_response.curl_code));
}
bool OAuth::PollToken() {
const std::string post_data = std::format("grant_type=urn:ietf:params:oauth:grant-type:device_code&client_id={}&device_code={}", CLIENT_ID, GetDeviceCode());
std::vector<std::string> headers = {
std::format("User-Agent: WiiLink Account Linker {}", version),
"Content-Type: application/x-www-form-urlencoded"
};
m_response = http_post(TOKEN_PATH, post_data, headers);
if ((m_response.status_code != 200 && m_response.status_code != 400) || m_response.curl_code != CURLE_OK) {
// JSON will probably be empty. Return and display message.
return false;
}
// Retrieve data
if (!m_response.j.contains("error")) {
m_access_token = m_response.j["access_token"];
m_authenticated = true;
}
return true;
}
bool OAuth::PerformLink(std::string_view wwfc_cert) {
std::vector<std::string> headers = {
std::format("User-Agent: WiiLink Account Linker {}", version),
"Accept: application/json",
"Content-Type: application/x-www-form-urlencoded",
std::format("Authorization: {}", m_access_token),
};
auto *config = new NWC24Config();
if (!config->ReadConfig()) {
return false;
}
// Create our payload
const std::string post_data = std::format("wii_num={}&serno={}&cert={}", config->GetWiiNumber(), GetSerialNumber(), curl_easy_escape(nullptr, wwfc_cert.data(), wwfc_cert.length()));
m_response = http_post(USER_UPDATE_PATH, post_data, headers);
if (m_response.status_code != 200 || m_response.curl_code != CURLE_OK) {
// JSON will probably be empty. Return and display message.
return false;
}
bool success = m_response.j["success"];
return success;
}
std::string OAuth::GetDeviceCode() const {
// curl_easy_escape destroys the entire POST body if we apply it. We will only escape the device code.
return std::string{curl_easy_escape(nullptr, m_device_code.c_str(), m_device_code.length())};
}
std::string_view OAuth::GetUserCode() const {
return m_user_code;
}
int OAuth::GetInterval() const {
return m_interval;
}
bool OAuth::IsAuthenticated() const {
return m_authenticated;
}