260706_220853
This commit is contained in:
parent
31afdd2aee
commit
760bb7e3a3
59
moira_db.go
59
moira_db.go
@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
|
"github.com/gorilla/securecookie"
|
||||||
//"path/filepath"
|
//"path/filepath"
|
||||||
//"regexp"
|
//"regexp"
|
||||||
//"strings"
|
//"strings"
|
||||||
@ -47,6 +48,12 @@ type UserInfo struct{
|
|||||||
UpdatedTS int64 // UNIX timestamps
|
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{
|
var oauth2_dc_config = &oauth2.Config{
|
||||||
ClientID: os.Getenv("MOIRA_OAUTH2_DC_CLI_ID"),
|
ClientID: os.Getenv("MOIRA_OAUTH2_DC_CLI_ID"),
|
||||||
ClientSecret: os.Getenv("MOIRA_OAUTH2_DC_CLI_SECRET"),
|
ClientSecret: os.Getenv("MOIRA_OAUTH2_DC_CLI_SECRET"),
|
||||||
@ -154,9 +161,58 @@ func oauth2_hdlr_clbk(resp http.ResponseWriter, req *http.Request){
|
|||||||
new_userInfo, err := oauth2_dcUserInfo_to_UserInfo(resp_text)
|
new_userInfo, err := oauth2_dcUserInfo_to_UserInfo(resp_text)
|
||||||
if err != nil { return } // TODO: should the error be reported somehow?
|
if err != nil { return } // TODO: should the error be reported somehow?
|
||||||
|
|
||||||
|
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, "", " ")
|
t_json, _ := json.MarshalIndent(new_userInfo, "", " ")
|
||||||
fmt.Printf("%+v\n", string(t_json))
|
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)
|
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"]
|
val := session.Values["user"]
|
||||||
if val == ""{
|
if val == "" || val == nil{
|
||||||
fmt.Println("empty user")
|
fmt.Println("empty user")
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//if val not in user_db: false,nil
|
//if val not in user_db: false,nil
|
||||||
|
fmt.Println(val)
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user