Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Remove random helper error returns

Manav Rathi141561f19f4e
diff --git a/server/pkg/controller/family/admin.go b/server/pkg/controller/family/admin.go
index 387f718a97..e21a545d7c 100644
--- a/server/pkg/controller/family/admin.go
+++ b/server/pkg/controller/family/admin.go
@@ -109,10 +109,7 @@ func (c *Controller) InviteMember(ctx *gin.Context, adminUserID int64, email str
return stacktrace.Propagate(ente.ErrCanNotInviteUserWithPaidPlan, "")
}
- inviteToken, err := auth.GenerateURLSafeRandomString(InviteTokenLength)
- if err != nil {
- return stacktrace.Propagate(err, "")
- }
+ inviteToken := auth.GenerateURLSafeRandomString(InviteTokenLength)
activeInviteToken, err := c.FamilyRepo.AddMemberInvite(ctx, adminUserID, potentialMemberUser.ID, inviteToken, storageLimit)
if err != nil {
diff --git a/server/pkg/controller/user/twofactor.go b/server/pkg/controller/user/twofactor.go
index 72b108f5c0..ffed129297 100644
--- a/server/pkg/controller/user/twofactor.go
+++ b/server/pkg/controller/user/twofactor.go
@@ -220,10 +220,7 @@ func (c *UserController) GetKeyAttributeAndToken(context *gin.Context, userID in
if err != nil {
return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")
}
- token, err := auth.GenerateURLSafeRandomString(TokenLength)
- if err != nil {
- return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")
- }
+ token := auth.GenerateURLSafeRandomString(TokenLength)
encryptedToken, err := crypto.GetEncryptedTokenNative(token, keyAttributes.PublicKey)
if err != nil {
return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")
diff --git a/server/pkg/controller/user/userauth.go b/server/pkg/controller/user/userauth.go
index 7a20f8f9a5..f8a65f7772 100644
--- a/server/pkg/controller/user/userauth.go
+++ b/server/pkg/controller/user/userauth.go
@@ -635,20 +635,14 @@ func (c *UserController) onVerificationSuccess(context *gin.Context, email strin
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
}
- passKeySessionID, err = auth.GenerateURLSafeRandomString(PassKeySessionIDLength)
- if err != nil {
- return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
- }
+ passKeySessionID = auth.GenerateURLSafeRandomString(PassKeySessionIDLength)
err = c.PasskeyRepo.AddPasskeyTwoFactorSession(userID, passKeySessionID, time.Microseconds()+TwoFactorValidityDurationInMicroSeconds)
if err != nil {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
}
}
if isTwoFactorEnabled {
- twoFactorSessionID, err = auth.GenerateURLSafeRandomString(TwoFactorSessionIDLength)
- if err != nil {
- return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
- }
+ twoFactorSessionID = auth.GenerateURLSafeRandomString(TwoFactorSessionIDLength)
err = c.TwoFactorRepo.AddTwoFactorSession(userID, twoFactorSessionID, time.Microseconds()+TwoFactorValidityDurationInMicroSeconds)
if err != nil {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
@@ -662,10 +656,7 @@ func (c *UserController) onVerificationSuccess(context *gin.Context, email strin
return ente.EmailAuthorizationResponse{ID: userID, TwoFactorSessionID: twoFactorSessionID}, nil
}
- token, err := auth.GenerateURLSafeRandomString(TokenLength)
- if err != nil {
- return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
- }
+ token := auth.GenerateURLSafeRandomString(TokenLength)
keyAttributes, err := c.UserRepo.GetKeyAttributes(userID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
diff --git a/server/pkg/utils/auth/auth.go b/server/pkg/utils/auth/auth.go
index cd7fae9c2a..83fef35b01 100644
--- a/server/pkg/utils/auth/auth.go
+++ b/server/pkg/utils/auth/auth.go
@@ -30,10 +30,10 @@ const (
)
// GenerateRandomBytes returns securely generated random bytes.
-func GenerateRandomBytes(n int) ([]byte, error) {
+func GenerateRandomBytes(n int) []byte {
b := make([]byte, n)
rand.Read(b)
- return b, nil
+ return b
}
// GenerateRandomInt returns a securely generated random integer in [0, n).
@@ -50,12 +50,8 @@ func GenerateRandomInt(n int64) (int64, error) {
// GenerateURLSafeRandomString returns a URL-safe, base64 encoded
// securely generated random string.
-// It will return an error if the system's secure random
-// number generator fails to function correctly, in which
-// case the caller should not continue.
-func GenerateURLSafeRandomString(s int) (string, error) {
- b, err := GenerateRandomBytes(s)
- return base64.URLEncoding.EncodeToString(b), stacktrace.Propagate(err, "")
+func GenerateURLSafeRandomString(s int) string {
+ return base64.URLEncoding.EncodeToString(GenerateRandomBytes(s))
}
// GetHashedPassword returns the has of a specified password
diff --git a/server/pkg/utils/crypto/crypto_native.go b/server/pkg/utils/crypto/crypto_native.go
index a29af731ae..80eca87c50 100644
--- a/server/pkg/utils/crypto/crypto_native.go
+++ b/server/pkg/utils/crypto/crypto_native.go
@@ -13,12 +13,7 @@ import (
)
func EncryptNative(data string, encryptionKey []byte) (ente.EncryptionResult, error) {
- // Generate nonce
- nonce, err := auth.GenerateRandomBytes(SecretBoxNonceBytes)
- if err != nil {
- return ente.EncryptionResult{}, stacktrace.Propagate(err, "")
- }
- return encryptWithNonceNative(data, encryptionKey, nonce)
+ return encryptWithNonceNative(data, encryptionKey, auth.GenerateRandomBytes(SecretBoxNonceBytes))
}
func encryptWithNonceNative(data string, encryptionKey []byte, nonce []byte) (ente.EncryptionResult, error) {
diff --git a/server/tools/gen-random-keys/main.go b/server/tools/gen-random-keys/main.go
index 8a9baa61c0..d3f8ba9544 100644
--- a/server/tools/gen-random-keys/main.go
+++ b/server/tools/gen-random-keys/main.go
@@ -4,28 +4,18 @@ import (
"encoding/base64"
"fmt"
"github.com/ente-io/museum/pkg/utils/auth"
- "log"
)
func main() {
keyLen := 32
hashByteLen := 64
- keyBytes, err := auth.GenerateRandomBytes(keyLen)
- if err != nil {
- log.Fatal(err)
- }
+ keyBytes := auth.GenerateRandomBytes(keyLen)
key := base64.StdEncoding.EncodeToString(keyBytes)
- hashBytes, err := auth.GenerateRandomBytes(hashByteLen)
- if err != nil {
- log.Fatal(err)
- }
+ hashBytes := auth.GenerateRandomBytes(hashByteLen)
hash := base64.StdEncoding.EncodeToString(hashBytes)
- jwtBytes, err := auth.GenerateRandomBytes(keyLen)
- if err != nil {
- log.Fatal(err)
- }
+ jwtBytes := auth.GenerateRandomBytes(keyLen)
jwt := base64.URLEncoding.EncodeToString(jwtBytes)
fmt.Printf("key.encryption: %s\n", key)