2016-07-25 20:00:28 +00:00
|
|
|
// Package github provides authentication strategies using GitHub.
|
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2017-03-08 18:33:19 +00:00
|
|
|
"context"
|
2017-04-07 21:55:22 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2016-07-25 20:00:28 +00:00
|
|
|
"encoding/json"
|
2016-11-18 21:40:41 +00:00
|
|
|
"errors"
|
2016-07-25 20:00:28 +00:00
|
|
|
"fmt"
|
2021-09-17 06:12:39 +00:00
|
|
|
"io"
|
2017-04-07 21:55:22 +00:00
|
|
|
"net"
|
2016-07-25 20:00:28 +00:00
|
|
|
"net/http"
|
2021-09-17 06:12:39 +00:00
|
|
|
"os"
|
2017-01-26 22:15:01 +00:00
|
|
|
"regexp"
|
2016-07-25 20:00:28 +00:00
|
|
|
"strconv"
|
2017-04-07 21:55:22 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2016-07-25 20:00:28 +00:00
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/github"
|
|
|
|
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/connector"
|
2019-07-03 08:40:31 +00:00
|
|
|
groups_pkg "github.com/dexidp/dex/pkg/groups"
|
2019-02-22 12:19:23 +00:00
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
const (
|
2017-08-18 18:04:12 +00:00
|
|
|
apiURL = "https://api.github.com"
|
|
|
|
// GitHub requires this scope to access '/user' and '/user/emails' API endpoints.
|
2016-11-18 21:40:41 +00:00
|
|
|
scopeEmail = "user:email"
|
2017-08-18 18:04:12 +00:00
|
|
|
// GitHub requires this scope to access '/user/teams' and '/orgs' API endpoints
|
|
|
|
// which are used when a client includes the 'groups' scope.
|
|
|
|
scopeOrgs = "read:org"
|
2016-11-18 21:40:41 +00:00
|
|
|
)
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2017-08-07 22:58:26 +00:00
|
|
|
// Pagination URL patterns
|
|
|
|
// https://developer.github.com/v3/#pagination
|
2020-11-03 19:50:09 +00:00
|
|
|
var (
|
|
|
|
reNext = regexp.MustCompile("<([^>]+)>; rel=\"next\"")
|
|
|
|
reLast = regexp.MustCompile("<([^>]+)>; rel=\"last\"")
|
|
|
|
)
|
2017-08-07 22:58:26 +00:00
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
// Config holds configuration options for github logins.
|
|
|
|
type Config struct {
|
2018-09-13 13:35:02 +00:00
|
|
|
ClientID string `json:"clientID"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
RedirectURI string `json:"redirectURI"`
|
|
|
|
Org string `json:"org"`
|
|
|
|
Orgs []Org `json:"orgs"`
|
|
|
|
HostName string `json:"hostName"`
|
|
|
|
RootCA string `json:"rootCA"`
|
|
|
|
TeamNameField string `json:"teamNameField"`
|
2018-11-19 18:14:38 +00:00
|
|
|
LoadAllGroups bool `json:"loadAllGroups"`
|
2019-02-04 13:53:59 +00:00
|
|
|
UseLoginAsID bool `json:"useLoginAsID"`
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 20:53:38 +00:00
|
|
|
// Org holds org-team filters, in which teams are optional.
|
|
|
|
type Org struct {
|
|
|
|
// Organization name in github (not slug, full name). Only users in this github
|
|
|
|
// organization can authenticate.
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
|
|
// Names of teams in a github organization. A user will be able to
|
|
|
|
// authenticate if they are members of at least one of these teams. Users
|
|
|
|
// in the organization can authenticate if this field is omitted from the
|
|
|
|
// config file.
|
|
|
|
Teams []string `json:"teams,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
// Open returns a strategy for logging in through GitHub.
|
2019-02-22 12:19:23 +00:00
|
|
|
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
|
2017-08-03 20:53:38 +00:00
|
|
|
if c.Org != "" {
|
|
|
|
// Return error if both 'org' and 'orgs' fields are used.
|
|
|
|
if len(c.Orgs) > 0 {
|
|
|
|
return nil, errors.New("github: cannot use both 'org' and 'orgs' fields simultaneously")
|
|
|
|
}
|
2019-02-22 12:19:23 +00:00
|
|
|
logger.Warn("github: legacy field 'org' being used. Switch to the newer 'orgs' field structure")
|
2017-08-03 20:53:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 21:55:22 +00:00
|
|
|
g := githubConnector{
|
2016-11-18 21:40:41 +00:00
|
|
|
redirectURI: c.RedirectURI,
|
|
|
|
org: c.Org,
|
2017-08-03 20:53:38 +00:00
|
|
|
orgs: c.Orgs,
|
2016-11-18 21:40:41 +00:00
|
|
|
clientID: c.ClientID,
|
|
|
|
clientSecret: c.ClientSecret,
|
2017-04-07 21:55:22 +00:00
|
|
|
apiURL: apiURL,
|
2016-11-22 23:35:46 +00:00
|
|
|
logger: logger,
|
2019-02-04 13:53:59 +00:00
|
|
|
useLoginAsID: c.UseLoginAsID,
|
2017-04-07 21:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostName != "" {
|
|
|
|
// ensure this is a hostname and not a URL or path.
|
|
|
|
if strings.Contains(c.HostName, "/") {
|
|
|
|
return nil, errors.New("invalid hostname: hostname cannot contain `/`")
|
|
|
|
}
|
|
|
|
|
|
|
|
g.hostName = c.HostName
|
|
|
|
g.apiURL = "https://" + c.HostName + "/api/v3"
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.RootCA != "" {
|
|
|
|
if c.HostName == "" {
|
|
|
|
return nil, errors.New("invalid connector config: Host name field required for a root certificate file")
|
|
|
|
}
|
|
|
|
g.rootCA = c.RootCA
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if g.httpClient, err = newHTTPClient(g.rootCA); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create HTTP client: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 18:14:38 +00:00
|
|
|
g.loadAllGroups = c.LoadAllGroups
|
2017-04-07 21:55:22 +00:00
|
|
|
|
2018-09-13 13:35:02 +00:00
|
|
|
switch c.TeamNameField {
|
2018-10-04 21:22:45 +00:00
|
|
|
case "name", "slug", "both", "":
|
2018-09-13 13:35:02 +00:00
|
|
|
g.teamNameField = c.TeamNameField
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid connector config: unsupported team name field value `%s`", c.TeamNameField)
|
|
|
|
}
|
|
|
|
|
2017-04-07 21:55:22 +00:00
|
|
|
return &g, nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-11-18 21:40:41 +00:00
|
|
|
_ connector.RefreshConnector = (*githubConnector)(nil)
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type githubConnector struct {
|
|
|
|
redirectURI string
|
|
|
|
org string
|
2017-08-03 20:53:38 +00:00
|
|
|
orgs []Org
|
2016-11-18 21:40:41 +00:00
|
|
|
clientID string
|
|
|
|
clientSecret string
|
2019-02-22 12:19:23 +00:00
|
|
|
logger log.Logger
|
2017-04-07 21:55:22 +00:00
|
|
|
// apiURL defaults to "https://api.github.com"
|
|
|
|
apiURL string
|
|
|
|
// hostName of the GitHub enterprise account.
|
|
|
|
hostName string
|
|
|
|
// Used to support untrusted/self-signed CA certs.
|
|
|
|
rootCA string
|
2019-12-18 14:51:44 +00:00
|
|
|
// HTTP Client that trusts the custom declared rootCA cert.
|
2018-11-19 18:14:38 +00:00
|
|
|
httpClient *http.Client
|
|
|
|
// optional choice between 'name' (default) or 'slug'
|
2018-09-13 13:35:02 +00:00
|
|
|
teamNameField string
|
2018-11-19 18:14:38 +00:00
|
|
|
// if set to true and no orgs are configured then connector loads all user claims (all orgs and team)
|
|
|
|
loadAllGroups bool
|
2019-07-26 10:35:35 +00:00
|
|
|
// if set to true will use the user's handle rather than their numeric id as the ID
|
2019-02-04 13:53:59 +00:00
|
|
|
useLoginAsID bool
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 18:04:12 +00:00
|
|
|
// groupsRequired returns whether dex requires GitHub's 'read:org' scope. Dex
|
|
|
|
// needs 'read:org' if 'orgs' or 'org' fields are populated in a config file.
|
|
|
|
// Clients can require 'groups' scope without setting 'orgs'/'org'.
|
|
|
|
func (c *githubConnector) groupsRequired(groupScope bool) bool {
|
|
|
|
return len(c.orgs) > 0 || c.org != "" || groupScope
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
func (c *githubConnector) oauth2Config(scopes connector.Scopes) *oauth2.Config {
|
2017-08-18 18:04:12 +00:00
|
|
|
// 'read:org' scope is required by the GitHub API, and thus for dex to ensure
|
|
|
|
// a user is a member of orgs and teams provided in configs.
|
2017-08-17 17:13:00 +00:00
|
|
|
githubScopes := []string{scopeEmail}
|
2017-08-18 18:04:12 +00:00
|
|
|
if c.groupsRequired(scopes.Groups) {
|
2017-08-17 17:13:00 +00:00
|
|
|
githubScopes = append(githubScopes, scopeOrgs)
|
2016-11-18 21:40:41 +00:00
|
|
|
}
|
2017-04-07 21:55:22 +00:00
|
|
|
|
|
|
|
endpoint := github.Endpoint
|
|
|
|
// case when it is a GitHub Enterprise account.
|
|
|
|
if c.hostName != "" {
|
|
|
|
endpoint = oauth2.Endpoint{
|
|
|
|
AuthURL: "https://" + c.hostName + "/login/oauth/authorize",
|
|
|
|
TokenURL: "https://" + c.hostName + "/login/oauth/access_token",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: c.clientID,
|
|
|
|
ClientSecret: c.clientSecret,
|
2017-04-07 21:55:22 +00:00
|
|
|
Endpoint: endpoint,
|
2016-11-18 21:40:41 +00:00
|
|
|
Scopes: githubScopes,
|
2020-05-12 11:23:00 +00:00
|
|
|
RedirectURL: c.redirectURI,
|
2016-11-18 21:40:41 +00:00
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
func (c *githubConnector) LoginURL(scopes connector.Scopes, callbackURL, state string) (string, error) {
|
2016-07-25 20:00:28 +00:00
|
|
|
if c.redirectURI != callbackURL {
|
2017-06-13 22:52:33 +00:00
|
|
|
return "", fmt.Errorf("expected callback URL %q did not match the URL in the config %q", callbackURL, c.redirectURI)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2017-04-07 21:55:22 +00:00
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
return c.oauth2Config(scopes).AuthCodeURL(state), nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type oauth2Error struct {
|
|
|
|
error string
|
|
|
|
errorDescription string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *oauth2Error) Error() string {
|
|
|
|
if e.errorDescription == "" {
|
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
return e.error + ": " + e.errorDescription
|
|
|
|
}
|
|
|
|
|
2019-12-18 14:51:44 +00:00
|
|
|
// newHTTPClient returns a new HTTP client that trusts the custom declared rootCA cert.
|
2017-04-07 21:55:22 +00:00
|
|
|
func newHTTPClient(rootCA string) (*http.Client, error) {
|
|
|
|
tlsConfig := tls.Config{RootCAs: x509.NewCertPool()}
|
2021-09-17 06:12:39 +00:00
|
|
|
rootCABytes, err := os.ReadFile(rootCA)
|
2017-04-07 21:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read root-ca: %v", err)
|
|
|
|
}
|
|
|
|
if !tlsConfig.RootCAs.AppendCertsFromPEM(rootCABytes) {
|
|
|
|
return nil, fmt.Errorf("no certs found in root CA file %q", rootCA)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tlsConfig,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext,
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
func (c *githubConnector) HandleCallback(s connector.Scopes, 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
|
|
|
}
|
2016-11-18 21:40:41 +00:00
|
|
|
|
|
|
|
oauth2Config := c.oauth2Config(s)
|
2017-04-07 21:55:22 +00:00
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
ctx := r.Context()
|
2017-04-07 21:55:22 +00:00
|
|
|
// GitHub Enterprise account
|
|
|
|
if c.httpClient != nil {
|
|
|
|
ctx = context.WithValue(r.Context(), oauth2.HTTPClient, c.httpClient)
|
|
|
|
}
|
2016-11-18 21:40:41 +00:00
|
|
|
|
|
|
|
token, err := oauth2Config.Exchange(ctx, q.Get("code"))
|
2016-07-25 20:00:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
client := oauth2Config.Client(ctx, token)
|
|
|
|
|
|
|
|
user, err := c.user(ctx, client)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
2016-11-18 21:40:41 +00:00
|
|
|
return identity, fmt.Errorf("github: get user: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
username := user.Name
|
|
|
|
if username == "" {
|
|
|
|
username = user.Login
|
|
|
|
}
|
2019-01-31 18:21:54 +00:00
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
identity = connector.Identity{
|
2019-10-10 14:43:41 +00:00
|
|
|
UserID: strconv.Itoa(user.ID),
|
|
|
|
Username: username,
|
|
|
|
PreferredUsername: user.Login,
|
|
|
|
Email: user.Email,
|
|
|
|
EmailVerified: true,
|
2016-11-18 21:40:41 +00:00
|
|
|
}
|
2019-02-04 13:53:59 +00:00
|
|
|
if c.useLoginAsID {
|
2019-01-31 18:21:54 +00:00
|
|
|
identity.UserID = user.Login
|
|
|
|
}
|
|
|
|
|
2017-08-18 18:04:12 +00:00
|
|
|
// Only set identity.Groups if 'orgs', 'org', or 'groups' scope are specified.
|
|
|
|
if c.groupsRequired(s.Groups) {
|
|
|
|
groups, err := c.getGroups(ctx, client, s.Groups, user.Login)
|
|
|
|
if err != nil {
|
|
|
|
return identity, err
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-18 21:40:41 +00:00
|
|
|
identity.Groups = groups
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-18 21:40:41 +00:00
|
|
|
|
|
|
|
if s.OfflineAccess {
|
|
|
|
data := connectorData{AccessToken: token.AccessToken}
|
|
|
|
connData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return identity, fmt.Errorf("marshal connector data: %v", err)
|
|
|
|
}
|
|
|
|
identity.ConnectorData = connData
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-18 21:40:41 +00:00
|
|
|
|
|
|
|
return identity, nil
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:53:38 +00:00
|
|
|
func (c *githubConnector) Refresh(ctx context.Context, s connector.Scopes, identity connector.Identity) (connector.Identity, error) {
|
|
|
|
if len(identity.ConnectorData) == 0 {
|
|
|
|
return identity, errors.New("no upstream access token found")
|
2016-11-18 21:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var data connectorData
|
2017-08-03 20:53:38 +00:00
|
|
|
if err := json.Unmarshal(identity.ConnectorData, &data); err != nil {
|
|
|
|
return identity, fmt.Errorf("github: unmarshal access token: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
client := c.oauth2Config(s).Client(ctx, &oauth2.Token{AccessToken: data.AccessToken})
|
|
|
|
user, err := c.user(ctx, client)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
2017-08-03 20:53:38 +00:00
|
|
|
return identity, fmt.Errorf("github: get user: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
username := user.Name
|
|
|
|
if username == "" {
|
|
|
|
username = user.Login
|
|
|
|
}
|
2017-08-03 20:53:38 +00:00
|
|
|
identity.Username = username
|
2019-10-10 14:43:41 +00:00
|
|
|
identity.PreferredUsername = user.Login
|
2017-08-03 20:53:38 +00:00
|
|
|
identity.Email = user.Email
|
|
|
|
|
2017-08-18 18:04:12 +00:00
|
|
|
// Only set identity.Groups if 'orgs', 'org', or 'groups' scope are specified.
|
|
|
|
if c.groupsRequired(s.Groups) {
|
|
|
|
groups, err := c.getGroups(ctx, client, s.Groups, user.Login)
|
|
|
|
if err != nil {
|
|
|
|
return identity, err
|
2017-08-03 20:53:38 +00:00
|
|
|
}
|
|
|
|
identity.Groups = groups
|
|
|
|
}
|
|
|
|
|
|
|
|
return identity, nil
|
|
|
|
}
|
2016-11-18 21:40:41 +00:00
|
|
|
|
2017-08-18 18:04:12 +00:00
|
|
|
// getGroups retrieves GitHub orgs and teams a user is in, if any.
|
|
|
|
func (c *githubConnector) getGroups(ctx context.Context, client *http.Client, groupScope bool, userLogin string) ([]string, error) {
|
2020-10-17 21:54:27 +00:00
|
|
|
switch {
|
|
|
|
case len(c.orgs) > 0:
|
2017-08-18 18:04:12 +00:00
|
|
|
return c.groupsForOrgs(ctx, client, userLogin)
|
2020-10-17 21:54:27 +00:00
|
|
|
case c.org != "":
|
2017-08-18 18:04:12 +00:00
|
|
|
return c.teamsForOrg(ctx, client, c.org)
|
2020-10-17 21:54:27 +00:00
|
|
|
case groupScope && c.loadAllGroups:
|
2018-11-14 23:31:31 +00:00
|
|
|
return c.userGroups(ctx, client)
|
2017-08-18 18:04:12 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
// formatTeamName returns unique team name.
|
|
|
|
// Orgs might have the same team names. To make team name unique it should be prefixed with the org name.
|
2018-11-14 23:31:31 +00:00
|
|
|
func formatTeamName(org string, team string) string {
|
|
|
|
return fmt.Sprintf("%s:%s", org, team)
|
|
|
|
}
|
|
|
|
|
2017-08-18 18:04:12 +00:00
|
|
|
// groupsForOrgs enforces org and team constraints on user authorization
|
2017-08-03 20:53:38 +00:00
|
|
|
// Cases in which user is authorized:
|
|
|
|
// N orgs, no teams: user is member of at least 1 org
|
|
|
|
// N orgs, M teams per org: user is member of any team from at least 1 org
|
|
|
|
// N-1 orgs, M teams per org, 1 org with no teams: user is member of any team
|
|
|
|
// from at least 1 org, or member of org with no teams
|
2018-11-15 16:12:28 +00:00
|
|
|
func (c *githubConnector) groupsForOrgs(ctx context.Context, client *http.Client, userName string) ([]string, error) {
|
|
|
|
groups := make([]string, 0)
|
2017-08-03 20:53:38 +00:00
|
|
|
var inOrgNoTeams bool
|
|
|
|
for _, org := range c.orgs {
|
|
|
|
inOrg, err := c.userInOrg(ctx, client, userName, org.Name)
|
2016-11-18 21:40:41 +00:00
|
|
|
if err != nil {
|
2018-11-15 16:12:28 +00:00
|
|
|
return nil, err
|
2017-08-03 20:53:38 +00:00
|
|
|
}
|
|
|
|
if !inOrg {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-18 18:04:12 +00:00
|
|
|
teams, err := c.teamsForOrg(ctx, client, org.Name)
|
2017-08-03 20:53:38 +00:00
|
|
|
if err != nil {
|
2018-11-15 16:12:28 +00:00
|
|
|
return nil, err
|
2017-08-03 20:53:38 +00:00
|
|
|
}
|
|
|
|
// User is in at least one org. User is authorized if no teams are specified
|
|
|
|
// in config; include all teams in claim. Otherwise filter out teams not in
|
|
|
|
// 'teams' list in config.
|
|
|
|
if len(org.Teams) == 0 {
|
|
|
|
inOrgNoTeams = true
|
2019-07-03 08:40:31 +00:00
|
|
|
} else if teams = groups_pkg.Filter(teams, org.Teams); len(teams) == 0 {
|
2017-08-17 17:40:22 +00:00
|
|
|
c.logger.Infof("github: user %q in org %q but no teams", userName, org.Name)
|
2017-08-03 20:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, teamName := range teams {
|
2018-11-14 23:31:31 +00:00
|
|
|
groups = append(groups, formatTeamName(org.Name, teamName))
|
2017-08-03 20:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if inOrgNoTeams || len(groups) > 0 {
|
2018-11-15 16:12:28 +00:00
|
|
|
return groups, nil
|
2017-08-03 20:53:38 +00:00
|
|
|
}
|
|
|
|
return groups, fmt.Errorf("github: user %q not in required orgs or teams", userName)
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
func (c *githubConnector) userGroups(ctx context.Context, client *http.Client) ([]string, error) {
|
2018-11-14 23:31:31 +00:00
|
|
|
orgs, err := c.userOrgs(ctx, client)
|
|
|
|
if err != nil {
|
2018-11-15 16:12:28 +00:00
|
|
|
return nil, err
|
2018-11-14 23:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orgTeams, err := c.userOrgTeams(ctx, client)
|
|
|
|
if err != nil {
|
2018-11-15 16:12:28 +00:00
|
|
|
return nil, err
|
2018-11-14 23:31:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
groups := make([]string, 0)
|
2018-11-15 17:00:37 +00:00
|
|
|
for _, o := range orgs {
|
2018-11-15 22:08:15 +00:00
|
|
|
groups = append(groups, o)
|
|
|
|
if teams, ok := orgTeams[o]; ok {
|
2018-11-15 17:00:37 +00:00
|
|
|
for _, t := range teams {
|
|
|
|
groups = append(groups, formatTeamName(o, t))
|
2018-11-14 23:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
return groups, nil
|
2018-11-14 23:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// userOrgs retrieves list of current user orgs
|
|
|
|
func (c *githubConnector) userOrgs(ctx context.Context, client *http.Client) ([]string, error) {
|
2018-11-15 16:12:28 +00:00
|
|
|
groups := make([]string, 0)
|
|
|
|
apiURL := c.apiURL + "/user/orgs"
|
2018-11-14 23:31:31 +00:00
|
|
|
for {
|
|
|
|
// https://developer.github.com/v3/orgs/#list-your-organizations
|
|
|
|
var (
|
|
|
|
orgs []org
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if apiURL, err = get(ctx, client, apiURL, &orgs); err != nil {
|
|
|
|
return nil, fmt.Errorf("github: get orgs: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
for _, o := range orgs {
|
|
|
|
groups = append(groups, o.Login)
|
2018-11-14 23:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if apiURL == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// userOrgTeams retrieves teams which current user belongs to.
|
|
|
|
// Method returns a map where key is an org name and value list of teams under the org.
|
|
|
|
func (c *githubConnector) userOrgTeams(ctx context.Context, client *http.Client) (map[string][]string, error) {
|
2019-07-30 09:08:57 +00:00
|
|
|
groups := make(map[string][]string)
|
2018-11-15 16:12:28 +00:00
|
|
|
apiURL := c.apiURL + "/user/teams"
|
2018-11-14 23:31:31 +00:00
|
|
|
for {
|
|
|
|
// https://developer.github.com/v3/orgs/teams/#list-user-teams
|
|
|
|
var (
|
|
|
|
teams []team
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if apiURL, err = get(ctx, client, apiURL, &teams); err != nil {
|
|
|
|
return nil, fmt.Errorf("github: get teams: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:12:28 +00:00
|
|
|
for _, t := range teams {
|
2018-10-04 21:22:45 +00:00
|
|
|
groups[t.Org.Login] = append(groups[t.Org.Login], c.teamGroupClaims(t)...)
|
2018-11-14 23:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if apiURL == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
2017-08-07 22:58:26 +00:00
|
|
|
// get creates a "GET `apiURL`" request with context, sends the request using
|
|
|
|
// the client, and decodes the resulting response body into v. A pagination URL
|
|
|
|
// is returned if one exists. Any errors encountered when building requests,
|
|
|
|
// sending requests, and reading and decoding response data are returned.
|
|
|
|
func get(ctx context.Context, client *http.Client, apiURL string, v interface{}) (string, error) {
|
|
|
|
req, err := http.NewRequest("GET", apiURL, nil)
|
2016-11-18 21:40:41 +00:00
|
|
|
if err != nil {
|
2017-08-07 22:58:26 +00:00
|
|
|
return "", fmt.Errorf("github: new req: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-18 21:40:41 +00:00
|
|
|
req = req.WithContext(ctx)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2017-08-07 22:58:26 +00:00
|
|
|
return "", fmt.Errorf("github: get URL %v", err)
|
2016-11-18 21:40:41 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2021-09-17 06:12:39 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2016-11-18 21:40:41 +00:00
|
|
|
if err != nil {
|
2017-08-07 22:58:26 +00:00
|
|
|
return "", fmt.Errorf("github: read body: %v", err)
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("%s: %s", resp.Status, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
|
|
|
|
return "", fmt.Errorf("failed to decode response: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getPagination(apiURL, resp), nil
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:57:31 +00:00
|
|
|
// getPagination checks the "Link" header field for "next" or "last" pagination URLs,
|
|
|
|
// and returns "next" page URL or empty string to indicate that there are no more pages.
|
|
|
|
// Non empty next pages' URL is returned if both "last" and "next" URLs are found and next page
|
|
|
|
// URL is not equal to last.
|
2017-08-07 22:58:26 +00:00
|
|
|
//
|
|
|
|
// https://developer.github.com/v3/#pagination
|
|
|
|
func getPagination(apiURL string, resp *http.Response) string {
|
|
|
|
if resp == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
links := resp.Header.Get("Link")
|
|
|
|
if len(reLast.FindStringSubmatch(links)) > 1 {
|
|
|
|
lastPageURL := reLast.FindStringSubmatch(links)[1]
|
|
|
|
if apiURL == lastPageURL {
|
|
|
|
return ""
|
2016-11-18 21:40:41 +00:00
|
|
|
}
|
2017-08-07 22:58:26 +00:00
|
|
|
} else {
|
|
|
|
return ""
|
2016-11-18 21:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 22:58:26 +00:00
|
|
|
if len(reNext.FindStringSubmatch(links)) > 1 {
|
|
|
|
return reNext.FindStringSubmatch(links)[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// user holds GitHub user information (relevant to dex) as defined by
|
|
|
|
// https://developer.github.com/v3/users/#response-with-public-profile-information
|
|
|
|
type user struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// user queries the GitHub API for profile information using the provided client.
|
|
|
|
//
|
|
|
|
// The HTTP client is expected to be constructed by the golang.org/x/oauth2 package,
|
|
|
|
// which inserts a bearer token as part of the request.
|
|
|
|
func (c *githubConnector) user(ctx context.Context, client *http.Client) (user, error) {
|
|
|
|
// https://developer.github.com/v3/users/#get-the-authenticated-user
|
|
|
|
var u user
|
|
|
|
if _, err := get(ctx, client, c.apiURL+"/user", &u); err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:55:11 +00:00
|
|
|
// Only public user emails are returned by 'GET /user'. u.Email will be empty
|
2017-08-07 22:58:26 +00:00
|
|
|
// if a users' email is private. We must retrieve private emails explicitly.
|
|
|
|
if u.Email == "" {
|
|
|
|
var err error
|
|
|
|
if u.Email, err = c.userEmail(ctx, client); err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
2016-11-18 21:40:41 +00:00
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2017-08-07 22:58:26 +00:00
|
|
|
// userEmail holds GitHub user email information as defined by
|
|
|
|
// https://developer.github.com/v3/users/emails/#response
|
|
|
|
type userEmail struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
Verified bool `json:"verified"`
|
|
|
|
Primary bool `json:"primary"`
|
|
|
|
Visibility string `json:"visibility"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// userEmail queries the GitHub API for a users' email information using the
|
|
|
|
// provided client. Only returns the users' verified, primary email (private or
|
|
|
|
// public).
|
|
|
|
//
|
|
|
|
// The HTTP client is expected to be constructed by the golang.org/x/oauth2 package,
|
|
|
|
// which inserts a bearer token as part of the request.
|
|
|
|
func (c *githubConnector) userEmail(ctx context.Context, client *http.Client) (string, error) {
|
|
|
|
apiURL := c.apiURL + "/user/emails"
|
|
|
|
for {
|
|
|
|
// https://developer.github.com/v3/users/emails/#list-email-addresses-for-a-user
|
|
|
|
var (
|
|
|
|
emails []userEmail
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if apiURL, err = get(ctx, client, apiURL, &emails); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, email := range emails {
|
2017-08-18 15:46:05 +00:00
|
|
|
/*
|
|
|
|
if GitHub Enterprise, set email.Verified to true
|
|
|
|
This change being made because GitHub Enterprise does not
|
|
|
|
support email verification. CircleCI indicated that GitHub
|
|
|
|
advised them not to check for verified emails
|
|
|
|
(https://circleci.com/enterprise/changelog/#1-47-1).
|
|
|
|
In addition, GitHub Enterprise support replied to a support
|
2017-09-14 16:44:15 +00:00
|
|
|
ticket with "There is no way to verify an email address in
|
|
|
|
GitHub Enterprise."
|
2017-08-18 15:46:05 +00:00
|
|
|
*/
|
2017-08-17 21:26:10 +00:00
|
|
|
if c.hostName != "" {
|
|
|
|
email.Verified = true
|
|
|
|
}
|
2017-09-14 16:44:15 +00:00
|
|
|
|
2017-08-07 22:58:26 +00:00
|
|
|
if email.Verified && email.Primary {
|
|
|
|
return email.Email, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if apiURL == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("github: user has no verified, primary email")
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:53:38 +00:00
|
|
|
// userInOrg queries the GitHub API for a users' org membership.
|
|
|
|
//
|
|
|
|
// The HTTP passed client is expected to be constructed by the golang.org/x/oauth2 package,
|
|
|
|
// which inserts a bearer token as part of the request.
|
|
|
|
func (c *githubConnector) userInOrg(ctx context.Context, client *http.Client, userName, orgName string) (bool, error) {
|
|
|
|
// requester == user, so GET-ing this endpoint should return 404/302 if user
|
|
|
|
// is not a member
|
|
|
|
//
|
|
|
|
// https://developer.github.com/v3/orgs/members/#check-membership
|
|
|
|
apiURL := fmt.Sprintf("%s/orgs/%s/members/%s", c.apiURL, orgName, userName)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", apiURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("github: new req: %v", err)
|
|
|
|
}
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("github: get teams: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusNoContent:
|
|
|
|
case http.StatusFound, http.StatusNotFound:
|
2017-08-17 17:40:22 +00:00
|
|
|
c.logger.Infof("github: user %q not in org %q or application not authorized to read org data", userName, orgName)
|
2017-08-03 20:53:38 +00:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("github: unexpected return status: %q", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 204 if user is a member
|
|
|
|
return resp.StatusCode == http.StatusNoContent, err
|
|
|
|
}
|
|
|
|
|
2017-08-07 22:58:26 +00:00
|
|
|
// teams holds GitHub a users' team information as defined by
|
|
|
|
// https://developer.github.com/v3/orgs/teams/#response-12
|
|
|
|
type team struct {
|
|
|
|
Name string `json:"name"`
|
2018-11-14 23:31:31 +00:00
|
|
|
Org org `json:"organization"`
|
2018-09-13 13:35:02 +00:00
|
|
|
Slug string `json:"slug"`
|
2017-08-07 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 23:31:31 +00:00
|
|
|
type org struct {
|
|
|
|
Login string `json:"login"`
|
|
|
|
}
|
|
|
|
|
2017-08-18 18:04:12 +00:00
|
|
|
// teamsForOrg queries the GitHub API for team membership within a specific organization.
|
2016-11-18 21:40:41 +00:00
|
|
|
//
|
|
|
|
// The HTTP passed client is expected to be constructed by the golang.org/x/oauth2 package,
|
|
|
|
// which inserts a bearer token as part of the request.
|
2017-08-18 18:04:12 +00:00
|
|
|
func (c *githubConnector) teamsForOrg(ctx context.Context, client *http.Client, orgName string) ([]string, error) {
|
2017-08-07 22:58:26 +00:00
|
|
|
apiURL, groups := c.apiURL+"/user/teams", []string{}
|
2017-01-26 22:15:01 +00:00
|
|
|
for {
|
2017-08-07 22:58:26 +00:00
|
|
|
// https://developer.github.com/v3/orgs/teams/#list-user-teams
|
|
|
|
var (
|
|
|
|
teams []team
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if apiURL, err = get(ctx, client, apiURL, &teams); err != nil {
|
2017-08-18 18:04:12 +00:00
|
|
|
return nil, fmt.Errorf("github: get teams: %v", err)
|
2017-01-26 22:15:01 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:23:57 +00:00
|
|
|
for _, t := range teams {
|
|
|
|
if t.Org.Login == orgName {
|
2018-10-04 21:22:45 +00:00
|
|
|
groups = append(groups, c.teamGroupClaims(t)...)
|
2017-01-26 22:15:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 22:58:26 +00:00
|
|
|
if apiURL == "" {
|
2017-01-26 22:15:01 +00:00
|
|
|
break
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-07 22:58:26 +00:00
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
return groups, nil
|
|
|
|
}
|
2018-11-15 17:23:57 +00:00
|
|
|
|
2018-10-04 21:22:45 +00:00
|
|
|
// teamGroupClaims returns team slug if 'teamNameField' option is set to
|
|
|
|
// 'slug', returns the slug *and* name if set to 'both', otherwise returns team
|
|
|
|
// name.
|
|
|
|
func (c *githubConnector) teamGroupClaims(t team) []string {
|
2018-11-15 17:23:57 +00:00
|
|
|
switch c.teamNameField {
|
2018-10-04 21:22:45 +00:00
|
|
|
case "both":
|
|
|
|
return []string{t.Name, t.Slug}
|
2018-11-15 17:23:57 +00:00
|
|
|
case "slug":
|
2018-10-04 21:22:45 +00:00
|
|
|
return []string{t.Slug}
|
2018-11-15 17:23:57 +00:00
|
|
|
default:
|
2018-10-04 21:22:45 +00:00
|
|
|
return []string{t.Name}
|
2018-11-15 17:23:57 +00:00
|
|
|
}
|
|
|
|
}
|