Fix CORS by allowing everything

This commit is contained in:
Alex 2023-10-16 16:10:14 +02:00
parent 224cb5e217
commit 75b27becf2
2 changed files with 51 additions and 14 deletions

30
cors.go Normal file
View file

@ -0,0 +1,30 @@
package main
import (
"net/http"
)
type CorsAllowAllOrigins struct {
AndThen http.Handler
}
func (c CorsAllowAllOrigins) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Methods", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
c.AndThen.ServeHTTP(w, r)
}
type OptionsNoError struct {
Error ErrorHandler
}
func (c OptionsNoError) WithError(err error) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
w.WriteHeader(200)
} else {
c.Error.WithError(err).ServeHTTP(w, r)
}
})
}

23
main.go
View file

@ -3,17 +3,18 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"git.deuxfleurs.fr/Deuxfleurs/bagage/s3"
"git.deuxfleurs.fr/Deuxfleurs/bagage/sftp"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"golang.org/x/crypto/ssh"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
"os" "os"
"git.deuxfleurs.fr/Deuxfleurs/bagage/s3"
"git.deuxfleurs.fr/Deuxfleurs/bagage/sftp"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"golang.org/x/crypto/ssh"
) )
func main() { func main() {
@ -176,11 +177,16 @@ func handleSSHConn(nConn net.Conn, dconfig *Config, config *ssh.ServerConfig) {
func httpServer(config *Config, done chan error) { func httpServer(config *Config, done chan error) {
// Assemble components to handle WebDAV requests // Assemble components to handle WebDAV requests
http.Handle(config.DavPath+"/", http.Handle(config.DavPath+"/",
BasicAuthExtract{ CorsAllowAllOrigins{
OnNotFound: NotAuthorized{}, AndThen: BasicAuthExtract{
OnNotFound: OptionsNoError{
NotAuthorized{},
},
OnCreds: LdapPreAuth{ OnCreds: LdapPreAuth{
WithConfig: config, WithConfig: config,
OnWrongPassword: NotAuthorized{}, OnWrongPassword: OptionsNoError{
Error: NotAuthorized{},
},
OnFailure: InternalError{}, OnFailure: InternalError{},
OnCreds: S3Auth{ OnCreds: S3Auth{
WithConfig: config, WithConfig: config,
@ -190,6 +196,7 @@ func httpServer(config *Config, done chan error) {
}, },
}, },
}, },
},
}) })
if err := http.ListenAndServe(config.HttpListen, nil); err != nil { if err := http.ListenAndServe(config.HttpListen, nil); err != nil {