70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package bouquins
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// GithubProvider implements OAuth2 client with github.com
|
|
type GithubProvider string
|
|
|
|
type gitHubEmail struct {
|
|
Email string `json:"email"`
|
|
Primary bool `json:"primary"`
|
|
Verified bool `json:"verified"`
|
|
Visibility string `json:"visibility"`
|
|
}
|
|
|
|
func init() {
|
|
Providers = append(Providers, GithubProvider("github"))
|
|
}
|
|
|
|
// Name returns name of provider
|
|
func (p GithubProvider) Name() string {
|
|
return string(p)
|
|
}
|
|
|
|
// GetUser returns github primary email
|
|
func (p GithubProvider) GetUser(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)
|
|
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 emails []gitHubEmail
|
|
err = dec.Decode(&emails)
|
|
if err != nil {
|
|
log.Println("Error reading github API response", err)
|
|
return "", fmt.Errorf("Error reading github API response")
|
|
}
|
|
fmt.Printf("Content: %s\n", emails)
|
|
var userEmail string
|
|
for _, email := range emails {
|
|
if email.Primary && email.Verified {
|
|
userEmail = email.Email
|
|
}
|
|
}
|
|
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
|
|
}
|