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.
 
 
 
 
 

74 lines
2.0 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. // InitKeys loads encryption keys into memory via the given Apper interface
  27. func InitKeys(apper Apper) error {
  28. log.Info("Loading encryption keys...")
  29. err := apper.LoadKeys()
  30. if err != nil {
  31. return err
  32. }
  33. return nil
  34. }
  35. func initKeyPaths(app *App) {
  36. emailKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, emailKeyPath)
  37. cookieAuthKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieAuthKeyPath)
  38. cookieKeyPath = filepath.Join(app.cfg.Server.KeysParentDir, cookieKeyPath)
  39. }
  40. // generateKey generates a key at the given path used for the encryption of
  41. // certain user data. Because user data becomes unrecoverable without these
  42. // keys, this won't overwrite any existing key, and instead outputs a message.
  43. func generateKey(path string) error {
  44. // Check if key file exists
  45. if _, err := os.Stat(path); err == nil {
  46. log.Info("%s already exists. rm the file if you understand the consquences.", path)
  47. return nil
  48. } else if !os.IsNotExist(err) {
  49. log.Error("%s", err)
  50. return err
  51. }
  52. log.Info("Generating %s.", path)
  53. b, err := key.GenerateBytes(key.EncKeysBytes)
  54. if err != nil {
  55. log.Error("FAILED. %s. Run writefreely --gen-keys again.", err)
  56. return err
  57. }
  58. err = ioutil.WriteFile(path, b, 0600)
  59. if err != nil {
  60. log.Error("FAILED writing file: %s", err)
  61. return err
  62. }
  63. log.Info("Success.")
  64. return nil
  65. }