mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2024-11-24 08:52:25 +02:00
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"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"
|
|
)
|
|
|
|
type KeycloakProvider struct {
|
|
*ProviderData
|
|
}
|
|
|
|
var _ Provider = (*KeycloakProvider)(nil)
|
|
|
|
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",
|
|
}
|
|
|
|
// 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",
|
|
}
|
|
|
|
// 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",
|
|
}
|
|
)
|
|
|
|
func NewKeycloakProvider(p *ProviderData) *KeycloakProvider {
|
|
p.setProviderDefaults(providerDefaults{
|
|
name: keycloakProviderName,
|
|
loginURL: keycloakDefaultLoginURL,
|
|
redeemURL: keycloakDefaultRedeemURL,
|
|
profileURL: nil,
|
|
validateURL: keycloakDefaultValidateURL,
|
|
scope: keycloakDefaultScope,
|
|
})
|
|
return &KeycloakProvider{ProviderData: p}
|
|
}
|
|
|
|
func (p *KeycloakProvider) EnrichSession(ctx context.Context, s *sessions.SessionState) error {
|
|
json, err := requests.New(p.ValidateURL.String()).
|
|
WithContext(ctx).
|
|
SetHeader("Authorization", "Bearer "+s.AccessToken).
|
|
Do().
|
|
UnmarshalJSON()
|
|
if err != nil {
|
|
logger.Errorf("failed making request %v", err)
|
|
return err
|
|
}
|
|
|
|
groups, err := json.Get("groups").StringArray()
|
|
if err != nil {
|
|
logger.Errorf("Warning: unable to extract groups from userinfo endpoint: %v", err)
|
|
} else {
|
|
for _, group := range groups {
|
|
if group != "" {
|
|
s.Groups = append(s.Groups, group)
|
|
}
|
|
}
|
|
}
|
|
|
|
email, err := json.Get("email").String()
|
|
if err != nil {
|
|
return fmt.Errorf("unable to extract email from userinfo endpoint: %v", err)
|
|
}
|
|
s.Email = email
|
|
|
|
return nil
|
|
}
|