A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

95 lines
2.3 KiB

  1. package writefreely
  2. import (
  3. "time"
  4. "github.com/guregu/null/zero"
  5. "github.com/writeas/web-core/data"
  6. "github.com/writeas/web-core/log"
  7. )
  8. type (
  9. userCredentials struct {
  10. Alias string `json:"alias" schema:"alias"`
  11. Pass string `json:"pass" schema:"pass"`
  12. Email string `json:"email" schema:"email"`
  13. Web bool `json:"web" schema:"-"`
  14. To string `json:"-" schema:"to"`
  15. EmailLogin bool `json:"via_email" schema:"via_email"`
  16. }
  17. userRegistration struct {
  18. userCredentials
  19. Honeypot string `json:"fullname" schema:"fullname"`
  20. Normalize bool `json:"normalize" schema:"normalize"`
  21. Signup bool `json:"signup" schema:"signup"`
  22. }
  23. // AuthUser contains information for a newly authenticated user (either
  24. // from signing up or logging in).
  25. AuthUser struct {
  26. AccessToken string `json:"access_token,omitempty"`
  27. Password string `json:"password,omitempty"`
  28. User *User `json:"user"`
  29. // Verbose user data
  30. Posts *[]PublicPost `json:"posts,omitempty"`
  31. Collections *[]Collection `json:"collections,omitempty"`
  32. }
  33. // User is a consistent user object in the database and all contexts (auth
  34. // and non-auth) in the API.
  35. User struct {
  36. ID int64 `json:"-"`
  37. Username string `json:"username"`
  38. HashedPass []byte `json:"-"`
  39. HasPass bool `json:"has_pass"`
  40. Email zero.String `json:"email"`
  41. Created time.Time `json:"created"`
  42. clearEmail string `json:"email"`
  43. }
  44. userMeStats struct {
  45. TotalCollections, TotalArticles, CollectionPosts uint64
  46. }
  47. ExportUser struct {
  48. *User
  49. Collections *[]CollectionObj `json:"collections"`
  50. AnonymousPosts []PublicPost `json:"posts"`
  51. }
  52. PublicUser struct {
  53. Username string `json:"username"`
  54. }
  55. )
  56. // EmailClear decrypts and returns the user's email, caching it in the user
  57. // object.
  58. func (u *User) EmailClear(keys *keychain) string {
  59. if u.clearEmail != "" {
  60. return u.clearEmail
  61. }
  62. if u.Email.Valid && u.Email.String != "" {
  63. email, err := data.Decrypt(keys.emailKey, []byte(u.Email.String))
  64. if err != nil {
  65. log.Error("Error decrypting user email: %v", err)
  66. } else {
  67. u.clearEmail = string(email)
  68. return u.clearEmail
  69. }
  70. }
  71. return ""
  72. }
  73. // Cookie strips down an AuthUser to contain only information necessary for
  74. // cookies.
  75. func (u User) Cookie() *User {
  76. u.HashedPass = []byte{}
  77. return &u
  78. }