Skip to content

Commit 0767091

Browse files
committed
Updated implementation of Authentication login actions with user-agent and client address information.
1 parent c494a49 commit 0767091

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

backend/auth/callback.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ func isUserEnabled(apiKey string, args []string) func(*sql.DB) {
583583
func loginUserWithUsername(apiKey string, args []string) func(*sql.DB) {
584584
username := args[2]
585585
password := args[3]
586+
useragent := args[4]
587+
address := args[5]
586588

587589
if !validateUsername(username) {
588590
proc.ShowFailedResponse("Invalid username string.")
@@ -610,13 +612,14 @@ func loginUserWithUsername(apiKey string, args []string) func(*sql.DB) {
610612
}
611613

612614
if count != 1 {
613-
proc.ShowResult("\"error\"")
615+
proc.ShowResult("\"0\"")
614616
}
615617

616618
uuid := uuid.New().String()
617619
query, err = d.Query("INSERT INTO " + apiKey +
618-
"_account_session (username, uuid) VALUES(\"" +
619-
username + "\", \"" + uuid + "\")")
620+
"_account_session (username, uuid, useragent, address) VALUES(\"" +
621+
username + "\", \"" + uuid + "\", \"" +
622+
useragent + "\", \"" + address + "\")")
620623

621624
if err != nil {
622625
proc.ShowFailedResponse("Internal error occured.")
@@ -631,6 +634,8 @@ func loginUserWithUsername(apiKey string, args []string) func(*sql.DB) {
631634
func loginUserWithEmail(apiKey string, args []string) func(*sql.DB) {
632635
email := args[2]
633636
password := args[3]
637+
useragent := args[4]
638+
address := args[5]
634639

635640
if !validateEmail(email) {
636641
proc.ShowFailedResponse("Invalid email string.")
@@ -643,7 +648,7 @@ func loginUserWithEmail(apiKey string, args []string) func(*sql.DB) {
643648
}
644649

645650
return func(d *sql.DB) {
646-
query, err := d.Query("SELECT * FROM " + apiKey +
651+
query, err := d.Query("SELECT username FROM " + apiKey +
647652
"_accounts WHERE email=\"" + email +
648653
"\" AND password=\"" + password + "\"")
649654

@@ -652,17 +657,29 @@ func loginUserWithEmail(apiKey string, args []string) func(*sql.DB) {
652657
return
653658
}
654659

660+
username := ""
655661
count := 0
656662
for query.Next() {
663+
query.Scan(&username)
657664
count += 1
658665
}
659666

660-
if count == 1 {
661-
proc.ShowResult("\"1\"")
662-
} else {
667+
if count != 1 {
663668
proc.ShowResult("\"0\"")
664669
}
665670

671+
uuid := uuid.New().String()
672+
query, err = d.Query("INSERT INTO " + apiKey +
673+
"_account_session (username, uuid, useragent, address) VALUES(\"" +
674+
username + "\", \"" + uuid + "\", \"" +
675+
useragent + "\", \"" + address + "\")")
676+
677+
if err != nil {
678+
proc.ShowFailedResponse("Internal error occured.")
679+
return
680+
}
681+
682+
proc.ShowResult("\"" + uuid + "\"")
666683
query.Close()
667684
}
668685
}

backend/auth/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of QLBase (https://github.com/nthnn/QLBase).
33
* Copyright 2024 - Nathanne Isip
4-
*
4+
*
55
* Permission is hereby granted, free of charge,
66
* to any person obtaining a copy of this software
77
* and associated documentation files (the “Software”),
@@ -11,11 +11,11 @@
1111
* sell copies of the Software, and to permit persons to
1212
* whom the Software is furnished to do so, subject to
1313
* the following conditions:
14-
*
14+
*
1515
* The above copyright notice and this permission notice
1616
* shall be included in all copies or substantial portions
1717
* of the Software.
18-
*
18+
*
1919
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF
2020
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
2121
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
@@ -104,11 +104,11 @@ func main() {
104104
callback = isUserEnabled(apiKey, args)
105105

106106
case "login_username":
107-
failOnUmatchedArgSize(4, args)
107+
failOnUmatchedArgSize(6, args)
108108
callback = loginUserWithUsername(apiKey, args)
109109

110110
case "login_email":
111-
failOnUmatchedArgSize(4, args)
111+
failOnUmatchedArgSize(6, args)
112112
callback = loginUserWithEmail(apiKey, args)
113113

114114
case "fetch_all":

0 commit comments

Comments
 (0)