Display user, logout

This commit is contained in:
Meutel 2017-09-08 18:13:22 +02:00
parent a873c0df36
commit 2909754be4
3 changed files with 54 additions and 17 deletions

View File

@ -45,6 +45,8 @@ const (
URLIndex = "/" URLIndex = "/"
// URLLogin url of login page (OAuth 2) // URLLogin url of login page (OAuth 2)
URLLogin = "/login" URLLogin = "/login"
// URLLogout url of logout page
URLLogout = "/logout"
// URLCallback url of OAuth callback // URLCallback url of OAuth callback
URLCallback = "/callback" URLCallback = "/callback"
// URLBooks url of books page // URLBooks url of books page
@ -163,11 +165,17 @@ type Model struct {
Title string Title string
Page string Page string
Version string Version string
Username string
} }
// NewModel constuctor for Model // NewModel constuctor for Model
func NewModel(title, page string) *Model { func (app *Bouquins) NewModel(title, page string, req *http.Request) *Model {
return &Model{title, page, Version} return &Model{
Title: title,
Page: page,
Version: Version,
Username: app.Username(req),
}
} }
// IndexModel is the model for index page // IndexModel is the model for index page
@ -177,13 +185,13 @@ type IndexModel struct {
} }
// NewIndexModel constructor IndexModel // NewIndexModel constructor IndexModel
func NewIndexModel(title string, count int64) *IndexModel { func (app *Bouquins) NewIndexModel(title string, count int64, req *http.Request) *IndexModel {
return &IndexModel{*NewModel(title, "index"), count} return &IndexModel{*app.NewModel(title, "index", req), count}
} }
// NewSearchModel constuctor for search page // NewSearchModel constuctor for search page
func NewSearchModel() *Model { func (app *Bouquins) NewSearchModel(req *http.Request) *Model {
return NewModel("Recherche", "search") return app.NewModel("Recherche", "search", req)
} }
// ResultsModel is a generic model for list pages // ResultsModel is a generic model for list pages
@ -277,6 +285,12 @@ func TemplatesFunc(prod bool) *template.Template {
}) })
} }
// RedirectHome redirects to home page
func RedirectHome(res http.ResponseWriter, req *http.Request) error {
http.Redirect(res, req, "/", http.StatusTemporaryRedirect)
return nil
}
// generates a 16 characters long random string // generates a 16 characters long random string
func securedRandString() string { func securedRandString() string {
b := make([]byte, 16) b := make([]byte, 16)
@ -292,6 +306,15 @@ func (app *Bouquins) Session(req *http.Request) *sessions.Session {
return session return session
} }
// logged in username
func (app *Bouquins) Username(req *http.Request) string {
username := app.Session(req).Values[sessionUser]
if username != nil {
return username.(string)
}
return ""
}
// sets value in session // sets value in session
func (app *Bouquins) SessionSet(name string, value string, res http.ResponseWriter, req *http.Request) { func (app *Bouquins) SessionSet(name string, value string, res http.ResponseWriter, req *http.Request) {
session := app.Session(req) session := app.Session(req)
@ -414,7 +437,7 @@ func (app *Bouquins) bookPage(idParam string, res http.ResponseWriter, req *http
if err != nil { if err != nil {
return err return err
} }
return app.render(res, tplBooks, &BookModel{*NewModel(book.Title, "book"), book}) return app.render(res, tplBooks, &BookModel{*app.NewModel(book.Title, "book", req), book})
} }
func (app *Bouquins) authorPage(idParam string, res http.ResponseWriter, req *http.Request) error { func (app *Bouquins) authorPage(idParam string, res http.ResponseWriter, req *http.Request) error {
id, err := strconv.Atoi(idParam) id, err := strconv.Atoi(idParam)
@ -425,7 +448,7 @@ func (app *Bouquins) authorPage(idParam string, res http.ResponseWriter, req *ht
if err != nil { if err != nil {
return err return err
} }
return app.render(res, tplAuthors, &AuthorModel{*NewModel(author.Name, "author"), author}) return app.render(res, tplAuthors, &AuthorModel{*app.NewModel(author.Name, "author", req), author})
} }
func (app *Bouquins) seriePage(idParam string, res http.ResponseWriter, req *http.Request) error { func (app *Bouquins) seriePage(idParam string, res http.ResponseWriter, req *http.Request) error {
id, err := strconv.Atoi(idParam) id, err := strconv.Atoi(idParam)
@ -436,7 +459,7 @@ func (app *Bouquins) seriePage(idParam string, res http.ResponseWriter, req *htt
if err != nil { if err != nil {
return err return err
} }
return app.render(res, tplSeries, &SeriesModel{*NewModel(series.Name, "series"), series}) return app.render(res, tplSeries, &SeriesModel{*app.NewModel(series.Name, "series", req), series})
} }
// ROUTES // // ROUTES //
@ -458,12 +481,12 @@ func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) erro
// SearchPage displays search form and results // SearchPage displays search form and results
func (app *Bouquins) SearchPage(res http.ResponseWriter, req *http.Request) error { func (app *Bouquins) SearchPage(res http.ResponseWriter, req *http.Request) error {
return app.render(res, tplSearch, NewSearchModel()) return app.render(res, tplSearch, app.NewSearchModel(req))
} }
// AboutPage displays about page // AboutPage displays about page
func (app *Bouquins) AboutPage(res http.ResponseWriter, req *http.Request) error { func (app *Bouquins) AboutPage(res http.ResponseWriter, req *http.Request) error {
return app.render(res, tplAbout, NewModel("A propos", "about")) return app.render(res, tplAbout, app.NewModel("A propos", "about", req))
} }
// LoginPage redirects to OAuth login page (github) // LoginPage redirects to OAuth login page (github)
@ -476,12 +499,19 @@ func (app *Bouquins) LoginPage(res http.ResponseWriter, req *http.Request) error
return nil return nil
} }
// LogoutPage logout connected user
func (app *Bouquins) LogoutPage(res http.ResponseWriter, req *http.Request) error {
app.SessionSet(sessionUser, "", res, req)
return RedirectHome(res, req)
}
// CallbackPage handle OAuth 2 callback // CallbackPage handle OAuth 2 callback
func (app *Bouquins) CallbackPage(res http.ResponseWriter, req *http.Request) error { func (app *Bouquins) CallbackPage(res http.ResponseWriter, req *http.Request) error {
savedState := app.Session(req).Values[sessionOAuthState] savedState := app.Session(req).Values[sessionOAuthState]
if savedState == "" { if savedState == "" {
return fmt.Errorf("missing saved oauth state") return fmt.Errorf("missing saved oauth state")
} }
app.SessionSet(sessionOAuthState, "", res, req)
state := req.FormValue("state") state := req.FormValue("state")
if state != savedState { if state != savedState {
return fmt.Errorf("invalid oauth state, expected '%s', got '%s'", "state", state) return fmt.Errorf("invalid oauth state, expected '%s', got '%s'", "state", state)
@ -521,8 +551,7 @@ func (app *Bouquins) CallbackPage(res http.ResponseWriter, req *http.Request) er
if userEmail == "meutel+github@meutel.net" { if userEmail == "meutel+github@meutel.net" {
app.SessionSet(sessionUser, "Meutel", res, req) app.SessionSet(sessionUser, "Meutel", res, req)
log.Println("User logged in", userEmail) log.Println("User logged in", userEmail)
http.Redirect(res, req, "/", http.StatusTemporaryRedirect) return RedirectHome(res, req)
return nil
} else { } else {
return fmt.Errorf("Unknown user") return fmt.Errorf("Unknown user")
} }
@ -535,7 +564,7 @@ func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) error
if err != nil { if err != nil {
return err return err
} }
model := NewIndexModel("", count) model := app.NewIndexModel("", count, req)
if isJSON(req) { if isJSON(req) {
return writeJSON(res, model) return writeJSON(res, model)
} }

View File

@ -113,6 +113,7 @@ func handleURL(url string, f func(res http.ResponseWriter, req *http.Request) er
func router(app *bouquins.Bouquins) { func router(app *bouquins.Bouquins) {
handleURL(bouquins.URLIndex, app.IndexPage) handleURL(bouquins.URLIndex, app.IndexPage)
handleURL(bouquins.URLLogin, app.LoginPage) handleURL(bouquins.URLLogin, app.LoginPage)
handleURL(bouquins.URLLogout, app.LogoutPage)
handleURL(bouquins.URLCallback, app.CallbackPage) handleURL(bouquins.URLCallback, app.CallbackPage)
handleURL(bouquins.URLBooks, app.BooksPage) handleURL(bouquins.URLBooks, app.BooksPage)
handleURL(bouquins.URLAuthors, app.AuthorsPage) handleURL(bouquins.URLAuthors, app.AuthorsPage)

View File

@ -23,5 +23,12 @@
<input name="q" type="text" class="form-control" placeholder="Recherche"> <input name="q" type="text" class="form-control" placeholder="Recherche">
</div> </div>
</form> </form>
<ul class="nav navbar-nav navbar-right">
{{ if .Username }}
<li><a href="/logout">{{ .Username }} <span title="Déconnexion" class="glyphicon glyphicon-log-out"></span></a></li>
{{ else }}
<li><a href="/login">Connexion <span class="glyphicon glyphicon-log-in"></span></a></li>
{{ end }}
</ul>
</div> </div>
</nav> </nav>