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.

accountreg.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "errors"
  6. "fmt"
  7. "github.com/tidwall/buntdb"
  8. )
  9. var (
  10. errAccountCreation = errors.New("Account could not be created")
  11. errCertfpAlreadyExists = errors.New("An account already exists with your certificate")
  12. )
  13. // AccountRegistration manages the registration of accounts.
  14. type AccountRegistration struct {
  15. Enabled bool
  16. EnabledCallbacks []string
  17. EnabledCredentialTypes []string
  18. AllowMultiplePerConnection bool
  19. }
  20. // AccountCredentials stores the various methods for verifying accounts.
  21. type AccountCredentials struct {
  22. PassphraseSalt []byte
  23. PassphraseHash []byte
  24. Certificate string // fingerprint
  25. }
  26. // NewAccountRegistration returns a new AccountRegistration, configured correctly.
  27. func NewAccountRegistration(config AccountRegistrationConfig) (accountReg AccountRegistration) {
  28. if config.Enabled {
  29. accountReg.Enabled = true
  30. accountReg.AllowMultiplePerConnection = config.AllowMultiplePerConnection
  31. for _, name := range config.EnabledCallbacks {
  32. // we store "none" as "*" internally
  33. if name == "none" {
  34. name = "*"
  35. }
  36. accountReg.EnabledCallbacks = append(accountReg.EnabledCallbacks, name)
  37. }
  38. // no need to make this configurable, right now at least
  39. accountReg.EnabledCredentialTypes = []string{
  40. "passphrase",
  41. "certfp",
  42. }
  43. }
  44. return accountReg
  45. }
  46. // removeFailedAccRegisterData removes the data created by ACC REGISTER if the account creation fails early.
  47. func removeFailedAccRegisterData(store *buntdb.DB, account string) {
  48. // error is ignored here, we can't do much about it anyways
  49. store.Update(func(tx *buntdb.Tx) error {
  50. tx.Delete(fmt.Sprintf(keyAccountExists, account))
  51. tx.Delete(fmt.Sprintf(keyAccountRegTime, account))
  52. tx.Delete(fmt.Sprintf(keyAccountCredentials, account))
  53. return nil
  54. })
  55. }