Improved oauth provide config

This commit is contained in:
Meutel 2017-09-09 09:34:19 +02:00
parent 5e0de4041c
commit bd3308f9a6
4 changed files with 25 additions and 19 deletions

View File

@ -39,6 +39,7 @@ func (app *Bouquins) NewLoginModel(req *http.Request) *LoginModel {
// OAuth2Provider allows to get a user from an OAuth2 token
type OAuth2Provider interface {
GetUser(token *oauth2.Token) (string, error)
Config(conf *BouquinsConf) *oauth2.Config
Name() string
Label() string
Icon() string

View File

@ -65,6 +65,14 @@ const (
URLCalibre = "/calibre/"
)
// BouquinsConf App configuration
type BouquinsConf struct {
BindAddress string `json:"bind-address"`
DbPath string `json:"db-path"`
CalibrePath string `json:"calibre-path"`
Prod bool `json:"prod"`
}
// Bouquins contains application common resources: templates, database
type Bouquins struct {
Tpl *template.Template

View File

@ -7,6 +7,7 @@ import (
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)
// GithubProvider implements OAuth2 client with github.com
@ -38,6 +39,16 @@ func (p GithubProvider) Icon() string {
return "" // TODO
}
func (p GithubProvider) Config(conf *BouquinsConf) *oauth2.Config {
// FIXME client ID and secret in conf file
return &oauth2.Config{
ClientID: "8b0aedf07828f06918a0",
ClientSecret: "eb26ec9c986fc28bd169bdddf169b794861e0d65",
Scopes: []string{"user:email"},
Endpoint: github.Endpoint,
}
}
// GetUser returns github primary email
func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) {
apiReq, err := http.NewRequest("GET", "https://api.github.com/user/emails", nil)

24
main.go
View File

@ -8,7 +8,6 @@ import (
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
"github.com/gorilla/sessions"
_ "github.com/mattn/go-sqlite3"
@ -16,19 +15,11 @@ import (
"meutel.net/meutel/go-bouquins/bouquins"
)
// BouquinsConf App configuration
type BouquinsConf struct {
BindAddress string `json:"bind-address"`
DbPath string `json:"db-path"`
CalibrePath string `json:"calibre-path"`
Prod bool `json:"prod"`
}
var db *sql.DB
// ReadConfig loads configuration file and initialize default value
func ReadConfig() (*BouquinsConf, error) {
conf := new(BouquinsConf)
func ReadConfig() (*bouquins.BouquinsConf, error) {
conf := new(bouquins.BouquinsConf)
confPath := "bouquins.json"
if len(os.Args) > 1 {
confPath = os.Args[1]
@ -54,7 +45,7 @@ func ReadConfig() (*BouquinsConf, error) {
return conf, err
}
func initApp() *BouquinsConf {
func initApp() *bouquins.BouquinsConf {
log.SetFlags(log.LstdFlags | log.Lshortfile)
conf, err := ReadConfig()
if err != nil {
@ -70,15 +61,10 @@ func initApp() *BouquinsConf {
log.Fatalln(err)
}
// TODO conf by provider, client ID and secret in conf file
oauthConf := make(map[string]*oauth2.Config)
oauthConf["github"] = &oauth2.Config{
ClientID: "8b0aedf07828f06918a0",
ClientSecret: "eb26ec9c986fc28bd169bdddf169b794861e0d65",
Scopes: []string{"user:email"},
Endpoint: github.Endpoint,
for _, provider := range bouquins.Providers {
oauthConf[provider.Name()] = provider.Config(conf)
}
// FIXME constructor, conf cookies secret
app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf, Cookies: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK"))}
err = app.PrepareAll()