From 063abcf9d17fedc253d912040e0190955f9b865e Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 8 Sep 2019 10:41:10 +0200 Subject: [PATCH] WIP: auth gitea --- bouquins/auth.go | 9 +++++ bouquins/gitea.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++ bouquins/github.go | 9 ----- 3 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 bouquins/gitea.go diff --git a/bouquins/auth.go b/bouquins/auth.go index 59af111..fed1bb0 100644 --- a/bouquins/auth.go +++ b/bouquins/auth.go @@ -45,6 +45,15 @@ type OAuth2Provider interface { Icon() string } +func findProvider(name string) OAuth2Provider { + for _, p := range Providers { + if p.Name() == name { + return p + } + } + return nil +} + // generates a 16 characters long random string func securedRandString() string { b := make([]byte, 16) diff --git a/bouquins/gitea.go b/bouquins/gitea.go new file mode 100644 index 0000000..8bc3ad0 --- /dev/null +++ b/bouquins/gitea.go @@ -0,0 +1,88 @@ +package bouquins + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + + "golang.org/x/oauth2" +) + +// GithubProvider implements OAuth2 client with github.com +type GiteaProvider string + +type giteaProfile struct { + AvatarURL string `json:"avatar_url"` + Created string `json:"created"` + Email string `json:"email"` + FullName string `json:"full_name"` + ID int64 `json:"id"` + IsAdmin bool `json:"is_admin"` + Language string `json:"language"` + LastLogin string `json:"last_login"` + Login string `json:"login"` +} + +func init() { + Providers = append(Providers, GiteaProvider("gitea")) +} + +// Name returns name of provider +func (p GiteaProvider) Name() string { + return string(p) +} + +// Label returns label of provider +func (p GiteaProvider) Label() string { + return "Gitea" +} + +// Icon returns icon CSS class for provider +func (p GiteaProvider) Icon() string { + return "giteaicon" // TODO +} + +// Config returns OAuth configuration for this provider +func (p GiteaProvider) Config(conf *Conf) *oauth2.Config { + for _, c := range conf.ProvidersConf { + if c.Name == p.Name() { + 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, + }, + } + } + } + return nil +} + +// 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 + apiReq.Header.Add("Accept", "application/json") + apiReq.Header.Add("Authorization", "token "+token.AccessToken) + client := &http.Client{} + response, err := client.Do(apiReq) + defer response.Body.Close() + if err != nil { + log.Println("Auth error", err) + return "", fmt.Errorf("Authentification error") + } + + dec := json.NewDecoder(response.Body) + var profile giteaProfile + err = dec.Decode(&profile) + if err != nil { + log.Printf("Error reading %s API response %v", p.Name(), err) + return "", fmt.Errorf("Error reading %s API response", p.Name()) + } + userEmail := profile.Email + log.Println("User email:", userEmail) + return userEmail, nil +} diff --git a/bouquins/github.go b/bouquins/github.go index 9355881..64129c1 100644 --- a/bouquins/github.go +++ b/bouquins/github.go @@ -83,12 +83,3 @@ func (p GithubProvider) GetUser(token *oauth2.Token) (string, error) { log.Println("User email:", userEmail) return userEmail, nil } - -func findProvider(name string) OAuth2Provider { - for _, p := range Providers { - if p.Name() == name { - return p - } - } - return nil -}