aboutsummaryrefslogtreecommitdiff
path: root/session.go
diff options
context:
space:
mode:
authorFChannel <=>2021-01-22 21:12:26 -0800
committerFChannel <=>2021-01-22 21:12:26 -0800
commit2566e314e8351ffbceae9a2688d90a5c811d49e5 (patch)
treee802622593c540265e464beecf76696b65e229ed /session.go
parent626fee35bacd68baf6e9bb38ca71d88fff700b13 (diff)
client to server conversion
Diffstat (limited to 'session.go')
-rw-r--r--session.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/session.go b/session.go
new file mode 100644
index 0000000..3478802
--- /dev/null
+++ b/session.go
@@ -0,0 +1,90 @@
+package main
+
+
+import (
+ "fmt"
+ "net/http"
+ "bufio"
+ "os"
+ "strings"
+ "github.com/gomodule/redigo/redis"
+)
+
+var cache redis.Conn
+
+func InitCache() {
+ conn, err := redis.DialURL("redis://localhost")
+ if err != nil {
+ panic(err)
+ }
+ cache = conn
+}
+
+func CheckSession(w http.ResponseWriter, r *http.Request) (interface{}, error){
+
+ c, err := r.Cookie("session_token")
+
+ if err != nil {
+ if err == http.ErrNoCookie {
+ w.WriteHeader(http.StatusUnauthorized)
+ return nil, err
+ }
+
+ w.WriteHeader(http.StatusBadRequest)
+ return nil, err
+ }
+
+ sessionToken := c.Value
+
+ response, err := cache.Do("GET", sessionToken)
+
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return nil, err
+ }
+ if response == nil {
+ w.WriteHeader(http.StatusUnauthorized)
+ return nil, err
+ }
+
+ return response, nil
+}
+
+func GetClientKey() string{
+ file, err := os.Open("clientkey")
+
+ CheckError(err, "could not open client key in file")
+
+ defer file.Close()
+
+ scanner := bufio.NewScanner(file)
+ var line string
+ for scanner.Scan() {
+ line = fmt.Sprintf("%s", scanner.Text())
+ }
+
+ return line
+}
+
+func GetPasswordFromSession(r *http.Request) (string, string) {
+
+ c, err := r.Cookie("session_token")
+
+ if err != nil {
+ return "", ""
+ }
+
+ sessionToken := c.Value
+
+ response, err := cache.Do("GET", sessionToken)
+
+ if CheckError(err, "could not get session from cache") != nil {
+ return "", ""
+ }
+
+ token := fmt.Sprintf("%s", response)
+
+ parts := strings.Split(token, "|")
+
+ return parts[0], parts[1]
+}