-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlastfm_auth.cpp
More file actions
167 lines (137 loc) · 4.19 KB
/
lastfm_auth.cpp
File metadata and controls
167 lines (137 loc) · 4.19 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
//
// lastfm_auth.cpp
// foo_scrobbler_win
//
// (c) 2025-2026 by Konstantinos Kyriakopoulos
//
#include "stdafx.h"
#include "sdk_bootstrap.h"
#include "lastfm_auth.h"
#include "lastfm_no.h"
#include "lastfm_util.h"
#include "debug.h"
#include <foobar2000/SDK/foobar2000.h>
#include <map>
#include <string>
namespace
{
static std::string lastfmPendingToken;
}
bool hasPendingToken()
{
return !lastfmPendingToken.empty();
}
// Get token & browser URL
bool beginAuth(std::string& outAuthUrl)
{
outAuthUrl.clear();
lastfmPendingToken.clear();
const std::string apiKey = __key();
const std::string apiSecret = __sec();
if (apiKey.empty() || apiSecret.empty())
{
LFM_INFO("API key/secret not configured.");
return false;
}
std::map<std::string, std::string> params = {
{"api_key", apiKey},
{"method", "auth.getToken"},
};
std::string sig;
for (const auto& kv : params)
{
sig += kv.first;
sig += kv.second;
}
sig += apiSecret;
sig = lastfm::util::md5HexLower(sig);
pfc::string8 url;
url << "https://ws.audioscrobbler.com/2.0/?method=auth.getToken"
<< "&api_key=" << apiKey.c_str() << "&api_sig=" << sig.c_str() << "&format=json";
pfc::string8 body;
std::string httpError;
if (!lastfm::util::httpGetToString(url, body, httpError))
{
LFM_INFO("auth.getToken request failed: " << (httpError.empty() ? "unknown error" : httpError.c_str()));
return false;
}
std::string token;
if (!lastfm::util::jsonFindStringValue(body.c_str(), "token", token) || token.empty())
{
LFM_INFO("auth.getToken: token not found. (response omitted, size=" << body.get_length() << ")");
return false;
}
lastfmPendingToken = token;
outAuthUrl = "https://www.last.fm/api/auth/"
"?api_key=" +
apiKey + "&token=" + lastfmPendingToken;
return true;
}
// Using the stored token, call auth.getSession
bool completeAuthFromCallbackUrl(const std::string& callbackUrl, LastfmAuthState& authState)
{
(void)callbackUrl;
if (lastfmPendingToken.empty())
{
LFM_DEBUG("No pending token. Run beginAuth() first.");
return false;
}
const std::string apiKey = __key();
const std::string apiSecret = __sec();
if (apiKey.empty() || apiSecret.empty())
{
LFM_INFO("API key/secret not configured.");
return false;
}
std::map<std::string, std::string> params = {
{"api_key", apiKey},
{"method", "auth.getSession"},
{"token", lastfmPendingToken},
};
std::string sig;
for (const auto& kv : params)
{
sig += kv.first;
sig += kv.second;
}
sig += apiSecret;
sig = lastfm::util::md5HexLower(sig);
pfc::string8 url;
url << "https://ws.audioscrobbler.com/2.0/?method=auth.getSession"
<< "&api_key=" << apiKey.c_str() << "&api_sig=" << sig.c_str() << "&token=" << lastfmPendingToken.c_str()
<< "&format=json";
pfc::string8 body;
std::string httpError;
if (!lastfm::util::httpGetToString(url, body, httpError))
{
LFM_INFO("auth.getSession request failed: " << (httpError.empty() ? "unknown error" : httpError.c_str()));
return false;
}
std::string name;
std::string key;
if (!lastfm::util::jsonFindStringValue(body.c_str(), "name", name) || name.empty())
{
LFM_INFO("auth.getSession: username not found. (response omitted, size=" << body.get_length() << ")");
return false;
}
if (!lastfm::util::jsonFindStringValue(body.c_str(), "key", key) || key.empty())
{
LFM_INFO("auth.getSession: session key not found. (response omitted, size=" << body.get_length() << ")");
return false;
}
authState.username = name;
authState.sessionKey = key;
authState.isAuthenticated = true;
LFM_INFO("Authentication complete. User: " << authState.username.c_str());
lastfmPendingToken.clear();
return true;
}
void logout()
{
LFM_INFO("Clearing stored Last.fm session.");
LastfmAuthState state;
state.isAuthenticated = false;
state.username.clear();
state.sessionKey.clear();
setAuthState(state);
}