2018-02-03 11:52:46 +00:00
|
|
|
// Package google implements logging in through Google's OpenID Connect provider.
|
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-09-17 06:12:39 +00:00
|
|
|
"os"
|
2018-02-03 11:52:46 +00:00
|
|
|
"time"
|
|
|
|
|
2021-01-13 18:56:09 +00:00
|
|
|
"github.com/coreos/go-oidc/v3/oidc"
|
2018-02-03 11:52:46 +00:00
|
|
|
"golang.org/x/oauth2"
|
2019-12-18 14:53:34 +00:00
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
admin "google.golang.org/api/admin/directory/v1"
|
2020-10-17 21:02:29 +00:00
|
|
|
"google.golang.org/api/option"
|
2018-02-03 11:52:46 +00:00
|
|
|
|
|
|
|
"github.com/dexidp/dex/connector"
|
2019-12-03 15:27:07 +00:00
|
|
|
pkg_groups "github.com/dexidp/dex/pkg/groups"
|
2018-02-04 17:17:17 +00:00
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2018-02-03 11:52:46 +00:00
|
|
|
)
|
|
|
|
|
2018-02-03 11:53:42 +00:00
|
|
|
const (
|
|
|
|
issuerURL = "https://accounts.google.com"
|
|
|
|
)
|
|
|
|
|
2018-02-05 21:53:32 +00:00
|
|
|
// Config holds configuration options for Google logins.
|
2018-02-03 11:52:46 +00:00
|
|
|
type Config struct {
|
|
|
|
ClientID string `json:"clientID"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
RedirectURI string `json:"redirectURI"`
|
|
|
|
|
|
|
|
Scopes []string `json:"scopes"` // defaults to "profile" and "email"
|
|
|
|
|
|
|
|
// Optional list of whitelisted domains
|
|
|
|
// If this field is nonempty, only users from a listed domain will be allowed to log in
|
|
|
|
HostedDomains []string `json:"hostedDomains"`
|
2018-02-04 17:17:17 +00:00
|
|
|
|
2019-12-03 15:27:07 +00:00
|
|
|
// Optional list of whitelisted groups
|
|
|
|
// If this field is nonempty, only users from a listed group will be allowed to log in
|
|
|
|
Groups []string `json:"groups"`
|
|
|
|
|
2018-02-04 17:17:17 +00:00
|
|
|
// Optional path to service account json
|
|
|
|
// If nonempty, and groups claim is made, will use authentication from file to
|
|
|
|
// check groups with the admin directory api
|
|
|
|
ServiceAccountFilePath string `json:"serviceAccountFilePath"`
|
|
|
|
|
|
|
|
// Required if ServiceAccountFilePath
|
|
|
|
// The email of a GSuite super user which the service account will impersonate
|
|
|
|
// when listing groups
|
|
|
|
AdminEmail string
|
2021-09-08 03:21:26 +00:00
|
|
|
|
|
|
|
// If this field is true, fetch direct group membership and transitive group membership
|
|
|
|
FetchTransitiveGroupMembership bool `json:"fetchTransitiveGroupMembership"`
|
2018-02-03 11:52:46 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 21:53:32 +00:00
|
|
|
// Open returns a connector which can be used to login users through Google.
|
2018-02-04 17:17:17 +00:00
|
|
|
func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, err error) {
|
2018-02-03 11:52:46 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2018-02-03 11:53:42 +00:00
|
|
|
provider, err := oidc.NewProvider(ctx, issuerURL)
|
2018-02-03 11:52:46 +00:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, fmt.Errorf("failed to get provider: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scopes := []string{oidc.ScopeOpenID}
|
|
|
|
if len(c.Scopes) > 0 {
|
|
|
|
scopes = append(scopes, c.Scopes...)
|
|
|
|
} else {
|
|
|
|
scopes = append(scopes, "profile", "email")
|
|
|
|
}
|
|
|
|
|
2022-09-28 17:59:28 +00:00
|
|
|
var srv *admin.Service
|
|
|
|
if len(c.Groups) > 0 {
|
|
|
|
srv, err = createDirectoryService(c.ServiceAccountFilePath, c.AdminEmail, logger)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, fmt.Errorf("could not create directory service: %v", err)
|
|
|
|
}
|
2019-01-28 13:09:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 11:52:46 +00:00
|
|
|
clientID := c.ClientID
|
|
|
|
return &googleConnector{
|
|
|
|
redirectURI: c.RedirectURI,
|
|
|
|
oauth2Config: &oauth2.Config{
|
|
|
|
ClientID: clientID,
|
|
|
|
ClientSecret: c.ClientSecret,
|
|
|
|
Endpoint: provider.Endpoint(),
|
|
|
|
Scopes: scopes,
|
|
|
|
RedirectURL: c.RedirectURI,
|
|
|
|
},
|
|
|
|
verifier: provider.Verifier(
|
|
|
|
&oidc.Config{ClientID: clientID},
|
|
|
|
),
|
2021-09-08 03:21:26 +00:00
|
|
|
logger: logger,
|
|
|
|
cancel: cancel,
|
|
|
|
hostedDomains: c.HostedDomains,
|
|
|
|
groups: c.Groups,
|
|
|
|
serviceAccountFilePath: c.ServiceAccountFilePath,
|
|
|
|
adminEmail: c.AdminEmail,
|
|
|
|
fetchTransitiveGroupMembership: c.FetchTransitiveGroupMembership,
|
|
|
|
adminSrv: srv,
|
2018-02-03 11:52:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ connector.CallbackConnector = (*googleConnector)(nil)
|
|
|
|
_ connector.RefreshConnector = (*googleConnector)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type googleConnector struct {
|
2021-09-08 03:21:26 +00:00
|
|
|
redirectURI string
|
|
|
|
oauth2Config *oauth2.Config
|
|
|
|
verifier *oidc.IDTokenVerifier
|
|
|
|
cancel context.CancelFunc
|
|
|
|
logger log.Logger
|
|
|
|
hostedDomains []string
|
|
|
|
groups []string
|
|
|
|
serviceAccountFilePath string
|
|
|
|
adminEmail string
|
|
|
|
fetchTransitiveGroupMembership bool
|
|
|
|
adminSrv *admin.Service
|
2018-02-03 11:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *googleConnector) Close() error {
|
|
|
|
c.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *googleConnector) LoginURL(s connector.Scopes, callbackURL, state string) (string, error) {
|
|
|
|
if c.redirectURI != callbackURL {
|
|
|
|
return "", fmt.Errorf("expected callback URL %q did not match the URL in the config %q", callbackURL, c.redirectURI)
|
|
|
|
}
|
|
|
|
|
|
|
|
var opts []oauth2.AuthCodeOption
|
|
|
|
if len(c.hostedDomains) > 0 {
|
|
|
|
preferredDomain := c.hostedDomains[0]
|
|
|
|
if len(c.hostedDomains) > 1 {
|
|
|
|
preferredDomain = "*"
|
|
|
|
}
|
|
|
|
opts = append(opts, oauth2.SetAuthURLParam("hd", preferredDomain))
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.OfflineAccess {
|
|
|
|
opts = append(opts, oauth2.AccessTypeOffline, oauth2.SetAuthURLParam("prompt", "consent"))
|
|
|
|
}
|
|
|
|
return c.oauth2Config.AuthCodeURL(state, opts...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type oauth2Error struct {
|
|
|
|
error string
|
|
|
|
errorDescription string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *oauth2Error) Error() string {
|
|
|
|
if e.errorDescription == "" {
|
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
return e.error + ": " + e.errorDescription
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *googleConnector) HandleCallback(s connector.Scopes, r *http.Request) (identity connector.Identity, err error) {
|
|
|
|
q := r.URL.Query()
|
|
|
|
if errType := q.Get("error"); errType != "" {
|
|
|
|
return identity, &oauth2Error{errType, q.Get("error_description")}
|
|
|
|
}
|
|
|
|
token, err := c.oauth2Config.Exchange(r.Context(), q.Get("code"))
|
|
|
|
if err != nil {
|
|
|
|
return identity, fmt.Errorf("google: failed to get token: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-04 17:17:17 +00:00
|
|
|
return c.createIdentity(r.Context(), identity, s, token)
|
2018-02-03 11:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *googleConnector) Refresh(ctx context.Context, s connector.Scopes, identity connector.Identity) (connector.Identity, error) {
|
|
|
|
t := &oauth2.Token{
|
|
|
|
RefreshToken: string(identity.ConnectorData),
|
|
|
|
Expiry: time.Now().Add(-time.Hour),
|
|
|
|
}
|
|
|
|
token, err := c.oauth2Config.TokenSource(ctx, t).Token()
|
|
|
|
if err != nil {
|
|
|
|
return identity, fmt.Errorf("google: failed to get token: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-04 17:17:17 +00:00
|
|
|
return c.createIdentity(ctx, identity, s, token)
|
2018-02-03 11:52:46 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 17:17:17 +00:00
|
|
|
func (c *googleConnector) createIdentity(ctx context.Context, identity connector.Identity, s connector.Scopes, token *oauth2.Token) (connector.Identity, error) {
|
2018-02-03 11:52:46 +00:00
|
|
|
rawIDToken, ok := token.Extra("id_token").(string)
|
|
|
|
if !ok {
|
|
|
|
return identity, errors.New("google: no id_token in token response")
|
|
|
|
}
|
|
|
|
idToken, err := c.verifier.Verify(ctx, rawIDToken)
|
|
|
|
if err != nil {
|
|
|
|
return identity, fmt.Errorf("google: failed to verify ID Token: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var claims struct {
|
|
|
|
Username string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
EmailVerified bool `json:"email_verified"`
|
|
|
|
HostedDomain string `json:"hd"`
|
|
|
|
}
|
|
|
|
if err := idToken.Claims(&claims); err != nil {
|
|
|
|
return identity, fmt.Errorf("oidc: failed to decode claims: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.hostedDomains) > 0 {
|
|
|
|
found := false
|
|
|
|
for _, domain := range c.hostedDomains {
|
|
|
|
if claims.HostedDomain == domain {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return identity, fmt.Errorf("oidc: unexpected hd claim %v", claims.HostedDomain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 17:17:17 +00:00
|
|
|
var groups []string
|
2020-05-20 18:00:06 +00:00
|
|
|
if s.Groups && c.adminSrv != nil {
|
2021-09-08 03:21:26 +00:00
|
|
|
groups, err = c.getGroups(claims.Email, c.fetchTransitiveGroupMembership)
|
2018-02-04 17:17:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return identity, fmt.Errorf("google: could not retrieve groups: %v", err)
|
|
|
|
}
|
2019-12-03 15:27:07 +00:00
|
|
|
|
|
|
|
if len(c.groups) > 0 {
|
|
|
|
groups = pkg_groups.Filter(groups, c.groups)
|
|
|
|
if len(groups) == 0 {
|
|
|
|
return identity, fmt.Errorf("google: user %q is not in any of the required groups", claims.Username)
|
|
|
|
}
|
|
|
|
}
|
2018-02-04 17:17:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-03 11:52:46 +00:00
|
|
|
identity = connector.Identity{
|
|
|
|
UserID: idToken.Subject,
|
|
|
|
Username: claims.Username,
|
|
|
|
Email: claims.Email,
|
|
|
|
EmailVerified: claims.EmailVerified,
|
|
|
|
ConnectorData: []byte(token.RefreshToken),
|
2018-02-04 17:17:17 +00:00
|
|
|
Groups: groups,
|
2018-02-03 11:52:46 +00:00
|
|
|
}
|
|
|
|
return identity, nil
|
|
|
|
}
|
2018-02-04 17:17:17 +00:00
|
|
|
|
2018-02-05 21:53:32 +00:00
|
|
|
// getGroups creates a connection to the admin directory service and lists
|
|
|
|
// all groups the user is a member of
|
2021-09-08 03:21:26 +00:00
|
|
|
func (c *googleConnector) getGroups(email string, fetchTransitiveGroupMembership bool) ([]string, error) {
|
2018-02-04 17:17:17 +00:00
|
|
|
var userGroups []string
|
2020-01-14 20:18:39 +00:00
|
|
|
var err error
|
|
|
|
groupsList := &admin.Groups{}
|
|
|
|
for {
|
|
|
|
groupsList, err = c.adminSrv.Groups.List().
|
|
|
|
UserKey(email).PageToken(groupsList.NextPageToken).Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not list groups: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range groupsList.Groups {
|
2020-05-20 18:00:06 +00:00
|
|
|
// TODO (joelspeed): Make desired group key configurable
|
2020-01-14 20:18:39 +00:00
|
|
|
userGroups = append(userGroups, group.Email)
|
2021-09-08 03:21:26 +00:00
|
|
|
|
|
|
|
// getGroups takes a user's email/alias as well as a group's email/alias
|
|
|
|
if fetchTransitiveGroupMembership {
|
|
|
|
transitiveGroups, err := c.getGroups(group.Email, fetchTransitiveGroupMembership)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not list transitive groups: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userGroups = append(userGroups, transitiveGroups...)
|
|
|
|
}
|
2020-01-14 20:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if groupsList.NextPageToken == "" {
|
|
|
|
break
|
|
|
|
}
|
2018-02-04 17:17:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 03:21:26 +00:00
|
|
|
return uniqueGroups(userGroups), nil
|
2018-02-04 17:17:17 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 11:56:56 +00:00
|
|
|
// createDirectoryService sets up super user impersonation and creates an admin client for calling
|
|
|
|
// the google admin api. If no serviceAccountFilePath is defined, the application default credential
|
|
|
|
// is used.
|
|
|
|
func createDirectoryService(serviceAccountFilePath, email string, logger log.Logger) (*admin.Service, error) {
|
|
|
|
if email == "" {
|
|
|
|
return nil, fmt.Errorf("directory service requires adminEmail")
|
2018-02-04 17:17:17 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 11:56:56 +00:00
|
|
|
var jsonCredentials []byte
|
|
|
|
var err error
|
2018-02-04 17:17:17 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2022-09-07 11:56:56 +00:00
|
|
|
if serviceAccountFilePath == "" {
|
|
|
|
logger.Warn("the application default credential is used since the service account file path is not used")
|
|
|
|
credential, err := google.FindDefaultCredentials(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch application default credentials: %w", err)
|
|
|
|
}
|
|
|
|
jsonCredentials = credential.JSON
|
|
|
|
} else {
|
|
|
|
jsonCredentials, err = os.ReadFile(serviceAccountFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading credentials from file: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config, err := google.JWTConfigFromJSON(jsonCredentials, admin.AdminDirectoryGroupReadonlyScope)
|
2018-02-04 17:17:17 +00:00
|
|
|
if err != nil {
|
2022-09-07 11:56:56 +00:00
|
|
|
return nil, fmt.Errorf("unable to parse credentials to config: %v", err)
|
2018-02-04 17:17:17 +00:00
|
|
|
}
|
2022-09-07 11:56:56 +00:00
|
|
|
config.Subject = email
|
|
|
|
return admin.NewService(ctx, option.WithHTTPClient(config.Client(ctx)))
|
2018-02-04 17:17:17 +00:00
|
|
|
}
|
2021-09-08 03:21:26 +00:00
|
|
|
|
|
|
|
// uniqueGroups returns the unique groups of a slice
|
|
|
|
func uniqueGroups(groups []string) []string {
|
|
|
|
keys := make(map[string]struct{})
|
|
|
|
unique := []string{}
|
|
|
|
for _, group := range groups {
|
|
|
|
if _, exists := keys[group]; !exists {
|
|
|
|
keys[group] = struct{}{}
|
|
|
|
unique = append(unique, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unique
|
|
|
|
}
|