package mfa import ( "strconv" "strings" "time" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" "github.com/pquerna/otp" ) type ( // Device defines MFA Device metadata. Device struct { Namespace string Name string OID oid.ID Meta map[string]string } // SecretDevice is an MFA device metadata with decoded OTP Key. SecretDevice struct { Device Key *otp.Key } ) const ( FilePathKey = "FilePath" OIDKey = "OID" PathKey = "Path" EnableDateKey = "EnableDate" EnabledKey = "EnabledKey" UserIDKey = "UserIDKey" TagPrefix = "tag-" ) // NewDevice returns new device metadata. Device is disabled by default. func NewDevice(namespace, name, path string) *Device { return &Device{ Namespace: namespace, Name: name, Meta: map[string]string{ EnabledKey: "false", PathKey: path, }, } } // String returns string representation of device. func (d *Device) String() string { return d.Namespace + "/" + d.Name } // EnableStatus returns true if device is enabled. func (d *Device) EnableStatus() bool { return d.Meta[EnabledKey] == "true" } // SetEnableStatus updates enable status of device. // It does not modify enable date. func (d *Device) SetEnableStatus(enabled bool) { d.Meta[EnabledKey] = strconv.FormatBool(enabled) } // EnableDate returns date when device was enabled. func (d *Device) EnableDate() *time.Time { return dateFromString(d.Meta[EnableDateKey]) } // SetEnableDate sets date when device was enabled. // It is not affected by 'SetEnableStatus'. func (d *Device) SetEnableDate(date *time.Time) { d.Meta[EnableDateKey] = convertDate(date) } // SetUserID sets id of the device owner. Use neo wallet address representation. func (d *Device) SetUserID(addr string) { d.Meta[UserIDKey] = addr } // UserID returns id of the device owner. func (d *Device) UserID() string { return d.Meta[UserIDKey] } // AddTags adds new set of tags to the device. // It does not remove existing set of tags. func (d *Device) AddTags(tags [][2]string) { for _, kv := range tags { k, v := kv[0], kv[1] d.Meta[TagPrefix+k] = v } } // DeleteTags removes all tags with provided keys. func (d *Device) DeleteTags(keys []string) { for _, k := range keys { delete(d.Meta, TagPrefix+k) } } // Tags returns available set of tags of the device. func (d *Device) Tags() map[string]string { tags := make(map[string]string) for k, v := range d.Meta { if after, ok := strings.CutPrefix(k, TagPrefix); ok { tags[after] = v } } return tags } func convertDate(t *time.Time) string { if t == nil { return "" } return t.UTC().Format(time.RFC3339) } func dateFromString(timeString string) *time.Time { var date *time.Time enableD, err := time.Parse(time.RFC3339, timeString) if err == nil { date = &enableD } return date }