diff --git a/assets/css/bouquins.css b/assets/css/bouquins.css index 1ac8b26..ea75d89 100644 --- a/assets/css/bouquins.css +++ b/assets/css/bouquins.css @@ -12,3 +12,7 @@ span.providericon { .googleicon { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAB1FBMVEUAAAD/AADsQzXrQzbqQzXpRDTqQzXqQjXqQzXqRDToRjbqQzXqQzXqQzXqRDXrRTHxRznqQzXpQzXqQzXpQzTjOTnrQTTqQzXqQzToRDPpQjfqQzXqQzXpQzbsRDjqQzXoRDfqQzXqQzXrQzXqRDXqQjXqQzbqQzXqQzXqQzTsQjn/rxDqRDTqQzXqRDXoRjr8vAX5sQrsTDHrQjT//wD7vAT7ugbvZif6vgX1jRjqQzX7vAT5sAo/jss9kb77uwRAieH6vQX6vQX6vAX6vQVBiOnkuA4/i9r6vAWstCQ1qVI3pFI9k7E+j8n4vAZvrT00qFM0p1M/jso+lLn8vAXkug1CqU00qFQ8lK45l6ffvxg3qFI0qFM9lqpBieU/jc00qFQzqFM1p09An2A0plk/jdA5lqwtpVo0qFM0qFM1qVQ1qFM1p1I0p1IzqVI1qFM1qFM+j8gzplM0qFNAi91Cl6o1qVM0qFM/jNQ7m602qFE0qFM1qFMxpVI0qVM0qFM0qFM0qFQktkk4p1A0qFM0qVM0qVMzo1IA/wA2p1M0qFIzqFIzp1QzqFI0p1PqQzX7vAVChfRChu9BhvA0qFNChfJBhfM1p1o9krs5mpQ3oHf////8WgVEAAAAj3RSTlMAATVylKafh24xIZXl5o8aEpD5940JJ9nWLUby8Tkp8Dje+rt8YF/7/pwbIOX2VhaV58xZAeP7xTfK63Kv47x8+6VgpGH+sfU1y+wcfd/7xv6d36SU6NJYYjEg5fdL/OePnx0IcPxQEdz7vX5gXXOl7tko8PgbRPH6OCbYzB+O+PaJByCT4+YZATRwpIZtMQ4TRwgAAAABYktHRJvv2FeEAAAAB3RJTUUH4QgKAjghFnOx6QAAAWBJREFUOMtjYCABMDIxs7CysXNwMmKV5uLm6YcCXj5+DGkBQaF+JCAsIooqLybejwYkJJHlpaTR5ftlZJHk5eQx5RWQ7VeEiiopq6iqqSiro+lnEIRIa2hqQf3DiqKfQVsHLK+rhxDSR/GBgaERSL8xrvAzMZ1gZq7Rr4kzgC0mAIGllRZOBdYgBRNsYFxbZGAHdgJYgT1MwURk4AAScQQrcMKqYBJIxBmswAWrgsmErHBFONINqwJ3kIgHSN7TyxvVbz5gBb7QgPLzD5gSiKogCKwgGMwOCQ2bMmVKQDiyfMRUkPy0SDAnKnoKCMQgqYiNAxsQD+UmgBVMCUhMgvCTU1LB8lPToArSMyAqpmRmZefk5uUXTJk+A6SgEG5iUfEUdDBz8sSSUoSdZeUYKmZVVCK7uqoaXUFNLaq/0+vqkaUbGpsw0kVzSytMui2hHWvS6ejsaulO7Ont6yAlywMAh+DsfszQdOIAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTctMDgtMTBUMDI6NTY6MzMrMDA6MDAy1cN5AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE3LTA4LTEwVDAyOjU2OjMzKzAwOjAwQ4h7xQAAAABJRU5ErkJggg==); } + +.giteaicon { + background-image: url(./gitea.svg); +} diff --git a/assets/css/gitea.svg b/assets/css/gitea.svg new file mode 100644 index 0000000..ac1594a --- /dev/null +++ b/assets/css/gitea.svg @@ -0,0 +1,160 @@ + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + diff --git a/bouquins/auth.go b/bouquins/auth.go index fed1bb0..5f39204 100644 --- a/bouquins/auth.go +++ b/bouquins/auth.go @@ -33,12 +33,13 @@ type LoginModel struct { // NewLoginModel constructor for LoginModel func (app *Bouquins) NewLoginModel(req *http.Request) *LoginModel { + // TODO filter configured providers return &LoginModel{*app.NewModel("Authentification", "provider", req), Providers} } // OAuth2Provider allows to get a user from an OAuth2 token type OAuth2Provider interface { - GetUser(token *oauth2.Token) (string, error) + GetUser(app *Bouquins, token *oauth2.Token) (string, error) Config(conf *Conf) *oauth2.Config Name() string Label() string @@ -132,7 +133,7 @@ func (app *Bouquins) CallbackPage(res http.ResponseWriter, req *http.Request) er if err != nil { return fmt.Errorf("Code exchange failed with '%s'", err) } - userEmail, err := provider.GetUser(token) + userEmail, err := provider.GetUser(app, token) if err != nil { return err } diff --git a/bouquins/bouquins.go b/bouquins/bouquins.go index 7f3374d..79faefa 100644 --- a/bouquins/bouquins.go +++ b/bouquins/bouquins.go @@ -85,6 +85,9 @@ type ProviderConf struct { Name string `json:"name"` ClientID string `json:"client-id"` ClientSecret string `json:"client-secret"` + AuthURL string `json:"auth-url"` + TokenURL string `json:"token-url"` + ProfileURL string `json:"profile-url"` } // Bouquins contains application common resources: templates, database diff --git a/bouquins/gitea.go b/bouquins/gitea.go index 8bc3ad0..98dea22 100644 --- a/bouquins/gitea.go +++ b/bouquins/gitea.go @@ -38,9 +38,19 @@ func (p GiteaProvider) Label() string { return "Gitea" } +// ProfileURL returns redirect URL for oauth2 auth +func (p GiteaProvider) ProfileURL(conf *Conf) string { + for _, c := range conf.ProvidersConf { + if c.Name == p.Name() { + return c.ProfileURL + } + } + return "" +} + // Icon returns icon CSS class for provider func (p GiteaProvider) Icon() string { - return "giteaicon" // TODO + return "giteaicon" } // Config returns OAuth configuration for this provider @@ -50,12 +60,8 @@ func (p GiteaProvider) Config(conf *Conf) *oauth2.Config { return &oauth2.Config{ ClientID: c.ClientID, ClientSecret: c.ClientSecret, - RedirectURL: "http://localhost:9000/callback", // TODO - Endpoint: oauth2.Endpoint{ - "https://git.meutel.net/login/oauth/authorize", // TODO - "https://git.meutel.net/login/oauth/access_token", // TODO - oauth2.AuthStyleAutoDetect, - }, + RedirectURL: conf.ExternalURL + "/callback", + Endpoint: oauth2.Endpoint{c.AuthURL, c.TokenURL, oauth2.AuthStyleAutoDetect}, } } } @@ -63,8 +69,8 @@ func (p GiteaProvider) Config(conf *Conf) *oauth2.Config { } // GetUser returns github primary email -func (p GiteaProvider) GetUser(token *oauth2.Token) (string, error) { - apiReq, err := http.NewRequest("GET", "https://git.meutel.net/api/v1/user", nil) // TODO +func (p GiteaProvider) GetUser(app *Bouquins, token *oauth2.Token) (string, error) { + apiReq, err := http.NewRequest("GET", p.ProfileURL(app.Conf), nil) apiReq.Header.Add("Accept", "application/json") apiReq.Header.Add("Authorization", "token "+token.AccessToken) client := &http.Client{} diff --git a/bouquins/github.go b/bouquins/github.go index 64129c1..0a6d6ed 100644 --- a/bouquins/github.go +++ b/bouquins/github.go @@ -55,7 +55,7 @@ func (p GithubProvider) Config(conf *Conf) *oauth2.Config { } // GetUser returns github primary email -func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) { +func (p GithubProvider) GetUser(app *Bouquins, token *oauth2.Token) (string, error) { apiReq, err := http.NewRequest("GET", "https://api.github.com/user/emails", nil) apiReq.Header.Add("Accept", "application/vnd.github.v3+json") apiReq.Header.Add("Authorization", "token "+token.AccessToken) diff --git a/bouquins/google.go b/bouquins/google.go index fb11cd2..cebd669 100644 --- a/bouquins/google.go +++ b/bouquins/google.go @@ -60,7 +60,7 @@ func (p GoogleProvider) Config(conf *Conf) *oauth2.Config { } // GetUser returns github primary email -func (p GoogleProvider) GetUser(token *oauth2.Token) (string, error) { +func (p GoogleProvider) GetUser(app *Bouquins, token *oauth2.Token) (string, error) { apiRes, err := http.Post("https://www.googleapis.com/oauth2/v2/tokeninfo?access_token="+token.AccessToken, "application/json", nil) defer apiRes.Body.Close() if err != nil {