From 62fe48e4515dd99fb91795ea1e0a41b5b909bc1e Mon Sep 17 00:00:00 2001 From: Sung Won Cho Date: Tue, 10 May 2022 20:35:43 +1000 Subject: [PATCH] Remove unused files (#601) --- pkg/server/crypt/crypt.go | 12 --- pkg/server/net/writer.go | 49 ----------- pkg/server/web/handlers.go | 141 -------------------------------- pkg/server/web/handlers_test.go | 110 ------------------------- pkg/server/web/main_test.go | 35 -------- 5 files changed, 347 deletions(-) delete mode 100644 pkg/server/net/writer.go delete mode 100644 pkg/server/web/handlers.go delete mode 100644 pkg/server/web/handlers_test.go delete mode 100644 pkg/server/web/main_test.go diff --git a/pkg/server/crypt/crypt.go b/pkg/server/crypt/crypt.go index b782ff41..9f2ca530 100644 --- a/pkg/server/crypt/crypt.go +++ b/pkg/server/crypt/crypt.go @@ -20,16 +20,11 @@ package crypt import ( "crypto/rand" - "crypto/sha256" "encoding/base64" "github.com/pkg/errors" - "golang.org/x/crypto/pbkdf2" ) -// ServerKDFIteration is the iteration count for PBKDF on the server -var ServerKDFIteration = 100000 - // getRandomBytes generates a cryptographically secure pseudorandom numbers of the // given size in byte func getRandomBytes(numBytes int) ([]byte, error) { @@ -51,10 +46,3 @@ func GetRandomStr(numBytes int) (string, error) { return base64.StdEncoding.EncodeToString(b), nil } - -// HashAuthKey hashes the authKey provided by a client -func HashAuthKey(authKey, salt string, iteration int) string { - keyHashBits := pbkdf2.Key([]byte(authKey), []byte(salt), iteration, 32, sha256.New) - - return base64.StdEncoding.EncodeToString(keyHashBits) -} diff --git a/pkg/server/net/writer.go b/pkg/server/net/writer.go deleted file mode 100644 index 12b9ccc6..00000000 --- a/pkg/server/net/writer.go +++ /dev/null @@ -1,49 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package net - -import ( - "github.com/dnote/dnote/pkg/server/log" - "net/http" -) - -// LifecycleWriter wraps http.ResponseWriter to track state of the http response. -// The optional interfaces of http.ResponseWriter are lost because of the wrapping, and -// such interfaces should be implemented if needed. (i.e. http.Pusher, http.Flusher, etc.) -type LifecycleWriter struct { - http.ResponseWriter - StatusCode int -} - -// WriteHeader wraps the WriteHeader call and marks the response state as done. -func (w *LifecycleWriter) WriteHeader(code int) { - w.StatusCode = code - w.ResponseWriter.WriteHeader(code) -} - -// IsHeaderWritten returns true if a response has been written. -func IsHeaderWritten(w http.ResponseWriter) bool { - if lw, ok := w.(*LifecycleWriter); ok { - return lw.StatusCode != 0 - } - - // the response writer must have been wrapped in the middleware chain. - log.Error("unable to log because writer is not a LifecycleWriter") - return false -} diff --git a/pkg/server/web/handlers.go b/pkg/server/web/handlers.go deleted file mode 100644 index bbbb6adb..00000000 --- a/pkg/server/web/handlers.go +++ /dev/null @@ -1,141 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -// Package web provides handlers for the web application -package web - -import ( - "net/http" - - "github.com/dnote/dnote/pkg/server/middleware" - "github.com/dnote/dnote/pkg/server/tmpl" - "github.com/jinzhu/gorm" - "github.com/pkg/errors" -) - -var ( - // ErrEmptyDatabase is an error for missing db in the context - ErrEmptyDatabase = errors.New("No DB was provided") - // ErrEmptyIndexHTML is an error for missing index.html content in the context - ErrEmptyIndexHTML = errors.New("No index.html content was provided") - // ErrEmptyRobotsTxt is an error for missing robots.txt content in the context - ErrEmptyRobotsTxt = errors.New("No robots.txt content was provided") - // ErrEmptyServiceWorkerJS is an error for missing service worker content in the context - ErrEmptyServiceWorkerJS = errors.New("No service-worker.js content was provided") - // ErrEmptyStaticFileSystem is an error for missing static filesystem in the context - ErrEmptyStaticFileSystem = errors.New("No static filesystem was provided") -) - -// Context contains contents of web assets -type Context struct { - DB *gorm.DB - IndexHTML []byte - RobotsTxt []byte - ServiceWorkerJs []byte - StaticFileSystem http.FileSystem -} - -// Handlers are a group of web handlers -type Handlers struct { - GetRoot http.HandlerFunc - GetRobots http.HandlerFunc - GetServiceWorker http.HandlerFunc - GetStatic http.Handler -} - -func validateContext(c Context) error { - if c.DB == nil { - return ErrEmptyDatabase - } - if c.IndexHTML == nil { - return ErrEmptyIndexHTML - } - if c.RobotsTxt == nil { - return ErrEmptyRobotsTxt - } - if c.ServiceWorkerJs == nil { - return ErrEmptyServiceWorkerJS - } - if c.StaticFileSystem == nil { - return ErrEmptyStaticFileSystem - } - - return nil -} - -// Init initializes the handlers -func Init(c Context) (Handlers, error) { - if err := validateContext(c); err != nil { - return Handlers{}, errors.Wrap(err, "validating context") - } - - return Handlers{ - GetRoot: getRootHandler(c), - GetRobots: getRobotsHandler(c), - GetServiceWorker: getSWHandler(c), - GetStatic: getStaticHandler(c), - }, nil -} - -// getRootHandler returns an HTTP handler that serves the app shell -func getRootHandler(c Context) http.HandlerFunc { - appShell, err := tmpl.NewAppShell(c.DB, c.IndexHTML) - if err != nil { - panic(errors.Wrap(err, "initializing app shell")) - } - - return func(w http.ResponseWriter, r *http.Request) { - // index.html must not be cached - w.Header().Set("Cache-Control", "no-cache") - - buf, err := appShell.Execute(r) - if err != nil { - if errors.Cause(err) == tmpl.ErrNotFound { - middleware.RespondNotFound(w) - } else { - middleware.DoError(w, "executing app shell", err, http.StatusInternalServerError) - } - return - } - - w.Write(buf) - } -} - -// getRobotsHandler returns an HTTP handler that serves robots.txt -func getRobotsHandler(c Context) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Cache-Control", "no-cache") - w.Write(c.RobotsTxt) - } -} - -// getSWHandler returns an HTTP handler that serves service worker -func getSWHandler(c Context) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Cache-Control", "no-cache") - w.Header().Set("Content-Type", "application/javascript") - w.Write(c.ServiceWorkerJs) - } -} - -// getStaticHandler returns an HTTP handler that serves static files from a filesystem -func getStaticHandler(c Context) http.Handler { - root := c.StaticFileSystem - return http.StripPrefix("/static/", http.FileServer(root)) -} diff --git a/pkg/server/web/handlers_test.go b/pkg/server/web/handlers_test.go deleted file mode 100644 index 1e4b9e7c..00000000 --- a/pkg/server/web/handlers_test.go +++ /dev/null @@ -1,110 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package web - -import ( - "fmt" - "net/http" - "testing" - - "github.com/dnote/dnote/pkg/assert" - "github.com/dnote/dnote/pkg/server/testutils" - "github.com/pkg/errors" -) - -func TestInit(t *testing.T) { - mockIndexHTML := []byte("") - mockRobotsTxt := []byte("Allow: *") - mockServiceWorkerJs := []byte("function() {}") - mockStaticFileSystem := http.Dir(".") - - testCases := []struct { - ctx Context - expectedErr error - }{ - { - ctx: Context{ - DB: testutils.DB, - IndexHTML: mockIndexHTML, - RobotsTxt: mockRobotsTxt, - ServiceWorkerJs: mockServiceWorkerJs, - StaticFileSystem: mockStaticFileSystem, - }, - expectedErr: nil, - }, - { - ctx: Context{ - DB: nil, - IndexHTML: mockIndexHTML, - RobotsTxt: mockRobotsTxt, - ServiceWorkerJs: mockServiceWorkerJs, - StaticFileSystem: mockStaticFileSystem, - }, - expectedErr: ErrEmptyDatabase, - }, - { - ctx: Context{ - DB: testutils.DB, - IndexHTML: nil, - RobotsTxt: mockRobotsTxt, - ServiceWorkerJs: mockServiceWorkerJs, - StaticFileSystem: mockStaticFileSystem, - }, - expectedErr: ErrEmptyIndexHTML, - }, - { - ctx: Context{ - DB: testutils.DB, - IndexHTML: mockIndexHTML, - RobotsTxt: nil, - ServiceWorkerJs: mockServiceWorkerJs, - StaticFileSystem: mockStaticFileSystem, - }, - expectedErr: ErrEmptyRobotsTxt, - }, - { - ctx: Context{ - DB: testutils.DB, - IndexHTML: mockIndexHTML, - RobotsTxt: mockRobotsTxt, - ServiceWorkerJs: nil, - StaticFileSystem: mockStaticFileSystem, - }, - expectedErr: ErrEmptyServiceWorkerJS, - }, - { - ctx: Context{ - DB: testutils.DB, - IndexHTML: mockIndexHTML, - RobotsTxt: mockRobotsTxt, - ServiceWorkerJs: mockServiceWorkerJs, - StaticFileSystem: nil, - }, - expectedErr: ErrEmptyStaticFileSystem, - }, - } - - for idx, tc := range testCases { - t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { - _, err := Init(tc.ctx) - - assert.Equal(t, errors.Cause(err), tc.expectedErr, "error mismatch") - }) - } -} diff --git a/pkg/server/web/main_test.go b/pkg/server/web/main_test.go deleted file mode 100644 index 25c48343..00000000 --- a/pkg/server/web/main_test.go +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package web - -import ( - "os" - "testing" - - "github.com/dnote/dnote/pkg/server/testutils" -) - -func TestMain(m *testing.M) { - testutils.InitTestDB() - - code := m.Run() - testutils.ClearData(testutils.DB) - - os.Exit(code) -}