1
0

userutil.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2022 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE and LICENSE.gogs file.
  4. // Copyright 2025 Huan-Gogs Authors. All rights reserved.
  5. // Use of this source code is governed by a MIT-style
  6. // license that can be found in the LICENSE file.
  7. package userutil
  8. import (
  9. "bytes"
  10. "crypto/sha256"
  11. "crypto/subtle"
  12. "fmt"
  13. "github.com/nfnt/resize"
  14. "github.com/pkg/errors"
  15. "golang.org/x/crypto/pbkdf2"
  16. "image"
  17. "image/png"
  18. "os"
  19. "path/filepath"
  20. "strconv"
  21. "github.com/SongZihuan/huan-gogs/internal/avatar"
  22. "github.com/SongZihuan/huan-gogs/internal/conf"
  23. "github.com/SongZihuan/huan-gogs/internal/strutil"
  24. )
  25. // DashboardURLPath returns the URL path to the user or organization dashboard.
  26. func DashboardURLPath(name string, isOrganization bool) string {
  27. if isOrganization {
  28. return conf.Server.Subpath + "/org/" + name + "/dashboard/"
  29. }
  30. return conf.Server.Subpath + "/"
  31. }
  32. // CustomAvatarPath returns the absolute path of the user custom avatar file.
  33. func CustomAvatarPath(userID int64) string {
  34. return filepath.Join(conf.Picture.AvatarUploadPath, strconv.FormatInt(userID, 10))
  35. }
  36. // GenerateRandomAvatar generates a random avatar and stores to local file
  37. // system using given user information.
  38. func GenerateRandomAvatar(userID int64, name, email string) error {
  39. seed := email
  40. if seed == "" {
  41. seed = name
  42. }
  43. img, err := avatar.RandomImage([]byte(seed))
  44. if err != nil {
  45. return errors.Wrap(err, "generate random image")
  46. }
  47. avatarPath := CustomAvatarPath(userID)
  48. err = os.MkdirAll(filepath.Dir(avatarPath), os.ModePerm)
  49. if err != nil {
  50. return errors.Wrap(err, "create avatar directory")
  51. }
  52. f, err := os.Create(avatarPath)
  53. if err != nil {
  54. return errors.Wrap(err, "create avatar file")
  55. }
  56. defer func() { _ = f.Close() }()
  57. if err = png.Encode(f, img); err != nil {
  58. return errors.Wrap(err, "encode avatar image to file")
  59. }
  60. return nil
  61. }
  62. // SaveAvatar saves the given avatar for the user.
  63. func SaveAvatar(userID int64, data []byte) error {
  64. img, _, err := image.Decode(bytes.NewReader(data))
  65. if err != nil {
  66. return errors.Wrap(err, "decode image")
  67. }
  68. avatarPath := CustomAvatarPath(userID)
  69. err = os.MkdirAll(filepath.Dir(avatarPath), os.ModePerm)
  70. if err != nil {
  71. return errors.Wrap(err, "create avatar directory")
  72. }
  73. f, err := os.Create(avatarPath)
  74. if err != nil {
  75. return errors.Wrap(err, "create avatar file")
  76. }
  77. defer func() { _ = f.Close() }()
  78. m := resize.Resize(avatar.DefaultSize, avatar.DefaultSize, img, resize.NearestNeighbor)
  79. if err = png.Encode(f, m); err != nil {
  80. return errors.Wrap(err, "encode avatar image to file")
  81. }
  82. return nil
  83. }
  84. // EncodePassword encodes password using PBKDF2 SHA256 with given salt.
  85. func EncodePassword(password, salt string) string {
  86. newPasswd := pbkdf2.Key([]byte(password), []byte(salt), 10000, 50, sha256.New)
  87. return fmt.Sprintf("%x", newPasswd)
  88. }
  89. // ValidatePassword returns true if the given password matches the encoded
  90. // version with given salt.
  91. func ValidatePassword(encoded, salt, password string) bool {
  92. got := EncodePassword(password, salt)
  93. return subtle.ConstantTimeCompare([]byte(encoded), []byte(got)) == 1
  94. }
  95. // MailResendCacheKey returns the key used for caching mail resend.
  96. func MailResendCacheKey(userID int64) string {
  97. return fmt.Sprintf("mailResend::%d", userID)
  98. }
  99. // TwoFactorCacheKey returns the key used for caching two factor passcode.
  100. func TwoFactorCacheKey(userID int64, passcode string) string {
  101. return fmt.Sprintf("twoFactor::%d::%s", userID, passcode)
  102. }
  103. // RandomSalt returns randomly generated 10-character string that can be used as
  104. // the user salt.
  105. func RandomSalt() (string, error) {
  106. return strutil.RandomChars(10)
  107. }