2021-01-17 21:29:08 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
2021-04-05 16:23:40 +00:00
|
|
|
"crypto/sha256"
|
2021-01-17 21:29:08 +00:00
|
|
|
"encoding/base64"
|
2021-10-10 11:08:05 +00:00
|
|
|
"encoding/json"
|
2021-01-17 21:29:08 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-07-31 08:39:53 +00:00
|
|
|
"os"
|
2021-01-17 21:29:08 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alexedwards/argon2id"
|
2021-08-06 16:56:07 +00:00
|
|
|
passwordvalidator "github.com/wagslane/go-password-validator"
|
2021-04-20 11:55:09 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2022-01-06 09:11:47 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/kms"
|
2021-10-10 11:08:05 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-09-04 10:11:04 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/mfa"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-01-17 21:29:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Available permissions for SFTPGo admins
|
|
|
|
const (
|
|
|
|
PermAdminAny = "*"
|
|
|
|
PermAdminAddUsers = "add_users"
|
|
|
|
PermAdminChangeUsers = "edit_users"
|
|
|
|
PermAdminDeleteUsers = "del_users"
|
|
|
|
PermAdminViewUsers = "view_users"
|
|
|
|
PermAdminViewConnections = "view_conns"
|
|
|
|
PermAdminCloseConnections = "close_conns"
|
|
|
|
PermAdminViewServerStatus = "view_status"
|
|
|
|
PermAdminManageAdmins = "manage_admins"
|
2022-04-25 13:49:11 +00:00
|
|
|
PermAdminManageGroups = "manage_groups"
|
2021-08-17 16:08:32 +00:00
|
|
|
PermAdminManageAPIKeys = "manage_apikeys"
|
2021-01-17 21:29:08 +00:00
|
|
|
PermAdminQuotaScans = "quota_scans"
|
|
|
|
PermAdminManageSystem = "manage_system"
|
|
|
|
PermAdminManageDefender = "manage_defender"
|
|
|
|
PermAdminViewDefender = "view_defender"
|
2021-09-25 10:20:31 +00:00
|
|
|
PermAdminRetentionChecks = "retention_checks"
|
2021-12-16 17:18:36 +00:00
|
|
|
PermAdminMetadataChecks = "metadata_checks"
|
2021-10-23 13:47:21 +00:00
|
|
|
PermAdminViewEvents = "view_events"
|
2021-01-17 21:29:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
validAdminPerms = []string{PermAdminAny, PermAdminAddUsers, PermAdminChangeUsers, PermAdminDeleteUsers,
|
2022-04-25 13:49:11 +00:00
|
|
|
PermAdminViewUsers, PermAdminManageGroups, PermAdminViewConnections, PermAdminCloseConnections,
|
|
|
|
PermAdminViewServerStatus, PermAdminManageAdmins, PermAdminManageAPIKeys, PermAdminQuotaScans,
|
|
|
|
PermAdminManageSystem, PermAdminManageDefender, PermAdminViewDefender, PermAdminRetentionChecks,
|
|
|
|
PermAdminMetadataChecks, PermAdminViewEvents}
|
2021-01-17 21:29:08 +00:00
|
|
|
)
|
|
|
|
|
2022-01-06 09:11:47 +00:00
|
|
|
// AdminTOTPConfig defines the time-based one time password configuration
|
|
|
|
type AdminTOTPConfig struct {
|
2021-09-04 10:11:04 +00:00
|
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
|
|
ConfigName string `json:"config_name,omitempty"`
|
|
|
|
Secret *kms.Secret `json:"secret,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:11:47 +00:00
|
|
|
func (c *AdminTOTPConfig) validate(username string) error {
|
2021-09-04 10:11:04 +00:00
|
|
|
if !c.Enabled {
|
|
|
|
c.ConfigName = ""
|
|
|
|
c.Secret = kms.NewEmptySecret()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if c.ConfigName == "" {
|
|
|
|
return util.NewValidationError("totp: config name is mandatory")
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if !util.Contains(mfa.GetAvailableTOTPConfigNames(), c.ConfigName) {
|
2021-09-04 10:11:04 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("totp: config name %#v not found", c.ConfigName))
|
|
|
|
}
|
|
|
|
if c.Secret.IsEmpty() {
|
|
|
|
return util.NewValidationError("totp: secret is mandatory")
|
|
|
|
}
|
|
|
|
if c.Secret.IsPlain() {
|
2021-09-05 12:10:12 +00:00
|
|
|
c.Secret.SetAdditionalData(username)
|
2021-09-04 10:11:04 +00:00
|
|
|
if err := c.Secret.Encrypt(); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("totp: unable to encrypt secret: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// AdminFilters defines additional restrictions for SFTPGo admins
|
2021-05-27 07:40:46 +00:00
|
|
|
// TODO: rename to AdminOptions in v3
|
2021-01-17 21:29:08 +00:00
|
|
|
type AdminFilters struct {
|
|
|
|
// only clients connecting from these IP/Mask are allowed.
|
|
|
|
// IP/Mask must be in CIDR notation as defined in RFC 4632 and RFC 4291
|
|
|
|
// for example "192.0.2.0/24" or "2001:db8::/32"
|
|
|
|
AllowList []string `json:"allow_list,omitempty"`
|
2021-08-17 16:08:32 +00:00
|
|
|
// API key auth allows to impersonate this administrator with an API key
|
|
|
|
AllowAPIKeyAuth bool `json:"allow_api_key_auth,omitempty"`
|
2021-09-04 10:11:04 +00:00
|
|
|
// Time-based one time passwords configuration
|
2022-01-06 09:11:47 +00:00
|
|
|
TOTPConfig AdminTOTPConfig `json:"totp_config,omitempty"`
|
2021-09-04 10:11:04 +00:00
|
|
|
// Recovery codes to use if the user loses access to their second factor auth device.
|
|
|
|
// Each code can only be used once, you should use these codes to login and disable or
|
|
|
|
// reset 2FA for your account
|
2022-01-06 09:11:47 +00:00
|
|
|
RecoveryCodes []RecoveryCode `json:"recovery_codes,omitempty"`
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Admin defines a SFTPGo admin
|
|
|
|
type Admin struct {
|
|
|
|
// Database unique identifier
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
// 1 enabled, 0 disabled (login is not allowed)
|
|
|
|
Status int `json:"status"`
|
|
|
|
// Username
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password,omitempty"`
|
2021-09-25 17:06:13 +00:00
|
|
|
Email string `json:"email,omitempty"`
|
2021-01-17 21:29:08 +00:00
|
|
|
Permissions []string `json:"permissions"`
|
|
|
|
Filters AdminFilters `json:"filters,omitempty"`
|
2021-02-24 18:40:29 +00:00
|
|
|
Description string `json:"description,omitempty"`
|
2021-01-17 21:29:08 +00:00
|
|
|
AdditionalInfo string `json:"additional_info,omitempty"`
|
2021-08-19 13:51:43 +00:00
|
|
|
// Creation time as unix timestamp in milliseconds. It will be 0 for admins created before v2.2.0
|
|
|
|
CreatedAt int64 `json:"created_at"`
|
|
|
|
// last update time as unix timestamp in milliseconds
|
|
|
|
UpdatedAt int64 `json:"updated_at"`
|
|
|
|
// Last login as unix timestamp in milliseconds
|
|
|
|
LastLogin int64 `json:"last_login"`
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
// CountUnusedRecoveryCodes returns the number of unused recovery codes
|
|
|
|
func (a *Admin) CountUnusedRecoveryCodes() int {
|
|
|
|
unused := 0
|
|
|
|
for _, code := range a.Filters.RecoveryCodes {
|
|
|
|
if !code.Used {
|
|
|
|
unused++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unused
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
func (a *Admin) hashPassword() error {
|
2021-07-11 13:26:51 +00:00
|
|
|
if a.Password != "" && !util.IsStringPrefixInSlice(a.Password, internalHashPwdPrefixes) {
|
2021-08-06 16:56:07 +00:00
|
|
|
if config.PasswordValidation.Admins.MinEntropy > 0 {
|
|
|
|
if err := passwordvalidator.Validate(a.Password, config.PasswordValidation.Admins.MinEntropy); err != nil {
|
|
|
|
return util.NewValidationError(err.Error())
|
|
|
|
}
|
|
|
|
}
|
2021-04-25 07:38:33 +00:00
|
|
|
if config.PasswordHashing.Algo == HashingAlgoBcrypt {
|
2021-04-20 11:55:09 +00:00
|
|
|
pwd, err := bcrypt.GenerateFromPassword([]byte(a.Password), config.PasswordHashing.BcryptOptions.Cost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.Password = string(pwd)
|
|
|
|
} else {
|
|
|
|
pwd, err := argon2id.CreateHash(a.Password, argon2Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.Password = pwd
|
2021-03-04 08:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func (a *Admin) hasRedactedSecret() bool {
|
|
|
|
return a.Filters.TOTPConfig.Secret.IsRedacted()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Admin) validateRecoveryCodes() error {
|
|
|
|
for i := 0; i < len(a.Filters.RecoveryCodes); i++ {
|
|
|
|
code := &a.Filters.RecoveryCodes[i]
|
|
|
|
if code.Secret.IsEmpty() {
|
|
|
|
return util.NewValidationError("mfa: recovery code cannot be empty")
|
|
|
|
}
|
|
|
|
if code.Secret.IsPlain() {
|
2021-09-05 12:10:12 +00:00
|
|
|
code.Secret.SetAdditionalData(a.Username)
|
2021-09-04 10:11:04 +00:00
|
|
|
if err := code.Secret.Encrypt(); err != nil {
|
|
|
|
return util.NewValidationError(fmt.Sprintf("mfa: unable to encrypt recovery code: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Admin) validatePermissions() error {
|
|
|
|
a.Permissions = util.RemoveDuplicates(a.Permissions)
|
|
|
|
if len(a.Permissions) == 0 {
|
|
|
|
return util.NewValidationError("please grant some permissions to this admin")
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(a.Permissions, PermAdminAny) {
|
2021-09-04 10:11:04 +00:00
|
|
|
a.Permissions = []string{PermAdminAny}
|
|
|
|
}
|
|
|
|
for _, perm := range a.Permissions {
|
2022-05-19 17:49:51 +00:00
|
|
|
if !util.Contains(validAdminPerms, perm) {
|
2021-09-04 10:11:04 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("invalid permission: %#v", perm))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func (a *Admin) validate() error {
|
2021-09-04 10:11:04 +00:00
|
|
|
a.SetEmptySecretsIfNil()
|
2021-01-17 21:29:08 +00:00
|
|
|
if a.Username == "" {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("username is mandatory")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
if a.Password == "" {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError("please set a password")
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
if a.hasRedactedSecret() {
|
|
|
|
return util.NewValidationError("cannot save an admin with a redacted secret")
|
|
|
|
}
|
2021-09-05 12:10:12 +00:00
|
|
|
if err := a.Filters.TOTPConfig.validate(a.Username); err != nil {
|
2021-09-04 10:11:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := a.validateRecoveryCodes(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-31 17:01:37 +00:00
|
|
|
if config.NamingRules&1 == 0 && !usernameRegex.MatchString(a.Username) {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("username %#v is not valid, the following characters are allowed: a-zA-Z0-9-_.~", a.Username))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
if err := a.hashPassword(); err != nil {
|
2021-03-04 08:48:53 +00:00
|
|
|
return err
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
if err := a.validatePermissions(); err != nil {
|
|
|
|
return err
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2022-05-27 05:39:55 +00:00
|
|
|
if a.Email != "" && !util.IsEmailValid(a.Email) {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("email %#v is not valid", a.Email))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-11-25 19:09:11 +00:00
|
|
|
a.Filters.AllowList = util.RemoveDuplicates(a.Filters.AllowList)
|
2021-01-17 21:29:08 +00:00
|
|
|
for _, IPMask := range a.Filters.AllowList {
|
|
|
|
_, _, err := net.ParseCIDR(IPMask)
|
|
|
|
if err != nil {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.NewValidationError(fmt.Sprintf("could not parse allow list entry %#v : %v", IPMask, err))
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckPassword verifies the admin password
|
|
|
|
func (a *Admin) CheckPassword(password string) (bool, error) {
|
2021-04-20 11:55:09 +00:00
|
|
|
if strings.HasPrefix(a.Password, bcryptPwdPrefix) {
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(a.Password), []byte(password)); err != nil {
|
2021-04-26 17:48:21 +00:00
|
|
|
return false, ErrInvalidCredentials
|
2021-04-20 11:55:09 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
match, err := argon2id.ComparePasswordAndHash(password, a.Password)
|
|
|
|
if !match || err != nil {
|
|
|
|
return false, ErrInvalidCredentials
|
|
|
|
}
|
|
|
|
return match, err
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanLoginFromIP returns true if login from the given IP is allowed
|
|
|
|
func (a *Admin) CanLoginFromIP(ip string) bool {
|
|
|
|
if len(a.Filters.AllowList) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
parsedIP := net.ParseIP(ip)
|
|
|
|
if parsedIP == nil {
|
|
|
|
return len(a.Filters.AllowList) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ipMask := range a.Filters.AllowList {
|
|
|
|
_, network, err := net.ParseCIDR(ipMask)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if network.Contains(parsedIP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
// CanLogin returns an error if the login is not allowed
|
|
|
|
func (a *Admin) CanLogin(ip string) error {
|
2021-01-17 21:29:08 +00:00
|
|
|
if a.Status != 1 {
|
|
|
|
return fmt.Errorf("admin %#v is disabled", a.Username)
|
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
if !a.CanLoginFromIP(ip) {
|
|
|
|
return fmt.Errorf("login from IP %v not allowed", ip)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Admin) checkUserAndPass(password, ip string) error {
|
|
|
|
if err := a.CanLogin(ip); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
if a.Password == "" || password == "" {
|
|
|
|
return errors.New("credentials cannot be null or empty")
|
|
|
|
}
|
|
|
|
match, err := a.CheckPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
return ErrInvalidCredentials
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
// RenderAsJSON implements the renderer interface used within plugins
|
|
|
|
func (a *Admin) RenderAsJSON(reload bool) ([]byte, error) {
|
|
|
|
if reload {
|
|
|
|
admin, err := provider.adminExists(a.Username)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "unable to reload admin before rendering as json: %v", err)
|
2021-10-10 11:08:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
admin.HideConfidentialData()
|
|
|
|
return json.Marshal(admin)
|
|
|
|
}
|
|
|
|
a.HideConfidentialData()
|
|
|
|
return json.Marshal(a)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// HideConfidentialData hides admin confidential data
|
|
|
|
func (a *Admin) HideConfidentialData() {
|
|
|
|
a.Password = ""
|
2021-09-04 10:11:04 +00:00
|
|
|
if a.Filters.TOTPConfig.Secret != nil {
|
|
|
|
a.Filters.TOTPConfig.Secret.Hide()
|
|
|
|
}
|
2021-09-11 12:19:17 +00:00
|
|
|
for _, code := range a.Filters.RecoveryCodes {
|
|
|
|
if code.Secret != nil {
|
|
|
|
code.Secret.Hide()
|
|
|
|
}
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
a.SetNilSecretsIfEmpty()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEmptySecretsIfNil sets the secrets to empty if nil
|
|
|
|
func (a *Admin) SetEmptySecretsIfNil() {
|
|
|
|
if a.Filters.TOTPConfig.Secret == nil {
|
|
|
|
a.Filters.TOTPConfig.Secret = kms.NewEmptySecret()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNilSecretsIfEmpty set the secrets to nil if empty.
|
|
|
|
// This is useful before rendering as JSON so the empty fields
|
|
|
|
// will not be serialized.
|
|
|
|
func (a *Admin) SetNilSecretsIfEmpty() {
|
2021-09-04 11:56:29 +00:00
|
|
|
if a.Filters.TOTPConfig.Secret != nil && a.Filters.TOTPConfig.Secret.IsEmpty() {
|
2021-09-04 10:11:04 +00:00
|
|
|
a.Filters.TOTPConfig.Secret = nil
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasPermission returns true if the admin has the specified permission
|
|
|
|
func (a *Admin) HasPermission(perm string) bool {
|
2022-05-19 17:49:51 +00:00
|
|
|
if util.Contains(a.Permissions, PermAdminAny) {
|
2021-01-17 21:29:08 +00:00
|
|
|
return true
|
|
|
|
}
|
2022-05-19 17:49:51 +00:00
|
|
|
return util.Contains(a.Permissions, perm)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPermissionsAsString returns permission as string
|
|
|
|
func (a *Admin) GetPermissionsAsString() string {
|
|
|
|
return strings.Join(a.Permissions, ", ")
|
|
|
|
}
|
|
|
|
|
2022-05-22 09:45:49 +00:00
|
|
|
// GetLastLoginAsString returns the last login as string
|
|
|
|
func (a *Admin) GetLastLoginAsString() string {
|
|
|
|
if a.LastLogin > 0 {
|
|
|
|
return util.GetTimeFromMsecSinceEpoch(a.LastLogin).UTC().Format(iso8601UTCFormat)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// GetAllowedIPAsString returns the allowed IP as comma separated string
|
|
|
|
func (a *Admin) GetAllowedIPAsString() string {
|
|
|
|
return strings.Join(a.Filters.AllowList, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValidPerms returns the allowed admin permissions
|
|
|
|
func (a *Admin) GetValidPerms() []string {
|
|
|
|
return validAdminPerms
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
// CanManageMFA returns true if the admin can add a multi-factor authentication configuration
|
|
|
|
func (a *Admin) CanManageMFA() bool {
|
|
|
|
return len(mfa.GetAvailableTOTPConfigs()) > 0
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// GetSignature returns a signature for this admin.
|
|
|
|
// It could change after an update
|
|
|
|
func (a *Admin) GetSignature() string {
|
|
|
|
data := []byte(a.Username)
|
|
|
|
data = append(data, []byte(a.Password)...)
|
|
|
|
signature := sha256.Sum256(data)
|
|
|
|
return base64.StdEncoding.EncodeToString(signature[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Admin) getACopy() Admin {
|
2021-09-04 10:11:04 +00:00
|
|
|
a.SetEmptySecretsIfNil()
|
2021-01-17 21:29:08 +00:00
|
|
|
permissions := make([]string, len(a.Permissions))
|
|
|
|
copy(permissions, a.Permissions)
|
|
|
|
filters := AdminFilters{}
|
|
|
|
filters.AllowList = make([]string, len(a.Filters.AllowList))
|
2021-08-17 16:08:32 +00:00
|
|
|
filters.AllowAPIKeyAuth = a.Filters.AllowAPIKeyAuth
|
2021-09-04 10:11:04 +00:00
|
|
|
filters.TOTPConfig.Enabled = a.Filters.TOTPConfig.Enabled
|
|
|
|
filters.TOTPConfig.ConfigName = a.Filters.TOTPConfig.ConfigName
|
|
|
|
filters.TOTPConfig.Secret = a.Filters.TOTPConfig.Secret.Clone()
|
2021-01-17 21:29:08 +00:00
|
|
|
copy(filters.AllowList, a.Filters.AllowList)
|
2022-01-06 09:11:47 +00:00
|
|
|
filters.RecoveryCodes = make([]RecoveryCode, 0)
|
2021-09-04 10:11:04 +00:00
|
|
|
for _, code := range a.Filters.RecoveryCodes {
|
|
|
|
if code.Secret == nil {
|
|
|
|
code.Secret = kms.NewEmptySecret()
|
|
|
|
}
|
2022-01-06 09:11:47 +00:00
|
|
|
filters.RecoveryCodes = append(filters.RecoveryCodes, RecoveryCode{
|
2021-09-04 10:11:04 +00:00
|
|
|
Secret: code.Secret.Clone(),
|
|
|
|
Used: code.Used,
|
|
|
|
})
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
return Admin{
|
|
|
|
ID: a.ID,
|
|
|
|
Status: a.Status,
|
|
|
|
Username: a.Username,
|
|
|
|
Password: a.Password,
|
|
|
|
Email: a.Email,
|
|
|
|
Permissions: permissions,
|
|
|
|
Filters: filters,
|
|
|
|
AdditionalInfo: a.AdditionalInfo,
|
2021-02-24 18:40:29 +00:00
|
|
|
Description: a.Description,
|
2021-08-19 13:51:43 +00:00
|
|
|
LastLogin: a.LastLogin,
|
|
|
|
CreatedAt: a.CreatedAt,
|
|
|
|
UpdatedAt: a.UpdatedAt,
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 08:37:51 +00:00
|
|
|
func (a *Admin) setFromEnv() error {
|
|
|
|
envUsername := strings.TrimSpace(os.Getenv("SFTPGO_DEFAULT_ADMIN_USERNAME"))
|
|
|
|
envPassword := strings.TrimSpace(os.Getenv("SFTPGO_DEFAULT_ADMIN_PASSWORD"))
|
|
|
|
if envUsername == "" || envPassword == "" {
|
|
|
|
return errors.New(`to create the default admin you need to set the env vars "SFTPGO_DEFAULT_ADMIN_USERNAME" and "SFTPGO_DEFAULT_ADMIN_PASSWORD"`)
|
2021-07-31 08:39:53 +00:00
|
|
|
}
|
2021-08-20 08:37:51 +00:00
|
|
|
a.Username = envUsername
|
|
|
|
a.Password = envPassword
|
2021-01-17 21:29:08 +00:00
|
|
|
a.Status = 1
|
|
|
|
a.Permissions = []string{PermAdminAny}
|
2021-08-20 08:37:51 +00:00
|
|
|
return nil
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|