diff options
Diffstat (limited to 'api/handlers/ical.go')
| -rw-r--r-- | api/handlers/ical.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/api/handlers/ical.go b/api/handlers/ical.go new file mode 100644 index 0000000..c4b3989 --- /dev/null +++ b/api/handlers/ical.go @@ -0,0 +1,43 @@ +package handlers + +import ( + "crypto/subtle" + + "github.com/LMBishop/confplanner/api/dto" + "github.com/LMBishop/confplanner/pkg/calendar" + "github.com/LMBishop/confplanner/pkg/ical" + "github.com/gofiber/fiber/v2" +) + +func GetIcal(icalService ical.Service, calendarService calendar.Service) fiber.Handler { + return func(c *fiber.Ctx) error { + name := c.Query("name") + key := c.Query("key") + + if name == "" || key == "" { + return &dto.ErrorResponse{ + Code: fiber.StatusBadRequest, + Message: "Both name and key must be specified", + } + } + + calendar, err := calendarService.GetCalendarByName(name) + if err != nil { + return err + } + + if subtle.ConstantTimeCompare([]byte(key), []byte(calendar.Key)) != 1 { + return &dto.ErrorResponse{ + Code: fiber.StatusUnauthorized, + Message: "Invalid key", + } + } + + ical, err := icalService.GenerateIcalForCalendar(*calendar) + if err != nil { + return err + } + + return c.SendString(ical) + } +} |
