dex/connector/google/google.go

284 lines
8.3 KiB
Go
Raw Normal View History

2018-02-03 17:22:46 +05:30
// Package google implements logging in through Google's OpenID Connect provider.
package google
import (
"context"
"errors"
"fmt"
2018-02-04 22:47:17 +05:30
"io/ioutil"
2018-02-03 17:22:46 +05:30
"net/http"
"time"
"github.com/coreos/go-oidc"
"golang.org/x/oauth2"
"github.com/dexidp/dex/connector"
pkg_groups "github.com/dexidp/dex/pkg/groups"
2018-02-04 22:47:17 +05:30
"github.com/dexidp/dex/pkg/log"
"golang.org/x/oauth2/google"
admin "google.golang.org/api/admin/directory/v1"
2018-02-03 17:22:46 +05:30
)
2018-02-03 17:23:42 +05:30
const (
issuerURL = "https://accounts.google.com"
)
2018-02-06 03:23:32 +05:30
// Config holds configuration options for Google logins.
2018-02-03 17:22:46 +05:30
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 22:47:17 +05:30
// 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 22:47:17 +05:30
// 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
2018-02-03 17:22:46 +05:30
}
2018-02-06 03:23:32 +05:30
// Open returns a connector which can be used to login users through Google.
2018-02-04 22:47:17 +05:30
func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, err error) {
2018-02-03 17:22:46 +05:30
ctx, cancel := context.WithCancel(context.Background())
2018-02-03 17:23:42 +05:30
provider, err := oidc.NewProvider(ctx, issuerURL)
2018-02-03 17:22:46 +05:30
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")
}
2019-01-28 18:39:23 +05:30
srv, err := createDirectoryService(c.ServiceAccountFilePath, c.AdminEmail)
if err != nil {
cancel()
return nil, fmt.Errorf("could not create directory service: %v", err)
}
2018-02-03 17:22:46 +05:30
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},
),
2018-02-04 22:47:17 +05:30
logger: logger,
cancel: cancel,
hostedDomains: c.HostedDomains,
groups: c.Groups,
2018-02-04 22:47:17 +05:30
serviceAccountFilePath: c.ServiceAccountFilePath,
adminEmail: c.AdminEmail,
2019-01-28 18:39:23 +05:30
adminSrv: srv,
2018-02-03 17:22:46 +05:30
}, nil
}
var (
_ connector.CallbackConnector = (*googleConnector)(nil)
_ connector.RefreshConnector = (*googleConnector)(nil)
)
type googleConnector struct {
2018-02-04 22:47:17 +05:30
redirectURI string
oauth2Config *oauth2.Config
verifier *oidc.IDTokenVerifier
ctx context.Context
cancel context.CancelFunc
logger log.Logger
hostedDomains []string
groups []string
2018-02-04 22:47:17 +05:30
serviceAccountFilePath string
adminEmail string
2019-01-28 18:39:23 +05:30
adminSrv *admin.Service
2018-02-03 17:22:46 +05:30
}
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 22:47:17 +05:30
return c.createIdentity(r.Context(), identity, s, token)
2018-02-03 17:22:46 +05:30
}
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 22:47:17 +05:30
return c.createIdentity(ctx, identity, s, token)
2018-02-03 17:22:46 +05:30
}
2018-02-04 22:47:17 +05:30
func (c *googleConnector) createIdentity(ctx context.Context, identity connector.Identity, s connector.Scopes, token *oauth2.Token) (connector.Identity, error) {
2018-02-03 17:22:46 +05:30
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 22:47:17 +05:30
var groups []string
2018-02-06 03:03:17 +05:30
if s.Groups && c.adminEmail != "" && c.serviceAccountFilePath != "" {
2018-02-04 22:47:17 +05:30
groups, err = c.getGroups(claims.Email)
if err != nil {
return identity, fmt.Errorf("google: could not retrieve groups: %v", err)
}
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 22:47:17 +05:30
}
2018-02-03 17:22:46 +05:30
identity = connector.Identity{
UserID: idToken.Subject,
Username: claims.Username,
Email: claims.Email,
EmailVerified: claims.EmailVerified,
ConnectorData: []byte(token.RefreshToken),
2018-02-04 22:47:17 +05:30
Groups: groups,
2018-02-03 17:22:46 +05:30
}
return identity, nil
}
2018-02-04 22:47:17 +05:30
2018-02-06 03:23:32 +05:30
// getGroups creates a connection to the admin directory service and lists
// all groups the user is a member of
2018-02-04 22:47:17 +05:30
func (c *googleConnector) getGroups(email string) ([]string, error) {
2019-01-28 18:39:23 +05:30
groupsList, err := c.adminSrv.Groups.List().UserKey(email).Do()
2018-02-04 22:47:17 +05:30
if err != nil {
return nil, fmt.Errorf("could not list groups: %v", err)
}
var userGroups []string
for _, group := range groupsList.Groups {
2019-01-28 17:24:10 +05:30
// TODO (joelspeed): Make desried group key configurable
2018-02-04 22:47:17 +05:30
userGroups = append(userGroups, group.Email)
}
return userGroups, nil
}
2018-02-06 03:23:32 +05:30
// createDirectoryService loads a google service account credentials file,
// sets up super user impersonation and creates an admin client for calling
// the google admin api
2018-02-04 22:47:17 +05:30
func createDirectoryService(serviceAccountFilePath string, email string) (*admin.Service, error) {
jsonCredentials, err := ioutil.ReadFile(serviceAccountFilePath)
if err != nil {
return nil, fmt.Errorf("error reading credentials from file: %v", err)
}
config, err := google.JWTConfigFromJSON(jsonCredentials, admin.AdminDirectoryGroupReadonlyScope)
if err != nil {
return nil, fmt.Errorf("unable to parse client secret file to config: %v", err)
}
2018-02-06 03:23:32 +05:30
// Impersonate an admin. This is mandatory for the admin APIs.
2018-02-04 22:47:17 +05:30
config.Subject = email
ctx := context.Background()
client := config.Client(ctx)
srv, err := admin.New(client)
if err != nil {
return nil, fmt.Errorf("unable to create directory service %v", err)
}
return srv, nil
}