Skip to content

Commit 21554bf

Browse files
committed
basic auth support for calendar (optional)
1 parent a85b237 commit 21554bf

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
using System.Net.Http.Headers;
2+
using System.Text;
13
using Ical.Net;
24
using ImmichFrame.Core.Helpers;
35
using ImmichFrame.Core.Interfaces;
46
using ImmichFrame.WebApi.Helpers;
7+
using Microsoft.Extensions.Logging;
58

69
public class IcalCalendarService : ICalendarService
710
{
811
private readonly IGeneralSettings _serverSettings;
9-
private readonly IApiCache _appointmentCache = new ApiCache(TimeSpan.FromMinutes(15));
12+
private readonly ILogger<IcalCalendarService> _logger;
13+
private readonly ApiCache<List<IAppointment>> _appointmentCache = new(TimeSpan.FromMinutes(15));
1014

11-
public IcalCalendarService(IGeneralSettings serverSettings)
15+
public IcalCalendarService(IGeneralSettings serverSettings, ILogger<IcalCalendarService> logger)
1216
{
17+
_logger = logger;
1318
_serverSettings = serverSettings;
1419
}
1520

@@ -19,7 +24,9 @@ public async Task<List<IAppointment>> GetAppointments()
1924
{
2025
var appointments = new List<IAppointment>();
2126

22-
var icals = await GetCalendars(_serverSettings.Webcalendars);
27+
List<(string? auth, string url)> cals = _serverSettings.Webcalendars.Select(x => x.Contains(';') ? (x.Split(';')[0], x.Split(';')[1]) : (null, x.ToString())).ToList();
28+
29+
var icals = await GetCalendars(cals);
2330

2431
foreach (var ical in icals)
2532
{
@@ -32,29 +39,38 @@ public async Task<List<IAppointment>> GetAppointments()
3239
});
3340
}
3441

35-
public async Task<List<string>> GetCalendars(IEnumerable<string> calendars)
42+
public async Task<List<string>> GetCalendars(IEnumerable<(string? auth, string url)> calendars)
3643
{
3744
var icals = new List<string>();
3845

39-
foreach (var webcal in calendars)
46+
using (HttpClient client = new HttpClient())
4047
{
41-
string httpUrl = webcal.Replace("webcal://", "https://");
42-
43-
using (HttpClient client = new HttpClient())
48+
foreach (var calendar in calendars)
4449
{
50+
_logger.LogDebug($"Loading calendar: {calendar.auth ?? "no auth"} - {calendar.url}");
51+
client.DefaultRequestHeaders.Authorization = null;
52+
53+
string httpUrl = calendar.url.Replace("webcal://", "https://");
54+
55+
if (!string.IsNullOrEmpty(calendar.auth))
56+
{
57+
var byteArray = Encoding.ASCII.GetBytes($"{calendar.auth}");
58+
client.DefaultRequestHeaders.Authorization =
59+
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
60+
}
61+
4562
HttpResponseMessage response = await client.GetAsync(httpUrl);
4663
if (response.IsSuccessStatusCode)
4764
{
4865
icals.Add(await response.Content.ReadAsStringAsync());
4966
}
5067
else
5168
{
52-
throw new Exception("Failed to load calendar data");
69+
_logger.LogError($"Failed to load calendar data from '{httpUrl}' (Status: {response.StatusCode})");
5370
}
5471
}
5572
}
5673

5774
return icals;
5875
}
59-
6076
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
6-
}
7-
}
8-
}
1+
{}

docs/docs/getting-started/configuration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ Weather is enabled by entering an API key. Get yours free from [OpenWeatherMap][
159159
### Calendar
160160
If you are using Google Calendar, more information can be found [here](https://support.google.com/calendar/answer/37648?hl=en#zippy=%2Cget-your-calendar-view-only).
161161

162+
Calendar supports basic authentication:
163+
Example:
164+
No Auth: `https://calendar.google.com/calendar/ical/XXXXXX/public/basic.ics`
165+
With Auth: `username:password;https://calendar.google.com/calendar/ical/XXXXXX/public/basic.ics`
166+
167+
### Metadata
168+
Needs documentation
169+
162170
### Misc
163171
#### Webhook
164172
A webhook to notify an external service is available. This is only enabled when the `Webhook`-Setting is set in your configuration. Your configured Webhook will be notified via `HTTP POST`-request.

0 commit comments

Comments
 (0)