2019-07-28 15:54:39 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2020-05-05 17:53:33 +02:00
|
|
|
"context"
|
2020-12-12 22:57:32 +02:00
|
|
|
"fmt"
|
2019-07-28 15:54:39 +02:00
|
|
|
"net/url"
|
|
|
|
|
2020-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
|
2019-07-28 15:54:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type KeycloakProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:33 +02:00
|
|
|
var _ Provider = (*KeycloakProvider)(nil)
|
|
|
|
|
2020-05-25 14:08:04 +02:00
|
|
|
const (
|
|
|
|
keycloakProviderName = "Keycloak"
|
|
|
|
keycloakDefaultScope = "api"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Default Login URL for Keycloak.
|
|
|
|
// Pre-parsed URL of https://keycloak.org/oauth/authorize.
|
|
|
|
keycloakDefaultLoginURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "keycloak.org",
|
|
|
|
Path: "/oauth/authorize",
|
2019-07-28 15:54:39 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
|
|
|
|
// Default Redeem URL for Keycloak.
|
|
|
|
// Pre-parsed URL of ttps://keycloak.org/oauth/token.
|
|
|
|
keycloakDefaultRedeemURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "keycloak.org",
|
|
|
|
Path: "/oauth/token",
|
2019-07-28 15:54:39 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
|
|
|
|
// Default Validation URL for Keycloak.
|
|
|
|
// Pre-parsed URL of https://keycloak.org/api/v3/user.
|
|
|
|
keycloakDefaultValidateURL = &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "keycloak.org",
|
|
|
|
Path: "/api/v3/user",
|
2019-07-28 15:54:39 +02:00
|
|
|
}
|
2020-05-25 14:08:04 +02:00
|
|
|
)
|
|
|
|
|
2020-12-12 23:22:15 +02:00
|
|
|
// NewKeycloakProvider creates a KeyCloakProvider using the passed ProviderData
|
2020-05-25 14:08:04 +02:00
|
|
|
func NewKeycloakProvider(p *ProviderData) *KeycloakProvider {
|
|
|
|
p.setProviderDefaults(providerDefaults{
|
|
|
|
name: keycloakProviderName,
|
|
|
|
loginURL: keycloakDefaultLoginURL,
|
|
|
|
redeemURL: keycloakDefaultRedeemURL,
|
|
|
|
profileURL: nil,
|
|
|
|
validateURL: keycloakDefaultValidateURL,
|
|
|
|
scope: keycloakDefaultScope,
|
|
|
|
})
|
2019-07-28 15:54:39 +02:00
|
|
|
return &KeycloakProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:22:15 +02:00
|
|
|
// EnrichSession uses the Keycloak userinfo endpoint to populate the session's
|
|
|
|
// email and groups.
|
2020-12-12 22:57:32 +02:00
|
|
|
func (p *KeycloakProvider) EnrichSession(ctx context.Context, s *sessions.SessionState) error {
|
2020-12-12 23:22:15 +02:00
|
|
|
// Fallback to ValidateURL if ProfileURL not set for legacy compatibility
|
|
|
|
userinfoURL := p.ValidateURL.String()
|
|
|
|
if p.ProfileURL != nil {
|
|
|
|
userinfoURL = p.ProfileURL.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := requests.New(userinfoURL).
|
2020-07-03 20:27:25 +02:00
|
|
|
WithContext(ctx).
|
|
|
|
SetHeader("Authorization", "Bearer "+s.AccessToken).
|
2020-07-06 18:42:26 +02:00
|
|
|
Do().
|
2020-07-03 20:27:25 +02:00
|
|
|
UnmarshalJSON()
|
2019-07-28 15:54:39 +02:00
|
|
|
if err != nil {
|
2020-12-12 22:57:32 +02:00
|
|
|
logger.Errorf("failed making request %v", err)
|
|
|
|
return err
|
2019-07-28 15:54:39 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 22:57:32 +02:00
|
|
|
groups, err := json.Get("groups").StringArray()
|
2020-12-12 23:50:34 +02:00
|
|
|
if err == nil {
|
2020-12-12 22:57:32 +02:00
|
|
|
for _, group := range groups {
|
|
|
|
if group != "" {
|
|
|
|
s.Groups = append(s.Groups, group)
|
2019-07-28 15:54:39 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 22:57:32 +02:00
|
|
|
}
|
2019-07-28 15:54:39 +02:00
|
|
|
|
2020-12-12 22:57:32 +02:00
|
|
|
email, err := json.Get("email").String()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to extract email from userinfo endpoint: %v", err)
|
2019-07-28 15:54:39 +02:00
|
|
|
}
|
2020-12-12 22:57:32 +02:00
|
|
|
s.Email = email
|
2019-07-28 15:54:39 +02:00
|
|
|
|
2020-12-12 22:57:32 +02:00
|
|
|
return nil
|
2019-07-28 15:54:39 +02:00
|
|
|
}
|