Improved oauth provide config
This commit is contained in:
parent
5e0de4041c
commit
bd3308f9a6
@ -39,6 +39,7 @@ func (app *Bouquins) NewLoginModel(req *http.Request) *LoginModel {
|
|||||||
// OAuth2Provider allows to get a user from an OAuth2 token
|
// OAuth2Provider allows to get a user from an OAuth2 token
|
||||||
type OAuth2Provider interface {
|
type OAuth2Provider interface {
|
||||||
GetUser(token *oauth2.Token) (string, error)
|
GetUser(token *oauth2.Token) (string, error)
|
||||||
|
Config(conf *BouquinsConf) *oauth2.Config
|
||||||
Name() string
|
Name() string
|
||||||
Label() string
|
Label() string
|
||||||
Icon() string
|
Icon() string
|
||||||
|
@ -65,6 +65,14 @@ const (
|
|||||||
URLCalibre = "/calibre/"
|
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
|
// Bouquins contains application common resources: templates, database
|
||||||
type Bouquins struct {
|
type Bouquins struct {
|
||||||
Tpl *template.Template
|
Tpl *template.Template
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/oauth2/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GithubProvider implements OAuth2 client with github.com
|
// GithubProvider implements OAuth2 client with github.com
|
||||||
@ -38,6 +39,16 @@ func (p GithubProvider) Icon() string {
|
|||||||
return "" // TODO
|
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
|
// GetUser returns github primary email
|
||||||
func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) {
|
func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) {
|
||||||
apiReq, err := http.NewRequest("GET", "https://api.github.com/user/emails", nil)
|
apiReq, err := http.NewRequest("GET", "https://api.github.com/user/emails", nil)
|
||||||
|
24
main.go
24
main.go
@ -8,7 +8,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/github"
|
|
||||||
|
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
@ -16,19 +15,11 @@ import (
|
|||||||
"meutel.net/meutel/go-bouquins/bouquins"
|
"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
|
var db *sql.DB
|
||||||
|
|
||||||
// ReadConfig loads configuration file and initialize default value
|
// ReadConfig loads configuration file and initialize default value
|
||||||
func ReadConfig() (*BouquinsConf, error) {
|
func ReadConfig() (*bouquins.BouquinsConf, error) {
|
||||||
conf := new(BouquinsConf)
|
conf := new(bouquins.BouquinsConf)
|
||||||
confPath := "bouquins.json"
|
confPath := "bouquins.json"
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
confPath = os.Args[1]
|
confPath = os.Args[1]
|
||||||
@ -54,7 +45,7 @@ func ReadConfig() (*BouquinsConf, error) {
|
|||||||
return conf, err
|
return conf, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func initApp() *BouquinsConf {
|
func initApp() *bouquins.BouquinsConf {
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
conf, err := ReadConfig()
|
conf, err := ReadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -70,15 +61,10 @@ func initApp() *BouquinsConf {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO conf by provider, client ID and secret in conf file
|
|
||||||
oauthConf := make(map[string]*oauth2.Config)
|
oauthConf := make(map[string]*oauth2.Config)
|
||||||
oauthConf["github"] = &oauth2.Config{
|
for _, provider := range bouquins.Providers {
|
||||||
ClientID: "8b0aedf07828f06918a0",
|
oauthConf[provider.Name()] = provider.Config(conf)
|
||||||
ClientSecret: "eb26ec9c986fc28bd169bdddf169b794861e0d65",
|
|
||||||
Scopes: []string{"user:email"},
|
|
||||||
Endpoint: github.Endpoint,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME constructor, conf cookies secret
|
// FIXME constructor, conf cookies secret
|
||||||
app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf, Cookies: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK"))}
|
app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf, Cookies: sessions.NewCookieStore([]byte("flQ6QzM/c3Jtdl9ycDx6OXRIfFgK"))}
|
||||||
err = app.PrepareAll()
|
err = app.PrepareAll()
|
||||||
|
Loading…
Reference in New Issue
Block a user