dispatch/server/auth.go

82 lines
1.5 KiB
Go
Raw Normal View History

package server
import (
"log"
"net/http"
"github.com/khlieng/dispatch/pkg/session"
"github.com/khlieng/dispatch/storage"
)
func (d *Dispatch) handleAuth(w http.ResponseWriter, r *http.Request, createUser, refresh bool) *State {
var state *State
cookie, err := r.Cookie(session.CookieName)
if err != nil {
if createUser {
state, err = d.newUser(w, r)
if err != nil {
log.Println(err)
}
}
} else {
session := d.states.getSession(cookie.Value)
2018-04-26 19:32:21 +00:00
if session != nil {
key := session.Key()
if !session.Expired() {
state = d.states.get(session.UserID)
if refresh {
newKey, err := session.Refresh()
if err != nil {
log.Println(err)
}
if newKey != "" {
d.states.setSession(session)
d.states.deleteSession(key)
session.SetCookie(w, r)
}
}
} else {
d.states.deleteSession(key)
}
}
if state != nil {
log.Println(r.RemoteAddr, "[Auth] GET", r.URL.Path, "| Valid token | User ID:", state.user.ID)
} else if createUser {
state, err = d.newUser(w, r)
if err != nil {
log.Println(err)
}
}
}
return state
}
func (d *Dispatch) newUser(w http.ResponseWriter, r *http.Request) (*State, error) {
user, err := storage.NewUser(d.Store)
if err != nil {
return nil, err
}
log.Println(r.RemoteAddr, "[Auth] New anonymous user | ID:", user.ID)
session, err := session.New(user.ID)
if err != nil {
return nil, err
}
d.states.setSession(session)
state := NewState(user, d)
d.states.set(state)
go state.run()
session.SetCookie(w, r)
return state, nil
}