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