Compare commits
	
		
			15 Commits
		
	
	
		
			733c0410fe
			...
			v0.3.7
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| bde2a38931 | |||
| 1a3ebcb1df | |||
| 17a484db91 | |||
| 889894a737 | |||
| 0e76cad5d7 | |||
| f664c886eb | |||
| 121b0396fa | |||
| 6822cd6bfe | |||
| db41f565ca | |||
| 20cb97f90f | |||
| ed4a10c966 | |||
| ff8c6aca64 | |||
| d583db5450 | |||
| 88d9a76785 | |||
| 193b0d3926 | 
@@ -4,7 +4,7 @@ pipeline:
 | 
				
			|||||||
    commands:
 | 
					    commands:
 | 
				
			||||||
      - go build -o gpaste-client ./cmd/client/client.go
 | 
					      - go build -o gpaste-client ./cmd/client/client.go
 | 
				
			||||||
      - go build -o gpaste-server ./cmd/server/server.go
 | 
					      - go build -o gpaste-server ./cmd/server/server.go
 | 
				
			||||||
      - go test -v ./...
 | 
					      - go test -cover ./...
 | 
				
			||||||
      - go vet ./...
 | 
					      - go vet ./...
 | 
				
			||||||
    when:
 | 
					    when:
 | 
				
			||||||
      branch: master
 | 
					      branch: master
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,4 +10,5 @@ RUN go build -o gpaste-server ./cmd/server/server.go
 | 
				
			|||||||
FROM alpine:latest
 | 
					FROM alpine:latest
 | 
				
			||||||
COPY --from=builder /src/gpaste /bin/gpaste
 | 
					COPY --from=builder /src/gpaste /bin/gpaste
 | 
				
			||||||
COPY --from=builder /src/gpaste-server /bin/gpaste-server
 | 
					COPY --from=builder /src/gpaste-server /bin/gpaste-server
 | 
				
			||||||
 | 
					EXPOSE 8080
 | 
				
			||||||
CMD ["/bin/gpaste-server"]
 | 
					CMD ["/bin/gpaste-server"]
 | 
				
			||||||
							
								
								
									
										35
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# gpaste
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Simple pastebin-style webapp.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Build
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### gpaste-server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```text
 | 
				
			||||||
 | 
					go build -o gpaste-server cmd/server/server.go
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### gpaste-client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```text
 | 
				
			||||||
 | 
					go build -o gpaste cmd/client/client.go
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Run using docker
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### From registry
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```text
 | 
				
			||||||
 | 
					docker pull registry.t-juice.club/gpaste:latest
 | 
				
			||||||
 | 
					docker run --rm -it -p 8080:8080 registry.t-juice.club/gpaste:latest
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### From Dockerfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```text
 | 
				
			||||||
 | 
					docker build -t gpaste:latest .
 | 
				
			||||||
 | 
					docker run --rm -it p 8080:8080 gpaste:latest
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
							
								
								
									
										84
									
								
								api/http.go
									
									
									
									
									
								
							
							
						
						
									
										84
									
								
								api/http.go
									
									
									
									
									
								
							@@ -4,7 +4,9 @@ import (
 | 
				
			|||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.t-juice.club/torjus/gpaste"
 | 
						"git.t-juice.club/torjus/gpaste"
 | 
				
			||||||
	"git.t-juice.club/torjus/gpaste/files"
 | 
						"git.t-juice.club/torjus/gpaste/files"
 | 
				
			||||||
@@ -37,7 +39,7 @@ func NewHTTPServer(cfg *gpaste.ServerConfig) *HTTPServer {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// Create initial user
 | 
						// Create initial user
 | 
				
			||||||
	// TODO: Do properly
 | 
						// TODO: Do properly
 | 
				
			||||||
	user := &users.User{Username: "admin"}
 | 
						user := &users.User{Username: "admin", Role: users.RoleAdmin}
 | 
				
			||||||
	user.SetPassword("admin")
 | 
						user.SetPassword("admin")
 | 
				
			||||||
	srv.Users.Store(user)
 | 
						srv.Users.Store(user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -49,6 +51,7 @@ func NewHTTPServer(cfg *gpaste.ServerConfig) *HTTPServer {
 | 
				
			|||||||
	r.Get("/", srv.HandlerIndex)
 | 
						r.Get("/", srv.HandlerIndex)
 | 
				
			||||||
	r.Post("/api/file", srv.HandlerAPIFilePost)
 | 
						r.Post("/api/file", srv.HandlerAPIFilePost)
 | 
				
			||||||
	r.Get("/api/file/{id}", srv.HandlerAPIFileGet)
 | 
						r.Get("/api/file/{id}", srv.HandlerAPIFileGet)
 | 
				
			||||||
 | 
						r.Delete("/api/file/{id}", srv.HandlerAPIFileDelete)
 | 
				
			||||||
	r.Post("/api/login", srv.HandlerAPILogin)
 | 
						r.Post("/api/login", srv.HandlerAPILogin)
 | 
				
			||||||
	r.Post("/api/user", srv.HandlerAPIUserCreate)
 | 
						r.Post("/api/user", srv.HandlerAPIUserCreate)
 | 
				
			||||||
	srv.Handler = r
 | 
						srv.Handler = r
 | 
				
			||||||
@@ -61,10 +64,6 @@ func (s *HTTPServer) HandlerIndex(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request) {
 | 
					func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	f := &files.File{
 | 
					 | 
				
			||||||
		ID:   uuid.Must(uuid.NewRandom()).String(),
 | 
					 | 
				
			||||||
		Body: r.Body,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	reqID := middleware.GetReqID(r.Context())
 | 
						reqID := middleware.GetReqID(r.Context())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Check if multipart form
 | 
						// Check if multipart form
 | 
				
			||||||
@@ -73,6 +72,11 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
 | 
				
			|||||||
		s.processMultiPartFormUpload(w, r)
 | 
							s.processMultiPartFormUpload(w, r)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						f := fileFromParams(r)
 | 
				
			||||||
 | 
						f.ID = uuid.NewString()
 | 
				
			||||||
 | 
						f.Body = r.Body
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err := s.Files.Store(f)
 | 
						err := s.Files.Store(f)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		w.WriteHeader(http.StatusInternalServerError)
 | 
							w.WriteHeader(http.StatusInternalServerError)
 | 
				
			||||||
@@ -117,6 +121,23 @@ func (s *HTTPServer) HandlerAPIFileGet(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *HTTPServer) HandlerAPIFileDelete(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						// TODO: Require auth
 | 
				
			||||||
 | 
						id := chi.URLParam(r, "id")
 | 
				
			||||||
 | 
						if id == "" {
 | 
				
			||||||
 | 
							w.WriteHeader(http.StatusBadRequest)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := s.Files.Delete(id)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							w.WriteHeader(http.StatusBadRequest)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						reqID := middleware.GetReqID(r.Context())
 | 
				
			||||||
 | 
						s.Logger.Infow("Deleted file", "id", id, "req_id", reqID)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
 | 
					func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	reqID := middleware.GetReqID(r.Context())
 | 
						reqID := middleware.GetReqID(r.Context())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -131,11 +152,10 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
 | 
				
			|||||||
			s.Logger.Warnw("Error reading file from multipart form.", "req_id", reqID, "error", err)
 | 
								s.Logger.Warnw("Error reading file from multipart form.", "req_id", reqID, "error", err)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		f := &files.File{
 | 
							f := fileFromParams(r)
 | 
				
			||||||
			ID:               uuid.Must(uuid.NewRandom()).String(),
 | 
							f.ID = uuid.NewString()
 | 
				
			||||||
			OriginalFilename: fh.Filename,
 | 
							f.OriginalFilename = fh.Filename
 | 
				
			||||||
			Body:             ff,
 | 
							f.Body = ff
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := s.Files.Store(f); err != nil {
 | 
							if err := s.Files.Store(f); err != nil {
 | 
				
			||||||
			w.WriteHeader(http.StatusInternalServerError)
 | 
								w.WriteHeader(http.StatusInternalServerError)
 | 
				
			||||||
@@ -202,7 +222,7 @@ func (s *HTTPServer) HandlerAPIUserCreate(w http.ResponseWriter, r *http.Request
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// TODO: Ensure user does not already exist
 | 
						// TODO: Ensure user does not already exist
 | 
				
			||||||
	user := &users.User{Username: req.Username}
 | 
						user := &users.User{Username: req.Username, Role: users.RoleUser}
 | 
				
			||||||
	if err := user.SetPassword(req.Password); err != nil {
 | 
						if err := user.SetPassword(req.Password); err != nil {
 | 
				
			||||||
		s.Logger.Warnw("Error setting user password.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
 | 
							s.Logger.Warnw("Error setting user password.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
 | 
				
			||||||
		w.WriteHeader(http.StatusBadRequest)
 | 
							w.WriteHeader(http.StatusBadRequest)
 | 
				
			||||||
@@ -217,3 +237,45 @@ func (s *HTTPServer) HandlerAPIUserCreate(w http.ResponseWriter, r *http.Request
 | 
				
			|||||||
	w.WriteHeader(http.StatusAccepted)
 | 
						w.WriteHeader(http.StatusAccepted)
 | 
				
			||||||
	s.Logger.Infow("Created user.", "req_id", reqID, "remote_addr", r.RemoteAddr, "username", req.Username)
 | 
						s.Logger.Infow("Created user.", "req_id", reqID, "remote_addr", r.RemoteAddr, "username", req.Username)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (s *HTTPServer) HandlerAPIUserList(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						reqID := middleware.GetReqID(r.Context())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						l, err := s.Users.List()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							s.Logger.Warnw("Error listing users.", "req_id", reqID, "error", err)
 | 
				
			||||||
 | 
							w.WriteHeader(http.StatusInternalServerError)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						encoder := json.NewEncoder(w)
 | 
				
			||||||
 | 
						if err := encoder.Encode(l); err != nil {
 | 
				
			||||||
 | 
							s.Logger.Warnw("Error encoding response.", "req_id", "error", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func fileFromParams(r *http.Request) *files.File {
 | 
				
			||||||
 | 
						const (
 | 
				
			||||||
 | 
							keyMaxViews  = "max_views"
 | 
				
			||||||
 | 
							keyExpiresOn = "exp"
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						var f files.File
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						q := r.URL.Query()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if q.Has(keyMaxViews) {
 | 
				
			||||||
 | 
							views, err := strconv.ParseUint(q.Get(keyMaxViews), 10, 64)
 | 
				
			||||||
 | 
							if err == nil {
 | 
				
			||||||
 | 
								f.MaxViews = uint(views)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if q.Has(keyExpiresOn) {
 | 
				
			||||||
 | 
							exp, err := time.Parse(time.RFC3339, q.Get(keyExpiresOn))
 | 
				
			||||||
 | 
							if err == nil {
 | 
				
			||||||
 | 
								f.ExpiresOn = exp
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &f
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										170
									
								
								api/http_test.go
									
									
									
									
									
								
							
							
						
						
									
										170
									
								
								api/http_test.go
									
									
									
									
									
								
							@@ -8,24 +8,30 @@ import (
 | 
				
			|||||||
	"mime/multipart"
 | 
						"mime/multipart"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/http/httptest"
 | 
						"net/http/httptest"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"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/api"
 | 
				
			||||||
 | 
						"git.t-juice.club/torjus/gpaste/files"
 | 
				
			||||||
	"git.t-juice.club/torjus/gpaste/users"
 | 
						"git.t-juice.club/torjus/gpaste/users"
 | 
				
			||||||
 | 
						"github.com/google/go-cmp/cmp"
 | 
				
			||||||
 | 
						"github.com/google/uuid"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestHandlers(t *testing.T) {
 | 
					func TestHandlers(t *testing.T) {
 | 
				
			||||||
	cfg := &gpaste.ServerConfig{
 | 
						//cfg := &gpaste.ServerConfig{
 | 
				
			||||||
		SigningSecret: "abc123",
 | 
						//	SigningSecret: "abc123",
 | 
				
			||||||
		Store: &gpaste.ServerStoreConfig{
 | 
						//	Store: &gpaste.ServerStoreConfig{
 | 
				
			||||||
			Type: "memory",
 | 
						//		Type: "memory",
 | 
				
			||||||
		},
 | 
						//	},
 | 
				
			||||||
		URL: "http://localhost:8080",
 | 
						//	URL: "http://localhost:8080",
 | 
				
			||||||
	}
 | 
						//}
 | 
				
			||||||
	hs := api.NewHTTPServer(cfg)
 | 
						//hs := api.NewHTTPServer(cfg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	t.Run("HandlerIndex", func(t *testing.T) {
 | 
						t.Run("index", func(t *testing.T) {
 | 
				
			||||||
 | 
							hs := newServer()
 | 
				
			||||||
		rr := httptest.NewRecorder()
 | 
							rr := httptest.NewRecorder()
 | 
				
			||||||
		req := httptest.NewRequest(http.MethodGet, "/", nil)
 | 
							req := httptest.NewRequest(http.MethodGet, "/", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -40,7 +46,11 @@ func TestHandlers(t *testing.T) {
 | 
				
			|||||||
			t.Errorf("Body does not match expected. Got %s want %s", body, expectedBody)
 | 
								t.Errorf("Body does not match expected. Got %s want %s", body, expectedBody)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	t.Run("HandlerAPIFilePost", func(t *testing.T) {
 | 
						t.Run("api", func(t *testing.T) {
 | 
				
			||||||
 | 
							t.Run("file", func(t *testing.T) {
 | 
				
			||||||
 | 
								// POST /api/file
 | 
				
			||||||
 | 
								t.Run("POST", func(t *testing.T) {
 | 
				
			||||||
 | 
									hs := newServer()
 | 
				
			||||||
				rr := httptest.NewRecorder()
 | 
									rr := httptest.NewRecorder()
 | 
				
			||||||
				buf := &bytes.Buffer{}
 | 
									buf := &bytes.Buffer{}
 | 
				
			||||||
				mw := multipart.NewWriter(buf)
 | 
									mw := multipart.NewWriter(buf)
 | 
				
			||||||
@@ -54,7 +64,7 @@ func TestHandlers(t *testing.T) {
 | 
				
			|||||||
				}
 | 
									}
 | 
				
			||||||
				mw.Close()
 | 
									mw.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		req := httptest.NewRequest(http.MethodPost, "/api/file", buf)
 | 
									req := httptest.NewRequest(http.MethodPost, "/api/file?max_views=99", buf)
 | 
				
			||||||
				req.Header.Add("Content-Type", mw.FormDataContentType())
 | 
									req.Header.Add("Content-Type", mw.FormDataContentType())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				hs.Handler.ServeHTTP(rr, req)
 | 
									hs.Handler.ServeHTTP(rr, req)
 | 
				
			||||||
@@ -83,9 +93,36 @@ func TestHandlers(t *testing.T) {
 | 
				
			|||||||
					t.Errorf("Response has empty id")
 | 
										t.Errorf("Response has empty id")
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		t.Run("HandlerAPIFileGet", func(t *testing.T) {
 | 
									retrieved, err := hs.Files.Get(uploadID)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										t.Errorf("Error retrieving file: %s", err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									defer retrieved.Body.Close()
 | 
				
			||||||
 | 
									retBuf := new(bytes.Buffer)
 | 
				
			||||||
 | 
									io.Copy(retBuf, retrieved.Body)
 | 
				
			||||||
 | 
									if diff := cmp.Diff(retBuf.String(), expectedData); diff != "" {
 | 
				
			||||||
 | 
										t.Errorf("Retrieved file mismatch: %s", diff)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if retrieved.MaxViews != 99 {
 | 
				
			||||||
 | 
										t.Errorf("Uploaded file has wrong max_views: %d", retrieved.MaxViews)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								// GET /api/file/id
 | 
				
			||||||
 | 
								t.Run("GET", func(t *testing.T) {
 | 
				
			||||||
 | 
									hs := newServer()
 | 
				
			||||||
 | 
									fileData := "abc123456"
 | 
				
			||||||
 | 
									sr := io.NopCloser(strings.NewReader(fileData))
 | 
				
			||||||
 | 
									file := &files.File{
 | 
				
			||||||
 | 
										ID:               uuid.NewString(),
 | 
				
			||||||
 | 
										OriginalFilename: "test-file.txt",
 | 
				
			||||||
 | 
										MaxViews:         99,
 | 
				
			||||||
 | 
										ExpiresOn:        time.Now().Add(90 * time.Second),
 | 
				
			||||||
 | 
										Body:             sr,
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									hs.Files.Store(file)
 | 
				
			||||||
				rr := httptest.NewRecorder()
 | 
									rr := httptest.NewRecorder()
 | 
				
			||||||
			url := fmt.Sprintf("/api/file/%s", uploadID)
 | 
									url := fmt.Sprintf("/api/file/%s", file.ID)
 | 
				
			||||||
				req := httptest.NewRequest(http.MethodGet, url, nil)
 | 
									req := httptest.NewRequest(http.MethodGet, url, nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				hs.Handler.ServeHTTP(rr, req)
 | 
									hs.Handler.ServeHTTP(rr, req)
 | 
				
			||||||
@@ -94,12 +131,100 @@ func TestHandlers(t *testing.T) {
 | 
				
			|||||||
					t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
 | 
										t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
 | 
				
			||||||
					t.Logf(url)
 | 
										t.Logf(url)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			if body := rr.Body.String(); body != expectedData {
 | 
									if diff := cmp.Diff(rr.Body.String(), fileData); diff != "" {
 | 
				
			||||||
				t.Errorf("Returned body does not match expected.")
 | 
										t.Errorf("Returned body does not match expected: %s", diff)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// DELETE /api/file/id
 | 
				
			||||||
 | 
								t.Run("DELETE", func(t *testing.T) {
 | 
				
			||||||
 | 
									hs := newServer()
 | 
				
			||||||
 | 
									fileBody := io.NopCloser(strings.NewReader("roflcopter"))
 | 
				
			||||||
 | 
									file := &files.File{
 | 
				
			||||||
 | 
										ID:               uuid.NewString(),
 | 
				
			||||||
 | 
										OriginalFilename: "testpls.txt",
 | 
				
			||||||
 | 
										MaxViews:         9,
 | 
				
			||||||
 | 
										ExpiresOn:        time.Now().Add(10 * time.Hour),
 | 
				
			||||||
 | 
										Body:             fileBody,
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if err := hs.Files.Store(file); err != nil {
 | 
				
			||||||
 | 
										t.Fatalf("Error storing file: %s", err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									rr := httptest.NewRecorder()
 | 
				
			||||||
 | 
									url := fmt.Sprintf("/api/file/%s", file.ID)
 | 
				
			||||||
 | 
									req := httptest.NewRequest(http.MethodDelete, url, nil)
 | 
				
			||||||
 | 
									hs.Handler.ServeHTTP(rr, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if rr.Result().StatusCode != http.StatusOK {
 | 
				
			||||||
 | 
										t.Fatalf("Delete returned wrong status: %s", rr.Result().Status)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if _, err := hs.Files.Get(file.ID); err == nil {
 | 
				
			||||||
 | 
										t.Errorf("Getting after delete returned no error")
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	t.Run("HandlerAPILogin", func(t *testing.T) {
 | 
							// /api/user
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							t.Run("user", func(t *testing.T) {
 | 
				
			||||||
 | 
								t.Run("POST", func(t *testing.T) {
 | 
				
			||||||
 | 
									hs := newServer()
 | 
				
			||||||
 | 
									adminPw := "admin"
 | 
				
			||||||
 | 
									admin := &users.User{
 | 
				
			||||||
 | 
										Username: "admin",
 | 
				
			||||||
 | 
										Role:     users.RoleAdmin,
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									_ = admin.SetPassword(adminPw)
 | 
				
			||||||
 | 
									_ = hs.Users.Store(admin)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									token, err := hs.Auth.Login(admin.Username, adminPw)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										t.Fatalf("error getting admin token: %s", err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									requestData := &api.RequestAPIUserCreate{
 | 
				
			||||||
 | 
										Username: "test",
 | 
				
			||||||
 | 
										Password: "test",
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									body := new(bytes.Buffer)
 | 
				
			||||||
 | 
									encoder := json.NewEncoder(body)
 | 
				
			||||||
 | 
									if err := encoder.Encode(requestData); err != nil {
 | 
				
			||||||
 | 
										t.Fatalf("Error encoding data: %s", err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									rr := httptest.NewRecorder()
 | 
				
			||||||
 | 
									req := httptest.NewRequest(http.MethodPost, "/api/user", body)
 | 
				
			||||||
 | 
									req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
 | 
				
			||||||
 | 
									hs.Handler.ServeHTTP(rr, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if rr.Result().StatusCode != http.StatusAccepted {
 | 
				
			||||||
 | 
										t.Fatalf("Create returned wrong status: %s", rr.Result().Status)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									user, err := hs.Users.Get(requestData.Username)
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										t.Fatalf("Unable to get user after create: %s", err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									expectedUser := &users.User{
 | 
				
			||||||
 | 
										Username: requestData.Username,
 | 
				
			||||||
 | 
										Role:     users.RoleUser,
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									ignorePW := cmp.FilterPath(func(p cmp.Path) bool {
 | 
				
			||||||
 | 
										return p.String() == "HashedPassword"
 | 
				
			||||||
 | 
									}, cmp.Ignore())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if diff := cmp.Diff(user, expectedUser, ignorePW); diff != "" {
 | 
				
			||||||
 | 
										t.Errorf("User does not match expected: %s", diff)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// /api/login
 | 
				
			||||||
 | 
							t.Run("Login", func(t *testing.T) {
 | 
				
			||||||
 | 
								hs := newServer()
 | 
				
			||||||
			// TODO: Add test
 | 
								// TODO: Add test
 | 
				
			||||||
			username := "admin"
 | 
								username := "admin"
 | 
				
			||||||
			password := "admin"
 | 
								password := "admin"
 | 
				
			||||||
@@ -143,4 +268,17 @@ func TestHandlers(t *testing.T) {
 | 
				
			|||||||
				t.Fatalf("Unable to validate received token: %s", err)
 | 
									t.Fatalf("Unable to validate received token: %s", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func newServer() *api.HTTPServer {
 | 
				
			||||||
 | 
						cfg := &gpaste.ServerConfig{
 | 
				
			||||||
 | 
							SigningSecret: "abc123",
 | 
				
			||||||
 | 
							Store: &gpaste.ServerStoreConfig{
 | 
				
			||||||
 | 
								Type: "memory",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							URL: "http://localhost:8080",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return api.NewHTTPServer(cfg)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,3 +18,7 @@ type ResponseAPIFilePost struct {
 | 
				
			|||||||
	ID      string `json:"id"`
 | 
						ID      string `json:"id"`
 | 
				
			||||||
	URL     string `json:"url"`
 | 
						URL     string `json:"url"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ResponseAPIUserList struct {
 | 
				
			||||||
 | 
						Usernames []string `json:"usernames"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -71,7 +71,7 @@ func (s *HTTPServer) MiddlewareAuthentication(next http.Handler) http.Handler {
 | 
				
			|||||||
		ctx = context.WithValue(ctx, authCtxAuthLevel, claims.Role)
 | 
							ctx = context.WithValue(ctx, authCtxAuthLevel, claims.Role)
 | 
				
			||||||
		ctx = context.WithValue(ctx, authCtxClaims, claims)
 | 
							ctx = context.WithValue(ctx, authCtxClaims, claims)
 | 
				
			||||||
		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, "role", claims.Role)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		next.ServeHTTP(w, withCtx)
 | 
							next.ServeHTTP(w, withCtx)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,20 +8,61 @@ import (
 | 
				
			|||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"mime/multipart"
 | 
						"mime/multipart"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.t-juice.club/torjus/gpaste/api"
 | 
						"git.t-juice.club/torjus/gpaste/api"
 | 
				
			||||||
	"git.t-juice.club/torjus/gpaste/files"
 | 
						"git.t-juice.club/torjus/gpaste/files"
 | 
				
			||||||
	"github.com/google/uuid"
 | 
						"github.com/google/uuid"
 | 
				
			||||||
 | 
						"github.com/kirsle/configdir"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Client struct {
 | 
					type Client struct {
 | 
				
			||||||
	BaseURL   string
 | 
						BaseURL   string `json:"base_url"`
 | 
				
			||||||
	AuthToken string
 | 
						AuthToken string `json:"auth_token"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	httpClient http.Client
 | 
						httpClient http.Client
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Client) WriteConfigToWriter(w io.Writer) error {
 | 
				
			||||||
 | 
						encoder := json.NewEncoder(w)
 | 
				
			||||||
 | 
						return encoder.Encode(c)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (c *Client) WriteConfig() error {
 | 
				
			||||||
 | 
						dir := configdir.LocalConfig("gpaste")
 | 
				
			||||||
 | 
						// Ensure dir exists
 | 
				
			||||||
 | 
						err := os.MkdirAll(dir, os.ModePerm)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						path := filepath.Join(dir, "client.json")
 | 
				
			||||||
 | 
						f, err := os.Create(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer f.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return c.WriteConfigToWriter(f)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Client) LoadConfig() error {
 | 
				
			||||||
 | 
						dir := configdir.LocalCache("gpaste")
 | 
				
			||||||
 | 
						path := filepath.Join(dir, "client.json")
 | 
				
			||||||
 | 
						f, err := os.Open(path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer f.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return c.LoadConfigFromReader(f)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Client) LoadConfigFromReader(r io.Reader) error {
 | 
				
			||||||
 | 
						decoder := json.NewDecoder(r)
 | 
				
			||||||
 | 
						return decoder.Decode(c)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Client) Login(ctx context.Context, username, password string) error {
 | 
					func (c *Client) Login(ctx context.Context, username, password string) error {
 | 
				
			||||||
	url := fmt.Sprintf("%s/api/login", c.BaseURL)
 | 
						url := fmt.Sprintf("%s/api/login", c.BaseURL)
 | 
				
			||||||
	// TODO: Change timeout
 | 
						// TODO: Change timeout
 | 
				
			||||||
@@ -162,3 +203,24 @@ func (c *Client) Upload(ctx context.Context, files ...*files.File) ([]api.Respon
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return expectedResp, nil
 | 
						return expectedResp, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Client) Delete(ctx context.Context, id string) error {
 | 
				
			||||||
 | 
						url := fmt.Sprintf("%s/api/file/%s", c.BaseURL, id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return fmt.Errorf("error creating request: %w", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AuthToken))
 | 
				
			||||||
 | 
						resp, err := c.httpClient.Do(req)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return fmt.Errorf("unable to perform request: %s", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if resp.StatusCode != http.StatusOK {
 | 
				
			||||||
 | 
							return fmt.Errorf("got non-ok response from server: %s", resp.Status)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package client_test
 | 
					package client_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
@@ -15,6 +16,7 @@ import (
 | 
				
			|||||||
	"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/google/go-cmp/cmp"
 | 
						"github.com/google/go-cmp/cmp"
 | 
				
			||||||
 | 
						"github.com/google/go-cmp/cmp/cmpopts"
 | 
				
			||||||
	"github.com/google/uuid"
 | 
						"github.com/google/uuid"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -162,4 +164,31 @@ func TestClient(t *testing.T) {
 | 
				
			|||||||
			t.Errorf("File contents does not match: %s", cmp.Diff(buf.String(), fileContents))
 | 
								t.Errorf("File contents does not match: %s", cmp.Diff(buf.String(), fileContents))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
						t.Run("Save", func(t *testing.T) {
 | 
				
			||||||
 | 
							c := client.Client{BaseURL: "http://example.org/gpaste", AuthToken: "tokenpls"}
 | 
				
			||||||
 | 
							expectedConfig := "{\"base_url\":\"http://example.org/gpaste\",\"auth_token\":\"tokenpls\"}\n"
 | 
				
			||||||
 | 
							buf := new(bytes.Buffer)
 | 
				
			||||||
 | 
							err := c.WriteConfigToWriter(buf)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error writing config: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if diff := cmp.Diff(buf.String(), expectedConfig); diff != "" {
 | 
				
			||||||
 | 
								t.Errorf("Written config does not match expected: %s", diff)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						t.Run("Load", func(t *testing.T) {
 | 
				
			||||||
 | 
							c := client.Client{}
 | 
				
			||||||
 | 
							config := "{\"base_url\":\"http://pasta.example.org\",\"auth_token\":\"tokenpls\"}\n"
 | 
				
			||||||
 | 
							expectedClient := client.Client{BaseURL: "http://pasta.example.org", AuthToken: "tokenpls"}
 | 
				
			||||||
 | 
							sr := strings.NewReader(config)
 | 
				
			||||||
 | 
							if err := c.LoadConfigFromReader(sr); err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error reading config: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if diff := cmp.Diff(c, expectedClient, cmpopts.IgnoreUnexported(client.Client{})); diff != "" {
 | 
				
			||||||
 | 
								t.Errorf("Client does not match expected: %s", diff)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,74 +1,56 @@
 | 
				
			|||||||
package actions
 | 
					package actions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bufio"
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"encoding/json"
 | 
					 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
					 | 
				
			||||||
	"mime/multipart"
 | 
					 | 
				
			||||||
	"net/http"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"syscall"
 | 
						"syscall"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.t-juice.club/torjus/gpaste/api"
 | 
						"git.t-juice.club/torjus/gpaste/client"
 | 
				
			||||||
	"github.com/google/uuid"
 | 
						"git.t-juice.club/torjus/gpaste/files"
 | 
				
			||||||
	"github.com/urfave/cli/v2"
 | 
						"github.com/urfave/cli/v2"
 | 
				
			||||||
	"golang.org/x/term"
 | 
						"golang.org/x/term"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ActionUpload(c *cli.Context) error {
 | 
					func ActionUpload(c *cli.Context) error {
 | 
				
			||||||
	url := fmt.Sprintf("%s/api/file", c.String("url"))
 | 
						clnt := client.Client{
 | 
				
			||||||
	client := &http.Client{}
 | 
							BaseURL: c.String("url"),
 | 
				
			||||||
	// TODO: Change timeout
 | 
						}
 | 
				
			||||||
	ctx, cancel := context.WithTimeout(c.Context, 10*time.Minute)
 | 
					 | 
				
			||||||
	defer cancel()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	buf := &bytes.Buffer{}
 | 
					 | 
				
			||||||
	mw := multipart.NewWriter(buf)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for _, arg := range c.Args().Slice() {
 | 
						for _, arg := range c.Args().Slice() {
 | 
				
			||||||
		f, err := os.Open(arg)
 | 
							f, err := os.Open(arg)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		defer f.Close()
 | 
							defer f.Close()
 | 
				
			||||||
		fw, err := mw.CreateFormFile(uuid.Must(uuid.NewRandom()).String(), arg)
 | 
							file := &files.File{
 | 
				
			||||||
 | 
								OriginalFilename: arg,
 | 
				
			||||||
 | 
								Body:             f,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							resp, err := clnt.Upload(c.Context, file)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								errmsg := fmt.Sprintf("Error uploading file: %s", err)
 | 
				
			||||||
 | 
								return cli.Exit(errmsg, 1)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if _, err := io.Copy(fw, f); err != nil {
 | 
							fmt.Printf("Uploaded file %s - %s", file.OriginalFilename, resp[0].URL)
 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	}
 | 
						return nil
 | 
				
			||||||
	mw.Close()
 | 
					}
 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	req.Header.Add("Content-Type", mw.FormDataContentType())
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	resp, err := client.Do(req)
 | 
					func ActionDelete(c *cli.Context) error {
 | 
				
			||||||
	if err != nil {
 | 
						clnt := client.Client{
 | 
				
			||||||
		return err
 | 
							BaseURL: c.String("url"),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer resp.Body.Close()
 | 
						for _, arg := range c.Args().Slice() {
 | 
				
			||||||
 | 
							ctx, cancel := context.WithTimeout(c.Context, 5*time.Second)
 | 
				
			||||||
	var expectedResp []struct {
 | 
							defer cancel()
 | 
				
			||||||
		Message string `json:"message"`
 | 
							if err := clnt.Delete(ctx, arg); err != nil {
 | 
				
			||||||
		ID      string `json:"id"`
 | 
								fmt.Printf("Error deleting file %s\n", arg)
 | 
				
			||||||
		URL     string `json:"url"`
 | 
								fmt.Printf("%s\n", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							fmt.Printf("Deleted %s\n", arg)
 | 
				
			||||||
	decoder := json.NewDecoder(resp.Body)
 | 
					 | 
				
			||||||
	if err := decoder.Decode(&expectedResp); err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("error decoding response: %w", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for _, r := range expectedResp {
 | 
					 | 
				
			||||||
		fmt.Printf("Uploaded file %s\n", r.ID)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -83,92 +65,53 @@ func ActionLogin(c *cli.Context) error {
 | 
				
			|||||||
		return fmt.Errorf("error reading password: %w", err)
 | 
							return fmt.Errorf("error reading password: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	url := fmt.Sprintf("%s/api/login", c.String("url"))
 | 
						clnt := client.Client{
 | 
				
			||||||
	client := &http.Client{}
 | 
							BaseURL: c.String("url"),
 | 
				
			||||||
	// TODO: Change timeout
 | 
					 | 
				
			||||||
	ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
 | 
					 | 
				
			||||||
	defer cancel()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	body := new(bytes.Buffer)
 | 
					 | 
				
			||||||
	requestData := struct {
 | 
					 | 
				
			||||||
		Username string `json:"username"`
 | 
					 | 
				
			||||||
		Password string `json:"password"`
 | 
					 | 
				
			||||||
	}{
 | 
					 | 
				
			||||||
		Username: username,
 | 
					 | 
				
			||||||
		Password: password,
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	encoder := json.NewEncoder(body)
 | 
						if err := clnt.Login(c.Context, username, password); err != nil {
 | 
				
			||||||
	if err := encoder.Encode(&requestData); err != nil {
 | 
							errmsg := fmt.Sprintf("Error logging in: %s", err)
 | 
				
			||||||
		return fmt.Errorf("error encoding response: %w", err)
 | 
							return cli.Exit(errmsg, 1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
 | 
						if err := clnt.WriteConfig(); err != nil {
 | 
				
			||||||
	if err != nil {
 | 
							errMsg := fmt.Sprintf("Failed to write config: %s", err)
 | 
				
			||||||
		return fmt.Errorf("error creating request: %w", err)
 | 
							return cli.Exit(errMsg, 1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						// TODO: Store this somewhere, so we don't need to log in each time
 | 
				
			||||||
	resp, err := client.Do(req)
 | 
						fmt.Println("Successfully logged in.")
 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("unable to perform request: %s", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer resp.Body.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if resp.StatusCode != http.StatusOK {
 | 
					 | 
				
			||||||
		return cli.Exit("got non-ok response from server", 0)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	responseData := struct {
 | 
					 | 
				
			||||||
		Token string `json:"token"`
 | 
					 | 
				
			||||||
	}{}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	decoder := json.NewDecoder(resp.Body)
 | 
					 | 
				
			||||||
	if err := decoder.Decode(&responseData); err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("unable to parse response: %s", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	fmt.Printf("Token: %s", responseData.Token)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ActionUserCreate(c *cli.Context) error {
 | 
					func ActionUserCreate(c *cli.Context) error {
 | 
				
			||||||
	// TODO: Needs to supply auth token to actually work
 | 
						// TODO: Needs to supply auth token to actually work
 | 
				
			||||||
	username := c.Args().First()
 | 
						fmt.Println("Need to be logged in to create user")
 | 
				
			||||||
	if username == "" {
 | 
						username := readString("Enter username: ")
 | 
				
			||||||
		return cli.Exit("USERNAME not supplied.", 1)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	password, err := readPassword()
 | 
						password, err := readPassword()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fmt.Errorf("error reading password: %w", err)
 | 
							return fmt.Errorf("error reading password: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	url := fmt.Sprintf("%s/api/user", c.String("url"))
 | 
						clnt := client.Client{
 | 
				
			||||||
	client := &http.Client{}
 | 
							BaseURL: c.String("url"),
 | 
				
			||||||
	// TODO: Change timeout
 | 
						}
 | 
				
			||||||
	ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
 | 
						ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
 | 
				
			||||||
	defer cancel()
 | 
						defer cancel()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	body := new(bytes.Buffer)
 | 
						if err := clnt.Login(ctx, username, password); err != nil {
 | 
				
			||||||
	requestData := &api.RequestAPIUserCreate{
 | 
							errmsg := fmt.Sprintf("Error logging in: %s", err)
 | 
				
			||||||
		Username: username,
 | 
							return cli.Exit(errmsg, 1)
 | 
				
			||||||
		Password: password,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	encoder := json.NewEncoder(body)
 | 
					 | 
				
			||||||
	if err := encoder.Encode(requestData); err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("error encoding response: %w", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return fmt.Errorf("error creating request: %w", err)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	resp, err := client.Do(req)
 | 
						fmt.Println("User to create:")
 | 
				
			||||||
 | 
						username = readString("Enter username: ")
 | 
				
			||||||
 | 
						password, err = readPassword()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fmt.Errorf("unable to perform request: %s", err)
 | 
							return fmt.Errorf("error reading password: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	defer resp.Body.Close()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if resp.StatusCode != http.StatusAccepted {
 | 
						if err := clnt.UserCreate(ctx, username, password); err != nil {
 | 
				
			||||||
		return cli.Exit("got non-ok response from server", 0)
 | 
							errmsg := fmt.Sprintf("Error creating user: %s", err)
 | 
				
			||||||
 | 
							return cli.Exit(errmsg, 1)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fmt.Printf("Created user %s\n", username)
 | 
						fmt.Printf("Created user %s\n", username)
 | 
				
			||||||
@@ -186,3 +129,12 @@ func readPassword() (string, error) {
 | 
				
			|||||||
	password := string(bytePassword)
 | 
						password := string(bytePassword)
 | 
				
			||||||
	return strings.TrimSpace(password), nil
 | 
						return strings.TrimSpace(password), nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func readString(prompt string) string {
 | 
				
			||||||
 | 
						fmt.Print(prompt)
 | 
				
			||||||
 | 
						scanner := bufio.NewScanner(os.Stdin)
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							return scanner.Text()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return ""
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,6 +37,12 @@ func main() {
 | 
				
			|||||||
				ArgsUsage: "FILE [FILE]...",
 | 
									ArgsUsage: "FILE [FILE]...",
 | 
				
			||||||
				Action:    actions.ActionUpload,
 | 
									Action:    actions.ActionUpload,
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									Name:      "delete",
 | 
				
			||||||
 | 
									Usage:     "Delete file(s)",
 | 
				
			||||||
 | 
									ArgsUsage: "FILE [FILE]...",
 | 
				
			||||||
 | 
									Action:    actions.ActionDelete,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				Name:      "login",
 | 
									Name:      "login",
 | 
				
			||||||
				Usage:     "Login to gpaste server",
 | 
									Usage:     "Login to gpaste server",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ package actions
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/signal"
 | 
						"os/signal"
 | 
				
			||||||
@@ -21,14 +22,27 @@ func ActionServe(c *cli.Context) error {
 | 
				
			|||||||
		configPath = c.String("config")
 | 
							configPath = c.String("config")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	f, err := os.Open(configPath)
 | 
						var cfg *gpaste.ServerConfig
 | 
				
			||||||
 | 
						var r io.ReadCloser
 | 
				
			||||||
 | 
						r, err := os.Open(configPath)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							cfg = &gpaste.ServerConfig{
 | 
				
			||||||
 | 
								LogLevel:      "INFO",
 | 
				
			||||||
 | 
								URL:           "localhost:8080",
 | 
				
			||||||
 | 
								ListenAddr:    ":8080",
 | 
				
			||||||
 | 
								SigningSecret: "TODO: CHANGE THIS LOL",
 | 
				
			||||||
 | 
								Store: &gpaste.ServerStoreConfig{
 | 
				
			||||||
 | 
									Type: "memory",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							defer r.Close()
 | 
				
			||||||
 | 
							cfg, err = gpaste.ServerConfigFromReader(r)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return cli.Exit(err, 1)
 | 
									return cli.Exit(err, 1)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
	defer f.Close()
 | 
							}
 | 
				
			||||||
	cfg, err := gpaste.ServerConfigFromReader(f)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return cli.Exit(err, 1)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Setup loggers
 | 
						// Setup loggers
 | 
				
			||||||
	rootLogger := getRootLogger(cfg.LogLevel)
 | 
						rootLogger := getRootLogger(cfg.LogLevel)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,6 +12,8 @@ type File struct {
 | 
				
			|||||||
	ExpiresOn        time.Time `json:"expires_on"`
 | 
						ExpiresOn        time.Time `json:"expires_on"`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Body io.ReadCloser
 | 
						Body io.ReadCloser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						FileSize int64 `json:"file_size"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type FileStore interface {
 | 
					type FileStore interface {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -40,10 +40,12 @@ func (s *FSFileStore) Store(f *File) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	defer dst.Close()
 | 
						defer dst.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if _, err := io.Copy(dst, f.Body); err != nil {
 | 
						n, err := io.Copy(dst, f.Body)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	s.metadata[f.ID] = metadata
 | 
						s.metadata[f.ID] = metadata
 | 
				
			||||||
 | 
						s.metadata[f.ID].FileSize = n
 | 
				
			||||||
	if err := s.writeMetadata(); err != nil {
 | 
						if err := s.writeMetadata(); err != nil {
 | 
				
			||||||
		delete(s.metadata, f.ID)
 | 
							delete(s.metadata, f.ID)
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,6 +14,7 @@ type fileData struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	MaxViews  uint
 | 
						MaxViews  uint
 | 
				
			||||||
	ExpiresOn time.Time
 | 
						ExpiresOn time.Time
 | 
				
			||||||
 | 
						FileSize  int64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type MemoryFileStore struct {
 | 
					type MemoryFileStore struct {
 | 
				
			||||||
@@ -35,9 +36,11 @@ func (s *MemoryFileStore) Store(f *File) error {
 | 
				
			|||||||
		ExpiresOn: f.ExpiresOn,
 | 
							ExpiresOn: f.ExpiresOn,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_, err := io.Copy(&data.Body, f.Body)
 | 
						n, err := io.Copy(&data.Body, f.Body)
 | 
				
			||||||
	_ = f.Body.Close()
 | 
						_ = f.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						data.FileSize = n
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	s.lock.Lock()
 | 
						s.lock.Lock()
 | 
				
			||||||
	defer s.lock.Unlock()
 | 
						defer s.lock.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -58,6 +61,7 @@ func (s *MemoryFileStore) Get(id string) (*File, error) {
 | 
				
			|||||||
		MaxViews:  fd.MaxViews,
 | 
							MaxViews:  fd.MaxViews,
 | 
				
			||||||
		ExpiresOn: fd.ExpiresOn,
 | 
							ExpiresOn: fd.ExpiresOn,
 | 
				
			||||||
		Body:      io.NopCloser(&fd.Body),
 | 
							Body:      io.NopCloser(&fd.Body),
 | 
				
			||||||
 | 
							FileSize:  fd.FileSize,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return f, nil
 | 
						return f, nil
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,8 +22,9 @@ func RunFilestoreTest(s files.FileStore, t *testing.T) {
 | 
				
			|||||||
		body := io.NopCloser(bodyBuf)
 | 
							body := io.NopCloser(bodyBuf)
 | 
				
			||||||
		f := &files.File{
 | 
							f := &files.File{
 | 
				
			||||||
			ID:        id,
 | 
								ID:        id,
 | 
				
			||||||
			MaxViews: 0,
 | 
								MaxViews:  99,
 | 
				
			||||||
			Body:      body,
 | 
								Body:      body,
 | 
				
			||||||
 | 
								ExpiresOn: time.Now().Add(99 * time.Second),
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		err := s.Store(f)
 | 
							err := s.Store(f)
 | 
				
			||||||
@@ -50,6 +51,17 @@ func RunFilestoreTest(s files.FileStore, t *testing.T) {
 | 
				
			|||||||
		if retrievedBuf.String() != dataString {
 | 
							if retrievedBuf.String() != dataString {
 | 
				
			||||||
			t.Fatalf("Data from retrieved body mismatch. Got %s want %s", retrievedBuf.String(), dataString)
 | 
								t.Fatalf("Data from retrieved body mismatch. Got %s want %s", retrievedBuf.String(), dataString)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							expected := &files.File{
 | 
				
			||||||
 | 
								ID:        f.ID,
 | 
				
			||||||
 | 
								MaxViews:  f.MaxViews,
 | 
				
			||||||
 | 
								ExpiresOn: f.ExpiresOn,
 | 
				
			||||||
 | 
								FileSize:  int64(len(dataString)),
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
 | 
				
			||||||
 | 
							if diff := cmp.Diff(retrieved, expected, ignoreBody); diff != "" {
 | 
				
			||||||
 | 
								t.Errorf("File comparison failed: %s", diff)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// List
 | 
							// List
 | 
				
			||||||
		ids, err := s.List()
 | 
							ids, err := s.List()
 | 
				
			||||||
@@ -92,6 +104,7 @@ func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.
 | 
				
			|||||||
				MaxViews:         5,
 | 
									MaxViews:         5,
 | 
				
			||||||
				ExpiresOn:        time.Now().Add(10 * time.Minute),
 | 
									ExpiresOn:        time.Now().Add(10 * time.Minute),
 | 
				
			||||||
				Body:             io.NopCloser(strings.NewReader("cocks!")),
 | 
									Body:             io.NopCloser(strings.NewReader("cocks!")),
 | 
				
			||||||
 | 
									FileSize:         6,
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			ExpectedData: "cocks!",
 | 
								ExpectedData: "cocks!",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
@@ -102,6 +115,7 @@ func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.
 | 
				
			|||||||
				MaxViews:         5,
 | 
									MaxViews:         5,
 | 
				
			||||||
				ExpiresOn:        time.Now().Add(10 * time.Minute),
 | 
									ExpiresOn:        time.Now().Add(10 * time.Minute),
 | 
				
			||||||
				Body:             io.NopCloser(strings.NewReader("derps!")),
 | 
									Body:             io.NopCloser(strings.NewReader("derps!")),
 | 
				
			||||||
 | 
									FileSize:         6,
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			ExpectedData: "derps!",
 | 
								ExpectedData: "derps!",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@@ -9,6 +9,7 @@ require github.com/go-chi/chi/v5 v5.0.7
 | 
				
			|||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/golang-jwt/jwt v3.2.2+incompatible
 | 
						github.com/golang-jwt/jwt v3.2.2+incompatible
 | 
				
			||||||
	github.com/google/go-cmp v0.5.6
 | 
						github.com/google/go-cmp v0.5.6
 | 
				
			||||||
 | 
						github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
 | 
				
			||||||
	github.com/pelletier/go-toml v1.9.4
 | 
						github.com/pelletier/go-toml v1.9.4
 | 
				
			||||||
	github.com/urfave/cli/v2 v2.3.0
 | 
						github.com/urfave/cli/v2 v2.3.0
 | 
				
			||||||
	go.etcd.io/bbolt v1.3.6
 | 
						go.etcd.io/bbolt v1.3.6
 | 
				
			||||||
@@ -23,4 +24,5 @@ require (
 | 
				
			|||||||
	go.uber.org/atomic v1.9.0 // indirect
 | 
						go.uber.org/atomic v1.9.0 // indirect
 | 
				
			||||||
	go.uber.org/multierr v1.7.0 // indirect
 | 
						go.uber.org/multierr v1.7.0 // indirect
 | 
				
			||||||
	golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
 | 
						golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
 | 
				
			||||||
 | 
						golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@@ -15,6 +15,8 @@ github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
 | 
				
			|||||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 | 
					github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 | 
				
			||||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
 | 
					github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
 | 
				
			||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 | 
					github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 | 
				
			||||||
 | 
					github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
 | 
				
			||||||
 | 
					github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
 | 
				
			||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 | 
					github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 | 
				
			||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 | 
					github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 | 
				
			||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 | 
					github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,10 @@
 | 
				
			|||||||
package users
 | 
					package users
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "golang.org/x/crypto/bcrypt"
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/crypto/bcrypt"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Role string
 | 
					type Role string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -16,10 +20,13 @@ type User struct {
 | 
				
			|||||||
	Role           Role   `json:"role"`
 | 
						Role           Role   `json:"role"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var ErrNoSuchUser = fmt.Errorf("no such user")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type UserStore interface {
 | 
					type UserStore interface {
 | 
				
			||||||
	Get(username string) (*User, error)
 | 
						Get(username string) (*User, error)
 | 
				
			||||||
	Store(user *User) error
 | 
						Store(user *User) error
 | 
				
			||||||
	Delete(username string) error
 | 
						Delete(username string) error
 | 
				
			||||||
 | 
						List() ([]string, error)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (u *User) ValidatePassword(password string) error {
 | 
					func (u *User) ValidatePassword(password string) error {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,8 @@ import (
 | 
				
			|||||||
	"go.etcd.io/bbolt"
 | 
						"go.etcd.io/bbolt"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var _ UserStore = &BoltUserStore{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var keyUsers = []byte("users")
 | 
					var keyUsers = []byte("users")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type BoltUserStore struct {
 | 
					type BoltUserStore struct {
 | 
				
			||||||
@@ -67,3 +69,22 @@ func (s *BoltUserStore) Delete(username string) error {
 | 
				
			|||||||
		return bkt.Delete([]byte(username))
 | 
							return bkt.Delete([]byte(username))
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					func (s *BoltUserStore) List() ([]string, error) {
 | 
				
			||||||
 | 
						var ids []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := s.db.View(func(tx *bbolt.Tx) error {
 | 
				
			||||||
 | 
							bkt := tx.Bucket(keyUsers)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							c := bkt.Cursor()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for k, _ := c.First(); k != nil; k, _ = c.Next() {
 | 
				
			||||||
 | 
								ids = append(ids, string(k))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return ids, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,8 @@ import (
 | 
				
			|||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var _ UserStore = &MemoryUserStore{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type MemoryUserStore struct {
 | 
					type MemoryUserStore struct {
 | 
				
			||||||
	users map[string]*User
 | 
						users map[string]*User
 | 
				
			||||||
	lock  sync.Mutex
 | 
						lock  sync.Mutex
 | 
				
			||||||
@@ -37,3 +39,14 @@ func (s *MemoryUserStore) Delete(username string) error {
 | 
				
			|||||||
	delete(s.users, username)
 | 
						delete(s.users, username)
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					func (s *MemoryUserStore) List() ([]string, error) {
 | 
				
			||||||
 | 
						s.lock.Lock()
 | 
				
			||||||
 | 
						defer s.lock.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var ids []string
 | 
				
			||||||
 | 
						for k := range s.users {
 | 
				
			||||||
 | 
							ids = append(ids, k)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return ids, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -47,5 +47,27 @@ func RunUserStoreTest(newFunc func() (func(), users.UserStore), t *testing.T) {
 | 
				
			|||||||
				t.Errorf("User mismatch: %s", cmp.Diff(user, userMap[k]))
 | 
									t.Errorf("User mismatch: %s", cmp.Diff(user, userMap[k]))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							l, err := s.List()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error listing users: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if len(l) != len(userMap) {
 | 
				
			||||||
 | 
								t.Errorf("List wrong amount of users.")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for _, username := range l {
 | 
				
			||||||
 | 
								if err := s.Delete(username); err != nil {
 | 
				
			||||||
 | 
									t.Fatalf("Error deleting user: %s", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							l, err = s.List()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("Error listing after delete: %s", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if len(l) != 0 {
 | 
				
			||||||
 | 
								t.Fatalf("List is not empty after deleting all")
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user