2017-09-08 18:33:16 +00:00
|
|
|
package bouquins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
2017-09-09 07:34:19 +00:00
|
|
|
"golang.org/x/oauth2/github"
|
2017-09-08 18:33:16 +00:00
|
|
|
)
|
|
|
|
|
2017-09-08 18:41:30 +00:00
|
|
|
// GithubProvider implements OAuth2 client with github.com
|
2017-09-08 18:33:16 +00:00
|
|
|
type GithubProvider string
|
|
|
|
|
2017-09-09 07:16:46 +00:00
|
|
|
type githubEmail struct {
|
2017-09-08 18:41:30 +00:00
|
|
|
Email string `json:"email"`
|
|
|
|
Primary bool `json:"primary"`
|
|
|
|
Verified bool `json:"verified"`
|
|
|
|
Visibility string `json:"visibility"`
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:33:16 +00:00
|
|
|
func init() {
|
|
|
|
Providers = append(Providers, GithubProvider("github"))
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:41:30 +00:00
|
|
|
// Name returns name of provider
|
2017-09-08 18:33:16 +00:00
|
|
|
func (p GithubProvider) Name() string {
|
|
|
|
return string(p)
|
|
|
|
}
|
|
|
|
|
2017-09-09 07:16:46 +00:00
|
|
|
// Label returns label of provider
|
|
|
|
func (p GithubProvider) Label() string {
|
|
|
|
return "Github"
|
|
|
|
}
|
|
|
|
|
2017-09-09 10:28:46 +00:00
|
|
|
// Icon returns icon CSS class for provider
|
2017-09-09 07:16:46 +00:00
|
|
|
func (p GithubProvider) Icon() string {
|
2017-09-09 10:28:46 +00:00
|
|
|
return "githubicon"
|
2017-09-09 07:16:46 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 11:27:07 +00:00
|
|
|
// Config returns OAuth configuration for this provider
|
|
|
|
func (p GithubProvider) Config(conf *Conf) *oauth2.Config {
|
2017-09-09 11:10:29 +00:00
|
|
|
for _, c := range conf.ProvidersConf {
|
|
|
|
if c.Name == p.Name() {
|
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: c.ClientID,
|
|
|
|
ClientSecret: c.ClientSecret,
|
|
|
|
Scopes: []string{"user:email"},
|
|
|
|
Endpoint: github.Endpoint,
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 07:34:19 +00:00
|
|
|
}
|
2017-09-09 11:10:29 +00:00
|
|
|
return nil
|
2017-09-09 07:34:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 18:41:30 +00:00
|
|
|
// GetUser returns github primary email
|
2017-09-08 18:33:16 +00:00
|
|
|
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)
|
2017-09-09 07:16:46 +00:00
|
|
|
var emails []githubEmail
|
2017-09-08 18:33:16 +00:00
|
|
|
err = dec.Decode(&emails)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error reading github API response", err)
|
|
|
|
return "", fmt.Errorf("Error reading github API response")
|
|
|
|
}
|
|
|
|
var userEmail string
|
|
|
|
for _, email := range emails {
|
|
|
|
if email.Primary && email.Verified {
|
|
|
|
userEmail = email.Email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Println("User email:", userEmail)
|
|
|
|
return userEmail, nil
|
|
|
|
}
|