WIP: auth gitea
This commit is contained in:
parent
23fec6721c
commit
063abcf9d1
@ -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)
|
||||
|
88
bouquins/gitea.go
Normal file
88
bouquins/gitea.go
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user