2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-09-04 11:25:33 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/tidwall/buntdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-09-07 13:32:58 +02:00
|
|
|
errAccountCreation = errors.New("Account could not be created")
|
|
|
|
errCertfpAlreadyExists = errors.New("An account already exists with your certificate")
|
2016-09-04 11:25:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// AccountRegistration manages the registration of accounts.
|
|
|
|
type AccountRegistration struct {
|
2017-09-11 01:16:13 +02:00
|
|
|
Enabled bool
|
|
|
|
EnabledCallbacks []string
|
|
|
|
EnabledCredentialTypes []string
|
|
|
|
AllowMultiplePerConnection bool
|
2016-09-04 11:25:33 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 10:45:42 +02:00
|
|
|
// AccountCredentials stores the various methods for verifying accounts.
|
|
|
|
type AccountCredentials struct {
|
|
|
|
PassphraseSalt []byte
|
|
|
|
PassphraseHash []byte
|
|
|
|
Certificate string // fingerprint
|
|
|
|
}
|
|
|
|
|
2016-09-04 11:25:33 +02:00
|
|
|
// NewAccountRegistration returns a new AccountRegistration, configured correctly.
|
|
|
|
func NewAccountRegistration(config AccountRegistrationConfig) (accountReg AccountRegistration) {
|
|
|
|
if config.Enabled {
|
|
|
|
accountReg.Enabled = true
|
2017-09-11 01:16:13 +02:00
|
|
|
accountReg.AllowMultiplePerConnection = config.AllowMultiplePerConnection
|
2016-09-04 12:08:53 +02:00
|
|
|
for _, name := range config.EnabledCallbacks {
|
|
|
|
// we store "none" as "*" internally
|
|
|
|
if name == "none" {
|
|
|
|
name = "*"
|
|
|
|
}
|
2016-09-04 13:15:28 +02:00
|
|
|
accountReg.EnabledCallbacks = append(accountReg.EnabledCallbacks, name)
|
|
|
|
}
|
|
|
|
// no need to make this configurable, right now at least
|
|
|
|
accountReg.EnabledCredentialTypes = []string{
|
|
|
|
"passphrase",
|
|
|
|
"certfp",
|
2016-09-04 12:08:53 +02:00
|
|
|
}
|
2016-09-04 11:25:33 +02:00
|
|
|
}
|
|
|
|
return accountReg
|
|
|
|
}
|
|
|
|
|
2017-03-08 12:36:13 +01:00
|
|
|
// removeFailedAccRegisterData removes the data created by ACC REGISTER if the account creation fails early.
|
|
|
|
func removeFailedAccRegisterData(store *buntdb.DB, account string) {
|
2016-09-05 10:45:42 +02:00
|
|
|
// error is ignored here, we can't do much about it anyways
|
|
|
|
store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
tx.Delete(fmt.Sprintf(keyAccountExists, account))
|
|
|
|
tx.Delete(fmt.Sprintf(keyAccountRegTime, account))
|
|
|
|
tx.Delete(fmt.Sprintf(keyAccountCredentials, account))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|