2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2019-05-22 19:48:48 +02:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2019-05-22 19:48:48 +02:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2019-05-22 19:48:48 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
package caldav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-10-11 22:10:03 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-07-11 15:03:50 +02:00
|
|
|
caldav2 "code.vikunja.io/api/pkg/caldav"
|
2019-05-22 19:48:48 +02:00
|
|
|
"code.vikunja.io/api/pkg/log"
|
|
|
|
"code.vikunja.io/api/pkg/models"
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2019-05-22 19:48:48 +02:00
|
|
|
"code.vikunja.io/web/handler"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/samedi/caldav-go"
|
|
|
|
"github.com/samedi/caldav-go/lib"
|
|
|
|
)
|
|
|
|
|
2020-04-01 22:51:57 +02:00
|
|
|
func getBasicAuthUserFromContext(c echo.Context) (*user.User, error) {
|
|
|
|
u, is := c.Get("userBasicAuth").(*user.User)
|
2019-05-22 19:48:48 +02:00
|
|
|
if !is {
|
2020-04-01 22:51:57 +02:00
|
|
|
return &user.User{}, fmt.Errorf("user is not user element, is %s", reflect.TypeOf(c.Get("userBasicAuth")))
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListHandler returns all tasks from a list
|
|
|
|
func ListHandler(c echo.Context) error {
|
|
|
|
listID, err := getIntParam(c, "list")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := getBasicAuthUserFromContext(c)
|
|
|
|
if err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error(err)
|
2019-05-22 19:48:48 +02:00
|
|
|
return echo.ErrInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := &VikunjaCaldavListStorage{
|
|
|
|
list: &models.List{ID: listID},
|
2020-04-01 22:51:57 +02:00
|
|
|
user: u,
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to parse a task from the request payload
|
|
|
|
body, _ := ioutil.ReadAll(c.Request().Body)
|
|
|
|
// Restore the io.ReadCloser to its original state
|
|
|
|
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
|
|
|
// Parse it
|
|
|
|
vtodo := string(body)
|
|
|
|
if vtodo != "" && strings.HasPrefix(vtodo, `BEGIN:VCALENDAR`) {
|
2021-07-11 15:03:50 +02:00
|
|
|
storage.task, err = caldav2.ParseTaskFromVTODO(vtodo)
|
2019-05-22 19:48:48 +02:00
|
|
|
if err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error(err)
|
2019-05-22 19:48:48 +02:00
|
|
|
return echo.ErrInternalServerError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Debugf("[CALDAV] Request Body: %v\n", string(body))
|
|
|
|
log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header)
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
caldav.SetupStorage(storage)
|
|
|
|
caldav.SetupUser("dav/lists")
|
|
|
|
caldav.SetupSupportedComponents([]string{lib.VCALENDAR, lib.VTODO})
|
|
|
|
response := caldav.HandleRequest(c.Request())
|
|
|
|
response.Write(c.Response())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskHandler is the handler which manages updating/deleting a single task
|
|
|
|
func TaskHandler(c echo.Context) error {
|
|
|
|
listID, err := getIntParam(c, "list")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := getBasicAuthUserFromContext(c)
|
|
|
|
if err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error(err)
|
2019-05-22 19:48:48 +02:00
|
|
|
return echo.ErrInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the task uid
|
|
|
|
taskUID := strings.TrimSuffix(c.Param("task"), ".ics")
|
|
|
|
|
|
|
|
storage := &VikunjaCaldavListStorage{
|
|
|
|
list: &models.List{ID: listID},
|
2019-08-14 22:19:04 +02:00
|
|
|
task: &models.Task{UID: taskUID},
|
2020-04-01 22:51:57 +02:00
|
|
|
user: u,
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
caldav.SetupStorage(storage)
|
|
|
|
response := caldav.HandleRequest(c.Request())
|
|
|
|
response.Write(c.Response())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrincipalHandler handles all request to principal resources
|
|
|
|
func PrincipalHandler(c echo.Context) error {
|
|
|
|
u, err := getBasicAuthUserFromContext(c)
|
|
|
|
if err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error(err)
|
2019-05-22 19:48:48 +02:00
|
|
|
return echo.ErrInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := &VikunjaCaldavListStorage{
|
2020-04-01 22:51:57 +02:00
|
|
|
user: u,
|
2019-05-22 19:48:48 +02:00
|
|
|
isPrincipal: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to parse a task from the request payload
|
|
|
|
body, _ := ioutil.ReadAll(c.Request().Body)
|
|
|
|
// Restore the io.ReadCloser to its original state
|
|
|
|
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
|
|
|
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Debugf("[CALDAV] Request Body: %v\n", string(body))
|
|
|
|
log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header)
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
caldav.SetupStorage(storage)
|
|
|
|
caldav.SetupUser("dav/principals/" + u.Username)
|
|
|
|
caldav.SetupSupportedComponents([]string{lib.VCALENDAR, lib.VTODO})
|
|
|
|
|
|
|
|
response := caldav.HandleRequest(c.Request())
|
|
|
|
response.Write(c.Response())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EntryHandler handles all request to principal resources
|
|
|
|
func EntryHandler(c echo.Context) error {
|
|
|
|
u, err := getBasicAuthUserFromContext(c)
|
|
|
|
if err != nil {
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Error(err)
|
2019-05-22 19:48:48 +02:00
|
|
|
return echo.ErrInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
storage := &VikunjaCaldavListStorage{
|
2020-04-01 22:51:57 +02:00
|
|
|
user: u,
|
2019-05-22 19:48:48 +02:00
|
|
|
isEntry: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to parse a task from the request payload
|
|
|
|
body, _ := ioutil.ReadAll(c.Request().Body)
|
|
|
|
// Restore the io.ReadCloser to its original state
|
|
|
|
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
|
|
|
|
2019-07-20 20:12:10 +02:00
|
|
|
log.Debugf("[CALDAV] Request Body: %v\n", string(body))
|
|
|
|
log.Debugf("[CALDAV] Request Headers: %v\n", c.Request().Header)
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
caldav.SetupStorage(storage)
|
|
|
|
caldav.SetupUser("dav/principals/" + u.Username)
|
|
|
|
caldav.SetupSupportedComponents([]string{lib.VCALENDAR, lib.VTODO})
|
|
|
|
|
|
|
|
response := caldav.HandleRequest(c.Request())
|
|
|
|
response.Write(c.Response())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIntParam(c echo.Context, paramName string) (intParam int64, err error) {
|
|
|
|
param := c.Param(paramName)
|
|
|
|
if param == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
intParam, err = strconv.ParseInt(param, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, handler.HandleHTTPError(err, c)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|