This commit is contained in:
parent
faa3cc102f
commit
e7c5a672ff
@ -1,4 +1,4 @@
|
||||
package gpaste
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.t-juice.club/torjus/gpaste"
|
||||
"git.t-juice.club/torjus/gpaste/files"
|
||||
"git.t-juice.club/torjus/gpaste/users"
|
||||
"github.com/go-chi/chi/v5"
|
||||
@ -17,14 +18,14 @@ import (
|
||||
type HTTPServer struct {
|
||||
Files files.FileStore
|
||||
Users users.UserStore
|
||||
Auth *AuthService
|
||||
config *ServerConfig
|
||||
Auth *gpaste.AuthService
|
||||
config *gpaste.ServerConfig
|
||||
Logger *zap.SugaredLogger
|
||||
AccessLogger *zap.SugaredLogger
|
||||
http.Server
|
||||
}
|
||||
|
||||
func NewHTTPServer(cfg *ServerConfig) *HTTPServer {
|
||||
func NewHTTPServer(cfg *gpaste.ServerConfig) *HTTPServer {
|
||||
srv := &HTTPServer{
|
||||
config: cfg,
|
||||
Logger: zap.NewNop().Sugar(),
|
||||
@ -32,7 +33,7 @@ func NewHTTPServer(cfg *ServerConfig) *HTTPServer {
|
||||
}
|
||||
srv.Files = files.NewMemoryFileStore()
|
||||
srv.Users = users.NewMemoryUserStore()
|
||||
srv.Auth = NewAuthService(srv.Users, []byte(srv.config.SigningSecret))
|
||||
srv.Auth = gpaste.NewAuthService(srv.Users, []byte(srv.config.SigningSecret))
|
||||
|
||||
// Create initial user
|
||||
// TODO: Do properly
|
||||
@ -202,7 +203,7 @@ func (s *HTTPServer) HandlerAPIUserCreate(w http.ResponseWriter, r *http.Request
|
||||
defer r.Body.Close()
|
||||
|
||||
level, err := AuthLevelFromRequest(r)
|
||||
if err != nil || level < AuthLevelAdmin {
|
||||
if err != nil || level < gpaste.AuthLevelAdmin {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package gpaste_test
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.t-juice.club/torjus/gpaste"
|
||||
"git.t-juice.club/torjus/gpaste/api"
|
||||
"git.t-juice.club/torjus/gpaste/users"
|
||||
)
|
||||
|
||||
@ -22,7 +23,7 @@ func TestHandlers(t *testing.T) {
|
||||
},
|
||||
URL: "http://localhost:8080",
|
||||
}
|
||||
hs := gpaste.NewHTTPServer(cfg)
|
||||
hs := api.NewHTTPServer(cfg)
|
||||
|
||||
t.Run("HandlerIndex", func(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
@ -1,4 +1,4 @@
|
||||
package gpaste
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.t-juice.club/torjus/gpaste"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
@ -65,7 +66,7 @@ func (s *HTTPServer) MiddlewareAuthentication(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), authCtxUsername, claims.Subject)
|
||||
ctx = context.WithValue(ctx, authCtxAuthLevel, AuthLevelUser)
|
||||
ctx = context.WithValue(ctx, authCtxAuthLevel, gpaste.AuthLevelUser)
|
||||
withCtx := r.WithContext(ctx)
|
||||
s.Logger.Debugw("Request is authenticated.", "req_id", reqID, "username", claims.Subject)
|
||||
|
||||
@ -88,14 +89,14 @@ func UsernameFromRequest(r *http.Request) (string, error) {
|
||||
return username, nil
|
||||
}
|
||||
|
||||
func AuthLevelFromRequest(r *http.Request) (AuthLevel, error) {
|
||||
func AuthLevelFromRequest(r *http.Request) (gpaste.AuthLevel, error) {
|
||||
rawLevel := r.Context().Value(authCtxAuthLevel)
|
||||
if rawLevel == nil {
|
||||
return AuthLevelUnset, fmt.Errorf("no username")
|
||||
return gpaste.AuthLevelUnset, fmt.Errorf("no username")
|
||||
}
|
||||
level, ok := rawLevel.(AuthLevel)
|
||||
level, ok := rawLevel.(gpaste.AuthLevel)
|
||||
if !ok {
|
||||
return AuthLevelUnset, fmt.Errorf("no username")
|
||||
return gpaste.AuthLevelUnset, fmt.Errorf("no username")
|
||||
}
|
||||
return level, nil
|
||||
}
|
@ -13,7 +13,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.t-juice.club/torjus/gpaste"
|
||||
"git.t-juice.club/torjus/gpaste/api"
|
||||
"github.com/google/uuid"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.org/x/term"
|
||||
@ -201,7 +201,7 @@ func ActionUserCreate(c *cli.Context) error {
|
||||
defer cancel()
|
||||
|
||||
body := new(bytes.Buffer)
|
||||
requestData := &gpaste.RequestAPIUserCreate{
|
||||
requestData := &api.RequestAPIUserCreate{
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.t-juice.club/torjus/gpaste"
|
||||
"git.t-juice.club/torjus/gpaste/api"
|
||||
"github.com/urfave/cli/v2"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
@ -68,7 +69,7 @@ func ActionServe(c *cli.Context) error {
|
||||
defer httpShutdownCancel()
|
||||
|
||||
go func() {
|
||||
srv := gpaste.NewHTTPServer(cfg)
|
||||
srv := api.NewHTTPServer(cfg)
|
||||
srv.Addr = cfg.ListenAddr
|
||||
srv.Logger = serverLogger
|
||||
srv.AccessLogger = accessLogger
|
||||
|
Loading…
Reference in New Issue
Block a user