mirror of
https://github.com/1f349/jasmine.git
synced 2024-12-21 15:14:18 +00:00
Major rewrite
This commit is contained in:
parent
9b56087ee9
commit
c4c2d8bd33
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
||||
.idea/
|
||||
.data/
|
||||
config.json
|
||||
|
62
auth.go
62
auth.go
@ -1,62 +0,0 @@
|
||||
package jasmine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type contextKey int
|
||||
|
||||
var authCtxKey contextKey = 0
|
||||
|
||||
type Context struct {
|
||||
UserName string
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, a *Context) context.Context {
|
||||
return context.WithValue(ctx, authCtxKey, a)
|
||||
}
|
||||
|
||||
func FromContext(ctx context.Context) (*Context, bool) {
|
||||
a, ok := ctx.Value(authCtxKey).(*Context)
|
||||
return a, ok
|
||||
}
|
||||
|
||||
type ProviderMiddleware interface {
|
||||
Middleware(next http.Handler) http.Handler
|
||||
}
|
||||
|
||||
var authError = errors.New("auth context error")
|
||||
|
||||
type Auth struct{}
|
||||
|
||||
func (u *Auth) Middleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Authorization") == "" {
|
||||
w.Header().Add("WWW-Authenticate", `Basic realm="Please provide a password", charset="UTF-8"`)
|
||||
http.Error(w, "HTTP auth is required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
username, accessToken, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
http.Error(w, "Authorization invalid", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: verify accessToken later
|
||||
_ = accessToken
|
||||
|
||||
r = r.WithContext(NewContext(r.Context(), &Context{UserName: username}))
|
||||
r.BasicAuth()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (u *Auth) CurrentUserPrincipal(ctx context.Context) (string, error) {
|
||||
authCtx, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
return "", authError
|
||||
}
|
||||
return "/" + authCtx.UserName + "/", nil
|
||||
}
|
6
go.mod
6
go.mod
@ -4,20 +4,22 @@ go 1.22
|
||||
|
||||
require (
|
||||
git.sr.ht/~sircmpwn/tokidoki v0.0.0-20240419154046-4ca7d8c4e7ca
|
||||
github.com/1f349/cardcaldav v0.0.3
|
||||
github.com/1f349/violet v0.0.13
|
||||
github.com/charmbracelet/log v0.4.0
|
||||
github.com/emersion/go-webdav v0.5.1-0.20240419143909-21f251fa1de2
|
||||
github.com/google/subcommands v1.2.0
|
||||
github.com/mrmelon54/exit-reload v0.0.2
|
||||
github.com/rs/zerolog v1.32.0
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/charmbracelet/lipgloss v0.10.0 // indirect
|
||||
github.com/emersion/go-ical v0.0.0-20240127095438-fc1c9d8fb2b6 // indirect
|
||||
github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9 // indirect
|
||||
github.com/go-logfmt/logfmt v0.6.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
@ -25,7 +27,9 @@ require (
|
||||
github.com/muesli/reflow v0.3.0 // indirect
|
||||
github.com/muesli/termenv v0.15.2 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/rs/zerolog v1.32.0 // indirect
|
||||
github.com/teambition/rrule-go v1.8.2 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
|
||||
golang.org/x/net v0.25.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
|
8
go.sum
8
go.sum
@ -1,5 +1,9 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
git.sr.ht/~sircmpwn/tokidoki v0.0.0-20240419154046-4ca7d8c4e7ca h1:HBRu4nwicaaZ4oqwTis2qy4Gb/CKLA2OSlibdKpMhb8=
|
||||
git.sr.ht/~sircmpwn/tokidoki v0.0.0-20240419154046-4ca7d8c4e7ca/go.mod h1:/1jsi+vx9b/T8UHrDRJXwoY8td9JUXd9UajtVCpVoeg=
|
||||
github.com/1f349/cardcaldav v0.0.3 h1:c2/wmOf+hwgKdaWLlKm5v8l0g9brD+Seqcp+xIDUXJI=
|
||||
github.com/1f349/cardcaldav v0.0.3/go.mod h1:MCoGRimM7p/rLWtaJxYO91tsvgXZ47M69X3lldb5dfk=
|
||||
github.com/1f349/violet v0.0.13 h1:lJpTz15Ea83Uc1VAISXTjtKuzr8Pe8NM4cMGp3Aiyhk=
|
||||
github.com/1f349/violet v0.0.13/go.mod h1:Ga5/hWqI+EkR6J1mAMNzs7aJhuGcA89XFqgQaDXC7Jo=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||
@ -19,6 +23,8 @@ github.com/emersion/go-webdav v0.5.1-0.20240419143909-21f251fa1de2 h1:k/NO/RfeXF
|
||||
github.com/emersion/go-webdav v0.5.1-0.20240419143909-21f251fa1de2/go.mod h1:mI8iBx3RAODwX7PJJ7qzsKAKs/vY429YfS2/9wKnDbQ=
|
||||
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
|
||||
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
|
||||
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
|
||||
@ -53,6 +59,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/teambition/rrule-go v1.8.2 h1:lIjpjvWTj9fFUZCmuoVDrKVOtdiyzbzc93qTmRVe/J8=
|
||||
github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5mD/wLBGl3p4=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
|
||||
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
|
||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||
|
42
logger.go
42
logger.go
@ -2,10 +2,7 @@ package jasmine
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/rs/zerolog"
|
||||
log2 "github.com/rs/zerolog/log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Logger = log.NewWithOptions(os.Stderr, log.Options{
|
||||
@ -13,42 +10,3 @@ var Logger = log.NewWithOptions(os.Stderr, log.Options{
|
||||
ReportTimestamp: true,
|
||||
Prefix: "Jasmine",
|
||||
})
|
||||
|
||||
type zeroToCharmLogger struct{}
|
||||
|
||||
func (z *zeroToCharmLogger) Write(p []byte) (n int, err error) {
|
||||
s := string(p)
|
||||
if len(s) <= 3 {
|
||||
return len(p), nil
|
||||
}
|
||||
inLevel := s[:3]
|
||||
var level log.Level
|
||||
switch inLevel {
|
||||
case "TRC", "DBG":
|
||||
level = log.DebugLevel
|
||||
case "INF":
|
||||
level = log.InfoLevel
|
||||
case "WRN":
|
||||
level = log.WarnLevel
|
||||
case "ERR":
|
||||
level = log.ErrorLevel
|
||||
case "FTL", "PNC":
|
||||
level = log.FatalLevel
|
||||
}
|
||||
Logger.Helper()
|
||||
translator := Logger.With()
|
||||
translator.SetCallerFormatter(func(s string, i int, s2 string) string {
|
||||
return "tokidoki internal"
|
||||
})
|
||||
translator.Log(level, strings.TrimSpace(s[4:]))
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
log2.Logger = log2.Output(zerolog.ConsoleWriter{
|
||||
Out: &zeroToCharmLogger{},
|
||||
FormatTimestamp: func(i interface{}) string {
|
||||
return ""
|
||||
},
|
||||
})
|
||||
}
|
||||
|
23
server.go
23
server.go
@ -2,6 +2,7 @@ package jasmine
|
||||
|
||||
import (
|
||||
"git.sr.ht/~sircmpwn/tokidoki/storage"
|
||||
"github.com/1f349/cardcaldav"
|
||||
"github.com/emersion/go-webdav"
|
||||
"github.com/emersion/go-webdav/caldav"
|
||||
"net/http"
|
||||
@ -11,10 +12,11 @@ import (
|
||||
|
||||
type Conf struct {
|
||||
Listen string `json:"listen"`
|
||||
DB string `json:"db"`
|
||||
}
|
||||
|
||||
type jasmineHandler struct {
|
||||
auth *Auth
|
||||
auth *cardcaldav.Auth
|
||||
backend caldav.Backend
|
||||
}
|
||||
|
||||
@ -54,8 +56,8 @@ func (j *jasmineHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
func NewHttpServer(conf Conf, wd string) *http.Server {
|
||||
// TODO: database auth
|
||||
principle := &Auth{}
|
||||
cardcaldav.SetupLogger(Logger)
|
||||
principle := cardcaldav.NewAuth(conf.DB, Logger)
|
||||
|
||||
calStorage, _, err := storage.NewFilesystem(filepath.Join(wd, "storage"), "/calendar/", "/contacts/", principle)
|
||||
if err != nil {
|
||||
@ -90,18 +92,3 @@ func NewHttpServer(conf Conf, wd string) *http.Server {
|
||||
MaxHeaderBytes: 2500,
|
||||
}
|
||||
}
|
||||
|
||||
func MainHandler(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
rw.Header().Set("Dav", "1, 2, 3, calendar-access, extended-mkcol")
|
||||
if (req.Method == http.MethodHead || req.Method == http.MethodGet || req.Method == http.MethodPost) && req.RequestURI == "/" {
|
||||
if req.Method == http.MethodHead {
|
||||
return
|
||||
}
|
||||
http.Error(rw, "Jasmine API Endpoint", http.StatusOK)
|
||||
return
|
||||
}
|
||||
Logger.Info("Request", "method", req.Method, "url", req.URL.String())
|
||||
next.ServeHTTP(rw, req)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user