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.
 
 
 
 
 

96 lines
2.3 KiB

  1. /*
  2. * Copyright © 2018-2019 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "github.com/writeas/web-core/log"
  13. "github.com/writeas/writefreely/key"
  14. "io/ioutil"
  15. "os"
  16. "path/filepath"
  17. )
  18. const (
  19. keysDir = "keys"
  20. )
  21. var (
  22. emailKeyPath = filepath.Join(keysDir, "email.aes256")
  23. cookieAuthKeyPath = filepath.Join(keysDir, "cookies_auth.aes256")
  24. cookieKeyPath = filepath.Join(keysDir, "cookies_enc.aes256")
  25. )
  26. func initKeyPaths(app *App) {
  27. emailKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, emailKeyPath)
  28. cookieAuthKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieAuthKeyPath)
  29. cookieKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieKeyPath)
  30. }
  31. func initKeys(app *App) error {
  32. var err error
  33. app.keys = &key.Keychain{}
  34. if debugging {
  35. log.Info(" %s", emailKeyPath)
  36. }
  37. app.keys.EmailKey, err = ioutil.ReadFile(emailKeyPath)
  38. if err != nil {
  39. return err
  40. }
  41. if debugging {
  42. log.Info(" %s", cookieAuthKeyPath)
  43. }
  44. app.keys.CookieAuthKey, err = ioutil.ReadFile(cookieAuthKeyPath)
  45. if err != nil {
  46. return err
  47. }
  48. if debugging {
  49. log.Info(" %s", cookieKeyPath)
  50. }
  51. app.keys.CookieKey, err = ioutil.ReadFile(cookieKeyPath)
  52. if err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. // generateKey generates a key at the given path used for the encryption of
  58. // certain user data. Because user data becomes unrecoverable without these
  59. // keys, this won't overwrite any existing key, and instead outputs a message.
  60. func generateKey(path string) error {
  61. // Check if key file exists
  62. if _, err := os.Stat(path); err == nil {
  63. log.Info("%s already exists. rm the file if you understand the consquences.", path)
  64. return nil
  65. } else if !os.IsNotExist(err) {
  66. log.Error("%s", err)
  67. return err
  68. }
  69. log.Info("Generating %s.", path)
  70. b, err := key.GenerateBytes(key.EncKeysBytes)
  71. if err != nil {
  72. log.Error("FAILED. %s. Run writefreely --gen-keys again.", err)
  73. return err
  74. }
  75. err = ioutil.WriteFile(path, b, 0600)
  76. if err != nil {
  77. log.Error("FAILED writing file: %s", err)
  78. return err
  79. }
  80. log.Info("Success.")
  81. return nil
  82. }