-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthService.cs
More file actions
215 lines (191 loc) · 8.93 KB
/
Copy pathAuthService.cs
File metadata and controls
215 lines (191 loc) · 8.93 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ToodledoConsole
{
public class AuthService
{
public const string AuthFile = "auth.txt";
private const string TokenFile = "token.txt";
private const string RedirectUri = "http://localhost:5000/";
private string _clientId = string.Empty;
private string _clientSecret = string.Empty;
private TokenStorage _tokens = new TokenStorage();
private readonly HttpClient _httpClient;
private readonly JsonSerializerOptions _jsonOptions;
public AuthService(HttpClient httpClient, JsonSerializerOptions jsonOptions)
{
_httpClient = httpClient;
_jsonOptions = jsonOptions;
}
public string AccessToken => _tokens.AccessToken;
public bool LoadSecrets()
{
if (!File.Exists(AuthFile)) return false;
var lines = File.ReadAllLines(AuthFile);
if (lines.Length < 2) return false;
_clientId = lines[0].Trim();
_clientSecret = lines[1].Trim();
return !string.IsNullOrEmpty(_clientId) && !string.IsNullOrEmpty(_clientSecret);
}
public void SetSecrets(string clientId, string clientSecret)
{
_clientId = clientId;
_clientSecret = clientSecret;
}
public void SaveSecrets()
{
File.WriteAllLines(AuthFile, new[] { _clientId, _clientSecret });
}
public async Task<bool> InitializeAsync()
{
if (File.Exists(TokenFile))
{
string content = File.ReadAllText(TokenFile);
if (!string.IsNullOrWhiteSpace(content) && content.Trim().StartsWith("{"))
{
_tokens = JsonSerializer.Deserialize<TokenStorage>(content, _jsonOptions) ?? new TokenStorage();
Console.Write("Verifying session... ");
bool authenticated = await CheckConnectionAsync();
if (!authenticated && !string.IsNullOrEmpty(_tokens.RefreshToken))
{
Console.WriteLine("\nAccess expired. Attempting refresh...");
authenticated = await RefreshTokenAsync();
}
return authenticated;
}
}
return false;
}
public async Task<bool> CheckConnectionAsync()
{
try { return (await _httpClient.GetAsync("https://api.toodledo.com/3/account/get.php?access_token=" + _tokens.AccessToken)).IsSuccessStatusCode; }
catch { return false; }
}
public async Task<bool> RefreshTokenAsync()
{
var values = new Dictionary<string, string> { { "grant_type", "refresh_token" }, { "refresh_token", _tokens.RefreshToken }, { "client_id", _clientId }, { "client_secret", _clientSecret } };
var response = await _httpClient.PostAsync("https://api.toodledo.com/3/account/token.php", new FormUrlEncodedContent(values));
if (!response.IsSuccessStatusCode) return false;
var data = JsonSerializer.Deserialize<TokenResponse>(await response.Content.ReadAsStringAsync(), _jsonOptions);
if (data == null) return false;
_tokens.AccessToken = data.access_token; _tokens.RefreshToken = data.refresh_token;
SaveTokens(); return true;
}
public async Task AuthorizeAsync()
{
using var listener = new HttpListener();
listener.Prefixes.Add(RedirectUri);
listener.Start();
Console.WriteLine("Authorize in browser at localhost:5000...");
// Open the browser automatically
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo { FileName = RedirectUri, UseShellExecute = true });
}
catch { }
string? code = null;
string? expectedState = null;
while (string.IsNullOrEmpty(code))
{
var context = await listener.GetContextAsync();
var request = context.Request;
var response = context.Response;
if (request.Url?.AbsolutePath == "/")
{
code = request.QueryString["code"];
string? receivedState = request.QueryString["state"];
if (!string.IsNullOrEmpty(code))
{
// Verify state
if (receivedState != expectedState)
{
byte[] errorBuffer = Encoding.UTF8.GetBytes("<html><body><h1>State verification failed.</h1><p>Possibility of CSRF attack. Please try again.</p></body></html>");
response.StatusCode = 403;
response.ContentType = "text/html; charset=utf-8";
response.ContentLength64 = errorBuffer.Length;
response.OutputStream.Write(errorBuffer, 0, errorBuffer.Length);
response.OutputStream.Close();
code = null; // Reset code to keep loop going or handle exit
continue;
}
// User has returned with code
string successHtml = GetSuccessHtml();
byte[] buffer = Encoding.UTF8.GetBytes(successHtml);
response.ContentType = "text/html; charset=utf-8";
response.ContentEncoding = Encoding.UTF8;
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.OutputStream.Close();
}
else
{
// Redirect to Toodledo Auth
string scope = "basic%20tasks%20notes%20lists%20write";
expectedState = Guid.NewGuid().ToString("N");
string authUrl = $"https://api.toodledo.com/3/account/authorize.php?response_type=code&client_id={_clientId}&state={expectedState}&scope={scope}";
response.Redirect(authUrl);
response.OutputStream.Close();
}
}
else
{
// Ignore other requests (favicon, etc)
response.StatusCode = 404;
response.Close();
}
}
listener.Stop();
var values = new Dictionary<string, string> {
{ "grant_type", "authorization_code" },
{ "code", code },
{ "redirect_uri", RedirectUri },
{ "client_id", _clientId },
{ "client_secret", _clientSecret }
};
var apiResponse = await _httpClient.PostAsync("https://api.toodledo.com/3/account/token.php", new FormUrlEncodedContent(values));
if (apiResponse.IsSuccessStatusCode)
{
var json = await apiResponse.Content.ReadAsStringAsync();
var data = JsonSerializer.Deserialize<TokenResponse>(json, _jsonOptions);
if (data != null)
{
_tokens.AccessToken = data.access_token;
_tokens.RefreshToken = data.refresh_token;
SaveTokens();
}
}
else
{
Console.WriteLine("Error exchanging code for token.");
}
}
private string GetSuccessHtml()
{
return @"
<html>
<head>
<style>
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; text-align: center; padding: 50px; background-color: #f0f2f5; color: #333; }
.container { background: white; padding: 40px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); display: inline-block; max-width: 400px; }
h1 { color: #28a745; margin-bottom: 20px; }
p { font-size: 18px; margin-bottom: 30px; color: #666; }
</style>
</head>
<body>
<div class='container'>
<h1>Authorization Successful!</h1>
<p>You have successfully logged in to Toodledo Console.</p>
<p>You can close this window now and return to the application.</p>
</div>
</body>
</html>";
}
private void SaveTokens() => File.WriteAllText(TokenFile, JsonSerializer.Serialize(_tokens, _jsonOptions));
}
}