Login page
hardcoded secret
This commit is contained in:
parent
73ba1f81c7
commit
dfae92a039
@ -12,6 +12,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
|
||||||
"github.com/c2h5oh/datasize"
|
"github.com/c2h5oh/datasize"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,6 +36,8 @@ const (
|
|||||||
|
|
||||||
// URLIndex url of index page
|
// URLIndex url of index page
|
||||||
URLIndex = "/"
|
URLIndex = "/"
|
||||||
|
// URLLogin url of login page (OAuth 2)
|
||||||
|
URLLogin = "/login"
|
||||||
// URLBooks url of books page
|
// URLBooks url of books page
|
||||||
URLBooks = "/books/"
|
URLBooks = "/books/"
|
||||||
// URLAuthors url of authors page
|
// URLAuthors url of authors page
|
||||||
@ -58,6 +62,7 @@ const (
|
|||||||
type Bouquins struct {
|
type Bouquins struct {
|
||||||
Tpl *template.Template
|
Tpl *template.Template
|
||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
|
OAuthConf *oauth2.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// Series is a book series.
|
// 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"))
|
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
|
// IndexPage displays index page: list of books/authors/series
|
||||||
func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) error {
|
func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) error {
|
||||||
count, err := app.BookCount()
|
count, err := app.BookCount()
|
||||||
|
14
main.go
14
main.go
@ -7,6 +7,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/oauth2/github"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
||||||
"meutel.net/meutel/go-bouquins/bouquins"
|
"meutel.net/meutel/go-bouquins/bouquins"
|
||||||
@ -65,7 +68,15 @@ func initApp() *BouquinsConf {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
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()
|
err = app.PrepareAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
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) {
|
func router(app *bouquins.Bouquins) {
|
||||||
handleURL(bouquins.URLIndex, app.IndexPage)
|
handleURL(bouquins.URLIndex, app.IndexPage)
|
||||||
|
handleURL(bouquins.URLLogin, app.LoginPage)
|
||||||
handleURL(bouquins.URLBooks, app.BooksPage)
|
handleURL(bouquins.URLBooks, app.BooksPage)
|
||||||
handleURL(bouquins.URLAuthors, app.AuthorsPage)
|
handleURL(bouquins.URLAuthors, app.AuthorsPage)
|
||||||
handleURL(bouquins.URLSeries, app.SeriesPage)
|
handleURL(bouquins.URLSeries, app.SeriesPage)
|
||||||
|
Loading…
Reference in New Issue
Block a user