79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package model
|
|
|
|
import "time"
|
|
|
|
type Key struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Username string `json:"username"`
|
|
URL string `json:"url"`
|
|
Passphrase string `json:"passphrase"`
|
|
Strength string `json:"strength"` // weak | moderate | secure
|
|
CreatedAt string `json:"createdAt"`
|
|
}
|
|
|
|
type KeyStore struct {
|
|
Keys []Key
|
|
}
|
|
|
|
func NewKeyStore() *KeyStore {
|
|
return &KeyStore{Keys: defaultKeys()}
|
|
}
|
|
|
|
func (s *KeyStore) All() []Key { return s.Keys }
|
|
func (s *KeyStore) Search(q string) []Key {
|
|
if q == "" { return s.Keys }
|
|
var res []Key
|
|
for _, k := range s.Keys {
|
|
if contains(k.Name, q) || contains(k.Username, q) || contains(k.URL, q) {
|
|
res = append(res, k)
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (s *KeyStore) Add(name, username, url, passphrase string) Key {
|
|
k := Key{
|
|
ID: randString(8),
|
|
Name: name,
|
|
Username: username,
|
|
URL: url,
|
|
Passphrase: passphrase,
|
|
Strength: calcStrength(passphrase),
|
|
CreatedAt: time.Now().Format("Jan 2, 2006"),
|
|
}
|
|
s.Keys = append(s.Keys, k)
|
|
return k
|
|
}
|
|
|
|
func (s *KeyStore) Delete(id string) {
|
|
for i, k := range s.Keys {
|
|
if k.ID == id { s.Keys = append(s.Keys[:i], s.Keys[i+1:]...); return }
|
|
}
|
|
}
|
|
|
|
func calcStrength(p string) string {
|
|
l, d, u, s := 0, 0, 0, 0
|
|
for _, c := range p {
|
|
switch {
|
|
case c >= 'a' && c <= 'z': l++
|
|
case c >= 'A' && c <= 'Z': u++
|
|
case c >= '0' && c <= '9': d++
|
|
default: s++
|
|
}
|
|
}
|
|
if len(p) >= 12 && u >= 1 && d >= 1 && s >= 1 { return "secure" }
|
|
if len(p) >= 8 && u+d+s >= 2 { return "moderate" }
|
|
return "weak"
|
|
}
|
|
|
|
func defaultKeys() []Key {
|
|
return []Key{
|
|
{ID: "k1", Name: "AWS Prod Root", Username: "ec2-user", URL: "aws-console.amazon.com", Passphrase: "••••••••••", Strength: "secure", CreatedAt: "Mar 15, 2026"},
|
|
{ID: "k2", Name: "GitHub Deploy", Username: "git", URL: "github.com", Passphrase: "••••••••", Strength: "secure", CreatedAt: "Apr 2, 2026"},
|
|
{ID: "k3", Name: "Dev DB Access", Username: "admin", URL: "dev-db.internal:5432", Passphrase: "••••••", Strength: "moderate", CreatedAt: "May 10, 2026"},
|
|
{ID: "k4", Name: "Old Server", Username: "root", URL: "192.168.1.100", Passphrase: "••••", Strength: "weak", CreatedAt: "Jan 5, 2026"},
|
|
{ID: "k5", Name: "Staging API Key", Username: "api-user", URL: "staging.api.company.com", Passphrase: "•••••••••••", Strength: "secure", CreatedAt: "Jun 1, 2026"},
|
|
}
|
|
}
|