From 73952003142c7106482a198ce6024cba00c35e44 Mon Sep 17 00:00:00 2001 From: Andinus Date: Fri, 27 Mar 2020 18:24:25 +0530 Subject: Move token functions to its own package --- auth/gentoken.go | 8 ---- auth/token.go | 101 ------------------------------------------------- auth/token/add.go | 58 ++++++++++++++++++++++++++++ auth/token/generate.go | 14 +++++++ auth/token/validate.go | 51 +++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 109 deletions(-) delete mode 100644 auth/gentoken.go delete mode 100644 auth/token.go create mode 100644 auth/token/add.go create mode 100644 auth/token/generate.go create mode 100644 auth/token/validate.go (limited to 'auth') diff --git a/auth/gentoken.go b/auth/gentoken.go deleted file mode 100644 index 1e01875..0000000 --- a/auth/gentoken.go +++ /dev/null @@ -1,8 +0,0 @@ -package auth - -// genToken generates a random token string of length n. Don't forget to -// seed the random number generator otherwise it won't be random. -func genToken(n int) string { - // Currently this is just a wrapper to genID. - return genID(n) -} diff --git a/auth/token.go b/auth/token.go deleted file mode 100644 index 3f831fb..0000000 --- a/auth/token.go +++ /dev/null @@ -1,101 +0,0 @@ -package auth - -import ( - "errors" - "log" - "time" - - "tildegit.org/andinus/perseus/storage/sqlite3" - "tildegit.org/andinus/perseus/user" -) - -// ValToken will validate the token and returns an error. If error -// doesn't equal nil then consider token invalid. -func ValToken(db *sqlite3.DB, uInfo map[string]string) error { - // Acquire read lock on the database. - db.Mu.RLock() - defer db.Mu.RUnlock() - - u := user.User{} - u.SetUsername(uInfo["username"]) - - // Set user id from username. - err := u.GetID(db) - if err != nil { - log.Printf("auth/token.go: %s\n", - "failed to get id from username") - return err - } - - // Check if user's token is valid. - stmt, err := db.Conn.Prepare("SELECT token FROM access WHERE id = ?") - if err != nil { - log.Printf("auth/token.go: %s\n", - "failed to prepare statement") - return err - } - defer stmt.Close() - - var token string - err = stmt.QueryRow(u.ID()).Scan(&token) - if err != nil { - log.Printf("auth/token.go: %s\n", - "query failed") - return err - } - - if token != uInfo["token"] { - err = errors.New("token mismatch") - } - - return err -} - -// AddToken will generate a random token, add it to database and -// return the token. -func AddToken(db *sqlite3.DB, uInfo map[string]string) (token string, err error) { - // Acquire write lock on the database. - db.Mu.Lock() - defer db.Mu.Unlock() - - token = genToken(64) - - u := user.User{} - u.SetUsername(uInfo["username"]) - - // Set user id from username. - err = u.GetID(db) - if err != nil { - log.Printf("auth/token.go: %s\n", - "failed to get id from username") - return - } - - // Start the transaction - tx, err := db.Conn.Begin() - if err != nil { - log.Printf("auth/token.go: %s\n", - "failed to begin transaction") - return - } - - stmt, err := db.Conn.Prepare(` -INSERT INTO access(id, username, genTime) values(?, ?, ?)`) - if err != nil { - log.Printf("auth/tokenr.go: %s\n", - "failed to prepare statement") - return - } - defer stmt.Close() - - _, err = stmt.Exec(u.ID(), u.Username(), time.Now().UTC()) - if err != nil { - log.Printf("auth/token.go: %s\n", - "failed to execute statement") - return - } - - tx.Commit() - return - -} diff --git a/auth/token/add.go b/auth/token/add.go new file mode 100644 index 0000000..c7f632c --- /dev/null +++ b/auth/token/add.go @@ -0,0 +1,58 @@ +package token + +import ( + "log" + "time" + + "tildegit.org/andinus/perseus/storage/sqlite3" + "tildegit.org/andinus/perseus/user" +) + +// AddToken will generate a random token, add it to database and +// return the token. +func AddToken(db *sqlite3.DB, uInfo map[string]string) (token string, err error) { + // Acquire write lock on the database. + db.Mu.Lock() + defer db.Mu.Unlock() + + token = genToken(64) + + u := user.User{} + u.SetUsername(uInfo["username"]) + + // Set user id from username. + err = u.GetID(db) + if err != nil { + log.Printf("auth/token.go: %s\n", + "failed to get id from username") + return + } + + // Start the transaction + tx, err := db.Conn.Begin() + if err != nil { + log.Printf("auth/token.go: %s\n", + "failed to begin transaction") + return + } + + stmt, err := db.Conn.Prepare(` +INSERT INTO access(id, username, genTime) values(?, ?, ?)`) + if err != nil { + log.Printf("auth/tokenr.go: %s\n", + "failed to prepare statement") + return + } + defer stmt.Close() + + _, err = stmt.Exec(u.ID(), u.Username(), time.Now().UTC()) + if err != nil { + log.Printf("auth/token.go: %s\n", + "failed to execute statement") + return + } + + tx.Commit() + return + +} diff --git a/auth/token/generate.go b/auth/token/generate.go new file mode 100644 index 0000000..0c717d9 --- /dev/null +++ b/auth/token/generate.go @@ -0,0 +1,14 @@ +package token + +import ( + "crypto/rand" + "encoding/base64" +) + +// genToken generates a random token string of length n. Don't forget to +// seed the random number generator otherwise it won't be random. +func genToken(n int) string { + b := make([]byte, n/2) + rand.Read(b) + return base64.StdEncoding.EncodeToString(b) +} diff --git a/auth/token/validate.go b/auth/token/validate.go new file mode 100644 index 0000000..f1ee149 --- /dev/null +++ b/auth/token/validate.go @@ -0,0 +1,51 @@ +package token + +import ( + "errors" + "log" + + "tildegit.org/andinus/perseus/storage/sqlite3" + "tildegit.org/andinus/perseus/user" +) + +// ValToken will validate the token and returns an error. If error +// doesn't equal nil then consider token invalid. +func ValToken(db *sqlite3.DB, uInfo map[string]string) error { + // Acquire read lock on the database. + db.Mu.RLock() + defer db.Mu.RUnlock() + + u := user.User{} + u.SetUsername(uInfo["username"]) + + // Set user id from username. + err := u.GetID(db) + if err != nil { + log.Printf("auth/token.go: %s\n", + "failed to get id from username") + return err + } + + // Check if user's token is valid. + stmt, err := db.Conn.Prepare("SELECT token FROM access WHERE id = ?") + if err != nil { + log.Printf("auth/token.go: %s\n", + "failed to prepare statement") + return err + } + defer stmt.Close() + + var token string + err = stmt.QueryRow(u.ID()).Scan(&token) + if err != nil { + log.Printf("auth/token.go: %s\n", + "query failed") + return err + } + + if token != uInfo["token"] { + err = errors.New("token mismatch") + } + + return err +} -- cgit 1.4.1-2-gfad0