From 760bb7e3a3c32c21d875137cdec048a08477d412 Mon Sep 17 00:00:00 2001 From: Maciej Bowszys Date: Mon, 6 Jul 2026 22:08:53 +0200 Subject: [PATCH] 260706_220853 --- moira_db.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/moira_db.go b/moira_db.go index 36f74f6..23f7b8b 100644 --- a/moira_db.go +++ b/moira_db.go @@ -21,6 +21,7 @@ import ( "golang.org/x/oauth2" "github.com/gorilla/sessions" + "github.com/gorilla/securecookie" //"path/filepath" //"regexp" //"strings" @@ -47,6 +48,12 @@ type UserInfo struct{ UpdatedTS int64 // UNIX timestamps } +type User struct{ + IntID string + UserInfos map[string]UserInfo // the string is the ProviderCode +} +var db_user = make(map[string]User) // the string is the IntID + var oauth2_dc_config = &oauth2.Config{ ClientID: os.Getenv("MOIRA_OAUTH2_DC_CLI_ID"), ClientSecret: os.Getenv("MOIRA_OAUTH2_DC_CLI_SECRET"), @@ -154,8 +161,57 @@ func oauth2_hdlr_clbk(resp http.ResponseWriter, req *http.Request){ new_userInfo, err := oauth2_dcUserInfo_to_UserInfo(resp_text) if err != nil { return } // TODO: should the error be reported somehow? - t_json, _ := json.MarshalIndent(new_userInfo, "", " ") - fmt.Printf("%+v\n", string(t_json)) + session, err := session_db.Get(req, "moira_db_session_main") + if err != nil{ + fmt.Println(err.Error()) + return + } + + user_found_in_db := false + for key,val := range db_user{ + if val_val,ok:=val.UserInfos[new_userInfo.ProviderCode]; ok{ + if val_val.ExtID == new_userInfo.ExtID { + // found the user in the DB! + session.Values["user"] = key + user_found_in_db = true + break + } + } + } + + if(!user_found_in_db){ + var new_user_intID string + new_user_intID_found := false + for range 100{ + new_user_intID = base64.RawURLEncoding.EncodeToString(securecookie.GenerateRandomKey(16)) + if _,ok:=db_user[new_user_intID]; !ok{ + new_user_intID_found = true + break + } + } + + if(!new_user_intID_found){ + fmt.Println("Whoops! 100 tries and no luck, take care of 'em:") + + t_json, _ := json.MarshalIndent(new_userInfo, "", " ") + fmt.Printf("%+v\n", string(t_json)) + + return + } + + db_user[new_user_intID] = User{ + IntID: new_user_intID, + UserInfos: map[string]UserInfo{new_userInfo.ProviderCode: new_userInfo}, + } + session.Values["user"] = new_user_intID + } + + err = session.Save(req, resp) + if err != nil{ + fmt.Println(err.Error()) + return + } + http.Redirect(resp, req, "/", http.StatusFound) } @@ -180,12 +236,13 @@ func sessions_is_loggedIn(resp http.ResponseWriter, req *http.Request) (ret bool } val := session.Values["user"] - if val == ""{ + if val == "" || val == nil{ fmt.Println("empty user") return false, nil } //if val not in user_db: false,nil + fmt.Println(val) return true, nil }