|
| 1 | +package apple |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/charmbracelet/log" |
| 9 | + |
| 10 | + "github.com/inovex/CalendarSync/internal/adapter/port" |
| 11 | + "github.com/inovex/CalendarSync/internal/auth" |
| 12 | + "github.com/inovex/CalendarSync/internal/models" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + baseUrl = "https://caldav.icloud.com" |
| 17 | + timeFormat = "20060102T150405Z" |
| 18 | +) |
| 19 | + |
| 20 | +type AppleCalendarClient interface { |
| 21 | + ListEvents(ctx context.Context, starttime time.Time, endtime time.Time) ([]models.Event, error) |
| 22 | + CreateEvent(ctx context.Context, event models.Event) error |
| 23 | + UpdateEvent(ctx context.Context, event models.Event) error |
| 24 | + DeleteEvent(ctx context.Context, event models.Event) error |
| 25 | + ResolveCalendarID(ctx context.Context, calendarID string) (string, string, error) |
| 26 | + DiscoverCalendars(ctx context.Context) ([]string, error) |
| 27 | + GetCalendarHash() string |
| 28 | +} |
| 29 | + |
| 30 | +type CalendarAPI struct { |
| 31 | + appleClient AppleCalendarClient |
| 32 | + calendarID string |
| 33 | + |
| 34 | + username string |
| 35 | + appPassword string |
| 36 | + authenticated bool |
| 37 | + |
| 38 | + logger *log.Logger |
| 39 | + |
| 40 | + storage auth.Storage |
| 41 | +} |
| 42 | + |
| 43 | +// Assert that the expected interfaces are implemented |
| 44 | +var _ port.Configurable = &CalendarAPI{} |
| 45 | +var _ port.LogSetter = &CalendarAPI{} |
| 46 | + |
| 47 | +func (c *CalendarAPI) SetupAuth(ctx context.Context, credentials auth.Credentials, storage auth.Storage, bindPort uint) error { |
| 48 | + // Reuse OAuth fields for basic auth: |
| 49 | + // clientId = Apple ID |
| 50 | + // clientSecret = App-specific password |
| 51 | + // tenantId = unused (empty) |
| 52 | + |
| 53 | + switch { |
| 54 | + case credentials.Client.Id == "": |
| 55 | + return fmt.Errorf("%s adapter 'clientId' (Apple ID) cannot be empty", c.Name()) |
| 56 | + case credentials.Client.Secret == "": |
| 57 | + return fmt.Errorf("%s adapter 'clientSecret' (app-specific password) cannot be empty", c.Name()) |
| 58 | + case credentials.CalendarId == "": |
| 59 | + return fmt.Errorf("%s adapter 'calendar' cannot be empty", c.Name()) |
| 60 | + } |
| 61 | + |
| 62 | + c.calendarID = credentials.CalendarId |
| 63 | + c.username = credentials.Client.Id |
| 64 | + c.appPassword = credentials.Client.Secret |
| 65 | + c.storage = storage |
| 66 | + c.authenticated = true |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +// Stub for interface compliance |
| 71 | +func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credentials, storage auth.Storage, bindPort uint) error { |
| 72 | + return c.SetupAuth(ctx, credentials, storage, bindPort) |
| 73 | +} |
| 74 | + |
| 75 | +func (c *CalendarAPI) SetupBasicAuth(ctx context.Context, credentials auth.Credentials, storage auth.Storage) error { |
| 76 | + switch { |
| 77 | + case credentials.Client.Id == "": // Using Client.Id as username for Apple ID |
| 78 | + return fmt.Errorf("%s adapter 'username' (Apple ID) cannot be empty", c.Name()) |
| 79 | + case credentials.Client.Secret == "": // Using Client.Secret as app-specific password |
| 80 | + return fmt.Errorf("%s adapter 'appPassword' cannot be empty", c.Name()) |
| 81 | + case credentials.CalendarId == "": |
| 82 | + return fmt.Errorf("%s adapter 'calendar' cannot be empty", c.Name()) |
| 83 | + } |
| 84 | + |
| 85 | + c.calendarID = credentials.CalendarId |
| 86 | + c.username = credentials.Client.Id |
| 87 | + c.appPassword = credentials.Client.Secret |
| 88 | + c.storage = storage |
| 89 | + |
| 90 | + // For Apple CalDAV, we don't need OAuth2, just basic auth with app-specific password |
| 91 | + c.authenticated = true |
| 92 | + c.logger.Debug("Apple adapter configured with basic auth") |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func (c *CalendarAPI) Initialize(ctx context.Context, openBrowser bool, config map[string]interface{}) error { |
| 98 | + if !c.authenticated { |
| 99 | + return fmt.Errorf("Apple adapter not properly authenticated") |
| 100 | + } |
| 101 | + |
| 102 | + c.appleClient = &ACalClient{ |
| 103 | + Username: c.username, |
| 104 | + AppPassword: c.appPassword, |
| 105 | + CalendarID: c.calendarID, |
| 106 | + } |
| 107 | + |
| 108 | + // Verify calendar resolution and discovery |
| 109 | + calendars, err := c.appleClient.DiscoverCalendars(ctx) |
| 110 | + if err != nil { |
| 111 | + log.Errorf("Calendar discovery failed: %v", err) |
| 112 | + } else { |
| 113 | + log.Infof("Discovered calendars: %v", calendars) |
| 114 | + |
| 115 | + principalID, resolvedID, err := c.appleClient.ResolveCalendarID(ctx, c.calendarID) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("failed to resolve calendar '%s': %w", c.calendarID, err) |
| 118 | + } |
| 119 | + log.Infof("Successfully resolved calendar '%s' to %s/%s", c.calendarID, principalID, resolvedID) |
| 120 | + } |
| 121 | + |
| 122 | + c.logger.Debug("Apple CalDAV client initialized") |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func (c *CalendarAPI) EventsInTimeframe(ctx context.Context, start time.Time, end time.Time) ([]models.Event, error) { |
| 127 | + events, err := c.appleClient.ListEvents(ctx, start, end) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + c.logger.Infof("loaded %d events between %s and %s.", len(events), start.Format(time.DateOnly), end.Format(time.DateOnly)) |
| 133 | + |
| 134 | + return events, nil |
| 135 | +} |
| 136 | + |
| 137 | +func (c *CalendarAPI) CreateEvent(ctx context.Context, e models.Event) error { |
| 138 | + err := c.appleClient.CreateEvent(ctx, e) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + c.logger.Info("Event created", "title", e.ShortTitle(), "time", e.StartTime.String()) |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func (c *CalendarAPI) UpdateEvent(ctx context.Context, e models.Event) error { |
| 149 | + err := c.appleClient.UpdateEvent(ctx, e) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + c.logger.Info("Event updated", "title", e.ShortTitle(), "time", e.StartTime.String()) |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +func (c *CalendarAPI) DeleteEvent(ctx context.Context, e models.Event) error { |
| 160 | + err := c.appleClient.DeleteEvent(ctx, e) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + c.logger.Info("Event deleted", "title", e.ShortTitle(), "time", e.StartTime.String()) |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func (c *CalendarAPI) GetCalendarHash() string { |
| 171 | + return c.appleClient.GetCalendarHash() |
| 172 | +} |
| 173 | + |
| 174 | +func (c *CalendarAPI) Name() string { |
| 175 | + return "Apple CalDAV" |
| 176 | +} |
| 177 | + |
| 178 | +func (c *CalendarAPI) SetLogger(logger *log.Logger) { |
| 179 | + c.logger = logger |
| 180 | +} |
0 commit comments