260706_212356
This commit is contained in:
parent
b9ea10d87f
commit
31afdd2aee
2
go.mod
2
go.mod
@ -3,6 +3,8 @@ module git.makemore.cloud/mjbow/moira_db
|
|||||||
go 1.26.4
|
go 1.26.4
|
||||||
|
|
||||||
require (
|
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/gjson v1.19.0 // indirect
|
||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
github.com/tidwall/pretty v1.2.0 // indirect
|
github.com/tidwall/pretty v1.2.0 // indirect
|
||||||
|
|||||||
4
go.sum
4
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 h1:xwxm7n691Uf3u5OFjzngavjGTh55KX5q/9w9xHW88JU=
|
||||||
github.com/tidwall/gjson v1.19.0/go.mod h1:V37/opeE/JbLUOfH0QTXiNez2l0RUjYUhpT4szFQAfc=
|
github.com/tidwall/gjson v1.19.0/go.mod h1:V37/opeE/JbLUOfH0QTXiNez2l0RUjYUhpT4szFQAfc=
|
||||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
|||||||
40
moira_db.go
40
moira_db.go
@ -20,6 +20,7 @@ import (
|
|||||||
"context" // <- TODO: what's this??
|
"context" // <- TODO: what's this??
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
//"path/filepath"
|
//"path/filepath"
|
||||||
//"regexp"
|
//"regexp"
|
||||||
//"strings"
|
//"strings"
|
||||||
@ -31,6 +32,8 @@ import (
|
|||||||
var templates *template.Template = template.New("")
|
var templates *template.Template = template.New("")
|
||||||
var client = &http.Client{Timeout: 2*time.Second}
|
var client = &http.Client{Timeout: 2*time.Second}
|
||||||
|
|
||||||
|
var session_db = sessions.NewCookieStore([]byte(os.Getenv("SESSIONS_SECRET")))
|
||||||
|
|
||||||
|
|
||||||
type UserInfo struct{
|
type UserInfo struct{
|
||||||
ProviderCode, // DC--Discord, GG--Google, GH--GitHub, LC--local database
|
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 }
|
if err != nil{ return }
|
||||||
|
|
||||||
ret_cookie := &http.Cookie{
|
ret_cookie := &http.Cookie{
|
||||||
Name: "oauth_state",
|
Name: "moira_db_oauth_state",
|
||||||
Value: state,
|
Value: state,
|
||||||
Path: "/",
|
Path: "/",
|
||||||
HttpOnly: true,
|
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){
|
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 err != nil{ return }
|
||||||
if cookie.Value != req.URL.Query().Get("state"){
|
if cookie.Value != req.URL.Query().Get("state"){
|
||||||
err = errors.New("oauth2_dc: csrf test failed")
|
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{
|
type Film struct{
|
||||||
Title_orig,
|
Title_orig,
|
||||||
Title_EN,
|
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{
|
if fileName, ok := template_renders[req.URL.Path]; ok{
|
||||||
fmt.Println("[srv] template", req.URL.Path, fileName.Template)
|
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 := req.URL.Query()
|
||||||
url_query_id := url_query.Get("id")
|
url_query_id := url_query.Get("id")
|
||||||
var context any
|
var context any
|
||||||
@ -394,6 +429,7 @@ func hdlr_film_parseMsg(resp http.ResponseWriter, req *http.Request){
|
|||||||
func main(){
|
func main(){
|
||||||
fmt.Println("CLIENT_ID:", oauth2_dc_config.ClientID);
|
fmt.Println("CLIENT_ID:", oauth2_dc_config.ClientID);
|
||||||
fmt.Println("CLIENT_SECRET:", oauth2_dc_config.ClientSecret);
|
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/Spirited+Away%3A+W+krainie+Bog%C3%B3w-2001-33288")
|
||||||
//t_film, _ := fetch_filmInfo("https://www.filmweb.pl/film/Pulp+Fiction-1994-1039")
|
//t_film, _ := fetch_filmInfo("https://www.filmweb.pl/film/Pulp+Fiction-1994-1039")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user