-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michele Mastrogiovanni
committed
Sep 27, 2023
1 parent
6a9c0dd
commit ee368dc
Showing
47 changed files
with
3,710 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
FROM golang:1.18-bullseye as base | ||
|
||
RUN adduser \ | ||
--disabled-password \ | ||
--gecos "" \ | ||
--home "/nonexistent" \ | ||
--shell "/sbin/nologin" \ | ||
--no-create-home \ | ||
--uid 65532 \ | ||
small-user | ||
|
||
WORKDIR $GOPATH/src/smallest-golang/app/ | ||
|
||
COPY . . | ||
|
||
RUN go mod download | ||
RUN go mod verify | ||
|
||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /etaireia-backend ./cmd/etaireia-backend/main.go | ||
|
||
FROM scratch | ||
|
||
COPY --from=base /usr/share/zoneinfo /usr/share/zoneinfo | ||
COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | ||
COPY --from=base /etc/passwd /etc/passwd | ||
COPY --from=base /etc/group /etc/group | ||
|
||
COPY --from=base /etaireia-backend . | ||
|
||
USER small-user:small-user | ||
|
||
CMD ["./etaireia-backend"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# APIs | ||
|
||
GET /health | ||
|
||
POST /api/v1/sign { document, signature, publicKey } -> | ||
Sign a document and save for that | ||
|
||
GET /api/v1/signed/:publicKey/:timestamp/:signedTimestam -> | ||
returns all signed documents for a given publicKey (timestamp can't be holder that | ||
60 seconds and signedTimestamp need to be timestamp signed with the corresponding | ||
privateKey | ||
|
||
POST /api/v1/subscription { document, name, surname, publicKey, signature } -> | ||
Create a new user for the given public Key. | ||
- publicKey cannot be replicated | ||
- signature need to be created from document and privateKey for that publicKey | ||
- name and surname not null | ||
|
||
POST /api/v1/request { title, description, document, publicKey } -> | ||
Create a request to sign a document. This API need to be protected agains unauthorized use. | ||
|
||
POST /api/v1/tosign { nonce, publicKey, signature } -> | ||
Return list of documents that need to be signed | ||
- id | ||
- title | ||
- description | ||
|
||
POST /api/v1/document { nonce, publicKey, signature, id } -> | ||
Return the document to sign | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/mastrogiovanni/etaireia/etaireia-backend/internal/server" | ||
) | ||
|
||
func main() { | ||
|
||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) { | ||
log.Fatalln("Error loading .env") | ||
} | ||
|
||
engine, err := server.NewRouter() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
engine.Run(os.ExpandEnv(":${PORT}")) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"crypto/ed25519" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"mime/multipart" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func createSubscription(url string) (ed25519.PublicKey, ed25519.PrivateKey) { | ||
|
||
document := []byte("Hello World!") | ||
|
||
publicKey, privateKey, err := ed25519.GenerateKey(nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
publicKeyString := hex.EncodeToString([]byte(publicKey)) | ||
|
||
var b bytes.Buffer | ||
|
||
w := multipart.NewWriter(&b) | ||
|
||
fw, err := w.CreateFormField("publicKey") | ||
if err != nil { | ||
panic(err) | ||
} | ||
io.Copy(fw, strings.NewReader(publicKeyString)) | ||
|
||
fw, err = w.CreateFormField("name") | ||
if err != nil { | ||
panic(err) | ||
} | ||
io.Copy(fw, strings.NewReader("Michele")) | ||
|
||
fw, err = w.CreateFormField("surname") | ||
if err != nil { | ||
panic(err) | ||
} | ||
io.Copy(fw, strings.NewReader("Mastrogiovanni")) | ||
|
||
fw, err = w.CreateFormFile("document", "test.txt") | ||
if err != nil { | ||
panic(err) | ||
} | ||
io.Copy(fw, strings.NewReader(string(document))) | ||
w.Close() | ||
|
||
// log.Printf("%s\n", string(b.String())) | ||
|
||
req, err := http.NewRequest("POST", url, &b) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
req.Header.Set("Content-Type", w.FormDataContentType()) | ||
|
||
// Submit the request | ||
client := &http.Client{} | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer res.Body.Close() | ||
|
||
bodyBytes, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
bodyString := string(bodyBytes) | ||
|
||
log.Printf("Response: %s\n", bodyString) | ||
|
||
// Check the response | ||
if res.StatusCode != http.StatusOK { | ||
err = fmt.Errorf("bad status: %s", res.Status) | ||
panic(err) | ||
} | ||
|
||
return publicKey, privateKey | ||
|
||
} | ||
|
||
func approveSubscription(url string, publicKey ed25519.PublicKey) { | ||
|
||
body := map[string]string{ | ||
"approverPublicKey": hex.EncodeToString(publicKey), | ||
"subscriptionPublicKey": hex.EncodeToString(publicKey), | ||
} | ||
|
||
bodyString, _ := json.Marshal(body) | ||
|
||
bodyReader := bytes.NewReader(bodyString) | ||
|
||
req, err := http.NewRequest(http.MethodPost, url, bodyReader) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := http.Client{} | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
log.Println(res) | ||
|
||
} | ||
|
||
func main() { | ||
|
||
publicKey, _ := createSubscription("https://digital-signature.mastrogiovanni.cloud/api/v1/subscription") | ||
|
||
approveSubscription("https://digital-signature.mastrogiovanni.cloud/api/v1/subscription/approve", publicKey) | ||
|
||
// signature := ed25519.Sign(privateKey, document) | ||
|
||
// signatureString := hex.EncodeToString([]byte(signature)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/piprate/json-gold/ld" | ||
) | ||
|
||
func main() { | ||
|
||
proc := ld.NewJsonLdProcessor() | ||
options := ld.NewJsonLdOptions("") | ||
|
||
// // expanding remote document | ||
|
||
// expanded, err := proc.Expand("http://json-ld.org/test-suite/tests/expand-0002-in.jsonld", options) | ||
// if err != nil { | ||
// log.Println("Error when expanding JSON-LD document:", err) | ||
// return | ||
// } | ||
|
||
// log.Println(expanded...) | ||
|
||
// expanding in-memory document | ||
|
||
doc := map[string]interface{}{ | ||
"@context": "http://schema.org/", | ||
"@type": "Person", | ||
"name": "Jane Doe", | ||
"jobTitle": "Professor", | ||
"telephone": "(425) 123-4567", | ||
"url": "http://www.janedoe.com", | ||
} | ||
|
||
expanded, _ := proc.Expand(doc, options) | ||
|
||
log.Println(expanded) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module github.com/mastrogiovanni/etaireia/etaireia-backend | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/gin-gonic/gin v1.8.1 | ||
github.com/piprate/json-gold v0.4.2 | ||
) | ||
|
||
require ( | ||
github.com/golang/snappy v0.0.1 // indirect | ||
github.com/klauspost/compress v1.13.6 // indirect | ||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect | ||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect | ||
github.com/xdg-go/scram v1.1.1 // indirect | ||
github.com/xdg-go/stringprep v1.0.3 // indirect | ||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect | ||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect | ||
) | ||
|
||
require ( | ||
github.com/gin-contrib/cors v1.4.0 | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/go-playground/locales v0.14.0 // indirect | ||
github.com/go-playground/universal-translator v0.18.0 // indirect | ||
github.com/go-playground/validator/v10 v10.10.0 // indirect | ||
github.com/goccy/go-json v0.9.7 // indirect | ||
github.com/joho/godotenv v1.4.0 | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/leodido/go-urn v1.2.1 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.1 // indirect | ||
github.com/ugorji/go/codec v1.2.7 // indirect | ||
go.mongodb.org/mongo-driver v1.10.3 | ||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect | ||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect | ||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
google.golang.org/protobuf v1.28.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
) |
Oops, something went wrong.