|
|
|
|
@@ -33,6 +33,10 @@ const authCookieName = "auth"
|
|
|
|
|
const loginPath = "/login"
|
|
|
|
|
type contextKey string
|
|
|
|
|
|
|
|
|
|
func getContextSession(ctx context.Context) sessions.SessionData {
|
|
|
|
|
return ctx.Value(contextKey("session")).(sessions.SessionData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checks sessionId from cookie
|
|
|
|
|
// when non-zero session found - pass to next http.Hander
|
|
|
|
|
// when no session available - render same as login page and redirect to /
|
|
|
|
|
@@ -168,35 +172,88 @@ func joinRoomHandler( templateFs *embed.FS,
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rn := r.PostFormValue("roomName")
|
|
|
|
|
rp := r.PostFormValue("roomPassword")
|
|
|
|
|
pn := r.PostFormValue("personalName")
|
|
|
|
|
pp := r.PostFormValue("personalPassword")
|
|
|
|
|
roomName := r.PostFormValue("roomName")
|
|
|
|
|
roomPass := r.PostFormValue("roomPassword")
|
|
|
|
|
personName := r.PostFormValue("personalName")
|
|
|
|
|
personPass := r.PostFormValue("personalPassword")
|
|
|
|
|
|
|
|
|
|
room, _, err := roomsM.Get(rn)
|
|
|
|
|
// a) get room data
|
|
|
|
|
room, _, err := roomsM.Get(roomName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("/login/submit error getting room %s", rn)
|
|
|
|
|
// return i guess
|
|
|
|
|
log.Printf("/login/join error getting room %s", roomName)
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
// TODO render error to be put in error place
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("/login/submit found room %+v", room)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
roomId := "room-name-actually" // would be taken from rooms interface from redis
|
|
|
|
|
// would be either taken from room info on correct person pass or created
|
|
|
|
|
personId := 111
|
|
|
|
|
id, err := sessionSM.Save(roomId, personId)
|
|
|
|
|
// b) check if room password OK
|
|
|
|
|
if room.PasswordHash != roomPass {
|
|
|
|
|
log.Printf("/login/join bad room pass for %+v", room)
|
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
|
// TODO render error to be put in error place
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var person rooms.Person
|
|
|
|
|
for _, participant := range room.Paricipants {
|
|
|
|
|
if participant.Name == personName {
|
|
|
|
|
person = participant
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// c) check if such person exists,
|
|
|
|
|
// knownPerson, found :=
|
|
|
|
|
// check the password
|
|
|
|
|
if (person != rooms.Person{}) && person.PasswordHash != personPass {
|
|
|
|
|
log.Printf("/login/join bad person pass for %+s", person.Name)
|
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
|
// TODO render error to be put in error place
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// person joining for thethe first time
|
|
|
|
|
if (person == rooms.Person{}) {
|
|
|
|
|
log.Printf("/login/join room pass correct, new person joins")
|
|
|
|
|
// creating a new person with provided password hash
|
|
|
|
|
person = rooms.Person{
|
|
|
|
|
Name: personName,
|
|
|
|
|
PasswordHash: personPass,
|
|
|
|
|
PersonId: rand.Int(),
|
|
|
|
|
}
|
|
|
|
|
err := roomsM.Update(context.TODO(), room.Name, func(fromRoom rooms.Room) (toRoom rooms.Room) {
|
|
|
|
|
toRoom = fromRoom
|
|
|
|
|
toRoom.Paricipants = append(toRoom.Paricipants, person)
|
|
|
|
|
return toRoom
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("/login/join problem adding person to room", person.Name)
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
// TODO render error to be put in error place
|
|
|
|
|
// with message try again
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// TODO handle context and cancells, with separate function that writeds new updated room
|
|
|
|
|
// now we have room and person, can create a session
|
|
|
|
|
// and we've checked password
|
|
|
|
|
|
|
|
|
|
newSessionId, err := sessionSM.Save(room.Name, person.PersonId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("/login/submit > error saving session %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "is is %d. room things %s & %s, personal things %s and %s. \n found room %+v", id, rn, rp, pn, pp, room)
|
|
|
|
|
// i suppose here i'll need to
|
|
|
|
|
// a) check if room password OK
|
|
|
|
|
// b) get room data
|
|
|
|
|
// c) check if such person exists,
|
|
|
|
|
// either create one, or check password
|
|
|
|
|
// d) how should i monitor sessions? - space in redis
|
|
|
|
|
// so save session to redis and add cookie with sessionId
|
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
|
Name: authCookieName,
|
|
|
|
|
Value: fmt.Sprint(newSessionId),
|
|
|
|
|
Secure: true,
|
|
|
|
|
HttpOnly: true,
|
|
|
|
|
Path: "/",
|
|
|
|
|
})
|
|
|
|
|
log.Printf("is is %d. room things %s & %s, personal things %s and %s. \n found room %+v",
|
|
|
|
|
newSessionId, roomName, roomPass, personName, personPass, room,
|
|
|
|
|
)
|
|
|
|
|
// TODO render what? index page with some data passed?
|
|
|
|
|
// or, what? i could just redirect to / for now
|
|
|
|
|
w.Header().Add("HX-Redirect", "/")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|