diff --git a/api/api.go b/api/api.go index 85a458764..9fae0cd2e 100644 --- a/api/api.go +++ b/api/api.go @@ -19,6 +19,7 @@ import ( "github.com/RTradeLtd/Temporal/api/middleware" "github.com/RTradeLtd/Temporal/database" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/models" "github.com/gin-gonic/gin" @@ -152,7 +153,7 @@ func (api *API) setupRoutes() { statsProtected.GET("/stats", func(c *gin.Context) { // admin locked username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } c.JSON(http.StatusOK, stats.Report()) diff --git a/api/routes_account.go b/api/routes_account.go index b368e1b5f..a389940bc 100644 --- a/api/routes_account.go +++ b/api/routes_account.go @@ -6,6 +6,7 @@ import ( "net/http" "strconv" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/queue" "github.com/RTradeLtd/Temporal/utils" "github.com/gin-gonic/gin" @@ -35,12 +36,12 @@ func (api *API) changeAccountPassword(c *gin.Context) { suceeded, err := api.um.ChangePassword(username, oldPassword, newPassword) if err != nil { - api.LogError(err, PasswordChangeError)(c) + api.LogError(err, eh.PasswordChangeError)(c) return } if !suceeded { err = fmt.Errorf("password changed failed for user %s to due an unspecified error", username) - api.LogError(err, PasswordChangeError)(c) + api.LogError(err, eh.PasswordChangeError)(c) return } @@ -74,11 +75,13 @@ func (api *API) registerUserAccount(c *gin.Context) { }).Info("user account registration detected") userModel, err := api.um.NewUserAccount(username, password, email, false) - if err != nil { - api.LogError(err, UserAccountCreationError)(c) + if err.Error() != eh.DuplicateEmailError || err.Error() != eh.DuplicateUserNameError { + api.LogError(err, eh.UserAccountCreationError)(c, http.StatusBadRequest) + return + } else if err != nil { + api.LogError(err, err.Error())(c, http.StatusBadRequest) return } - api.l.WithFields(log.Fields{ "service": "api", "user": username, @@ -111,7 +114,7 @@ func (api *API) createIPFSKey(c *gin.Context) { user, err := api.um.FindByUserName(username) if err != nil { - api.LogError(err, UserSearchError)(c, http.StatusNotFound) + api.LogError(err, eh.UserSearchError)(c, http.StatusNotFound) return } var cost float64 @@ -127,11 +130,11 @@ func (api *API) createIPFSKey(c *gin.Context) { } } if err != nil { - api.LogError(err, CallCostCalculationError)(c, http.StatusBadRequest) + api.LogError(err, eh.CallCostCalculationError)(c, http.StatusBadRequest) return } if err := api.validateUserCredits(username, cost); err != nil && cost > 0 { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } keyBits, exists := c.GetPostForm("key_bits") @@ -150,7 +153,7 @@ func (api *API) createIPFSKey(c *gin.Context) { keys, err := api.um.GetKeysForUser(username) if err != nil { - api.LogError(err, KeySearchError)(c, http.StatusNotFound) + api.LogError(err, eh.KeySearchError)(c, http.StatusNotFound) api.refundUserCredits(username, "key", cost) return } @@ -158,7 +161,7 @@ func (api *API) createIPFSKey(c *gin.Context) { for _, v := range keys["key_names"] { if v == keyNamePrefixed { err = fmt.Errorf("key with name already exists") - api.LogError(err, DuplicateKeyCreationError)(c, http.StatusConflict) + api.LogError(err, eh.DuplicateKeyCreationError)(c, http.StatusConflict) api.refundUserCredits(username, "key", cost) return } @@ -187,13 +190,13 @@ func (api *API) createIPFSKey(c *gin.Context) { qm, err := queue.Initialize(queue.IpfsKeyCreationQueue, mqConnectionURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) api.refundUserCredits(username, "key", cost) return } if err = qm.PublishMessageWithExchange(key, queue.IpfsKeyExchange); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) api.refundUserCredits(username, "key", cost) return } @@ -212,12 +215,12 @@ func (api *API) getIPFSKeyNamesForAuthUser(c *gin.Context) { keys, err := api.um.GetKeysForUser(username) if err != nil { - api.LogError(err, KeySearchError)(c) + api.LogError(err, eh.KeySearchError)(c) return } // if the user has no keys, fail with an error if len(keys["key_names"]) == 0 || len(keys["key_ids"]) == 0 { - Fail(c, errors.New(NoKeyError), http.StatusNotFound) + Fail(c, errors.New(eh.NoKeyError), http.StatusNotFound) return } api.LogWithUser(username).Info("key name list requested") @@ -230,7 +233,7 @@ func (api *API) getCredits(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) credits, err := api.um.GetCreditsForUser(username) if err != nil { - api.LogError(err, CreditCheckError)(c, http.StatusBadRequest) + api.LogError(err, eh.CreditCheckError)(c, http.StatusBadRequest) return } api.LogWithUser(username).Info("credit check requested") diff --git a/api/routes_database.go b/api/routes_database.go index 3886d6a32..dcd7cebcb 100644 --- a/api/routes_database.go +++ b/api/routes_database.go @@ -3,6 +3,7 @@ package api import ( "net/http" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/models" "github.com/gin-gonic/gin" ) @@ -13,14 +14,14 @@ var dev = false func (api *API) getUploadsFromDatabase(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } um := models.NewUploadManager(api.dbm.DB) // fetch the uplaods uploads, err := um.GetUploads() if err != nil { - api.LogError(err, UploadSearchError)(c, http.StatusInternalServerError) + api.LogError(err, eh.UploadSearchError)(c, http.StatusInternalServerError) return } api.LogInfo("all uploads from database requested") @@ -42,7 +43,7 @@ func (api *API) getUploadsForUser(c *gin.Context) { // fetch all uploads for that address uploads, err := um.GetUploadsForUser(queryUser) if err != nil { - api.LogError(err, UploadSearchError)(c, http.StatusInternalServerError) + api.LogError(err, eh.UploadSearchError)(c, http.StatusInternalServerError) return } api.LogInfo("specific uploads from database requested") diff --git a/api/routes_frontend.go b/api/routes_frontend.go index 095cfd8af..c76aeed97 100644 --- a/api/routes_frontend.go +++ b/api/routes_frontend.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/rtfs" "github.com/RTradeLtd/Temporal/utils" "github.com/gin-gonic/gin" @@ -22,7 +23,7 @@ func (api *API) calculatePinCost(c *gin.Context) { holdTime := c.Param("holdtime") manager, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } holdTimeInt, err := strconv.ParseInt(holdTime, 10, 64) @@ -40,7 +41,7 @@ func (api *API) calculatePinCost(c *gin.Context) { } totalCost, err := utils.CalculatePinCost(hash, holdTimeInt, manager.Shell, isPrivate) if err != nil { - api.LogError(err, PinCostCalculationError) + api.LogError(err, eh.PinCostCalculationError) Fail(c, err) return } diff --git a/api/routes_ipns.go b/api/routes_ipns.go index 7f91f6e2e..c11d075b2 100644 --- a/api/routes_ipns.go +++ b/api/routes_ipns.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/queue" "github.com/RTradeLtd/Temporal/utils" gocid "github.com/ipfs/go-cid" @@ -54,23 +55,23 @@ func (api *API) publishToIPNSDetails(c *gin.Context) { cost, err := utils.CalculateAPICallCost("ipns", false) if err != nil { - api.LogError(err, CallCostCalculationError)(c, http.StatusBadRequest) + api.LogError(err, eh.CallCostCalculationError)(c, http.StatusBadRequest) return } if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } ownsKey, err := api.um.CheckIfKeyOwnedByUser(username, key) if err != nil { - api.LogError(err, KeySearchError)(c) + api.LogError(err, eh.KeySearchError)(c) api.refundUserCredits(username, "ipns", cost) return } if !ownsKey { err = fmt.Errorf("user %s attempted to generate IPFS entry with unowned key", username) - api.LogError(err, KeyUseError)(c) + api.LogError(err, eh.KeyUseError)(c) api.refundUserCredits(username, "ipns", cost) return } @@ -106,14 +107,14 @@ func (api *API) publishToIPNSDetails(c *gin.Context) { qm, err := queue.Initialize(queue.IpnsEntryQueue, mqURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) api.refundUserCredits(username, "ipns", cost) return } // in order to avoid generating too much IPFS dht traffic, we publish round-robin style // as we announce the records to the swarm, we will eventually achieve consistency across nodes automatically if err = qm.PublishMessage(ie); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) api.refundUserCredits(username, "ipns", cost) return } @@ -131,7 +132,7 @@ func (api *API) getIPNSRecordsPublishedByUser(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) records, err := api.im.FindByUserName(username) if err != nil { - api.LogError(err, IpnsRecordSearchError)(c, http.StatusBadRequest) + api.LogError(err, eh.IpnsRecordSearchError)(c, http.StatusBadRequest) return } // check if records is nil, or no entries. For len we must dereference first @@ -146,7 +147,7 @@ func (api *API) getIPNSRecordsPublishedByUser(c *gin.Context) { func (api *API) generateDNSLinkEntry(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } recordName, exists := c.GetPostForm("record_name") @@ -188,14 +189,14 @@ func (api *API) generateDNSLinkEntry(c *gin.Context) { awsManager, err := dlink.GenerateAwsLinkManager("get", aKey, aSecret, awsZone, region) if err != nil { - api.LogError(err, DNSLinkManagerError) + api.LogError(err, eh.DNSLinkManagerError) Fail(c, err) return } resp, err := awsManager.AddDNSLinkEntry(recordName, recordValue) if err != nil { - api.LogError(err, DNSLinkEntryError) + api.LogError(err, eh.DNSLinkEntryError) Fail(c, err) return } diff --git a/api/routes_mini.go b/api/routes_mini.go index 37aff9418..9a11b2601 100644 --- a/api/routes_mini.go +++ b/api/routes_mini.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/mini" "github.com/gin-gonic/gin" ) @@ -12,7 +13,7 @@ import ( func (api *API) makeBucket(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } bucketName, exists := c.GetPostForm("bucket_name") @@ -28,14 +29,14 @@ func (api *API) makeBucket(c *gin.Context) { ) manager, err := mini.NewMinioManager(endpoint, accessKey, secretKey, true) if err != nil { - api.LogError(err, MinioConnectionError)(c) + api.LogError(err, eh.MinioConnectionError)(c) return } args := make(map[string]string) args["name"] = bucketName if err = manager.MakeBucket(args); err != nil { - api.LogError(err, MinioBucketCreationError)(c) + api.LogError(err, eh.MinioBucketCreationError)(c) return } diff --git a/api/routes_payment.go b/api/routes_payment.go index 790726362..e9ded4c04 100644 --- a/api/routes_payment.go +++ b/api/routes_payment.go @@ -4,6 +4,7 @@ import ( "errors" "net/http" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/models" "github.com/RTradeLtd/Temporal/queue" "github.com/RTradeLtd/Temporal/utils" @@ -20,12 +21,12 @@ func (api *API) CreatePayment(c *gin.Context) { } usdValue, err := api.getUSDValue(paymentType) if err != nil { - api.LogError(err, CmcCheckError)(c, http.StatusBadRequest) + api.LogError(err, eh.CmcCheckError)(c, http.StatusBadRequest) return } depositAddress, err := api.getDepositAddress(paymentType) if err != nil { - api.LogError(err, DepositAddressCheckError)(c, http.StatusBadRequest) + api.LogError(err, eh.DepositAddressCheckError)(c, http.StatusBadRequest) return } txHash, exists := c.GetPostForm("tx_hash") @@ -39,12 +40,12 @@ func (api *API) CreatePayment(c *gin.Context) { return } if check := api.validateBlockchain(blockchain); !check { - api.LogError(err, InvalidPaymentBlockchainError)(c, http.StatusBadRequest) + api.LogError(err, eh.InvalidPaymentBlockchainError)(c, http.StatusBadRequest) return } pm := models.NewPaymentManager(api.dbm.DB) if _, err := pm.NewPayment(depositAddress, txHash, usdValue, blockchain, paymentType, username); err != nil { - api.LogError(err, PaymentCreationError)(c, http.StatusBadRequest) + api.LogError(err, eh.PaymentCreationError)(c, http.StatusBadRequest) return } pc := queue.PaymentCreation{ @@ -55,11 +56,11 @@ func (api *API) CreatePayment(c *gin.Context) { mqURL := api.cfg.RabbitMQ.URL qm, err := queue.Initialize(queue.PaymentCreationQueue, mqURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c, http.StatusBadRequest) + api.LogError(err, eh.QueueInitializationError)(c, http.StatusBadRequest) return } if err = qm.PublishMessage(pc); err != nil { - api.LogError(err, QueuePublishError)(c, http.StatusBadRequest) + api.LogError(err, eh.QueuePublishError)(c, http.StatusBadRequest) return } Respond(c, http.StatusOK, gin.H{"response": "payment created"}) @@ -70,7 +71,7 @@ func (api *API) GetDepositAddress(c *gin.Context) { paymentType := c.Param("type") address, err := api.getDepositAddress(paymentType) if err != nil { - api.LogError(err, InvalidPaymentTypeError)(c, http.StatusBadRequest) + api.LogError(err, eh.InvalidPaymentTypeError)(c, http.StatusBadRequest) return } Respond(c, http.StatusOK, gin.H{"response": address}) @@ -90,5 +91,5 @@ func (api *API) getUSDValue(paymentType string) (float64, error) { case "rtc": return RtcCostUsd, nil } - return 0, errors.New(InvalidPaymentTypeError) + return 0, errors.New(eh.InvalidPaymentTypeError) } diff --git a/api/routes_rtfs.go b/api/routes_rtfs.go index d22cc89a5..dda7149a0 100644 --- a/api/routes_rtfs.go +++ b/api/routes_rtfs.go @@ -7,6 +7,7 @@ import ( "net/http" "strconv" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/mini" "github.com/RTradeLtd/Temporal/utils" "github.com/RTradeLtd/crypto" @@ -29,13 +30,13 @@ func (api *API) calculateContentHashForFile(c *gin.Context) { reader, err := fileHandler.Open() if err != nil { - api.LogError(err, FileOpenError)(c) + api.LogError(err, eh.FileOpenError)(c) return } defer reader.Close() hash, err := utils.GenerateIpfsMultiHashForFile(reader) if err != nil { - api.LogError(err, IPFSMultiHashGenerationError)(c) + api.LogError(err, eh.IPFSMultiHashGenerationError)(c) return } @@ -64,16 +65,16 @@ func (api *API) pinHashLocally(c *gin.Context) { } shell, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c, http.StatusBadRequest) + api.LogError(err, eh.IPFSConnectionError)(c, http.StatusBadRequest) return } cost, err := utils.CalculatePinCost(hash, holdTimeInt, shell.Shell, false) if err != nil { - api.LogError(err, PinCostCalculationError)(c, http.StatusBadRequest) + api.LogError(err, eh.PinCostCalculationError)(c, http.StatusBadRequest) return } if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } ip := queue.IPFSPin{ @@ -88,13 +89,13 @@ func (api *API) pinHashLocally(c *gin.Context) { qm, err := queue.Initialize(queue.IpfsPinQueue, mqConnectionURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) api.refundUserCredits(username, "pin", cost) return } if err = qm.PublishMessageWithExchange(ip, queue.PinExchange); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) api.refundUserCredits(username, "pin", cost) return } @@ -125,7 +126,7 @@ func (api *API) addFileLocallyAdvanced(c *gin.Context) { miniManager, err := mini.NewMinioManager(endpoint, accessKey, secretKey, false) if err != nil { - api.LogError(err, MinioConnectionError)(c) + api.LogError(err, eh.MinioConnectionError)(c) return } fileHandler, err := c.FormFile("file") @@ -144,13 +145,13 @@ func (api *API) addFileLocallyAdvanced(c *gin.Context) { } cost := utils.CalculateFileCost(holdTimeInt, fileHandler.Size, false) if err = api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } logger.Debug("opening file") openFile, err := fileHandler.Open() if err != nil { - api.LogError(err, FileOpenError, + api.LogError(err, eh.FileOpenError, "user", username)(c) api.refundUserCredits(username, "file", cost) return @@ -167,7 +168,7 @@ func (api *API) addFileLocallyAdvanced(c *gin.Context) { Bucket: FilesUploadBucket, EncryptPassphrase: c.PostForm("passphrase"), }); err != nil { - api.LogError(err, MinioPutError, + api.LogError(err, eh.MinioPutError, "user", username)(c) api.refundUserCredits(username, "file", cost) return @@ -187,14 +188,14 @@ func (api *API) addFileLocallyAdvanced(c *gin.Context) { } qm, err := queue.Initialize(queue.IpfsFileQueue, mqURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError, + api.LogError(err, eh.QueueInitializationError, "user", username)(c) api.refundUserCredits(username, "file", cost) return } if err = qm.PublishMessage(ifp); err != nil { - api.LogError(err, QueuePublishError, + api.LogError(err, eh.QueuePublishError, "user", username)(c) api.refundUserCredits(username, "file", cost) return @@ -234,14 +235,14 @@ func (api *API) addFileLocally(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) cost := utils.CalculateFileCost(holdTimeinMonthsInt, fileHandler.Size, false) if err = api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } // open the file api.LogDebug("opening file") openFile, err := fileHandler.Open() if err != nil { - api.LogError(err, FileOpenError)(c) + api.LogError(err, eh.FileOpenError)(c) api.refundUserCredits(username, "file", cost) return } @@ -250,7 +251,7 @@ func (api *API) addFileLocally(c *gin.Context) { // initialize a connection to the local ipfs node manager, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) api.refundUserCredits(username, "file", cost) return } @@ -258,7 +259,7 @@ func (api *API) addFileLocally(c *gin.Context) { api.LogDebug("adding file...") resp, err := manager.Add(openFile) if err != nil { - api.LogError(err, IPFSAddError)(c) + api.LogError(err, eh.IPFSAddError)(c) api.refundUserCredits(username, "file", cost) return } @@ -277,19 +278,19 @@ func (api *API) addFileLocally(c *gin.Context) { // initialize a connectino to rabbitmq qm, err := queue.Initialize(queue.DatabaseFileAddQueue, mqConnectionURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) return } // publish the database file add message if err = qm.PublishMessage(dfa); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) return } qm, err = queue.Initialize(queue.IpfsPinQueue, mqConnectionURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) return } @@ -300,7 +301,7 @@ func (api *API) addFileLocally(c *gin.Context) { HoldTimeInMonths: holdTimeinMonthsInt, CreditCost: 0, }, queue.PinExchange); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) return } @@ -319,21 +320,21 @@ func (api *API) ipfsPubSubPublish(c *gin.Context) { } cost, err := utils.CalculateAPICallCost("pubsub", false) if err != nil { - api.LogError(err, CallCostCalculationError)(c, http.StatusBadRequest) + api.LogError(err, eh.CallCostCalculationError)(c, http.StatusBadRequest) return } if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } manager, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) api.refundUserCredits(username, "pubsub", cost) return } if err = manager.PublishPubSubMessage(topic, message); err != nil { - api.LogError(err, IPFSPubSubPublishError)(c) + api.LogError(err, eh.IPFSPubSubPublishError)(c) api.refundUserCredits(username, "pubsub", cost) return } @@ -347,20 +348,20 @@ func (api *API) ipfsPubSubPublish(c *gin.Context) { func (api *API) getLocalPins(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } // initialize a connection toe the local ipfs node manager, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } // get all the known local pins // WARNING: THIS COULD BE A VERY LARGE LIST pinInfo, err := manager.Shell.Pins() if err != nil { - api.LogError(err, IPFSPinParseError)(c) + api.LogError(err, eh.IPFSPinParseError)(c) return } @@ -378,13 +379,13 @@ func (api *API) getObjectStatForIpfs(c *gin.Context) { } manager, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError) + api.LogError(err, eh.IPFSConnectionError) Fail(c, err) return } stats, err := manager.ObjectStat(key) if err != nil { - api.LogError(err, IPFSObjectStatError) + api.LogError(err, eh.IPFSObjectStatError) Fail(c, err) return } @@ -397,7 +398,7 @@ func (api *API) getObjectStatForIpfs(c *gin.Context) { func (api *API) checkLocalNodeForPin(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } hash := c.Param("hash") @@ -407,12 +408,12 @@ func (api *API) checkLocalNodeForPin(c *gin.Context) { } manager, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } present, err := manager.ParseLocalPinsForHash(hash) if err != nil { - api.LogError(err, IPFSPinParseError)(c) + api.LogError(err, eh.IPFSPinParseError)(c) return } @@ -444,7 +445,7 @@ func (api *API) downloadContentHash(c *gin.Context) { // initialize our connection to IPFS manager, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } @@ -455,7 +456,7 @@ func (api *API) downloadContentHash(c *gin.Context) { // read the contents of the file if reader, err = manager.Shell.Cat(contentHash); err != nil { - api.LogError(err, IPFSCatError)(c) + api.LogError(err, eh.IPFSCatError)(c) return } @@ -472,7 +473,7 @@ func (api *API) downloadContentHash(c *gin.Context) { } else { // get the size of the file in bytes if size, err = manager.GetObjectFileSizeInBytes(contentHash); err != nil { - api.LogError(err, IPFSObjectStatError)(c) + api.LogError(err, eh.IPFSObjectStatError)(c) return } } diff --git a/api/routes_rtfs_cluster.go b/api/routes_rtfs_cluster.go index 376d868b9..696a57f56 100644 --- a/api/routes_rtfs_cluster.go +++ b/api/routes_rtfs_cluster.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/rtfs" "github.com/RTradeLtd/Temporal/utils" @@ -23,7 +24,7 @@ func (api *API) pinHashToCluster(c *gin.Context) { } manager, err := rtfs.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c, http.StatusInternalServerError) + api.LogError(err, eh.IPFSConnectionError)(c, http.StatusInternalServerError) return } holdTime, exists := c.GetPostForm("hold_time") @@ -39,18 +40,18 @@ func (api *API) pinHashToCluster(c *gin.Context) { } cost, err := utils.CalculatePinCost(hash, holdTimeInt, manager.Shell, false) if err != nil { - api.LogError(err, CallCostCalculationError)(c, http.StatusBadRequest) + api.LogError(err, eh.CallCostCalculationError)(c, http.StatusBadRequest) return } if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } mqURL := api.cfg.RabbitMQ.URL qm, err := queue.Initialize(queue.IpfsClusterPinQueue, mqURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) api.refundUserCredits(username, "cluster-pin", cost) return } @@ -64,7 +65,7 @@ func (api *API) pinHashToCluster(c *gin.Context) { } if err = qm.PublishMessage(ipfsClusterPin); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) api.refundUserCredits(username, "cluster-pin", cost) return } @@ -77,19 +78,19 @@ func (api *API) pinHashToCluster(c *gin.Context) { func (api *API) syncClusterErrorsLocally(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } // initialize a conection to the cluster manager, err := rtfs_cluster.Initialize("", "") if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } // parse the local cluster status, and sync any errors, retunring the cids that were in an error state syncedCids, err := manager.ParseLocalStatusAllAndSync() if err != nil { - api.LogError(err, IPFSClusterStatusError)(c) + api.LogError(err, eh.IPFSClusterStatusError)(c) return } @@ -101,7 +102,7 @@ func (api *API) syncClusterErrorsLocally(c *gin.Context) { func (api *API) getLocalStatusForClusterPin(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } hash := c.Param("hash") @@ -112,13 +113,13 @@ func (api *API) getLocalStatusForClusterPin(c *gin.Context) { // initialize a connection to the cluster manager, err := rtfs_cluster.Initialize("", "") if err != nil { - api.LogError(err, IPFSClusterConnectionError)(c) + api.LogError(err, eh.IPFSClusterConnectionError)(c) return } // get the cluster status for the cid only asking the local cluster node status, err := manager.GetStatusForCidLocally(hash) if err != nil { - api.LogError(err, IPFSClusterStatusError)(c) + api.LogError(err, eh.IPFSClusterStatusError)(c) return } @@ -131,7 +132,7 @@ func (api *API) getLocalStatusForClusterPin(c *gin.Context) { func (api *API) getGlobalStatusForClusterPin(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } hash := c.Param("hash") @@ -142,13 +143,13 @@ func (api *API) getGlobalStatusForClusterPin(c *gin.Context) { // initialize a connection to the cluster manager, err := rtfs_cluster.Initialize("", "") if err != nil { - api.LogError(err, IPFSClusterConnectionError)(c) + api.LogError(err, eh.IPFSClusterConnectionError)(c) return } // get teh cluster wide status for this particular pin status, err := manager.GetStatusForCidGlobally(hash) if err != nil { - api.LogError(err, IPFSClusterStatusError)(c) + api.LogError(err, eh.IPFSClusterStatusError)(c) return } @@ -161,7 +162,7 @@ func (api *API) getGlobalStatusForClusterPin(c *gin.Context) { func (api *API) fetchLocalClusterStatus(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } // this will hold all the retrieved content hashes @@ -171,13 +172,13 @@ func (api *API) fetchLocalClusterStatus(c *gin.Context) { // initialize a connection to the cluster manager, err := rtfs_cluster.Initialize("", "") if err != nil { - api.LogError(err, IPFSClusterConnectionError)(c) + api.LogError(err, eh.IPFSClusterConnectionError)(c) return } // fetch a map of all the statuses maps, err := manager.FetchLocalStatus() if err != nil { - api.LogError(err, IPFSClusterStatusError)(c) + api.LogError(err, eh.IPFSClusterStatusError)(c) return } // parse the maps diff --git a/api/routes_rtfsp.go b/api/routes_rtfsp.go index 010273187..c60545862 100644 --- a/api/routes_rtfsp.go +++ b/api/routes_rtfsp.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/mini" "github.com/RTradeLtd/Temporal/queue" "github.com/RTradeLtd/Temporal/rtfs" @@ -30,18 +31,18 @@ func (api *API) pinToHostedIPFSNetwork(c *gin.Context) { err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB) if err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } im := models.NewHostedIPFSNetworkManager(api.dbm.DB) url, err := im.GetAPIURLByName(networkName) if err != nil { - api.LogError(err, APIURLCheckError)(c, http.StatusBadRequest) + api.LogError(err, eh.APIURLCheckError)(c, http.StatusBadRequest) return } manager, err := rtfs.Initialize("", url) if err != nil { - api.LogError(err, IPFSConnectionError)(c, http.StatusBadRequest) + api.LogError(err, eh.IPFSConnectionError)(c, http.StatusBadRequest) return } @@ -62,11 +63,11 @@ func (api *API) pinToHostedIPFSNetwork(c *gin.Context) { } cost, err := utils.CalculatePinCost(hash, holdTimeInt, manager.Shell, true) if err != nil { - api.LogError(err, CallCostCalculationError)(c, http.StatusBadRequest) + api.LogError(err, eh.CallCostCalculationError)(c, http.StatusBadRequest) return } if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } ip := queue.IPFSPin{ @@ -81,13 +82,13 @@ func (api *API) pinToHostedIPFSNetwork(c *gin.Context) { qm, err := queue.Initialize(queue.IpfsPinQueue, mqConnectionURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) api.refundUserCredits(username, "private-pin", cost) return } if err = qm.PublishMessageWithExchange(ip, queue.PinExchange); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) api.refundUserCredits(username, "private-pin", cost) return } @@ -111,7 +112,7 @@ func (api *API) addFileToHostedIPFSNetworkAdvanced(c *gin.Context) { return } if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError)(c, http.StatusBadRequest) + api.LogError(err, eh.PrivateNetworkAccessError)(c, http.StatusBadRequest) return } @@ -132,7 +133,7 @@ func (api *API) addFileToHostedIPFSNetworkAdvanced(c *gin.Context) { endpoint := fmt.Sprintf("%s:%s", api.cfg.MINIO.Connection.IP, api.cfg.MINIO.Connection.Port) miniManager, err := mini.NewMinioManager(endpoint, accessKey, secretKey, false) if err != nil { - api.LogError(err, MinioConnectionError) + api.LogError(err, eh.MinioConnectionError) Fail(c, err) return } @@ -147,13 +148,13 @@ func (api *API) addFileToHostedIPFSNetworkAdvanced(c *gin.Context) { } cost := utils.CalculateFileCost(holdTimeInt, fileHandler.Size, true) if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } api.LogDebug("opening file") openFile, err := fileHandler.Open() if err != nil { - api.LogError(err, FileOpenError) + api.LogError(err, eh.FileOpenError) api.refundUserCredits(username, "private-file", cost) Fail(c, err) return @@ -169,7 +170,7 @@ func (api *API) addFileToHostedIPFSNetworkAdvanced(c *gin.Context) { Bucket: FilesUploadBucket, EncryptPassphrase: c.PostForm("passphrase"), }); err != nil { - api.LogError(err, MinioPutError) + api.LogError(err, eh.MinioPutError) api.refundUserCredits(username, "private-file", cost) Fail(c, err) return @@ -189,7 +190,7 @@ func (api *API) addFileToHostedIPFSNetworkAdvanced(c *gin.Context) { mqURL := api.cfg.RabbitMQ.URL qm, err := queue.Initialize(queue.IpfsFileQueue, mqURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError) + api.LogError(err, eh.QueueInitializationError) api.refundUserCredits(username, "private-file", cost) Fail(c, err) return @@ -197,7 +198,7 @@ func (api *API) addFileToHostedIPFSNetworkAdvanced(c *gin.Context) { // we don't use an exchange for file publishes so that rabbitmq distributes round robin if err = qm.PublishMessage(ifp); err != nil { - api.LogError(err, QueuePublishError) + api.LogError(err, eh.QueuePublishError) api.refundUserCredits(username, "private-file", cost) Fail(c, err) return @@ -218,7 +219,7 @@ func (api *API) addFileToHostedIPFSNetwork(c *gin.Context) { } if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError) + api.LogError(err, eh.PrivateNetworkAccessError) Fail(c, err) return } @@ -239,18 +240,18 @@ func (api *API) addFileToHostedIPFSNetwork(c *gin.Context) { im := models.NewHostedIPFSNetworkManager(api.dbm.DB) apiURL, err := im.GetAPIURLByName(networkName) if err != nil { - api.LogError(err, APIURLCheckError)(c) + api.LogError(err, eh.APIURLCheckError)(c) return } ipfsManager, err := rtfs.Initialize("", apiURL) if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } qm, err := queue.Initialize(queue.DatabaseFileAddQueue, mqURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) return } @@ -268,18 +269,18 @@ func (api *API) addFileToHostedIPFSNetwork(c *gin.Context) { } cost := utils.CalculateFileCost(holdTimeInt, fileHandler.Size, true) if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } file, err := fileHandler.Open() if err != nil { - api.LogError(err, FileOpenError)(c) + api.LogError(err, eh.FileOpenError)(c) api.refundUserCredits(username, "private-file", cost) return } resp, err := ipfsManager.Add(file) if err != nil { - api.LogError(err, IPFSAddError)(c) + api.LogError(err, eh.IPFSAddError)(c) api.refundUserCredits(username, "private-file", cost) return } @@ -292,7 +293,7 @@ func (api *API) addFileToHostedIPFSNetwork(c *gin.Context) { CreditCost: 0, } if err = qm.PublishMessage(dfa); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) return } @@ -306,11 +307,11 @@ func (api *API) addFileToHostedIPFSNetwork(c *gin.Context) { qm, err = queue.Initialize(queue.IpfsPinQueue, mqURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) return } if err = qm.PublishMessageWithExchange(pin, queue.PinExchange); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) return } @@ -328,22 +329,22 @@ func (api *API) ipfsPubSubPublishToHostedIPFSNetwork(c *gin.Context) { return } if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } cost, err := utils.CalculateAPICallCost("pubsub", true) if err != nil { - api.LogError(err, CallCostCalculationError)(c, http.StatusBadRequest) + api.LogError(err, eh.CallCostCalculationError)(c, http.StatusBadRequest) return } if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } im := models.NewHostedIPFSNetworkManager(api.dbm.DB) apiURL, err := im.GetAPIURLByName(networkName) if err != nil { - api.LogError(err, APIURLCheckError)(c) + api.LogError(err, eh.APIURLCheckError)(c) return } topic := c.Param("topic") @@ -354,11 +355,11 @@ func (api *API) ipfsPubSubPublishToHostedIPFSNetwork(c *gin.Context) { } manager, err := rtfs.Initialize("", apiURL) if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } if err = manager.PublishPubSubMessage(topic, message); err != nil { - api.LogError(err, IPFSPubSubPublishError)(c) + api.LogError(err, eh.IPFSPubSubPublishError)(c) return } @@ -371,7 +372,7 @@ func (api *API) ipfsPubSubPublishToHostedIPFSNetwork(c *gin.Context) { func (api *API) getLocalPinsForHostedIPFSNetwork(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } networkName, exists := c.GetPostForm("network_name") @@ -380,26 +381,26 @@ func (api *API) getLocalPinsForHostedIPFSNetwork(c *gin.Context) { return } if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } im := models.NewHostedIPFSNetworkManager(api.dbm.DB) apiURL, err := im.GetAPIURLByName(networkName) if err != nil { - api.LogError(err, APIURLCheckError)(c) + api.LogError(err, eh.APIURLCheckError)(c) return } // initialize a connection toe the local ipfs node manager, err := rtfs.Initialize("", apiURL) if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } // get all the known local pins // WARNING: THIS COULD BE A VERY LARGE LIST pinInfo, err := manager.Shell.Pins() if err != nil { - api.LogError(err, IPFSPinParseError)(c) + api.LogError(err, eh.IPFSPinParseError)(c) return } @@ -416,14 +417,14 @@ func (api *API) getObjectStatForIpfsForHostedIPFSNetwork(c *gin.Context) { return } if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } im := models.NewHostedIPFSNetworkManager(api.dbm.DB) apiURL, err := im.GetAPIURLByName(networkName) if err != nil { - api.LogError(err, APIURLCheckError)(c) + api.LogError(err, eh.APIURLCheckError)(c) return } key := c.Param("key") @@ -433,12 +434,12 @@ func (api *API) getObjectStatForIpfsForHostedIPFSNetwork(c *gin.Context) { } manager, err := rtfs.Initialize("", apiURL) if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } stats, err := manager.ObjectStat(key) if err != nil { - api.LogError(err, IPFSObjectStatError)(c) + api.LogError(err, eh.IPFSObjectStatError)(c) return } @@ -450,7 +451,7 @@ func (api *API) getObjectStatForIpfsForHostedIPFSNetwork(c *gin.Context) { func (api *API) checkLocalNodeForPinForHostedIPFSNetwork(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } networkName, exists := c.GetPostForm("network_name") @@ -460,13 +461,13 @@ func (api *API) checkLocalNodeForPinForHostedIPFSNetwork(c *gin.Context) { } if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } im := models.NewHostedIPFSNetworkManager(api.dbm.DB) apiURL, err := im.GetAPIURLByName(networkName) if err != nil { - api.LogError(err, APIURLCheckError)(c) + api.LogError(err, eh.APIURLCheckError)(c) return } hash := c.Param("hash") @@ -476,12 +477,12 @@ func (api *API) checkLocalNodeForPinForHostedIPFSNetwork(c *gin.Context) { } manager, err := rtfs.Initialize("", apiURL) if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } present, err := manager.ParseLocalPinsForHash(hash) if err != nil { - api.LogError(err, IPFSPinParseError)(c) + api.LogError(err, eh.IPFSPinParseError)(c) return } @@ -500,23 +501,23 @@ func (api *API) publishDetailedIPNSToHostedIPFSNetwork(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) cost, err := utils.CalculateAPICallCost("ipns", true) if err != nil { - api.LogError(err, CallCostCalculationError)(c, http.StatusBadRequest) + api.LogError(err, eh.CallCostCalculationError)(c, http.StatusBadRequest) return } if err := api.validateUserCredits(username, cost); err != nil { - api.LogError(err, InvalidBalanceError)(c, http.StatusPaymentRequired) + api.LogError(err, eh.InvalidBalanceError)(c, http.StatusPaymentRequired) return } mqURL := api.cfg.RabbitMQ.URL if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } qm, err := queue.Initialize(queue.IpnsEntryQueue, mqURL, true, false) if err != nil { - api.LogError(err, QueueInitializationError)(c) + api.LogError(err, eh.QueueInitializationError)(c) return } hash, present := c.GetPostForm("hash") @@ -551,13 +552,13 @@ func (api *API) publishDetailedIPNSToHostedIPFSNetwork(c *gin.Context) { ownsKey, err := api.um.CheckIfKeyOwnedByUser(username, key) if err != nil { - api.LogError(err, KeySearchError)(c) + api.LogError(err, eh.KeySearchError)(c) return } if !ownsKey { err = fmt.Errorf("unauthorized access to key by user %s", username) - api.LogError(err, KeyUseError)(c) + api.LogError(err, eh.KeyUseError)(c) return } @@ -590,7 +591,7 @@ func (api *API) publishDetailedIPNSToHostedIPFSNetwork(c *gin.Context) { CreditCost: cost, } if err := qm.PublishMessage(ipnsUpdate); err != nil { - api.LogError(err, QueuePublishError)(c) + api.LogError(err, eh.QueuePublishError)(c) return } @@ -605,7 +606,7 @@ func (api *API) createHostedIPFSNetworkEntryInDatabase(c *gin.Context) { // lock down as admin route for now username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } networkName, exists := c.GetPostForm("network_name") @@ -691,20 +692,20 @@ func (api *API) createHostedIPFSNetworkEntryInDatabase(c *gin.Context) { manager := models.NewHostedIPFSNetworkManager(api.dbm.DB) network, err := manager.CreateHostedPrivateNetwork(networkName, apiURL, swarmKey, args, users) if err != nil { - api.LogError(err, NetworkCreationError)(c) + api.LogError(err, eh.NetworkCreationError)(c) return } if len(users) > 0 { for _, v := range users { if err := api.um.AddIPFSNetworkForUser(v, networkName); err != nil { - api.LogError(err, NetworkCreationError)(c) + api.LogError(err, eh.NetworkCreationError)(c) return } } } else { if err := api.um.AddIPFSNetworkForUser(username, networkName); err != nil { - api.LogError(err, NetworkCreationError)(c) + api.LogError(err, eh.NetworkCreationError)(c) return } } @@ -718,14 +719,14 @@ func (api *API) createHostedIPFSNetworkEntryInDatabase(c *gin.Context) { func (api *API) getIPFSPrivateNetworkByName(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := api.validateAdminRequest(username); err != nil { - FailNotAuthorized(c, UnAuthorizedAdminAccess) + FailNotAuthorized(c, eh.UnAuthorizedAdminAccess) return } netName := c.Param("name") manager := models.NewHostedIPFSNetworkManager(api.dbm.DB) net, err := manager.GetNetworkByName(netName) if err != nil { - api.LogError(err, NetworkSearchError)(c) + api.LogError(err, eh.NetworkSearchError)(c) return } @@ -740,7 +741,7 @@ func (api *API) getAuthorizedPrivateNetworks(c *gin.Context) { networks, err := api.um.GetPrivateIPFSNetworksForUser(username) if err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } @@ -759,14 +760,14 @@ func (api *API) getUploadsByNetworkName(c *gin.Context) { } if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } um := models.NewUploadManager(api.dbm.DB) uploads, err := um.FindUploadsByNetwork(networkName) if err != nil { - api.LogError(err, UploadSearchError)(c) + api.LogError(err, eh.UploadSearchError)(c) return } @@ -785,7 +786,7 @@ func (api *API) downloadContentHashForPrivateNetwork(c *gin.Context) { username := GetAuthenticatedUserFromContext(c) if err := CheckAccessForPrivateNetwork(username, networkName, api.dbm.DB); err != nil { - api.LogError(err, PrivateNetworkAccessError)(c) + api.LogError(err, eh.PrivateNetworkAccessError)(c) return } @@ -803,7 +804,7 @@ func (api *API) downloadContentHashForPrivateNetwork(c *gin.Context) { im := models.NewHostedIPFSNetworkManager(api.dbm.DB) apiURL, err := im.GetAPIURLByName(networkName) if err != nil { - api.LogError(err, APIURLCheckError)(c) + api.LogError(err, eh.APIURLCheckError)(c) return } // get the content hash that is to be downloaded @@ -815,19 +816,19 @@ func (api *API) downloadContentHashForPrivateNetwork(c *gin.Context) { // initialize our connection to IPFS manager, err := rtfs.Initialize("", apiURL) if err != nil { - api.LogError(err, IPFSConnectionError)(c) + api.LogError(err, eh.IPFSConnectionError)(c) return } // read the contents of the file reader, err := manager.Shell.Cat(contentHash) if err != nil { - api.LogError(err, IPFSCatError)(c) + api.LogError(err, eh.IPFSCatError)(c) return } // get the size of hte file in bytes sizeInBytes, err := manager.GetObjectFileSizeInBytes(contentHash) if err != nil { - api.LogError(err, IPFSObjectStatError)(c) + api.LogError(err, eh.IPFSObjectStatError)(c) return } // parse extra headers if there are any diff --git a/api/utils.go b/api/utils.go index 5e88343f0..a209c3b25 100644 --- a/api/utils.go +++ b/api/utils.go @@ -6,6 +6,7 @@ import ( "strconv" "time" + "github.com/RTradeLtd/Temporal/eh" "github.com/RTradeLtd/Temporal/models" "github.com/RTradeLtd/Temporal/utils" "github.com/c2h5oh/datasize" @@ -60,7 +61,7 @@ func (api *API) FileSizeCheck(size int64) error { } gbInt := int64(datasize.GB.Bytes()) * sizeInt if size > gbInt { - return errors.New(FileTooBigError) + return errors.New(eh.FileTooBigError) } return nil } @@ -76,7 +77,7 @@ func (api *API) getDepositAddress(paymentType string) (string, error) { case "xmr": return "", nil } - return "", errors.New(InvalidPaymentTypeError) + return "", errors.New(eh.InvalidPaymentTypeError) } func (api *API) validateBlockchain(blockchain string) bool { @@ -94,7 +95,7 @@ func (api *API) validateUserCredits(username string, cost float64) error { return err } if availableCredits < cost { - return errors.New(InvalidBalanceError) + return errors.New(eh.InvalidBalanceError) } if _, err := api.um.RemoveCredits(username, cost); err != nil { return err @@ -112,7 +113,7 @@ func (api *API) refundUserCredits(username, callType string, cost float64) { "user": username, "call_type": callType, "error": err.Error(), - }).Error(CreditRefundError) + }).Error(eh.CreditRefundError) } } @@ -123,7 +124,7 @@ func (api *API) validateAdminRequest(username string) error { return err } if !isAdmin { - return errors.New(UnAuthorizedAdminAccess) + return errors.New(eh.UnAuthorizedAdminAccess) } return nil } diff --git a/api/errors.go b/eh/errors.go similarity index 94% rename from api/errors.go rename to eh/errors.go index fcf9ce981..8e2370f58 100644 --- a/api/errors.go +++ b/eh/errors.go @@ -1,4 +1,4 @@ -package api +package eh const ( // IPFSConnectionError is an error used for ipfs connection failures @@ -89,4 +89,8 @@ const ( IpnsRecordSearchError = "failed to search for IPNS records, user likely has published none" // UnAuthorizedAdminAccess is an error message used whena user attempts to access an administrative route UnAuthorizedAdminAccess = "user is not an administrator" + // DuplicateEmailError is an error used when a user attempts to register with an already taken email address + DuplicateEmailError = "email address already taken" + // DuplicateUserNameError is an error used whe na user attempts to register with an already taken user name + DuplicateUserNameError = "username is already taken" ) diff --git a/models/user.go b/models/user.go index 55c8655a9..015539c24 100644 --- a/models/user.go +++ b/models/user.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/RTradeLtd/Temporal/eh" "github.com/jinzhu/gorm" "github.com/lib/pq" "golang.org/x/crypto/bcrypt" @@ -207,8 +208,15 @@ func (um *UserManager) NewUserAccount(username, password, email string, enterpri if check.Error != nil && check.Error != gorm.ErrRecordNotFound { return nil, check.Error } - if user.CreatedAt != nilTime { - return nil, errors.New("user account already created") + if check.Error == nil { + return nil, errors.New(eh.DuplicateUserNameError) + } + check = um.DB.Where("email_address = ?", email).First(&user) + if check.Error != nil && check.Error != gorm.ErrRecordNotFound { + return nil, check.Error + } + if check.Error == nil { + return nil, errors.New(eh.DuplicateEmailError) } hashedPass, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil {