Remove unused files (#601)

This commit is contained in:
Sung Won Cho 2022-05-10 20:35:43 +10:00 committed by GitHub
commit 62fe48e451
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 0 additions and 347 deletions

View file

@ -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)
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
// 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))
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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("<html></html>")
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")
})
}
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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)
}