Files
ocis/services/invitations/pkg/backends/keycloak/backend.go
David WalterandClaude Opus 4.8 5a23c88296 feat(invitations): configurable Keycloak required actions and working ocis_full SMTP (#12467)
* feat(invitations): configurable Keycloak actions + working ocis_full SMTP

The invitations service already triggers Keycloak execute-actions-email after
provisioning a guest (service.Invite -> backend.SendMail ->
gocloak.ExecuteActionsEmail). Two things still limited it:

- The required actions were hardcoded to UPDATE_PASSWORD,VERIFY_EMAIL. They are
  now configurable via INVITATIONS_KEYCLOAK_EXECUTE_ACTIONS (comma-separated,
  same default). Unknown actions are logged and skipped, and the service falls
  back to the defaults if none remain valid so a guest always has a setup path.

- The Keycloak realm shipped with the ocis_full example had an empty smtpServer,
  so Keycloak silently dropped every mail it tried to send, including the guest
  invitation mail. The realm now points SMTP at the bundled mailpit
  (mailserver:1025).

Verified: execute-actions-email delivers to mailpit against the ocis_full realm;
go test ./services/invitations/... ./ocis-pkg/keycloak/... -race passes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: David Walter <david.walter@kiteworks.com>

* fix(invitations): return the created user as invitedUser in the response

service.Invite echoed the request body unchanged, leaving the invitedUser
relation of the Graph invitation response empty, so a client could not learn
the identity of the guest that was just created. The keycloak backend now
records the created user (OWNCLOUD_ID, email, user type) on the invitation.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: David Walter <david.walter@kiteworks.com>

* refactor(invitations): pass Keycloak actions through verbatim (no allowlist)

Address review (@kobergj): the configured execute-actions no longer need to
match a hardcoded enum. UserAction is now a string type passed to Keycloak
as-is, so operators can configure any required action their Keycloak supports
without oCIS keeping a list in sync. The convenience constants remain, the
default (UPDATE_PASSWORD, VERIFY_EMAIL) is unchanged, and validation is left to
Keycloak. Removes UserActionFromString and the two lookup maps.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: David Walter <david.walter@kiteworks.com>

---------

Signed-off-by: David Walter <david.walter@kiteworks.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-31 15:54:23 +02:00

137 lines
4.2 KiB
Go

// Package keycloak offers an invitation backend for the invitation service.
package keycloak
import (
"context"
"strings"
"github.com/google/uuid"
libregraph "github.com/owncloud/libre-graph-api-go"
"github.com/owncloud/ocis/v2/ocis-pkg/keycloak"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/invitations/pkg/invitations"
)
const (
userType = "Guest"
)
// defaultUserActions are the Keycloak required actions used when no valid
// actions are configured. They ensure an invited guest can always set a
// password and verify their email address.
var defaultUserActions = []keycloak.UserAction{
keycloak.UserActionUpdatePassword,
keycloak.UserActionVerifyEmail,
}
// Backend represents the keycloak backend.
type Backend struct {
logger log.Logger
client keycloak.Client
userRealm string
userActions []keycloak.UserAction
}
// New instantiates a new keycloak.Backend with a default gocloak client.
func New(
logger log.Logger,
baseURL, clientID, clientSecret, clientRealm, userRealm string,
insecureSkipVerify bool,
executeActions []string,
) *Backend {
logger = log.Logger{
Logger: logger.With().
Str("invitationBackend", "keycloak").
Str("clientID", clientID).
Str("clientRealm", clientRealm).
Str("userRealm", userRealm).
Logger(),
}
client := keycloak.New(baseURL, clientID, clientSecret, clientRealm, insecureSkipVerify)
return NewWithClient(logger, client, userRealm, parseUserActions(executeActions))
}
// NewWithClient creates a new backend with the supplied keycloak client.
func NewWithClient(
logger log.Logger,
client keycloak.Client,
userRealm string,
userActions []keycloak.UserAction,
) *Backend {
if len(userActions) == 0 {
userActions = defaultUserActions
}
return &Backend{
logger: logger,
client: client,
userRealm: userRealm,
userActions: userActions,
}
}
// CreateUser creates a user in the keycloak backend.
func (b Backend) CreateUser(ctx context.Context, invitation *invitations.Invitation) (string, error) {
u := uuid.New()
b.logger.Info().
Str("email", invitation.InvitedUserEmailAddress).
Msg("Creating new user")
user := &libregraph.User{
Mail: &invitation.InvitedUserEmailAddress,
AccountEnabled: boolP(true),
OnPremisesSamAccountName: invitation.InvitedUserEmailAddress,
Id: stringP(u.String()),
UserType: stringP(userType),
}
id, err := b.client.CreateUser(ctx, b.userRealm, user, b.userActions)
if err != nil {
b.logger.Error().
Str("userID", u.String()).
Str("email", invitation.InvitedUserEmailAddress).
Err(err).
Msg("Failed to create user")
return "", err
}
// Record the created user on the invitation so the service can return it as
// the invitedUser relation of the Graph invitation response. user.Id holds the
// OWNCLOUD_ID, i.e. the identity oCIS uses once the guest is provisioned locally
// (not the Keycloak-internal id returned for sending the actions mail).
invitation.InvitedUser = user
return id, nil
}
// CanSendMail returns true because keycloak does allow sending mail.
func (b Backend) CanSendMail() bool { return true }
// SendMail sends a mail to the user with details on how to redeem the invitation.
func (b Backend) SendMail(ctx context.Context, id string) error {
return b.client.SendActionsMail(ctx, b.userRealm, id, b.userActions)
}
// parseUserActions converts the configured Keycloak required-action strings
// into UserActions. Each non-empty value is passed through to Keycloak as-is
// (Keycloak validates the actions), so operators can configure any action their
// Keycloak supports without this list having to be kept in sync. If none are
// configured, the defaults (UPDATE_PASSWORD, VERIFY_EMAIL) are used so an
// invited guest always has a way to set up their account.
func parseUserActions(executeActions []string) []keycloak.UserAction {
actions := make([]keycloak.UserAction, 0, len(executeActions))
for _, a := range executeActions {
a = strings.TrimSpace(a)
if a == "" {
continue
}
actions = append(actions, keycloak.UserAction(a))
}
if len(actions) == 0 {
return defaultUserActions
}
return actions
}
func boolP(b bool) *bool { return &b }
func stringP(s string) *string { return &s }