userutil.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 file.
  4. package userutil
  5. import (
  6. "crypto/sha256"
  7. "crypto/subtle"
  8. "encoding/hex"
  9. "fmt"
  10. "image/png"
  11. "os"
  12. "path/filepath"
  13. "strconv"
  14. "strings"
  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/tool"
  20. )
  21. // DashboardURLPath returns the URL path to the user or organization dashboard.
  22. func DashboardURLPath(name string, isOrganization bool) string {
  23. if isOrganization {
  24. return conf.Server.Subpath + "/org/" + name + "/dashboard/"
  25. }
  26. return conf.Server.Subpath + "/"
  27. }
  28. // GenerateActivateCode generates an activate code based on user information and
  29. // the given email.
  30. func GenerateActivateCode(userID int64, email, name, password, rands string) string {
  31. code := tool.CreateTimeLimitCode(
  32. fmt.Sprintf("%d%s%s%s%s", userID, email, strings.ToLower(name), password, rands),
  33. conf.Auth.ActivateCodeLives,
  34. nil,
  35. )
  36. // Add tailing hex username
  37. code += hex.EncodeToString([]byte(strings.ToLower(name)))
  38. return code
  39. }
  40. // CustomAvatarPath returns the absolute path of the user custom avatar file.
  41. func CustomAvatarPath(userID int64) string {
  42. return filepath.Join(conf.Picture.AvatarUploadPath, strconv.FormatInt(userID, 10))
  43. }
  44. // GenerateRandomAvatar generates a random avatar and stores to local file
  45. // system using given user information.
  46. func GenerateRandomAvatar(userID int64, name, email string) error {
  47. seed := email
  48. if seed == "" {
  49. seed = name
  50. }
  51. img, err := avatar.RandomImage([]byte(seed))
  52. if err != nil {
  53. return errors.Wrap(err, "generate random image")
  54. }
  55. avatarPath := CustomAvatarPath(userID)
  56. err = os.MkdirAll(filepath.Dir(avatarPath), os.ModePerm)
  57. if err != nil {
  58. return errors.Wrap(err, "create avatar directory")
  59. }
  60. f, err := os.Create(avatarPath)
  61. if err != nil {
  62. return errors.Wrap(err, "create avatar file")
  63. }
  64. defer func() { _ = f.Close() }()
  65. if err = png.Encode(f, img); err != nil {
  66. return errors.Wrap(err, "encode avatar image to file")
  67. }
  68. return nil
  69. }
  70. // EncodePassword encodes password using PBKDF2 SHA256 with given salt.
  71. func EncodePassword(password, salt string) string {
  72. newPasswd := pbkdf2.Key([]byte(password), []byte(salt), 10000, 50, sha256.New)
  73. return fmt.Sprintf("%x", newPasswd)
  74. }
  75. // ValidatePassword returns true if the given password matches the encoded
  76. // version with given salt.
  77. func ValidatePassword(encoded, salt, password string) bool {
  78. got := EncodePassword(password, salt)
  79. return subtle.ConstantTimeCompare([]byte(encoded), []byte(got)) == 1
  80. }