Skip to content

Commit

Permalink
Merge pull request #11 from lwileczek/feat/login
Browse files Browse the repository at this point in the history
User login page, but doesn't really work
  • Loading branch information
lwileczek authored Apr 16, 2024
2 parents 2688309 + 6468efe commit dfcc916
Show file tree
Hide file tree
Showing 19 changed files with 595 additions and 19 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/basic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "Go - Basic Checks"
on:
push:
branches: [ "trunk" ]
paths:
- '**.go'
pull_request:
paths:
- '**.go'
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21.x', '1.22.x' ]

steps:
- name: Download This Repository
uses: actions/checkout@v4

- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Display Go version
run: go version

- name: Build
run: go build -v ./...

- name: Generate
run: go generate -v ./...

- name: Vet
run: go vet ./...

- name: Test
run: go test -v ./... -short
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ app/kodata
bootstrap
main

# Generated static pages
pages

# For testing files
tmp
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
build:
mkdir -p static/img
npm run build
go generate ./...
go build -o server ./app/main.go

run:
mkdir -p static/img
npm run build
go generate ./...
go run ./app/main.go

deployLambda:
bash utils/deploy.sh

buildContainer:
npm run build
go generate ./...
mkdir app/kodata
cp -r static templates app/kodata
ko build ./app/...
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.45.1
github.com/aws/smithy-go v1.17.0
github.com/awslabs/aws-lambda-go-api-proxy v0.16.0
github.com/go-playground/validator/v10 v10.14.0
github.com/gorilla/mux v1.8.0
github.com/jackc/pgx/v5 v5.4.3
github.com/rs/xid v1.5.0
Expand All @@ -29,10 +30,16 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.17.4 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.20.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.25.5 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
20 changes: 18 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
Expand All @@ -55,6 +65,8 @@ github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
Expand All @@ -66,10 +78,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
Expand Down
168 changes: 168 additions & 0 deletions initdb/0000_xid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
source code: https://github.com/modfin/pg-xid
ref: https://stackoverflow.com/a/48382296
Copyright 2022 Rasmus Holm
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
DO $$ BEGIN
CREATE DOMAIN public.xid AS CHAR(20) CHECK (VALUE ~ '^[a-v0-9]{20}$');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;

CREATE SEQUENCE IF NOT EXISTS public.xid_serial MINVALUE 0 MAXVALUE 16777215 CYCLE; -- ((255<<16) + (255<<8) + 255))

SELECT setval('xid_serial', (random() * 16777215)::INT); -- ((255<<16) + (255<<8) + 255))

CREATE OR REPLACE FUNCTION public._xid_machine_id()
RETURNS INT
LANGUAGE plpgsql
IMMUTABLE
AS
$$
DECLARE
BEGIN
RETURN (SELECT system_identifier & 16777215 FROM pg_control_system());
END
$$;

CREATE OR REPLACE FUNCTION public.xid_encode(_id int[])
RETURNS public.xid
LANGUAGE plpgsql
AS
$$
DECLARE
_encoding CHAR(1)[] = '{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v}';
BEGIN
RETURN _encoding[1 + (_id[1] >> 3)]
|| _encoding[1 + ((_id[2] >> 6) & 31 | (_id[1] << 2) & 31)]
|| _encoding[1 + ((_id[2] >> 1) & 31)]
|| _encoding[1 + ((_id[3] >> 4) & 31 | (_id[2] << 4) & 31)]
|| _encoding[1 + (_id[4] >> 7 | (_id[3] << 1) & 31)]
|| _encoding[1 + ((_id[4] >> 2) & 31)]
|| _encoding[1 + (_id[5] >> 5 | (_id[4] << 3) & 31)]
|| _encoding[1 + (_id[5] & 31)]
|| _encoding[1 + (_id[6] >> 3)]
|| _encoding[1 + ((_id[7] >> 6) & 31 | (_id[6] << 2) & 31)]
|| _encoding[1 + ((_id[7] >> 1) & 31)]
|| _encoding[1 + ((_id[8] >> 4) & 31 | (_id[7] << 4) & 31)]
|| _encoding[1 + (_id[9] >> 7 | (_id[8] << 1) & 31)]
|| _encoding[1 + ((_id[9] >> 2) & 31)]
|| _encoding[1 + ((_id[10] >> 5) | (_id[9] << 3) & 31)]
|| _encoding[1 + (_id[10] & 31)]
|| _encoding[1 + (_id[11] >> 3)]
|| _encoding[1 + ((_id[12] >> 6) & 31 | (_id[11] << 2) & 31)]
|| _encoding[1 + ((_id[12] >> 1) & 31)]
|| _encoding[1 + ((_id[12] << 4) & 31)];
END;
$$;

CREATE OR REPLACE FUNCTION public.xid_decode(_xid public.xid)
RETURNS int[]
LANGUAGE plpgsql
AS
$$
DECLARE
_dec int[] = '{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}';
_b bytea;
BEGIN
_b := _xid::BYTEA;
return ARRAY [
((_dec[get_byte(_b, 0)] << 3) | (_dec[get_byte(_b, 1)] >> 2)) & 255,
((_dec[get_byte(_b, 1)] << 6) | (_dec[get_byte(_b, 2)] << 1) | (_dec[get_byte(_b, 3)] >> 4)) & 255,
((_dec[get_byte(_b, 3)] << 4) | (_dec[get_byte(_b, 4)] >> 1)) & 255,
((_dec[get_byte(_b, 4)] << 7) | (_dec[get_byte(_b, 5)] << 2) | (_dec[get_byte(_b, 6)] >> 3)) & 255,
((_dec[get_byte(_b, 6)] << 5) | (_dec[get_byte(_b, 7)])) & 255,
((_dec[get_byte(_b, 8)] << 3) | (_dec[get_byte(_b, 9)] >> 2)) & 255,
((_dec[get_byte(_b, 9)] << 6) | (_dec[get_byte(_b, 10)] << 1) | (_dec[get_byte(_b, 11)] >> 4)) & 255,
((_dec[get_byte(_b, 11)] << 4) | (_dec[get_byte(_b, 12)] >> 1)) & 255,
((_dec[get_byte(_b, 12)] << 7) | (_dec[get_byte(_b, 13)] << 2) | (_dec[get_byte(_b, 14)] >> 3)) & 255,
((_dec[get_byte(_b, 14)] << 5) | (_dec[get_byte(_b, 15)])) & 255,
((_dec[get_byte(_b, 16)] << 3) | (_dec[get_byte(_b, 17)] >> 2)) & 255,
((_dec[get_byte(_b, 17)] << 6) | (_dec[get_byte(_b, 18)] << 1) | (_dec[get_byte(_b, 19)] >> 4)) & 255
];
END;
$$;

CREATE OR REPLACE FUNCTION xid(_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP)
RETURNS public.xid
LANGUAGE plpgsql
AS
$$
DECLARE
_t INT;
_m INT;
_p INT;
_c INT;
BEGIN
_t := floor(EXTRACT(epoch FROM _at));
_m := _xid_machine_id();
_p := pg_backend_pid();
_c := nextval('xid_serial')::INT;

return public.xid_encode(ARRAY [
(_t >> 24) & 255, (_t >> 16) & 255, (_t >> 8) & 255 , _t & 255,
(_m >> 16) & 255, (_m >> 8) & 255 , _m & 255,
(_p >> 8) & 255, _p & 255,
(_c >> 16) & 255, (_c >> 8) & 255 , _c & 255
]);
END;
$$;

CREATE OR REPLACE FUNCTION public.xid_time(_xid public.xid)
RETURNS TIMESTAMPTZ
LANGUAGE plpgsql
AS
$$
DECLARE
_id int[];
BEGIN
_id := public.xid_decode(_xid);
return to_timestamp((_id[1] << 24)::BIGINT + (_id[2] << 16) + (_id[3] << 8) + (_id[4]));
END;
$$;

CREATE OR REPLACE FUNCTION public.xid_machine(_xid public.xid)
RETURNS INT[]
LANGUAGE plpgsql
AS
$$
DECLARE
_id int[];
BEGIN
_id := public.xid_decode(_xid);
return ARRAY [_id[5], _id[6], _id[7]];
END;
$$;

CREATE OR REPLACE FUNCTION public.xid_pid(_xid public.xid)
RETURNS INT
LANGUAGE plpgsql
AS
$$
DECLARE
_id int[];
BEGIN
_id := public.xid_decode(_xid);
return (_id[8] << 8) + (_id[9]);
END;
$$;

CREATE OR REPLACE FUNCTION public.xid_counter(_xid public.xid)
RETURNS INT
LANGUAGE plpgsql
AS
$$
DECLARE
_id int[];
BEGIN
_id := public.xid_decode(_xid);
return (_id[10] << 16) + (_id[11] << 8) + (_id[12]);
END;
$$;
14 changes: 9 additions & 5 deletions initdb/0003_users.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
-- Table to store users so they can login
CREATE TABLE users (
-- unique id
id SERIAL not null primary key,
id CHAR(21) NOT NULL PRIMARY KEY DEFAULT 'u' || xid(),
-- some name for the user
name VARCHAR(48) not null,
name VARCHAR(48) NOT NULL,
-- salt for this user's password
salt CHAR(8) not null,
salt CHAR(8) NOT NULL,
-- hashed password
passwd CHAR(64) not null
passwd CHAR(64) NOT NULL,
-- Time the user was created
created_at DATE DEFAULT NOW()::date,
-- Admin: if the user is an admin user or not
admin BOOLEAN DEFAULT FALSE
);

INSERT INTO users(name, salt, passwd) VALUES('admin', 'CWWo5vK7', 'efc48adf01fc6f785244f28677bee8a884541861c4d2b3a4ff4db691cab6259e');
INSERT INTO users(name, salt, passwd, admin) VALUES('admin', 'CWWo5vK7', 'efc48adf01fc6f785244f28677bee8a884541861c4d2b3a4ff4db691cab6259e', true);
12 changes: 12 additions & 0 deletions initdb/0005_sessions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Table to store user sessionss for logins
CREATE TABLE IF NOT EXISTS sessions (
-- unique id
session_id CHAR(64) PRIMARY KEY,
-- Time the session was created
created_at TIMESTAMP DEFAULT NOW(),
-- The ID of the task the image is related too
user_id public.xid NOT NULL,
CONSTRAINT fk_task
FOREIGN KEY (user_id)
REFERENCES users(id)
);
14 changes: 12 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ func postComment(w http.ResponseWriter, r *http.Request) {
err = tmpl.ExecuteTemplate(
w,
"make-comment.html",
Comment{TaskID: uint32(id), User: pu, Content: r.FormValue("comments"), CreatedAt: time.Now()},
)
Comment{TaskID: uint32(id), User: pu, Content: r.FormValue("comments"), CreatedAt: time.Now()},
)
if err != nil {
Logr.Error("could not render template for new comment", "error", err)
}
Expand Down Expand Up @@ -257,9 +257,19 @@ func NewServer() *mux.Router {
mux.HandleFunc("/tasks/{id}/comments", postComment).Methods("POST")
mux.HandleFunc("/tasks/{id}/images", displayTaskImages).Methods("GET")

//static assets that are generated like CSS and JavaScript
s := http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))
mux.PathPrefix("/static/").Handler(s)

//serve static files from go generate
mux.PathPrefix("/login").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./pages/login.html")
})
//serve static files from go generate
mux.PathPrefix("/signup").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./pages/signup.html")
})

mux.NotFoundHandler = http.HandlerFunc(notFound)

return mux
Expand Down
Loading

0 comments on commit dfcc916

Please sign in to comment.