summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-03-16 10:35:18 +0530
committersinanmohd <sinan@sinanmohd.com>2024-03-16 10:35:18 +0530
commitfb387fe290d907dd8a1b0e3600c5468043071628 (patch)
tree228430b4d95ea2582c1d31bff49de6b59dbbe946
parent8fd42493709792ffe85f519c75b085ed65123baa (diff)
api/login: init
-rw-r--r--api/login.go54
-rw-r--r--api/main.go2
-rw-r--r--db/account.go5
3 files changed, 59 insertions, 2 deletions
diff --git a/api/login.go b/api/login.go
new file mode 100644
index 0000000..ef2195e
--- /dev/null
+++ b/api/login.go
@@ -0,0 +1,54 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http"
+
+ redqdb "sinanmohd.com/redq/db"
+)
+
+type loginAPI struct {
+ db *redqdb.SafeDB
+ req *RequestLogin
+ resp *ResponseLogin
+}
+
+type RequestLogin struct {
+ Account *redqdb.Account
+}
+
+type ResponseLogin struct {
+ Account *redqdb.Account
+}
+
+func newLogin(db *redqdb.SafeDB) *loginAPI {
+ a := &loginAPI{}
+ a.db = db
+
+ return a
+}
+
+func (a *loginAPI) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
+ a.req = &RequestLogin{}
+ a.resp = &ResponseLogin{}
+ err := unmarshal(r.Body, a.req)
+ if err != nil {
+ handleError(err, rw, http.StatusUnprocessableEntity)
+ return
+ }
+
+ err = a.req.Account.Login(a.db)
+ if err != nil {
+ handleError(err, rw, http.StatusUnauthorized)
+ return
+ }
+ a.resp.Account = a.req.Account
+
+ json, err := json.Marshal(a.resp)
+ if err != nil {
+ handleError(err, rw, http.StatusInternalServerError)
+ return
+ }
+
+ rw.Write(json)
+}
diff --git a/api/main.go b/api/main.go
index 29f71c9..c4645b8 100644
--- a/api/main.go
+++ b/api/main.go
@@ -10,6 +10,8 @@ import (
func Run(db *redqdb.SafeDB) {
const prefix string = "POST /_redq/api"
+ login := newLogin(db)
+ http.Handle(prefix+"/ac/login", login)
exampleApi := newExamplApiName(db)
http.Handle(prefix+"/example", exampleApi)
diff --git a/db/account.go b/db/account.go
index b7bcaa0..c7f76bf 100644
--- a/db/account.go
+++ b/db/account.go
@@ -4,7 +4,7 @@ import "errors"
type Account struct {
UserName string
- PassHash string
+ PassHash string `json:",omitempty"`
Info *Login
}
@@ -69,9 +69,10 @@ func (ac *Account) Login(safe *SafeDB) error {
if err != nil {
return err
}
- if PassHash != ac.PassHash {
+ if PassHash != ToBlake3(ac.PassHash) {
return errors.New("Auth failed")
}
+ ac.PassHash = ""
err = ac.Info.Bearer.Generate(safe, ac.Info)
if err != nil {