aboutsummaryrefslogtreecommitdiff
path: root/indentalUserDB/indentalUserDB.go
blob: 588ededff9496186ff45005e460ec08600141ec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package indentalUserDB

import (
  "time"
  "nilfm.cc/git/goldbug/cookie"
  "nilfm.cc/git/goldbug/auth"
  "golang.org/x/crypto/bcrypt"
  //"io"
  "os"
  "strings"
  "fmt"
  "errors"
)

type IndentalUserDB struct {
  Users map[string]*auth.User
  Basis string
}

func CreateIndentalUserDB(filePath string) *IndentalUserDB {
  u, err := readDB(filePath)
  if err == nil {
    uMap := map[string]*auth.User{}
    for _, usr := range u {
      uMap[usr.Name] = usr
    }
    return &IndentalUserDB{
      Users: uMap,
      Basis: filePath,
    }
  } else {
    return &IndentalUserDB{
      Users: map[string]*auth.User{},
      Basis: filePath,
    }
  }
}

func (self *IndentalUserDB) InitiateSession(user string, password string) (string, error) {
  if _, exists := self.Users[user]; !exists {
    return "", errors.New("User not in DB")
  }
  if bcrypt.CompareHashAndPassword([]byte(self.Users[user].Pass), []byte(password)) != nil {
    return "", errors.New("Incorrect password")
  }
  sessionId := cookie.GenToken(64)
  self.Users[user].Session = sessionId
  writeDB(self.Basis, self.Users)
  return sessionId, nil
}

func (self *IndentalUserDB) ValidateUser(user string, sessionId string) (bool, error) {
  if _, exists := self.Users[user]; !exists {
    return false, errors.New("User not in DB")
  }

  validated := self.Users[user].Session == sessionId
  if validated {
    self.Users[user].LastSeen = time.Now()
    writeDB(self.Basis, self.Users)
  }

  return validated, nil
}

func (self *IndentalUserDB) EndSession(user string) error {
  if _, exists := self.Users[user]; !exists {
    return errors.New("User not in DB")
  }

  self.Users[user].Session = ""
  self.Users[user].LastSeen = time.Now()
  writeDB(self.Basis, self.Users)
  return nil
}

func (self *IndentalUserDB) DeleteUser(user string) error {
  if _, exists := self.Users[user]; !exists {
    return errors.New("User not in DB")
  }

  delete(self.Users, user)
  writeDB(self.Basis, self.Users)
  return nil
}

func (self *IndentalUserDB) ChangePassword(user string, password string, oldPassword string) error {
  if _, exists := self.Users[user]; !exists {
    return errors.New("User not in DB")
  }
  if bcrypt.CompareHashAndPassword([]byte(self.Users[user].Pass), []byte(oldPassword)) != nil {
    return errors.New("Incorrect password")
  }

  hash, _ := bcrypt.GenerateFromPassword([]byte(password), 10)
  self.Users[user].Pass = string(hash[:])
  writeDB(self.Basis, self.Users)
  return nil
}

func (self *IndentalUserDB) AddUser(user string, password string) error{
  if _, exists := self.Users[user]; exists {
    return errors.New("User already in DB")
  }

  hash, _ := bcrypt.GenerateFromPassword([]byte(password), 10)

  self.Users[user] = &auth.User{
    Name: user,
    Pass: string(hash[:]),
    LastSeen: time.UnixMicro(0),
    LoginTime: time.UnixMicro(0),
    Session: "",
  }
  writeDB(self.Basis, self.Users)
  return nil;
}

const timeFmt = "2006-01-02T15:04Z"

func readDB(filePath string) (map[string]*auth.User, error) {
  f, err := os.ReadFile(filePath)
  if err != nil {
    return nil, err
  }

  data := string(f[:])
  users := map[string]*auth.User{}

  lines := strings.Split(data, "\n")
  var name string
  var pass string
  var session string
  var loginTime time.Time
  var lastSeen time.Time
  procFields := 0
  for _, l := range lines {
    if !strings.HasPrefix(l, "  ") {
      name = l
      procFields++
    } else {
      kvp := strings.Split(l, ":")
      k := strings.TrimSpace(kvp[0])
      v := strings.TrimSpace(kvp[1])
      switch k {
        case "pass":
          pass = v
        case "session":
          session = v
        case "loginTime":
          loginTime, _ = time.Parse(timeFmt, v)
        case "lastSeen":
          lastSeen, _ = time.Parse(timeFmt, v)
      }
      procFields++
      if procFields == 5 {
        users[name] = &auth.User{
          Name: name,
          Pass: pass,
          Session: session,
          LoginTime: loginTime,
          LastSeen: lastSeen,
        }
        procFields = 0
      }
    }
  }
  return users, nil
}

func writeDB(filePath string, users map[string]*auth.User) error {
  f, err := os.Create(filePath)
  if err != nil {
    return err
  }

  defer f.Close()

  for _, user := range users {
    f.WriteString(fmt.Sprintf("%s:\n  pass: %s\n  session: %s\n  loginTime: %s\n  lastSeen: %s\n",
      user.Name,
      user.Pass,
      user.Session,
      user.LoginTime,
      user.LastSeen));
  }
  f.Sync()
  return nil
}