This commit is contained in:
Meutel 2017-09-09 13:27:07 +02:00
parent 32a1ca955a
commit d844432641
5 changed files with 15 additions and 15 deletions

View File

@ -39,7 +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 Config(conf *Conf) *oauth2.Config
Name() string Name() string
Label() string Label() string
Icon() string Icon() string

View File

@ -65,14 +65,14 @@ const (
URLCalibre = "/calibre/" URLCalibre = "/calibre/"
) )
// BouquinsConf App configuration // Conf App configuration
type BouquinsConf struct { type Conf struct {
BindAddress string `json:"bind-address"` BindAddress string `json:"bind-address"`
DbPath string `json:"db-path"` DbPath string `json:"db-path"`
CalibrePath string `json:"calibre-path"` CalibrePath string `json:"calibre-path"`
Prod bool `json:"prod"` Prod bool `json:"prod"`
CookieSecret string `json:"cookie-secret"` CookieSecret string `json:"cookie-secret"`
ExternalUrl string `json:"external-url"` ExternalURL string `json:"external-url"`
ProvidersConf []ProviderConf `json:"providers"` ProvidersConf []ProviderConf `json:"providers"`
} }

View File

@ -39,7 +39,8 @@ func (p GithubProvider) Icon() string {
return "githubicon" return "githubicon"
} }
func (p GithubProvider) Config(conf *BouquinsConf) *oauth2.Config { // Config returns OAuth configuration for this provider
func (p GithubProvider) Config(conf *Conf) *oauth2.Config {
for _, c := range conf.ProvidersConf { for _, c := range conf.ProvidersConf {
if c.Name == p.Name() { if c.Name == p.Name() {
return &oauth2.Config{ return &oauth2.Config{

View File

@ -13,10 +13,10 @@ import (
// GoogleProvider implements OAuth2 client with google account // GoogleProvider implements OAuth2 client with google account
type GoogleProvider string type GoogleProvider string
type GoogleTokenInfo struct { type googleTokenInfo struct {
IssuedTo string `json:"issued_to"` IssuedTo string `json:"issued_to"`
Audience string `json:"audience"` Audience string `json:"audience"`
UserId string `json:"user_id"` UserID string `json:"user_id"`
Scope string `json:"scope"` Scope string `json:"scope"`
ExpiresIn int64 `json:"expires_in"` ExpiresIn int64 `json:"expires_in"`
Email string `json:"email"` Email string `json:"email"`
@ -43,7 +43,8 @@ func (p GoogleProvider) Icon() string {
return "googleicon" return "googleicon"
} }
func (p GoogleProvider) Config(conf *BouquinsConf) *oauth2.Config { // Config returns OAuth configuration for this provider
func (p GoogleProvider) Config(conf *Conf) *oauth2.Config {
for _, c := range conf.ProvidersConf { for _, c := range conf.ProvidersConf {
if c.Name == p.Name() { if c.Name == p.Name() {
return &oauth2.Config{ return &oauth2.Config{
@ -51,7 +52,7 @@ func (p GoogleProvider) Config(conf *BouquinsConf) *oauth2.Config {
ClientSecret: c.ClientSecret, ClientSecret: c.ClientSecret,
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"}, Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"},
Endpoint: google.Endpoint, Endpoint: google.Endpoint,
RedirectURL: conf.ExternalUrl + URLCallback, RedirectURL: conf.ExternalURL + URLCallback,
} }
} }
} }
@ -60,7 +61,6 @@ func (p GoogleProvider) Config(conf *BouquinsConf) *oauth2.Config {
// GetUser returns github primary email // GetUser returns github primary email
func (p GoogleProvider) GetUser(token *oauth2.Token) (string, error) { func (p GoogleProvider) GetUser(token *oauth2.Token) (string, error) {
// POST https://www.googleapis.com/oauth2/v2/tokeninfo access_token
apiRes, err := http.Post("https://www.googleapis.com/oauth2/v2/tokeninfo?access_token="+token.AccessToken, "application/json", nil) apiRes, err := http.Post("https://www.googleapis.com/oauth2/v2/tokeninfo?access_token="+token.AccessToken, "application/json", nil)
defer apiRes.Body.Close() defer apiRes.Body.Close()
if err != nil { if err != nil {
@ -68,7 +68,7 @@ func (p GoogleProvider) GetUser(token *oauth2.Token) (string, error) {
return "", fmt.Errorf("Authentification error") return "", fmt.Errorf("Authentification error")
} }
dec := json.NewDecoder(apiRes.Body) dec := json.NewDecoder(apiRes.Body)
var tokenInfo GoogleTokenInfo var tokenInfo googleTokenInfo
err = dec.Decode(&tokenInfo) err = dec.Decode(&tokenInfo)
if err != nil { if err != nil {
log.Println("Error reading google API response", err) log.Println("Error reading google API response", err)

View File

@ -18,8 +18,8 @@ import (
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() (*bouquins.BouquinsConf, error) { func ReadConfig() (*bouquins.Conf, error) {
conf := new(bouquins.BouquinsConf) conf := new(bouquins.Conf)
confPath := "bouquins.json" confPath := "bouquins.json"
if len(os.Args) > 1 { if len(os.Args) > 1 {
confPath = os.Args[1] confPath = os.Args[1]
@ -45,7 +45,7 @@ func ReadConfig() (*bouquins.BouquinsConf, error) {
return conf, err return conf, err
} }
func initApp() *bouquins.BouquinsConf { func initApp() *bouquins.Conf {
log.SetFlags(log.LstdFlags | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lshortfile)
conf, err := ReadConfig() conf, err := ReadConfig()
if err != nil { if err != nil {
@ -65,7 +65,6 @@ func initApp() *bouquins.BouquinsConf {
for _, provider := range bouquins.Providers { for _, provider := range bouquins.Providers {
oauthConf[provider.Name()] = provider.Config(conf) oauthConf[provider.Name()] = provider.Config(conf)
} }
// FIXME constructor, conf cookies secret
app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf, Cookies: sessions.NewCookieStore([]byte(conf.CookieSecret))} app := &bouquins.Bouquins{Tpl: tpl, DB: db, OAuthConf: oauthConf, Cookies: sessions.NewCookieStore([]byte(conf.CookieSecret))}
err = app.PrepareAll() err = app.PrepareAll()
if err != nil { if err != nil {