userutil.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/nfnt/resize"
  15. "github.com/pkg/errors"
  16. "golang.org/x/crypto/pbkdf2"
  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. m := resize.Resize(avatar.DefaultSize, avatar.DefaultSize, img, resize.NearestNeighbor)
  88. if err = png.Encode(f, m); err != nil {
  89. return errors.Wrap(err, "encode avatar image to file")
  90. }
  91. return nil
  92. }
  93. // EncodePassword encodes password using PBKDF2 SHA256 with given salt.
  94. func EncodePassword(password, salt string) string {
  95. newPasswd := pbkdf2.Key([]byte(password), []byte(salt), 10000, 50, sha256.New)
  96. return fmt.Sprintf("%x", newPasswd)
  97. }
  98. // ValidatePassword returns true if the given password matches the encoded
  99. // version with given salt.
  100. func ValidatePassword(encoded, salt, password string) bool {
  101. got := EncodePassword(password, salt)
  102. return subtle.ConstantTimeCompare([]byte(encoded), []byte(got)) == 1
  103. }
  104. // MailResendCacheKey returns the key used for caching mail resend.
  105. func MailResendCacheKey(userID int64) string {
  106. return fmt.Sprintf("mailResend::%d", userID)
  107. }
  108. // TwoFactorCacheKey returns the key used for caching two factor passcode.
  109. func TwoFactorCacheKey(userID int64, passcode string) string {
  110. return fmt.Sprintf("twoFactor::%d::%s", userID, passcode)
  111. }
  112. // RandomSalt returns randomly generated 10-character string that can be used as
  113. // the user salt.
  114. func RandomSalt() (string, error) {
  115. return strutil.RandomChars(10)
  116. }