2023-01-23 07:11:40 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2023-10-17 15:19:51 +00:00
|
|
|
openid "github.com/zitadel/oidc/v3/pkg/oidc"
|
2023-01-23 07:11:40 +00:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/idp"
|
|
|
|
"github.com/zitadel/zitadel/internal/idp/providers/oidc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
issuer = "https://accounts.google.com"
|
|
|
|
name = "Google"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ idp.Provider = (*Provider)(nil)
|
|
|
|
|
|
|
|
// Provider is the [idp.Provider] implementation for Google
|
|
|
|
type Provider struct {
|
|
|
|
*oidc.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a Google provider using the [oidc.Provider] (OIDC generic provider)
|
2023-02-28 20:20:58 +00:00
|
|
|
func New(clientID, clientSecret, redirectURI string, scopes []string, opts ...oidc.ProviderOpts) (*Provider, error) {
|
2023-03-13 16:34:29 +00:00
|
|
|
rp, err := oidc.New(name, issuer, clientID, clientSecret, redirectURI, scopes, userMapper, append(opts, oidc.WithSelectAccount())...)
|
2023-01-23 07:11:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Provider{
|
|
|
|
Provider: rp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-28 11:28:56 +00:00
|
|
|
var userMapper = func(info *openid.UserInfo) idp.User {
|
2023-01-23 07:11:40 +00:00
|
|
|
return &User{oidc.DefaultMapper(info)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// User is a representation of the authenticated Google and implements the [idp.User] interface
|
|
|
|
// by wrapping an [idp.User] (implemented by [oidc.User]). It overwrites the [GetPreferredUsername] to use the `email` claim.
|
|
|
|
type User struct {
|
|
|
|
idp.User
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPreferredUsername implements the [idp.User] interface.
|
|
|
|
// It returns the email, because Google does not return a username.
|
|
|
|
func (u *User) GetPreferredUsername() string {
|
2023-03-14 19:20:38 +00:00
|
|
|
return string(u.GetEmail())
|
2023-01-23 07:11:40 +00:00
|
|
|
}
|