From 31afdd2aeed93ac9143ebe6d386c110da03aab81 Mon Sep 17 00:00:00 2001 From: Maciej Bowszys Date: Mon, 6 Jul 2026 21:23:56 +0200 Subject: [PATCH] 260706_212356 --- go.mod | 2 ++ go.sum | 4 ++++ moira_db.go | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index c9d57ce..caf8e91 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module git.makemore.cloud/mjbow/moira_db go 1.26.4 require ( + github.com/gorilla/securecookie v1.1.2 // indirect + github.com/gorilla/sessions v1.4.0 // indirect github.com/tidwall/gjson v1.19.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect diff --git a/go.sum b/go.sum index 6808ace..3384bb3 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= +github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= +github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ= +github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik= github.com/tidwall/gjson v1.19.0 h1:xwxm7n691Uf3u5OFjzngavjGTh55KX5q/9w9xHW88JU= github.com/tidwall/gjson v1.19.0/go.mod h1:V37/opeE/JbLUOfH0QTXiNez2l0RUjYUhpT4szFQAfc= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= diff --git a/moira_db.go b/moira_db.go index 6edcd6a..36f74f6 100644 --- a/moira_db.go +++ b/moira_db.go @@ -20,6 +20,7 @@ import ( "context" // <- TODO: what's this?? "golang.org/x/oauth2" + "github.com/gorilla/sessions" //"path/filepath" //"regexp" //"strings" @@ -31,6 +32,8 @@ import ( var templates *template.Template = template.New("") var client = &http.Client{Timeout: 2*time.Second} +var session_db = sessions.NewCookieStore([]byte(os.Getenv("SESSIONS_SECRET"))) + type UserInfo struct{ ProviderCode, // DC--Discord, GG--Google, GH--GitHub, LC--local database @@ -69,7 +72,7 @@ func oauth2_dc_genLoginURL() (_ string, _ *http.Cookie, err error){ if err != nil{ return } ret_cookie := &http.Cookie{ - Name: "oauth_state", + Name: "moira_db_oauth_state", Value: state, Path: "/", HttpOnly: true, @@ -82,7 +85,7 @@ func oauth2_dc_genLoginURL() (_ string, _ *http.Cookie, err error){ } func oauth2_dc_exchangeReceivedCode(req *http.Request) (_ *http.Client, err error){ - cookie, err := req.Cookie("oauth_state") + cookie, err := req.Cookie("moira_db_oauth_state") if err != nil{ return } if cookie.Value != req.URL.Query().Get("state"){ err = errors.New("oauth2_dc: csrf test failed") @@ -158,6 +161,35 @@ func oauth2_hdlr_clbk(resp http.ResponseWriter, req *http.Request){ } +func sessions_is_loggedIn(resp http.ResponseWriter, req *http.Request) (ret bool, err error){ + session, err := session_db.Get(req, "moira_db_session_main") + if err != nil{ + fmt.Println(err.Error()) + return + } + + if session.IsNew{ + err = session.Save(req, resp) + if err != nil{ + fmt.Println(err.Error()) + return + } + + fmt.Println("new session") + return false, nil + } + + val := session.Values["user"] + if val == ""{ + fmt.Println("empty user") + return false, nil + } + + //if val not in user_db: false,nil + return true, nil +} + + type Film struct{ Title_orig, Title_EN, @@ -339,6 +371,9 @@ func hdlr_root(template_renders map[string]struct{Template string;Context *map[s if fileName, ok := template_renders[req.URL.Path]; ok{ fmt.Println("[srv] template", req.URL.Path, fileName.Template) + is_loggedIn, _ := sessions_is_loggedIn(resp, req) + fmt.Println(">>LoggedIn?:", is_loggedIn) + url_query := req.URL.Query() url_query_id := url_query.Get("id") var context any @@ -394,6 +429,7 @@ func hdlr_film_parseMsg(resp http.ResponseWriter, req *http.Request){ func main(){ fmt.Println("CLIENT_ID:", oauth2_dc_config.ClientID); fmt.Println("CLIENT_SECRET:", oauth2_dc_config.ClientSecret); + fmt.Println("SESSIONS_SECRET:", os.Getenv("SESSIONS_SECRET")); //t_film, _ := fetch_filmInfo("https://www.filmweb.pl/film/Spirited+Away%3A+W+krainie+Bog%C3%B3w-2001-33288") //t_film, _ := fetch_filmInfo("https://www.filmweb.pl/film/Pulp+Fiction-1994-1039")