Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Rewrite app in Go #147

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@
# PROJECT FILES #
#################
.env
db/

###########
# CLOJURE #
###########
pom.xml
pom.xml.asc
*.jar
*.class
/lib/
/classes/
/target/
/checkouts/
.lein-deps-sum
.lein-repl-history
.lein-plugins/
.lein-failures
.nrepl-port
.cpcache/
################
# GOLANG FILES #
################
# https://github.com/github/gitignore
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum

12 changes: 0 additions & 12 deletions Dockerfile

This file was deleted.

122 changes: 122 additions & 0 deletions business/business.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package business

import (
"errors"
"github.com/liberdade-organizacao/no-backend-api/model"
"github.com/liberdade-organizacao/no-backend-api/utils"
)

type Context struct {
Database *model.Database
}

func (context *Context) Free() error {
return context.Database.Close()
}

func newClientAuthKey(clientId int, isAdmin bool) (string, error) {
message := map[string]any {
"client_id": clientId,
"is_admin": isAdmin,
}
authKey, err := utils.EncodeSecret(message)
if err != nil {
return "", err
}
return authKey, nil
}

// Returns `auth_key` of new client, or nil if client already exists
func (context *Context) NewClient(email, password string, isAdmin bool) (map[string]any, error) {
if email == "" || password == "" {
return nil, errors.New("invalid email/password combination")
}


rawSql := context.Database.Operations["create-client-account.sql"]
params := map[string]any {
"email": email,
"password": utils.HideSecret(password),
"is_admin": isAdmin,
}
query, err := utils.Format(rawSql, params)
if err != nil {
return nil, err
}

rows, err := context.Database.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
var resultId int = -1
var resultIsAdmin bool = false
for rows.Next() {
err = rows.Scan(&resultId, &resultIsAdmin)
if err != nil {
return nil, err
}
}

if resultId < 0 {
return nil, errors.New("client already exists")
}

authKey, err := newClientAuthKey(resultId, resultIsAdmin)
if err != nil {
return nil, err
}

outlet := map[string]any {
"auth_key": authKey,
}

return outlet, nil
}

// Returns `auth_key` if email and password match a valid user; nil otherwise
func (context *Context) AuthClient(email, password string) (map[string]any, error) {
if email == "" || password == "" {
return nil, errors.New("invalid email/password combination")
}

rawSql := context.Database.Operations["auth-client.sql"]
params := map[string]any {
"email": email,
"password": utils.HideSecret(password),
}
query, err := utils.Format(rawSql, params)
if err != nil {
return nil, err
}

rows, err := context.Database.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
var resultId int = -1
var resultIsAdmin bool = false
for rows.Next() {
err = rows.Scan(&resultId, &resultIsAdmin)
if err != nil {
return nil, err
}
}

if resultId < 0 {
return nil, errors.New("email and password didn't match any existing users")
}

authKey, err := newClientAuthKey(resultId, resultIsAdmin)
if err != nil {
return nil, err
}

outlet := map[string]any {
"auth_key": authKey,
}

return outlet, nil
}

88 changes: 88 additions & 0 deletions business/business_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package business

import (
"testing"
"github.com/liberdade-organizacao/no-backend-api/model"
)

func setup() *Context {
db, err := model.NewDatabaseInstance("../db/db.sqlite", "../resources")
if err != nil {
panic(err)
}
err = db.MigrateUp()
if err != nil {
panic(err)
}
context := Context {
Database: db,
}
return &context
}

func teardown(context *Context) {
err := context.Database.MigrateDown()
if err != nil {
panic(err)
}
context.Database.Close()
return
}

func TestHandleClientsAccounts(t *testing.T) {
context := setup()
defer teardown(context)

// signup
email := "[email protected]"
password := "password"
isAdmin := false
result, err := context.NewClient(email, password, isAdmin)
if err != nil {
t.Fatalf("Failed to create client: %s", err)
return
}
firstAuthKey := result["auth_key"].(string)
if firstAuthKey == "" {
t.Fatal("Failed to generate auth key after creation")
return
}

// login
result, err = context.AuthClient(email, password)
if err != nil {
t.Fatalf("Failed to authorize client: %s", err)
return
}
secondAuthKey := result["auth_key"].(string)
if secondAuthKey == "" {
t.Fatal("Failed to generate auth key after auth")
return
}
if firstAuthKey == secondAuthKey {
t.Fatal("unsafe auth keys were generated")
return
}

// gracefully fail repeated signup
result, err = context.NewClient(email, password, !isAdmin)
if err == nil || result != nil {
t.Fatalf("created repeated client")
return
}

// gracefully fail login
result, err = context.AuthClient("[email protected]", "random")
if result != nil || err == nil {
t.Fatalf("authenticated invalid user")
return
}

result, err = context.AuthClient("", "")
if result != nil || err == nil {
t.Fatalf("authenticated empty user")
return
}

}

Loading