From b914a9f75c7c1726a710f5d9beddfdcdaa7d33cb Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Tue, 6 Sep 2022 21:05:03 +0930 Subject: [PATCH] Move go server into docker container and start adding tests --- Backend/Api/Auth/AddProfileImage.go | 6 +- Backend/Api/Auth/ChangeMessageExpiry.go | 4 +- Backend/Api/Auth/ChangePassword.go | 4 +- Backend/Api/Auth/Login.go | 4 +- Backend/Api/Auth/Login_test.go | 140 +++++++++++++++ Backend/Api/Auth/Logout.go | 7 +- Backend/Api/Auth/Logout_test.go | 131 ++++++++++++++ Backend/Api/Auth/Session.go | 6 +- Backend/Api/Auth/Signup.go | 88 ++++----- Backend/Api/Auth/Signup_test.go | 168 ++++++++++++++++++ Backend/Api/Friends/AcceptFriendRequest.go | 4 +- Backend/Api/Friends/EncryptedFriendsList.go | 6 +- Backend/Api/Friends/FriendRequest.go | 6 +- Backend/Api/Friends/Friends.go | 4 +- Backend/Api/Friends/RejectFriendRequest.go | 4 +- .../JsonSerialization/DeserializeUserJson.go | 2 +- Backend/Api/Messages/AddConversationImage.go | 6 +- Backend/Api/Messages/Conversations.go | 6 +- Backend/Api/Messages/CreateConversation.go | 4 +- Backend/Api/Messages/CreateMessage.go | 6 +- Backend/Api/Messages/MessageThread.go | 4 +- Backend/Api/Messages/UpdateConversation.go | 4 +- Backend/Api/Routes.go | 8 +- Backend/Api/Users/SearchUsers.go | 4 +- Backend/Database/Attachments.go | 2 +- Backend/Database/ConversationDetailUsers.go | 2 +- Backend/Database/ConversationDetails.go | 2 +- Backend/Database/FriendRequests.go | 2 +- Backend/Database/Init.go | 6 +- Backend/Database/MessageData.go | 2 +- Backend/Database/Messages.go | 2 +- Backend/Database/Seeder/FriendSeeder.go | 14 +- Backend/Database/Seeder/MessageSeeder.go | 16 +- Backend/Database/Seeder/Seed.go | 55 ++++-- Backend/Database/Seeder/UserSeeder.go | 14 +- Backend/Database/Seeder/encryption.go | 4 +- Backend/Database/Sessions.go | 16 +- Backend/Database/UserConversations.go | 2 +- Backend/Database/Users.go | 2 +- Backend/Dockerfile | 16 ++ Backend/Util/UserHelper.go | 4 +- Backend/go.mod | 2 +- Backend/main.go | 6 +- README.md | 2 +- docker-compose.yml | 49 +++++ .../android/app/src/main/AndroidManifest.xml | 2 +- mobile/lib/components/view_image.dart | 4 +- mobile/lib/main.dart | 2 +- mobile/lib/models/conversation_users.dart | 4 +- mobile/lib/models/conversations.dart | 4 +- mobile/lib/models/image_message.dart | 6 +- mobile/lib/models/my_profile.dart | 2 +- mobile/lib/utils/storage/conversations.dart | 4 +- mobile/lib/utils/storage/database.dart | 4 +- mobile/lib/utils/storage/messages.dart | 4 +- mobile/lib/views/authentication/login.dart | 2 +- mobile/lib/views/authentication/signup.dart | 6 +- .../unauthenticated_landing.dart | 2 +- .../conversation/create_add_users_list.dart | 4 +- .../lib/views/main/conversation/detail.dart | 2 +- .../views/main/conversation/edit_details.dart | 2 +- mobile/lib/views/main/conversation/list.dart | 6 +- .../lib/views/main/conversation/message.dart | 8 +- .../lib/views/main/conversation/settings.dart | 14 +- mobile/lib/views/main/friend/list.dart | 8 +- mobile/lib/views/main/friend/list_item.dart | 12 +- .../views/main/profile/change_server_url.dart | 2 +- mobile/lib/views/main/profile/profile.dart | 10 +- mobile/pubspec.yaml | 4 +- test.sh | 3 + 70 files changed, 740 insertions(+), 227 deletions(-) create mode 100644 Backend/Api/Auth/Login_test.go create mode 100644 Backend/Api/Auth/Logout_test.go create mode 100644 Backend/Api/Auth/Signup_test.go create mode 100644 Backend/Dockerfile create mode 100644 docker-compose.yml create mode 100644 test.sh diff --git a/Backend/Api/Auth/AddProfileImage.go b/Backend/Api/Auth/AddProfileImage.go index 31c7f64..deaea1c 100644 --- a/Backend/Api/Auth/AddProfileImage.go +++ b/Backend/Api/Auth/AddProfileImage.go @@ -5,9 +5,9 @@ import ( "encoding/json" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Util" ) // AddProfileImage adds a profile image diff --git a/Backend/Api/Auth/ChangeMessageExpiry.go b/Backend/Api/Auth/ChangeMessageExpiry.go index acad218..883f7e7 100644 --- a/Backend/Api/Auth/ChangeMessageExpiry.go +++ b/Backend/Api/Auth/ChangeMessageExpiry.go @@ -5,8 +5,8 @@ import ( "io/ioutil" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) type rawChangeMessageExpiry struct { diff --git a/Backend/Api/Auth/ChangePassword.go b/Backend/Api/Auth/ChangePassword.go index f4335cc..2688251 100644 --- a/Backend/Api/Auth/ChangePassword.go +++ b/Backend/Api/Auth/ChangePassword.go @@ -5,8 +5,8 @@ import ( "io/ioutil" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) type rawChangePassword struct { diff --git a/Backend/Api/Auth/Login.go b/Backend/Api/Auth/Login.go index d217493..7c72e07 100644 --- a/Backend/Api/Auth/Login.go +++ b/Backend/Api/Auth/Login.go @@ -6,8 +6,8 @@ import ( "net/http" "time" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) type credentials struct { diff --git a/Backend/Api/Auth/Login_test.go b/Backend/Api/Auth/Login_test.go new file mode 100644 index 0000000..11ea4af --- /dev/null +++ b/Backend/Api/Auth/Login_test.go @@ -0,0 +1,140 @@ +package Auth_test + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "io/ioutil" + "log" + "net/http" + "net/http/httptest" + "testing" + + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" + "github.com/gorilla/mux" +) + +func Test_Login(t *testing.T) { + log.SetOutput(ioutil.Discard) + Database.InitTest() + + r := mux.NewRouter() + Api.InitAPIEndpoints(r) + ts := httptest.NewServer(r) + defer ts.Close() + + userKey, _ := Seeder.GenerateAesKey() + pubKey := Seeder.GetPubKey() + + p, _ := Auth.HashPassword("password") + + u := Models.User{ + Username: "test", + Password: p, + AsymmetricPublicKey: Seeder.PublicKey, + AsymmetricPrivateKey: Seeder.EncryptedPrivateKey, + SymmetricKey: base64.StdEncoding.EncodeToString( + Seeder.EncryptWithPublicKey(userKey.Key, pubKey), + ), + } + + err := Database.CreateUser(&u) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + d := struct { + Username string `json:"username"` + Password string `json:"password"` + }{ + Username: "test", + Password: "password", + } + + jsonStr, _ := json.Marshal(d) + req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + if resp.StatusCode != http.StatusOK { + t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) + return + } + + var session Models.Session + + err = Database.DB.First(&session, "user_id = ?", u.ID.String()).Error + + if err != nil { + t.Errorf("Expected user record, recieved %s", err.Error()) + return + } +} + +func Test_Login_PasswordFails(t *testing.T) { + log.SetOutput(ioutil.Discard) + Database.InitTest() + + r := mux.NewRouter() + Api.InitAPIEndpoints(r) + ts := httptest.NewServer(r) + defer ts.Close() + + userKey, _ := Seeder.GenerateAesKey() + pubKey := Seeder.GetPubKey() + + p, _ := Auth.HashPassword("password") + + u := Models.User{ + Username: "test", + Password: p, + AsymmetricPublicKey: Seeder.PublicKey, + AsymmetricPrivateKey: Seeder.EncryptedPrivateKey, + SymmetricKey: base64.StdEncoding.EncodeToString( + Seeder.EncryptWithPublicKey(userKey.Key, pubKey), + ), + } + + err := Database.CreateUser(&u) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + d := struct { + Username string `json:"username"` + Password string `json:"password"` + }{ + Username: "test", + Password: "password1", + } + + jsonStr, _ := json.Marshal(d) + req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + if resp.StatusCode != http.StatusUnauthorized { + t.Errorf("Expected %d, recieved %d", http.StatusUnauthorized, resp.StatusCode) + return + } +} diff --git a/Backend/Api/Auth/Logout.go b/Backend/Api/Auth/Logout.go index 486b575..484fcc7 100644 --- a/Backend/Api/Auth/Logout.go +++ b/Backend/Api/Auth/Logout.go @@ -5,9 +5,10 @@ import ( "net/http" "time" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" ) +// Logout logs out from system func Logout(w http.ResponseWriter, r *http.Request) { var ( c *http.Cookie @@ -27,7 +28,7 @@ func Logout(w http.ResponseWriter, r *http.Request) { sessionToken = c.Value - err = Database.DeleteSessionById(sessionToken) + err = Database.DeleteSessionByID(sessionToken) if err != nil { log.Println("Could not delete session cookie") } @@ -37,4 +38,6 @@ func Logout(w http.ResponseWriter, r *http.Request) { Value: "", Expires: time.Now(), }) + + w.WriteHeader(http.StatusOK) } diff --git a/Backend/Api/Auth/Logout_test.go b/Backend/Api/Auth/Logout_test.go new file mode 100644 index 0000000..89e2262 --- /dev/null +++ b/Backend/Api/Auth/Logout_test.go @@ -0,0 +1,131 @@ +package Auth_test + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "io/ioutil" + "log" + "net/http" + "net/http/cookiejar" + "net/http/httptest" + "net/url" + "sync" + "testing" + + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" + "github.com/gorilla/mux" +) + +type Jar struct { + lk sync.Mutex + cookies map[string][]*http.Cookie +} + +func Test_Logout(t *testing.T) { + log.SetOutput(ioutil.Discard) + Database.InitTest() + + r := mux.NewRouter() + Api.InitAPIEndpoints(r) + ts := httptest.NewServer(r) + defer ts.Close() + + userKey, _ := Seeder.GenerateAesKey() + pubKey := Seeder.GetPubKey() + + p, _ := Auth.HashPassword("password") + + u := Models.User{ + Username: "test", + Password: p, + AsymmetricPublicKey: Seeder.PublicKey, + AsymmetricPrivateKey: Seeder.EncryptedPrivateKey, + SymmetricKey: base64.StdEncoding.EncodeToString( + Seeder.EncryptWithPublicKey(userKey.Key, pubKey), + ), + } + + err := Database.CreateUser(&u) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + d := struct { + Username string `json:"username"` + Password string `json:"password"` + }{ + Username: "test", + Password: "password", + } + + jsonStr, _ := json.Marshal(d) + req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + if resp.StatusCode != http.StatusOK { + t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) + return + } + + var session Models.Session + + err = Database.DB.First(&session, "user_id = ?", u.ID.String()).Error + + if err != nil { + t.Errorf("Expected session record, recieved %s", err.Error()) + return + } + + jar, err := cookiejar.New(nil) + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + url, _ := url.Parse(ts.URL) + + jar.SetCookies( + url, + []*http.Cookie{ + &http.Cookie{ + Name: "session_token", + Value: session.ID.String(), + MaxAge: 300, + }, + }, + ) + + client = &http.Client{ + Jar: jar, + } + resp, err = client.Get(ts.URL + "/api/v1/logout") + + if err != nil { + t.Errorf("Expected user record, recieved %s", err.Error()) + return + } + + if resp.StatusCode != http.StatusOK { + t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) + return + } + + err = Database.DB.First(&session, "user_id = ?", u.ID.String()).Error + if err == nil { + t.Errorf("Expected no session record, recieved %s", session.UserID) + return + } +} diff --git a/Backend/Api/Auth/Session.go b/Backend/Api/Auth/Session.go index ffcfae2..4c4c5a1 100644 --- a/Backend/Api/Auth/Session.go +++ b/Backend/Api/Auth/Session.go @@ -4,8 +4,8 @@ import ( "errors" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) func CheckCookie(r *http.Request) (Models.Session, error) { @@ -23,7 +23,7 @@ func CheckCookie(r *http.Request) (Models.Session, error) { sessionToken = c.Value // We then get the session from our session map - userSession, err = Database.GetSessionById(sessionToken) + userSession, err = Database.GetSessionByID(sessionToken) if err != nil { return userSession, errors.New("Cookie not found") } diff --git a/Backend/Api/Auth/Signup.go b/Backend/Api/Auth/Signup.go index b60f880..90252a9 100644 --- a/Backend/Api/Auth/Signup.go +++ b/Backend/Api/Auth/Signup.go @@ -2,95 +2,67 @@ package Auth import ( "encoding/json" - "io/ioutil" - "log" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/JsonSerialization" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) -type signupResponse struct { - Status string `json:"status"` - Message string `json:"message"` -} - -func makeSignupResponse(w http.ResponseWriter, code int, message string) { - var ( - status = "error" - returnJSON []byte - err error - ) - if code > 200 && code < 300 { - status = "success" - } - - returnJSON, err = json.MarshalIndent(signupResponse{ - Status: status, - Message: message, - }, "", " ") - if err != nil { - http.Error(w, "Error", http.StatusInternalServerError) - return - } - - // Return updated json - w.WriteHeader(code) - w.Write(returnJSON) - +type signup struct { + Username string `json:"username"` + Password string `json:"password"` + ConfirmPassword string `json:"confirm_password"` + PublicKey string `json:"asymmetric_public_key"` + PrivateKey string `json:"asymmetric_private_key"` + SymmetricKey string `json:"symmetric_key"` } // Signup to the platform func Signup(w http.ResponseWriter, r *http.Request) { var ( - userData Models.User - requestBody []byte - err error + user Models.User + err error ) - requestBody, err = ioutil.ReadAll(r.Body) + err = json.NewDecoder(r.Body).Decode(&user) if err != nil { - log.Printf("Error encountered reading POST body: %s\n", err.Error()) - makeSignupResponse(w, http.StatusInternalServerError, "An error occurred") + http.Error(w, "Invalid Data", http.StatusUnprocessableEntity) return } - userData, err = JsonSerialization.DeserializeUser(requestBody, []string{ - "id", - }, false) - if err != nil { - log.Printf("Invalid data provided to Signup: %s\n", err.Error()) - makeSignupResponse(w, http.StatusUnprocessableEntity, "Invalid data provided") + if user.Username == "" || + user.Password == "" || + user.ConfirmPassword == "" || + len(user.AsymmetricPrivateKey) == 0 || + len(user.AsymmetricPublicKey) == 0 || + len(user.SymmetricKey) == 0 { + + http.Error(w, "Invalid Data", http.StatusUnprocessableEntity) return } - if userData.Username == "" || - userData.Password == "" || - userData.ConfirmPassword == "" || - len(userData.AsymmetricPrivateKey) == 0 || - len(userData.AsymmetricPublicKey) == 0 { - makeSignupResponse(w, http.StatusUnprocessableEntity, "Invalid data provided") + if user.Password != user.ConfirmPassword { + http.Error(w, "Invalid Data", http.StatusUnprocessableEntity) return } - err = Database.CheckUniqueUsername(userData.Username) + err = Database.CheckUniqueUsername(user.Username) if err != nil { - makeSignupResponse(w, http.StatusUnprocessableEntity, "Invalid data provided") + http.Error(w, "Invalid Data", http.StatusUnprocessableEntity) return } - userData.Password, err = HashPassword(userData.Password) + user.Password, err = HashPassword(user.Password) if err != nil { - makeSignupResponse(w, http.StatusInternalServerError, "An error occurred") + http.Error(w, "Error", http.StatusInternalServerError) return } - err = Database.CreateUser(&userData) + err = Database.CreateUser(&user) if err != nil { - makeSignupResponse(w, http.StatusInternalServerError, "An error occurred") + http.Error(w, "Error", http.StatusInternalServerError) return } - makeSignupResponse(w, http.StatusCreated, "Successfully signed up") + w.WriteHeader(http.StatusNoContent) } diff --git a/Backend/Api/Auth/Signup_test.go b/Backend/Api/Auth/Signup_test.go new file mode 100644 index 0000000..c57b125 --- /dev/null +++ b/Backend/Api/Auth/Signup_test.go @@ -0,0 +1,168 @@ +package Auth_test + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "io/ioutil" + "log" + "net/http" + "net/http/httptest" + "testing" + + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" + "github.com/gorilla/mux" +) + +func Test_Signup(t *testing.T) { + log.SetOutput(ioutil.Discard) + Database.InitTest() + + r := mux.NewRouter() + Api.InitAPIEndpoints(r) + ts := httptest.NewServer(r) + defer ts.Close() + + userKey, _ := Seeder.GenerateAesKey() + + pubKey := Seeder.GetPubKey() + + d := struct { + Username string `json:"username"` + Password string `json:"password"` + ConfirmPassword string `json:"confirm_password"` + PubKey string `json:"asymmetric_public_key"` + PrivKey string `json:"asymmetric_private_key"` + SymKey string `json:"symmetric_key"` + }{ + Username: "test", + Password: "password", + ConfirmPassword: "password", + PubKey: Seeder.PublicKey, + PrivKey: Seeder.EncryptedPrivateKey, + SymKey: base64.StdEncoding.EncodeToString( + Seeder.EncryptWithPublicKey(userKey.Key, pubKey), + ), + } + + jsonStr, _ := json.Marshal(d) + req, _ := http.NewRequest("POST", ts.URL+"/api/v1/signup", bytes.NewBuffer(jsonStr)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + if resp.StatusCode != http.StatusNoContent { + t.Errorf("Expected %d, recieved %d", http.StatusNoContent, resp.StatusCode) + return + } + + var user Models.User + + err = Database.DB.First(&user, "username = ?", "test").Error + + if err != nil { + t.Errorf("Expected user record, recieved %s", err.Error()) + return + } +} + +func Test_Signup_PasswordMismatchFails(t *testing.T) { + log.SetOutput(ioutil.Discard) + Database.InitTest() + + r := mux.NewRouter() + Api.InitAPIEndpoints(r) + ts := httptest.NewServer(r) + defer ts.Close() + + userKey, _ := Seeder.GenerateAesKey() + + pubKey := Seeder.GetPubKey() + + d := struct { + Username string `json:"username"` + Password string `json:"password"` + ConfirmPassword string `json:"confirm_password"` + PubKey string `json:"asymmetric_public_key"` + PrivKey string `json:"asymmetric_private_key"` + SymKey string `json:"symmetric_key"` + }{ + Username: "test", + Password: "password", + ConfirmPassword: "password1", + PubKey: Seeder.PublicKey, + PrivKey: Seeder.EncryptedPrivateKey, + SymKey: base64.StdEncoding.EncodeToString( + Seeder.EncryptWithPublicKey(userKey.Key, pubKey), + ), + } + + jsonStr, _ := json.Marshal(d) + req, _ := http.NewRequest("POST", ts.URL+"/api/v1/signup", bytes.NewBuffer(jsonStr)) + req.Header.Set("X-Custom-Header", "myvalue") + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + if resp.StatusCode != http.StatusUnprocessableEntity { + t.Errorf("Expected %d, recieved %d", http.StatusUnprocessableEntity, resp.StatusCode) + return + } +} + +func Test_Signup_MissingDataFails(t *testing.T) { + log.SetOutput(ioutil.Discard) + Database.InitTest() + + r := mux.NewRouter() + Api.InitAPIEndpoints(r) + ts := httptest.NewServer(r) + defer ts.Close() + + d := struct { + Username string `json:"username"` + Password string `json:"password"` + ConfirmPassword string `json:"confirm_password"` + PubKey string `json:"asymmetric_public_key"` + PrivKey string `json:"asymmetric_private_key"` + SymKey string `json:"symmetric_key"` + }{ + Username: "test", + Password: "password", + ConfirmPassword: "password", + PubKey: "", + PrivKey: "", + SymKey: "", + } + + jsonStr, _ := json.Marshal(d) + req, _ := http.NewRequest("POST", ts.URL+"/api/v1/signup", bytes.NewBuffer(jsonStr)) + req.Header.Set("X-Custom-Header", "myvalue") + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + } + + if resp.StatusCode != http.StatusUnprocessableEntity { + t.Errorf("Expected %d, recieved %d", http.StatusUnprocessableEntity, resp.StatusCode) + } +} diff --git a/Backend/Api/Friends/AcceptFriendRequest.go b/Backend/Api/Friends/AcceptFriendRequest.go index aa9e233..9b68bdd 100644 --- a/Backend/Api/Friends/AcceptFriendRequest.go +++ b/Backend/Api/Friends/AcceptFriendRequest.go @@ -6,8 +6,8 @@ import ( "net/http" "time" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "github.com/gorilla/mux" ) diff --git a/Backend/Api/Friends/EncryptedFriendsList.go b/Backend/Api/Friends/EncryptedFriendsList.go index 410c75c..c2ea274 100644 --- a/Backend/Api/Friends/EncryptedFriendsList.go +++ b/Backend/Api/Friends/EncryptedFriendsList.go @@ -4,9 +4,9 @@ import ( "encoding/json" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) // EncryptedFriendRequestList gets friend request list diff --git a/Backend/Api/Friends/FriendRequest.go b/Backend/Api/Friends/FriendRequest.go index 126605d..c704800 100644 --- a/Backend/Api/Friends/FriendRequest.go +++ b/Backend/Api/Friends/FriendRequest.go @@ -5,9 +5,9 @@ import ( "io/ioutil" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Util" ) func FriendRequest(w http.ResponseWriter, r *http.Request) { diff --git a/Backend/Api/Friends/Friends.go b/Backend/Api/Friends/Friends.go index a1db196..d7f0b53 100644 --- a/Backend/Api/Friends/Friends.go +++ b/Backend/Api/Friends/Friends.go @@ -6,8 +6,8 @@ import ( "net/http" "time" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) // CreateFriendRequest creates a FriendRequest from post data diff --git a/Backend/Api/Friends/RejectFriendRequest.go b/Backend/Api/Friends/RejectFriendRequest.go index e341858..8ba5829 100644 --- a/Backend/Api/Friends/RejectFriendRequest.go +++ b/Backend/Api/Friends/RejectFriendRequest.go @@ -3,8 +3,8 @@ package Friends import ( "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "github.com/gorilla/mux" ) diff --git a/Backend/Api/JsonSerialization/DeserializeUserJson.go b/Backend/Api/JsonSerialization/DeserializeUserJson.go index 9220be8..4d5af16 100644 --- a/Backend/Api/JsonSerialization/DeserializeUserJson.go +++ b/Backend/Api/JsonSerialization/DeserializeUserJson.go @@ -6,7 +6,7 @@ import ( "fmt" "strings" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" schema "github.com/Kangaroux/go-map-schema" ) diff --git a/Backend/Api/Messages/AddConversationImage.go b/Backend/Api/Messages/AddConversationImage.go index 1da2866..31c36e9 100644 --- a/Backend/Api/Messages/AddConversationImage.go +++ b/Backend/Api/Messages/AddConversationImage.go @@ -5,9 +5,9 @@ import ( "encoding/json" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Util" "github.com/gorilla/mux" ) diff --git a/Backend/Api/Messages/Conversations.go b/Backend/Api/Messages/Conversations.go index a1681da..4678108 100644 --- a/Backend/Api/Messages/Conversations.go +++ b/Backend/Api/Messages/Conversations.go @@ -6,9 +6,9 @@ import ( "net/url" "strings" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) // EncryptedConversationList returns an encrypted list of all Conversations diff --git a/Backend/Api/Messages/CreateConversation.go b/Backend/Api/Messages/CreateConversation.go index 41de38c..728ecb0 100644 --- a/Backend/Api/Messages/CreateConversation.go +++ b/Backend/Api/Messages/CreateConversation.go @@ -6,8 +6,8 @@ import ( "github.com/gofrs/uuid" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) // RawCreateConversationData for holding POST payload diff --git a/Backend/Api/Messages/CreateMessage.go b/Backend/Api/Messages/CreateMessage.go index 052f128..becc0c2 100644 --- a/Backend/Api/Messages/CreateMessage.go +++ b/Backend/Api/Messages/CreateMessage.go @@ -5,9 +5,9 @@ import ( "encoding/json" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Util" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Util" ) type rawMessageData struct { diff --git a/Backend/Api/Messages/MessageThread.go b/Backend/Api/Messages/MessageThread.go index 686f1c1..ff466d3 100644 --- a/Backend/Api/Messages/MessageThread.go +++ b/Backend/Api/Messages/MessageThread.go @@ -4,8 +4,8 @@ import ( "encoding/json" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "github.com/gorilla/mux" ) diff --git a/Backend/Api/Messages/UpdateConversation.go b/Backend/Api/Messages/UpdateConversation.go index 4900ba8..67d2a2c 100644 --- a/Backend/Api/Messages/UpdateConversation.go +++ b/Backend/Api/Messages/UpdateConversation.go @@ -6,8 +6,8 @@ import ( "github.com/gofrs/uuid" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) type rawUpdateConversationData struct { diff --git a/Backend/Api/Routes.go b/Backend/Api/Routes.go index 8b0c280..4058644 100644 --- a/Backend/Api/Routes.go +++ b/Backend/Api/Routes.go @@ -4,10 +4,10 @@ import ( "log" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Friends" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Messages" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Users" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Friends" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Messages" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Users" "github.com/gorilla/mux" ) diff --git a/Backend/Api/Users/SearchUsers.go b/Backend/Api/Users/SearchUsers.go index 56ecd89..51f2e62 100644 --- a/Backend/Api/Users/SearchUsers.go +++ b/Backend/Api/Users/SearchUsers.go @@ -5,8 +5,8 @@ import ( "net/http" "net/url" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) // SearchUsers searches a for a user by username diff --git a/Backend/Database/Attachments.go b/Backend/Database/Attachments.go index 3097a04..bda1415 100644 --- a/Backend/Database/Attachments.go +++ b/Backend/Database/Attachments.go @@ -1,7 +1,7 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm" "gorm.io/gorm/clause" diff --git a/Backend/Database/ConversationDetailUsers.go b/Backend/Database/ConversationDetailUsers.go index 6396acb..9215c0f 100644 --- a/Backend/Database/ConversationDetailUsers.go +++ b/Backend/Database/ConversationDetailUsers.go @@ -1,7 +1,7 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm" "gorm.io/gorm/clause" diff --git a/Backend/Database/ConversationDetails.go b/Backend/Database/ConversationDetails.go index af04edb..811fa62 100644 --- a/Backend/Database/ConversationDetails.go +++ b/Backend/Database/ConversationDetails.go @@ -1,7 +1,7 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm" "gorm.io/gorm/clause" diff --git a/Backend/Database/FriendRequests.go b/Backend/Database/FriendRequests.go index 0f6e58a..d93c9e7 100644 --- a/Backend/Database/FriendRequests.go +++ b/Backend/Database/FriendRequests.go @@ -1,7 +1,7 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm" "gorm.io/gorm/clause" diff --git a/Backend/Database/Init.go b/Backend/Database/Init.go index f4b6fb9..0d1a729 100644 --- a/Backend/Database/Init.go +++ b/Backend/Database/Init.go @@ -3,15 +3,15 @@ package Database import ( "log" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/driver/postgres" "gorm.io/gorm" ) const ( - dbURL = "postgres://postgres:@localhost:5432/envelope" - dbTestURL = "postgres://postgres:@localhost:5432/envelope_test" + dbURL = "postgres://postgres:password@postgres:5432/capsule" + dbTestURL = "postgres://postgres:password@postgres-testing:5432/capsule-testing" ) // DB db diff --git a/Backend/Database/MessageData.go b/Backend/Database/MessageData.go index 80c6515..4198f2a 100644 --- a/Backend/Database/MessageData.go +++ b/Backend/Database/MessageData.go @@ -1,7 +1,7 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm" "gorm.io/gorm/clause" diff --git a/Backend/Database/Messages.go b/Backend/Database/Messages.go index f415c0e..dd0fbfe 100644 --- a/Backend/Database/Messages.go +++ b/Backend/Database/Messages.go @@ -1,7 +1,7 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm" "gorm.io/gorm/clause" diff --git a/Backend/Database/Seeder/FriendSeeder.go b/Backend/Database/Seeder/FriendSeeder.go index e317d13..a527004 100644 --- a/Backend/Database/Seeder/FriendSeeder.go +++ b/Backend/Database/Seeder/FriendSeeder.go @@ -6,8 +6,8 @@ import ( "os" "time" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error { @@ -18,12 +18,12 @@ func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error err error ) - symKey, err = generateAesKey() + symKey, err = GenerateAesKey() if err != nil { return err } - encPublicKey, err = symKey.aesEncrypt([]byte(publicKey)) + encPublicKey, err = symKey.aesEncrypt([]byte(PublicKey)) if err != nil { return err } @@ -31,13 +31,13 @@ func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error friendRequest = Models.FriendRequest{ UserID: userRequestTo.ID, FriendID: base64.StdEncoding.EncodeToString( - encryptWithPublicKey( + EncryptWithPublicKey( []byte(userRequestFrom.ID.String()), decodedPublicKey, ), ), FriendUsername: base64.StdEncoding.EncodeToString( - encryptWithPublicKey( + EncryptWithPublicKey( []byte(userRequestFrom.Username), decodedPublicKey, ), @@ -46,7 +46,7 @@ func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error encPublicKey, ), SymmetricKey: base64.StdEncoding.EncodeToString( - encryptWithPublicKey(symKey.Key, decodedPublicKey), + EncryptWithPublicKey(symKey.Key, decodedPublicKey), ), } diff --git a/Backend/Database/Seeder/MessageSeeder.go b/Backend/Database/Seeder/MessageSeeder.go index 1bdffb9..efe4646 100644 --- a/Backend/Database/Seeder/MessageSeeder.go +++ b/Backend/Database/Seeder/MessageSeeder.go @@ -3,8 +3,8 @@ package Seeder import ( "encoding/base64" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "github.com/gofrs/uuid" ) @@ -27,12 +27,12 @@ func seedMessage( plaintext = "Test Message" - userKey, err = generateAesKey() + userKey, err = GenerateAesKey() if err != nil { panic(err) } - key, err = generateAesKey() + key, err = GenerateAesKey() if err != nil { panic(err) } @@ -70,7 +70,7 @@ func seedMessage( message = Models.Message{ MessageData: messageData, SymmetricKey: base64.StdEncoding.EncodeToString( - encryptWithPublicKey(userKey.Key, decodedPublicKey), + EncryptWithPublicKey(userKey.Key, decodedPublicKey), ), AssociationKey: primaryUserAssociationKey, } @@ -83,7 +83,7 @@ func seedMessage( message = Models.Message{ MessageData: messageData, SymmetricKey: base64.StdEncoding.EncodeToString( - encryptWithPublicKey(userKey.Key, decodedPublicKey), + EncryptWithPublicKey(userKey.Key, decodedPublicKey), ), AssociationKey: secondaryUserAssociationKey, } @@ -148,7 +148,7 @@ func seedUserConversation( ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext), Admin: base64.StdEncoding.EncodeToString(adminCiphertext), SymmetricKey: base64.StdEncoding.EncodeToString( - encryptWithPublicKey(key.Key, decodedPublicKey), + EncryptWithPublicKey(key.Key, decodedPublicKey), ), } @@ -233,7 +233,7 @@ func SeedMessages() { err error ) - key, err = generateAesKey() + key, err = GenerateAesKey() if err != nil { panic(err) } diff --git a/Backend/Database/Seeder/Seed.go b/Backend/Database/Seeder/Seed.go index 7bd5c40..bdfc1d0 100644 --- a/Backend/Database/Seeder/Seed.go +++ b/Backend/Database/Seeder/Seed.go @@ -9,11 +9,11 @@ import ( ) const ( - // Encrypted with "password" - encryptedPrivateKey string = `sPhQsHpXYFqPb7qdmTY7APFwBb4m7meCITujDeKMQFnIjplOVm9ijjXU+YAmGvrX13ukBj8zo9MTVhjJUjJ917pyLhl4w8uyg1jCvplUYtJVXhGA9Wy3NqHMuq3SU3fKdlEM+oR4zYkbAYWp42XvulbcuVBEWiWkvHOrbdKPFpMmd54SL2c/vcWrmjgC7rTlJf2TYICZwRK+6Y0XZi5fSWeU0vg7+rHWKHc5MHHtAdAiL+HCa90c5gfh+hXkT5ojGHOkhT9kdLy3PTPN19EGpdXgZ3WFq1z9CZ6zX7uM091uR0IvgzfwaLx8HJCx7ViWQhioH9LJZgC73RMf/dwzejg2COy4QT/E59RPOczgd779rxiRmphMoR8xJYBFRlkTVmcUO4NcUE50Cc39hXezcekHuV1YQK4BXTrxGX1ceiCXYlKAWS9wHZpog9OldTCPBpw5XAWExh3kRzqdvsdHxHVE+TpAEIjDljAlc3r+FPHYH1zWWk41eQ/zz3Vkx5Zl4dMF9x+uUOspQXVb/4K42e9fMKychNUN5o/JzIwy7xOzgXa6iwf223On/mXKV6FK6Q8lojK7Wc8g7AwfqnN9//HjI14pVqGBJtn5ggL/g4qt0JFl3pV/6n/ZLMG6k8wpsaApLGvsTPqZHcv+C69Z33rZQ4TagXVxpmnWMpPCaR0+Dawn4i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KTj3CuH7FuAkMHvwU0odMFeaS9T+8+4OGeprHwogWTzTbPnoOqOP/RC6vGfBvpju5s264hYguT24iXzhDFYk/8JQQe+USIbkQ7wXRw+/9cK8h5cs4LyaxMOx0pXHooxJ01bF8BYgYG4s0RB2gItzMk/L5/XhrOdWxEAdYR27s0dCN58gyvoU6phgQbTqvNTFYAObRcjfKfHu3PrFCYBBAKJ7Nm58C3rz832+ZTGVdQ3490TvO+sCLYKzpgtsqr8KyedG9LKa8wn/wlRD7kYn+J2SrMPY2Q0e4evyJaCAsolp/BQfy9JFtyRDPWTHn+jOHjW8ZN7vswGkRwYlSJSl0UC8mmJyS4lwnO/Vv4wBnDHQEzIycjn3JZAlV5ing0HKqUfW6G07453JXd8oZiMC/kIQjgWkdg34zxBYarVVrHFG5FIH9w7QWY8PCDU/kkcLniT0yD1/gkqAG2HpwaXEcSqX8Ofrbpd/IA7R7iCXYE5Q1mAvSvICpPg9Cf3CHjLyAEDz9cwKnZHkocXC8evdsTf2e7Wz8FFPAI3onFvym0MfZuRrIZitX1V8NOLedd3y74CwuErfzrr60DjyPRxGbJ4llMbm+ojeENe0HBedNm71jf+McSihKbSo5GDBxfVYVreYZ8A4iP0LsxtzQFxuzdeDL5KA9uNNw+LN9FN9vKhdALhQSnSfLPfMBsM/ey7dbxb4eRT0fpApX` - // Private key for testing server side - privateKey string = `-----BEGIN PRIVATE KEY----- + // PrivateKey for testing server side + PrivateKey string = `-----BEGIN PRIVATE KEY----- MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDJScQQJxWxKwqf FmXH64QnRBVyW7cU25F+O9Zy96dqTjbV4ruWrzb4+txmK20ZPQvMxDLefhEzTXWb HZV1P/XxgmEpaBVHwHnkhaPzzChOa/G18CDoCNrgyVzh5a31OotTCuGlS1bSkR53 @@ -42,7 +42,8 @@ b0XvaLzh1iKG7HZ9tvPt/VhHlKKosNBK/j4fvgMZg7/bhRfHmaDQKoqlGbtyWjEQ mj1b2/Gnbk3VYDR16BFfj7m2 -----END PRIVATE KEY-----` - publicKey string = `-----BEGIN PUBLIC KEY----- + // PublicKey for encryption + PublicKey string = `-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyUnEECcVsSsKnxZlx+uE J0QVclu3FNuRfjvWcvenak421eK7lq82+PrcZittGT0LzMQy3n4RM011mx2VdT/1 8YJhKWgVR8B55IWj88woTmvxtfAg6Aja4Mlc4eWt9TqLUwrhpUtW0pEedxMT10Kv @@ -58,35 +59,57 @@ var ( decodedPrivateKey *rsa.PrivateKey ) -// Seed seeds semi random data for use in testing & development -func Seed() { +// GetPubKey for seeding & tests +func GetPubKey() *rsa.PublicKey { var ( - block *pem.Block - decKey any - ok bool - err error + block *pem.Block + decKey any + decPubKey *rsa.PublicKey + ok bool + err error ) - - block, _ = pem.Decode([]byte(publicKey)) + block, _ = pem.Decode([]byte(PublicKey)) decKey, err = x509.ParsePKIXPublicKey(block.Bytes) if err != nil { panic(err) } - decodedPublicKey, ok = decKey.(*rsa.PublicKey) + + decPubKey, ok = decKey.(*rsa.PublicKey) if !ok { panic(errors.New("Invalid decodedPublicKey")) } - block, _ = pem.Decode([]byte(privateKey)) + return decPubKey +} + +// GetPrivKey for seeding & tests +func GetPrivKey() *rsa.PrivateKey { + var ( + block *pem.Block + decKey any + decPrivKey *rsa.PrivateKey + ok bool + err error + ) + block, _ = pem.Decode([]byte(PrivateKey)) decKey, err = x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { panic(err) } - decodedPrivateKey, ok = decKey.(*rsa.PrivateKey) + decPrivKey, ok = decKey.(*rsa.PrivateKey) if !ok { panic(errors.New("Invalid decodedPrivateKey")) } + return decPrivKey +} + +// Seed seeds semi random data for use in testing & development +func Seed() { + + decodedPublicKey = GetPubKey() + decodedPrivateKey = GetPrivKey() + log.Println("Seeding users...") SeedUsers() diff --git a/Backend/Database/Seeder/UserSeeder.go b/Backend/Database/Seeder/UserSeeder.go index c65a94e..e47f983 100644 --- a/Backend/Database/Seeder/UserSeeder.go +++ b/Backend/Database/Seeder/UserSeeder.go @@ -3,9 +3,9 @@ package Seeder import ( "encoding/base64" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" ) var userNames = []string{ @@ -30,7 +30,7 @@ func createUser(username string) (Models.User, error) { err error ) - userKey, err = generateAesKey() + userKey, err = GenerateAesKey() if err != nil { panic(err) } @@ -43,10 +43,10 @@ func createUser(username string) (Models.User, error) { userData = Models.User{ Username: username, Password: password, - AsymmetricPrivateKey: encryptedPrivateKey, - AsymmetricPublicKey: publicKey, + AsymmetricPrivateKey: EncryptedPrivateKey, + AsymmetricPublicKey: PublicKey, SymmetricKey: base64.StdEncoding.EncodeToString( - encryptWithPublicKey(userKey.Key, decodedPublicKey), + EncryptWithPublicKey(userKey.Key, decodedPublicKey), ), } diff --git a/Backend/Database/Seeder/encryption.go b/Backend/Database/Seeder/encryption.go index a116134..61f9013 100644 --- a/Backend/Database/Seeder/encryption.go +++ b/Backend/Database/Seeder/encryption.go @@ -71,7 +71,7 @@ func pkcs7strip(data []byte, blockSize int) ([]byte, error) { return data[:length-padLen], nil } -func generateAesKey() (aesKey, error) { +func GenerateAesKey() (aesKey, error) { var ( saltBytes []byte = []byte{} password []byte @@ -157,7 +157,7 @@ func (key aesKey) aesDecrypt(ciphertext []byte) ([]byte, error) { } // EncryptWithPublicKey encrypts data with public key -func encryptWithPublicKey(msg []byte, pub *rsa.PublicKey) []byte { +func EncryptWithPublicKey(msg []byte, pub *rsa.PublicKey) []byte { var ( hash hash.Hash ) diff --git a/Backend/Database/Sessions.go b/Backend/Database/Sessions.go index 1f125df..afee878 100644 --- a/Backend/Database/Sessions.go +++ b/Backend/Database/Sessions.go @@ -1,12 +1,13 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm/clause" ) -func GetSessionById(id string) (Models.Session, error) { +// GetSessionByID Gets session +func GetSessionByID(id string) (Models.Session, error) { var ( session Models.Session err error @@ -19,6 +20,7 @@ func GetSessionById(id string) (Models.Session, error) { return session, err } +// CreateSession creates session func CreateSession(session *Models.Session) error { var ( err error @@ -29,10 +31,16 @@ func CreateSession(session *Models.Session) error { return err } +// DeleteSession deletes session func DeleteSession(session *Models.Session) error { return DB.Delete(session).Error } -func DeleteSessionById(id string) error { - return DB.Delete(&Models.Session{}, id).Error +// DeleteSessionByID deletes session +func DeleteSessionByID(id string) error { + return DB.Delete( + &Models.Session{}, + "id = ?", + id, + ).Error } diff --git a/Backend/Database/UserConversations.go b/Backend/Database/UserConversations.go index 930a98f..2e77ce7 100644 --- a/Backend/Database/UserConversations.go +++ b/Backend/Database/UserConversations.go @@ -1,7 +1,7 @@ package Database import ( - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm" "gorm.io/gorm/clause" diff --git a/Backend/Database/Users.go b/Backend/Database/Users.go index 2df6a73..c27ba2c 100644 --- a/Backend/Database/Users.go +++ b/Backend/Database/Users.go @@ -3,7 +3,7 @@ package Database import ( "errors" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "gorm.io/gorm" "gorm.io/gorm/clause" diff --git a/Backend/Dockerfile b/Backend/Dockerfile new file mode 100644 index 0000000..9fea42e --- /dev/null +++ b/Backend/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:1.19-alpine + +RUN mkdir -p /go/src/git.tovijaeschke.xyz/Capsule/Backend + +COPY ./ /go/src/git.tovijaeschke.xyz/Capsule/Backend + +WORKDIR /go/src/git.tovijaeschke.xyz/Capsule/Backend + +# For "go test" +RUN apk add gcc libc-dev + +RUN go mod download + +RUN go build -o /go/bin/capsule-server main.go + +CMD [ "/go/bin/capsule-server" ] diff --git a/Backend/Util/UserHelper.go b/Backend/Util/UserHelper.go index 32616a6..47b2569 100644 --- a/Backend/Util/UserHelper.go +++ b/Backend/Util/UserHelper.go @@ -5,8 +5,8 @@ import ( "log" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" "github.com/gorilla/mux" ) diff --git a/Backend/go.mod b/Backend/go.mod index 127bb75..bebe75f 100644 --- a/Backend/go.mod +++ b/Backend/go.mod @@ -1,4 +1,4 @@ -module git.tovijaeschke.xyz/tovi/Envelope/Backend +module git.tovijaeschke.xyz/tovi/Capsule/Backend go 1.18 diff --git a/Backend/main.go b/Backend/main.go index e9dc701..8001402 100644 --- a/Backend/main.go +++ b/Backend/main.go @@ -5,9 +5,9 @@ import ( "log" "net/http" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database" - "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" + "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder" "github.com/gorilla/mux" ) diff --git a/README.md b/README.md index d52d837..d43ef78 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Envelope +# Capsule Encrypted messaging app diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6c89730 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,49 @@ +version: "3" + +services: + server: + build: + context: ./Backend + ports: + - "8080:8080" + volumes: + - "./Backend:/app" + links: + - postgres + - postgres-testing + depends_on: + postgres: + condition: service_healthy + depends_on: + postgres-testing: + condition: service_healthy + postgres: + image: postgres:14.5 + ports: + - "54321:5432" + environment: + POSTGRES_DB: capsule + POSTGRES_PASSWORD: password + volumes: + - /var/lib/postgres + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 + postgres-testing: + image: postgres:14.5 + ports: + - "54322:5432" + environment: + POSTGRES_DB: capsule-testing + POSTGRES_PASSWORD: password + tmpfs: + - /var/lib/mysql + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 + + diff --git a/mobile/android/app/src/main/AndroidManifest.xml b/mobile/android/app/src/main/AndroidManifest.xml index c3bfaaa..26cb9e0 100644 --- a/mobile/android/app/src/main/AndroidManifest.xml +++ b/mobile/android/app/src/main/AndroidManifest.xml @@ -2,7 +2,7 @@ package="com.example.mobile"> deleteDb() async { - final path = join(await getDatabasesPath(), 'envelope.db'); + final path = join(await getDatabasesPath(), 'capsule.db'); deleteDatabase(path); } Future getDatabaseConnection() async { WidgetsFlutterBinding.ensureInitialized(); - final path = join(await getDatabasesPath(), 'envelope.db'); + final path = join(await getDatabasesPath(), 'capsule.db'); final database = openDatabase( path, diff --git a/mobile/lib/utils/storage/messages.dart b/mobile/lib/utils/storage/messages.dart index fd7ced7..8c07669 100644 --- a/mobile/lib/utils/storage/messages.dart +++ b/mobile/lib/utils/storage/messages.dart @@ -1,8 +1,8 @@ import 'dart:convert'; import 'dart:io'; -import 'package:Envelope/models/messages.dart'; -import 'package:Envelope/utils/storage/write_file.dart'; +import 'package:Capsule/models/messages.dart'; +import 'package:Capsule/utils/storage/write_file.dart'; import 'package:http/http.dart' as http; import 'package:sqflite/sqflite.dart'; import 'package:uuid/uuid.dart'; diff --git a/mobile/lib/views/authentication/login.dart b/mobile/lib/views/authentication/login.dart index 6dce978..d4c56a0 100644 --- a/mobile/lib/views/authentication/login.dart +++ b/mobile/lib/views/authentication/login.dart @@ -177,7 +177,7 @@ class _LoginWidgetState extends State { }).catchError((error) { print(error); showMessage( - 'Could not login to Envelope, please try again later.', + 'Could not login to Capsule, please try again later.', context, ); }); diff --git a/mobile/lib/views/authentication/signup.dart b/mobile/lib/views/authentication/signup.dart index 2a190e0..22ac9ab 100644 --- a/mobile/lib/views/authentication/signup.dart +++ b/mobile/lib/views/authentication/signup.dart @@ -1,8 +1,8 @@ import 'dart:convert'; import 'dart:typed_data'; -import 'package:Envelope/components/flash_message.dart'; -import 'package:Envelope/models/my_profile.dart'; +import 'package:Capsule/components/flash_message.dart'; +import 'package:Capsule/models/my_profile.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; @@ -176,7 +176,7 @@ class _SignupWidgetState extends State { .then((dynamic) { Navigator.of(context).popUntil((route) => route.isFirst); }).catchError((error) { - showMessage('Failed to signup to Envelope, please try again later', context); + showMessage('Failed to signup to Capsule, please try again later', context); }); }, child: const Text('Submit'), diff --git a/mobile/lib/views/authentication/unauthenticated_landing.dart b/mobile/lib/views/authentication/unauthenticated_landing.dart index 6bcd086..3a7e432 100644 --- a/mobile/lib/views/authentication/unauthenticated_landing.dart +++ b/mobile/lib/views/authentication/unauthenticated_landing.dart @@ -46,7 +46,7 @@ class _UnauthenticatedLandingWidgetState extends State