2016-07-25 20:00:28 +00:00
|
|
|
// Package github provides authentication strategies using GitHub.
|
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/github"
|
|
|
|
|
2016-08-11 05:31:42 +00:00
|
|
|
"github.com/coreos/dex/connector"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const baseURL = "https://api.github.com"
|
|
|
|
|
|
|
|
// Config holds configuration options for github logins.
|
|
|
|
type Config struct {
|
2016-11-03 21:32:23 +00:00
|
|
|
ClientID string `json:"clientID"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
RedirectURI string `json:"redirectURI"`
|
|
|
|
Org string `json:"org"`
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns a strategy for logging in through GitHub.
|
|
|
|
func (c *Config) Open() (connector.Connector, error) {
|
|
|
|
return &githubConnector{
|
|
|
|
redirectURI: c.RedirectURI,
|
|
|
|
org: c.Org,
|
|
|
|
oauth2Config: &oauth2.Config{
|
2016-10-22 20:36:31 +00:00
|
|
|
ClientID: c.ClientID,
|
|
|
|
ClientSecret: c.ClientSecret,
|
2016-07-25 20:00:28 +00:00
|
|
|
Endpoint: github.Endpoint,
|
|
|
|
Scopes: []string{
|
|
|
|
"user:email", // View user's email
|
|
|
|
"read:org", // View user's org teams.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type connectorData struct {
|
|
|
|
// GitHub's OAuth2 tokens never expire. We don't need a refresh token.
|
|
|
|
AccessToken string `json:"accessToken"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ connector.CallbackConnector = (*githubConnector)(nil)
|
|
|
|
_ connector.GroupsConnector = (*githubConnector)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type githubConnector struct {
|
|
|
|
redirectURI string
|
|
|
|
org string
|
|
|
|
oauth2Config *oauth2.Config
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *githubConnector) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *githubConnector) LoginURL(callbackURL, state string) (string, error) {
|
|
|
|
if c.redirectURI != callbackURL {
|
|
|
|
return "", fmt.Errorf("expected callback URL did not match the URL in the config")
|
|
|
|
}
|
|
|
|
return c.oauth2Config.AuthCodeURL(state), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type oauth2Error struct {
|
|
|
|
error string
|
|
|
|
errorDescription string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *oauth2Error) Error() string {
|
|
|
|
if e.errorDescription == "" {
|
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
return e.error + ": " + e.errorDescription
|
|
|
|
}
|
|
|
|
|
2016-10-27 17:08:08 +00:00
|
|
|
func (c *githubConnector) HandleCallback(r *http.Request) (identity connector.Identity, err error) {
|
2016-07-25 20:00:28 +00:00
|
|
|
q := r.URL.Query()
|
|
|
|
if errType := q.Get("error"); errType != "" {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, &oauth2Error{errType, q.Get("error_description")}
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
token, err := c.oauth2Config.Exchange(c.ctx, q.Get("code"))
|
|
|
|
if err != nil {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("github: failed to get token: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.oauth2Config.Client(c.ctx, token).Get(baseURL + "/user")
|
|
|
|
if err != nil {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("github: get URL %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("github: read body: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("%s: %s", resp.Status, body)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
var user struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("failed to decode response: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data := connectorData{AccessToken: token.AccessToken}
|
|
|
|
connData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, fmt.Errorf("marshal connector data: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
username := user.Name
|
|
|
|
if username == "" {
|
|
|
|
username = user.Login
|
|
|
|
}
|
2016-08-03 04:14:24 +00:00
|
|
|
identity = connector.Identity{
|
2016-07-25 20:00:28 +00:00
|
|
|
UserID: strconv.Itoa(user.ID),
|
|
|
|
Username: username,
|
|
|
|
Email: user.Email,
|
|
|
|
EmailVerified: true,
|
|
|
|
ConnectorData: connData,
|
|
|
|
}
|
2016-10-27 17:08:08 +00:00
|
|
|
return identity, nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-03 04:14:24 +00:00
|
|
|
func (c *githubConnector) Groups(identity connector.Identity) ([]string, error) {
|
2016-07-25 20:00:28 +00:00
|
|
|
var data connectorData
|
|
|
|
if err := json.Unmarshal(identity.ConnectorData, &data); err != nil {
|
|
|
|
return nil, fmt.Errorf("decode connector data: %v", err)
|
|
|
|
}
|
|
|
|
token := &oauth2.Token{AccessToken: data.AccessToken}
|
|
|
|
resp, err := c.oauth2Config.Client(c.ctx, token).Get(baseURL + "/user/teams")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("github: get teams: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("github: read body: %v", err)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("%s: %s", resp.Status, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://developer.github.com/v3/orgs/teams/#response-12
|
|
|
|
var teams []struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Org struct {
|
|
|
|
Login string `json:"login"`
|
|
|
|
} `json:"organization"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&teams); err != nil {
|
|
|
|
return nil, fmt.Errorf("github: unmarshal groups: %v", err)
|
|
|
|
}
|
|
|
|
groups := []string{}
|
|
|
|
for _, team := range teams {
|
|
|
|
if team.Org.Login == c.org {
|
|
|
|
groups = append(groups, team.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return groups, nil
|
|
|
|
}
|