1
0

userutil.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package userutil
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "crypto/subtle"
  6. "encoding/hex"
  7. "fmt"
  8. "image"
  9. "image/png"
  10. "os"
  11. "path/filepath"
  12. "strconv"
  13. "strings"
  14. "github.com/cockroachdb/errors"
  15. "golang.org/x/crypto/pbkdf2"
  16. "golang.org/x/image/draw"
  17. "gogs.io/gogs/internal/avatar"
  18. "gogs.io/gogs/internal/conf"
  19. "gogs.io/gogs/internal/strutil"
  20. "gogs.io/gogs/internal/tool"
  21. )
  22. // DashboardURLPath returns the URL path to the user or organization dashboard.
  23. func DashboardURLPath(name string, isOrganization bool) string {
  24. if isOrganization {
  25. return conf.Server.Subpath + "/org/" + name + "/dashboard/"
  26. }
  27. return conf.Server.Subpath + "/"
  28. }
  29. // GenerateActivateCode generates an activate code based on user information and
  30. // the given email.
  31. func GenerateActivateCode(userID int64, email, name, password, rands string) string {
  32. code := tool.CreateTimeLimitCode(
  33. fmt.Sprintf("%d%s%s%s%s", userID, email, strings.ToLower(name), password, rands),
  34. conf.Auth.ActivateCodeLives,
  35. nil,
  36. )
  37. // Add tailing hex username
  38. code += hex.EncodeToString([]byte(strings.ToLower(name)))
  39. return code
  40. }
  41. // CustomAvatarPath returns the absolute path of the user custom avatar file.
  42. func CustomAvatarPath(userID int64) string {
  43. return filepath.Join(conf.Picture.AvatarUploadPath, strconv.FormatInt(userID, 10))
  44. }
  45. // GenerateRandomAvatar generates a random avatar and stores to local file
  46. // system using given user information.
  47. func GenerateRandomAvatar(userID int64, name, email string) error {
  48. seed := email
  49. if seed == "" {
  50. seed = name
  51. }
  52. img, err := avatar.RandomImage([]byte(seed))
  53. if err != nil {
  54. return errors.Wrap(err, "generate random image")
  55. }
  56. avatarPath := CustomAvatarPath(userID)
  57. err = os.MkdirAll(filepath.Dir(avatarPath), os.ModePerm)
  58. if err != nil {
  59. return errors.Wrap(err, "create avatar directory")
  60. }
  61. f, err := os.Create(avatarPath)
  62. if err != nil {
  63. return errors.Wrap(err, "create avatar file")
  64. }
  65. defer func() { _ = f.Close() }()
  66. if err = png.Encode(f, img); err != nil {
  67. return errors.Wrap(err, "encode avatar image to file")
  68. }
  69. return nil
  70. }
  71. // SaveAvatar saves the given avatar for the user.
  72. func SaveAvatar(userID int64, data []byte) error {
  73. img, _, err := image.Decode(bytes.NewReader(data))
  74. if err != nil {
  75. return errors.Wrap(err, "decode image")
  76. }
  77. avatarPath := CustomAvatarPath(userID)
  78. err = os.MkdirAll(filepath.Dir(avatarPath), os.ModePerm)
  79. if err != nil {
  80. return errors.Wrap(err, "create avatar directory")
  81. }
  82. f, err := os.Create(avatarPath)
  83. if err != nil {
  84. return errors.Wrap(err, "create avatar file")
  85. }
  86. defer func() { _ = f.Close() }()
  87. dst := image.NewRGBA(image.Rect(0, 0, avatar.DefaultSize, avatar.DefaultSize))
  88. draw.NearestNeighbor.Scale(dst, dst.Bounds(), img, img.Bounds(), draw.Over, nil)
  89. if err = png.Encode(f, dst); err != nil {
  90. return errors.Wrap(err, "encode avatar image to file")
  91. }
  92. return nil
  93. }
  94. // EncodePassword encodes password using PBKDF2 SHA256 with given salt.
  95. func EncodePassword(password, salt string) string {
  96. newPasswd := pbkdf2.Key([]byte(password), []byte(salt), 10000, 50, sha256.New)
  97. return fmt.Sprintf("%x", newPasswd)
  98. }
  99. // ValidatePassword returns true if the given password matches the encoded
  100. // version with given salt.
  101. func ValidatePassword(encoded, salt, password string) bool {
  102. got := EncodePassword(password, salt)
  103. return subtle.ConstantTimeCompare([]byte(encoded), []byte(got)) == 1
  104. }
  105. // MailResendCacheKey returns the key used for caching mail resend.
  106. func MailResendCacheKey(userID int64) string {
  107. return fmt.Sprintf("mailResend::%d", userID)
  108. }
  109. // TwoFactorCacheKey returns the key used for caching two factor passcode.
  110. func TwoFactorCacheKey(userID int64, passcode string) string {
  111. return fmt.Sprintf("twoFactor::%d::%s", userID, passcode)
  112. }
  113. // RandomSalt returns randomly generated 10-character string that can be used as
  114. // the user salt.
  115. func RandomSalt() (string, error) {
  116. return strutil.RandomChars(10)
  117. }