Login page

hardcoded secret
This commit is contained in:
Meutel 2017-09-08 12:02:54 +02:00
parent 73ba1f81c7
commit dfae92a039
2 changed files with 27 additions and 3 deletions

View File

@ -12,6 +12,8 @@ import (
"strconv"
"strings"
"golang.org/x/oauth2"
"github.com/c2h5oh/datasize"
)
@ -34,6 +36,8 @@ const (
// URLIndex url of index page
URLIndex = "/"
// URLLogin url of login page (OAuth 2)
URLLogin = "/login"
// URLBooks url of books page
URLBooks = "/books/"
// URLAuthors url of authors page
@ -56,8 +60,9 @@ const (
// Bouquins contains application common resources: templates, database
type Bouquins struct {
Tpl *template.Template
DB *sql.DB
Tpl *template.Template
DB *sql.DB
OAuthConf *oauth2.Config
}
// Series is a book series.
@ -422,6 +427,13 @@ func (app *Bouquins) AboutPage(res http.ResponseWriter, req *http.Request) error
return app.render(res, tplAbout, NewModel("A propos", "about"))
}
// LoginPage redirects to OAuth login page (github)
func (app *Bouquins) LoginPage(res http.ResponseWriter, req *http.Request) error {
url := app.OAuthConf.AuthCodeURL("state", oauth2.AccessTypeOffline)
http.Redirect(res, req, url, http.StatusTemporaryRedirect)
return nil
}
// IndexPage displays index page: list of books/authors/series
func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) error {
count, err := app.BookCount()

14
main.go
View File

@ -7,6 +7,9 @@ import (
"net/http"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
_ "github.com/mattn/go-sqlite3"
"meutel.net/meutel/go-bouquins/bouquins"
@ -65,7 +68,15 @@ func initApp() *BouquinsConf {
if err != nil {
log.Fatalln(err)
}
app := &bouquins.Bouquins{Tpl: tpl, DB: db}
oauthConf := &oauth2.Config{
ClientID: "8b0aedf07828f06918a0",
ClientSecret: "eb26ec9c986fc28bd169bdddf169b794861e0d65",
Scopes: []string{"user"},
Endpoint: github.Endpoint,
}
app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf}
err = app.PrepareAll()
if err != nil {
log.Fatalln(err)
@ -98,6 +109,7 @@ func handleURL(url string, f func(res http.ResponseWriter, req *http.Request) er
func router(app *bouquins.Bouquins) {
handleURL(bouquins.URLIndex, app.IndexPage)
handleURL(bouquins.URLLogin, app.LoginPage)
handleURL(bouquins.URLBooks, app.BooksPage)
handleURL(bouquins.URLAuthors, app.AuthorsPage)
handleURL(bouquins.URLSeries, app.SeriesPage)