diff --git a/changelog/unreleased/bugfix-invitations-return-invited-user.md b/changelog/unreleased/bugfix-invitations-return-invited-user.md new file mode 100644 index 00000000000..827c1d2d81c --- /dev/null +++ b/changelog/unreleased/bugfix-invitations-return-invited-user.md @@ -0,0 +1,10 @@ +Bugfix: Return the created user in the invitation response + +The invitations service created the guest account but returned the original request +body unchanged, so the `invitedUser` relation defined by the Graph invitation +response was always empty. Clients had no way to learn the identity of the user that +was just created. The service now populates `invitedUser` with the created user (id, +email address and user type), where the id is the OWNCLOUD_ID that oCIS uses once the +guest is provisioned locally. + +https://github.com/owncloud/ocis/pull/12467 diff --git a/changelog/unreleased/enhancement-invitations-keycloak-execute-actions.md b/changelog/unreleased/enhancement-invitations-keycloak-execute-actions.md new file mode 100644 index 00000000000..ceaa5ce64cd --- /dev/null +++ b/changelog/unreleased/enhancement-invitations-keycloak-execute-actions.md @@ -0,0 +1,16 @@ +Enhancement: Configurable Keycloak invitation actions and working ocis_full SMTP + +The invitations service now lets operators configure the Keycloak required actions +that are sent to invited guests via the execute-actions email, using the new +`INVITATIONS_KEYCLOAK_EXECUTE_ACTIONS` environment variable. It defaults to +`UPDATE_PASSWORD,VERIFY_EMAIL`; configured values are passed to Keycloak as-is, and +the service falls back to the defaults when none are configured, so an invited guest +always has a way to set up their account. + +The Keycloak realm shipped with the `ocis_full` example deployment now points its +SMTP server at the bundled mailpit instance. Previously the realm shipped with an +empty SMTP configuration, so Keycloak silently dropped every mail it tried to send +(including the guest invitation email) even though the invitations service +correctly requested it. + +https://github.com/owncloud/ocis/pull/12467 diff --git a/deployments/examples/ocis_full/config/keycloak/ocis-realm.dist.json b/deployments/examples/ocis_full/config/keycloak/ocis-realm.dist.json index 63200a3ac07..e9fe3acbcaa 100644 --- a/deployments/examples/ocis_full/config/keycloak/ocis-realm.dist.json +++ b/deployments/examples/ocis_full/config/keycloak/ocis-realm.dist.json @@ -1964,7 +1964,15 @@ "contentSecurityPolicy": "frame-src 'self'; frame-ancestors 'self'; object-src 'none';", "strictTransportSecurity": "max-age=31536000; includeSubDomains" }, - "smtpServer": {}, + "smtpServer": { + "host": "mailserver", + "port": "1025", + "from": "keycloak@owncloud.test", + "fromDisplayName": "oCIS", + "ssl": "false", + "starttls": "false", + "auth": "false" + }, "eventsEnabled": false, "eventsListeners": [ "jboss-logging" diff --git a/ocis-pkg/keycloak/client.go b/ocis-pkg/keycloak/client.go index c41381e710e..346279c3594 100644 --- a/ocis-pkg/keycloak/client.go +++ b/ocis-pkg/keycloak/client.go @@ -213,7 +213,7 @@ func (c *ConcreteClient) getKeyCloakID(u *libregraph.User) (string, error) { func convertUserActions(userActions []UserAction) *[]string { stringActions := make([]string, len(userActions)) for i, a := range userActions { - stringActions[i] = userActionsToString[a] + stringActions[i] = string(a) } return &stringActions } diff --git a/ocis-pkg/keycloak/types.go b/ocis-pkg/keycloak/types.go index 632d0975c0f..a2ef9e0810e 100644 --- a/ocis-pkg/keycloak/types.go +++ b/ocis-pkg/keycloak/types.go @@ -7,23 +7,38 @@ import ( libregraph "github.com/owncloud/libre-graph-api-go" ) -// UserAction defines a type for user actions -type UserAction int8 +// UserAction is a Keycloak required-action provider ID. It is a free-form +// string so operators can configure any action Keycloak supports; it is passed +// to Keycloak verbatim. The constants below are the common built-ins, provided +// for convenience only - the list does not need to be exhaustive or kept in +// sync with Keycloak. +type UserAction string -// An incomplete list of UserActions const ( // UserActionUpdatePassword sets it that the user needs to change their password. - UserActionUpdatePassword UserAction = iota + UserActionUpdatePassword UserAction = "UPDATE_PASSWORD" // UserActionVerifyEmail sets it that the user needs to verify their email address. - UserActionVerifyEmail + UserActionVerifyEmail UserAction = "VERIFY_EMAIL" + // UserActionUpdateProfile sets it that the user needs to update their profile. + UserActionUpdateProfile UserAction = "UPDATE_PROFILE" + // UserActionConfigureTOTP sets it that the user needs to configure a one-time password. + UserActionConfigureTOTP UserAction = "CONFIGURE_TOTP" + // UserActionTermsAndConditions sets it that the user needs to accept the terms and conditions. + UserActionTermsAndConditions UserAction = "TERMS_AND_CONDITIONS" + // UserActionVerifyProfile sets it that the user needs to verify their profile. + UserActionVerifyProfile UserAction = "VERIFY_PROFILE" + // UserActionDeleteAccount sets it that the user is allowed to delete their account. + UserActionDeleteAccount UserAction = "delete_account" + // UserActionUpdateLocale sets it that the user needs to update their locale. + UserActionUpdateLocale UserAction = "update_user_locale" + // UserActionWebauthnRegister sets it that the user needs to register a WebAuthn authenticator. + UserActionWebauthnRegister UserAction = "webauthn-register" + // UserActionWebauthnRegisterPasswordless sets it that the user needs to register a passwordless WebAuthn authenticator. + UserActionWebauthnRegisterPasswordless UserAction = "webauthn-register-passwordless" + // UserActionConfigureRecoveryAuthnCodes sets it that the user needs to configure recovery authentication codes. + UserActionConfigureRecoveryAuthnCodes UserAction = "CONFIGURE_RECOVERY_AUTHN_CODES" ) -// A lookup table to translate user actions to their string equivalents -var userActionsToString = map[UserAction]string{ - UserActionUpdatePassword: "UPDATE_PASSWORD", - UserActionVerifyEmail: "VERIFY_EMAIL", -} - // PIIReport is a structure of all the PersonalIdentifiableInformation contained in keycloak. type PIIReport struct { UserData *libregraph.User diff --git a/ocis-pkg/keycloak/types_test.go b/ocis-pkg/keycloak/types_test.go new file mode 100644 index 00000000000..d6c957c634d --- /dev/null +++ b/ocis-pkg/keycloak/types_test.go @@ -0,0 +1,21 @@ +package keycloak_test + +import ( + "testing" + + "github.com/owncloud/ocis/v2/ocis-pkg/keycloak" + "github.com/stretchr/testify/assert" +) + +func TestUserActionStringValues(t *testing.T) { + // UserAction is a free-form string passed to Keycloak verbatim; the + // convenience constants must carry the exact Keycloak required-action + // provider IDs. + assert.Equal(t, "UPDATE_PASSWORD", string(keycloak.UserActionUpdatePassword)) + assert.Equal(t, "VERIFY_EMAIL", string(keycloak.UserActionVerifyEmail)) + assert.Equal(t, "delete_account", string(keycloak.UserActionDeleteAccount)) + assert.Equal(t, "webauthn-register", string(keycloak.UserActionWebauthnRegister)) + + // Arbitrary, non-builtin actions are valid - there is no allowlist. + assert.Equal(t, "CUSTOM_ACTION", string(keycloak.UserAction("CUSTOM_ACTION"))) +} diff --git a/services/invitations/pkg/backends/keycloak/backend.go b/services/invitations/pkg/backends/keycloak/backend.go index 85d98aceaae..b1fc7290f10 100644 --- a/services/invitations/pkg/backends/keycloak/backend.go +++ b/services/invitations/pkg/backends/keycloak/backend.go @@ -3,6 +3,7 @@ package keycloak import ( "context" + "strings" "github.com/google/uuid" libregraph "github.com/owncloud/libre-graph-api-go" @@ -15,16 +16,20 @@ const ( userType = "Guest" ) -var userRequiredActions = []keycloak.UserAction{ +// 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 + logger log.Logger + client keycloak.Client + userRealm string + userActions []keycloak.UserAction } // New instantiates a new keycloak.Backend with a default gocloak client. @@ -32,6 +37,7 @@ func New( logger log.Logger, baseURL, clientID, clientSecret, clientRealm, userRealm string, insecureSkipVerify bool, + executeActions []string, ) *Backend { logger = log.Logger{ Logger: logger.With(). @@ -42,7 +48,7 @@ func New( Logger(), } client := keycloak.New(baseURL, clientID, clientSecret, clientRealm, insecureSkipVerify) - return NewWithClient(logger, client, userRealm) + return NewWithClient(logger, client, userRealm, parseUserActions(executeActions)) } // NewWithClient creates a new backend with the supplied keycloak client. @@ -50,11 +56,16 @@ 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, + logger: logger, + client: client, + userRealm: userRealm, + userActions: userActions, } } @@ -73,7 +84,7 @@ func (b Backend) CreateUser(ctx context.Context, invitation *invitations.Invitat UserType: stringP(userType), } - id, err := b.client.CreateUser(ctx, b.userRealm, user, userRequiredActions) + id, err := b.client.CreateUser(ctx, b.userRealm, user, b.userActions) if err != nil { b.logger.Error(). Str("userID", u.String()). @@ -83,6 +94,12 @@ func (b Backend) CreateUser(ctx context.Context, invitation *invitations.Invitat 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 } @@ -91,7 +108,28 @@ 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, userRequiredActions) + 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 } diff --git a/services/invitations/pkg/backends/keycloak/backend_test.go b/services/invitations/pkg/backends/keycloak/backend_test.go index 4d4a197c335..3179fc02470 100644 --- a/services/invitations/pkg/backends/keycloak/backend_test.go +++ b/services/invitations/pkg/backends/keycloak/backend_test.go @@ -36,18 +36,20 @@ func TestBackend_CreateUser(t *testing.T) { tests := []struct { name string args args + userActions []kcpkg.UserAction want string clientMocks []mockInputs assertion assert.ErrorAssertionFunc }{ { - name: "Test without diplay name", + name: "nil actions fall back to the default actions", args: args{ invitation: &invitations.Invitation{ InvitedUserEmailAddress: "test@example.org", }, }, - want: "test-id", + userActions: nil, + want: "test-id", clientMocks: []mockInputs{ { funcName: "CreateUser", @@ -70,6 +72,36 @@ func TestBackend_CreateUser(t *testing.T) { return assert.Nil(t, err, args...) }, }, + { + name: "configured actions are passed through to keycloak", + args: args{ + invitation: &invitations.Invitation{ + InvitedUserEmailAddress: "test@example.org", + }, + }, + userActions: []kcpkg.UserAction{kcpkg.UserActionUpdatePassword}, + want: "test-id", + clientMocks: []mockInputs{ + { + funcName: "CreateUser", + args: []interface{}{ + mock.Anything, + userRealm, + mock.Anything, + []kcpkg.UserAction{ + kcpkg.UserActionUpdatePassword, + }, + }, + returns: []interface{}{ + "test-id", + nil, + }, + }, + }, + assertion: func(t assert.TestingT, err error, args ...interface{}) bool { + return assert.Nil(t, err, args...) + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -78,10 +110,16 @@ func TestBackend_CreateUser(t *testing.T) { for _, m := range tt.clientMocks { c.On(m.funcName, m.args...).Return(m.returns...) } - b := keycloak.NewWithClient(log.NopLogger(), c, userRealm) + b := keycloak.NewWithClient(log.NopLogger(), c, userRealm, tt.userActions) got, err := b.CreateUser(ctx, tt.args.invitation) tt.assertion(t, err) assert.Equal(t, tt.want, got) + // On success the created user is recorded on the invitation as invitedUser. + if err == nil && assert.NotNil(t, tt.args.invitation.InvitedUser) { + assert.Equal(t, tt.args.invitation.InvitedUserEmailAddress, tt.args.invitation.InvitedUser.GetMail()) + assert.Equal(t, "Guest", tt.args.invitation.InvitedUser.GetUserType()) + assert.NotEmpty(t, tt.args.invitation.InvitedUser.GetId()) + } }) } } @@ -98,14 +136,16 @@ func TestBackend_SendMail(t *testing.T) { tests := []struct { name string args args + userActions []kcpkg.UserAction clientMocks []mockInputs assertion assert.ErrorAssertionFunc }{ { - name: "Mail successfully sent", + name: "mail successfully sent with default actions", args: args{ id: "test-id", }, + userActions: nil, clientMocks: []mockInputs{ { funcName: "SendActionsMail", @@ -127,6 +167,32 @@ func TestBackend_SendMail(t *testing.T) { return assert.Nil(t, err, args...) }, }, + { + name: "mail sent with only the configured action", + args: args{ + id: "test-id", + }, + userActions: []kcpkg.UserAction{kcpkg.UserActionUpdatePassword}, + clientMocks: []mockInputs{ + { + funcName: "SendActionsMail", + args: []interface{}{ + mock.Anything, + userRealm, + "test-id", + []kcpkg.UserAction{ + kcpkg.UserActionUpdatePassword, + }, + }, + returns: []interface{}{ + nil, + }, + }, + }, + assertion: func(t assert.TestingT, err error, args ...interface{}) bool { + return assert.Nil(t, err, args...) + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -135,7 +201,7 @@ func TestBackend_SendMail(t *testing.T) { for _, m := range tt.clientMocks { c.On(m.funcName, m.args...).Return(m.returns...) } - b := keycloak.NewWithClient(log.NopLogger(), c, userRealm) + b := keycloak.NewWithClient(log.NopLogger(), c, userRealm, tt.userActions) tt.assertion(t, b.SendMail(ctx, tt.args.id)) }) } diff --git a/services/invitations/pkg/config/config.go b/services/invitations/pkg/config/config.go index f0635190e37..77477fbc282 100644 --- a/services/invitations/pkg/config/config.go +++ b/services/invitations/pkg/config/config.go @@ -26,10 +26,11 @@ type Config struct { // Keycloak configuration type Keycloak struct { - BasePath string `yaml:"base_path" env:"OCIS_KEYCLOAK_BASE_PATH;INVITATIONS_KEYCLOAK_BASE_PATH" desc:"The URL to access keycloak." introductionVersion:"pre5.0"` - ClientID string `yaml:"client_id" env:"OCIS_KEYCLOAK_CLIENT_ID;INVITATIONS_KEYCLOAK_CLIENT_ID" desc:"The client ID to authenticate with keycloak." introductionVersion:"pre5.0"` - ClientSecret string `yaml:"client_secret" env:"OCIS_KEYCLOAK_CLIENT_SECRET;INVITATIONS_KEYCLOAK_CLIENT_SECRET" desc:"The client secret to use in authentication." introductionVersion:"pre5.0"` - ClientRealm string `yaml:"client_realm" env:"OCIS_KEYCLOAK_CLIENT_REALM;INVITATIONS_KEYCLOAK_CLIENT_REALM" desc:"The realm the client is defined in." introductionVersion:"pre5.0"` - UserRealm string `yaml:"user_realm" env:"OCIS_KEYCLOAK_USER_REALM;INVITATIONS_KEYCLOAK_USER_REALM" desc:"The realm users are defined." introductionVersion:"pre5.0"` - InsecureSkipVerify bool `yaml:"insecure_skip_verify" env:"OCIS_KEYCLOAK_INSECURE_SKIP_VERIFY;INVITATIONS_KEYCLOAK_INSECURE_SKIP_VERIFY" desc:"Disable TLS certificate validation for Keycloak connections. Do not set this in production environments." introductionVersion:"pre5.0"` + BasePath string `yaml:"base_path" env:"OCIS_KEYCLOAK_BASE_PATH;INVITATIONS_KEYCLOAK_BASE_PATH" desc:"The URL to access keycloak." introductionVersion:"pre5.0"` + ClientID string `yaml:"client_id" env:"OCIS_KEYCLOAK_CLIENT_ID;INVITATIONS_KEYCLOAK_CLIENT_ID" desc:"The client ID to authenticate with keycloak." introductionVersion:"pre5.0"` + ClientSecret string `yaml:"client_secret" env:"OCIS_KEYCLOAK_CLIENT_SECRET;INVITATIONS_KEYCLOAK_CLIENT_SECRET" desc:"The client secret to use in authentication." introductionVersion:"pre5.0"` + ClientRealm string `yaml:"client_realm" env:"OCIS_KEYCLOAK_CLIENT_REALM;INVITATIONS_KEYCLOAK_CLIENT_REALM" desc:"The realm the client is defined in." introductionVersion:"pre5.0"` + UserRealm string `yaml:"user_realm" env:"OCIS_KEYCLOAK_USER_REALM;INVITATIONS_KEYCLOAK_USER_REALM" desc:"The realm users are defined." introductionVersion:"pre5.0"` + InsecureSkipVerify bool `yaml:"insecure_skip_verify" env:"OCIS_KEYCLOAK_INSECURE_SKIP_VERIFY;INVITATIONS_KEYCLOAK_INSECURE_SKIP_VERIFY" desc:"Disable TLS certificate validation for Keycloak connections. Do not set this in production environments." introductionVersion:"pre5.0"` + ExecuteActions []string `yaml:"execute_actions" env:"INVITATIONS_KEYCLOAK_EXECUTE_ACTIONS" desc:"A comma-separated list of Keycloak required actions sent to invited guests via the execute-actions email. Values are passed to Keycloak as-is. Defaults to 'UPDATE_PASSWORD,VERIFY_EMAIL'. See the Environment Variable Types description for more details." introductionVersion:"NEXT"` } diff --git a/services/invitations/pkg/config/defaults/defaultconfig.go b/services/invitations/pkg/config/defaults/defaultconfig.go index 7c4f4cfde98..bc64d239995 100644 --- a/services/invitations/pkg/config/defaults/defaultconfig.go +++ b/services/invitations/pkg/config/defaults/defaultconfig.go @@ -33,11 +33,12 @@ func DefaultConfig() *config.Config { Name: "invitations", }, Keycloak: config.Keycloak{ - BasePath: "", - ClientID: "", - ClientSecret: "", - ClientRealm: "", - UserRealm: "", + BasePath: "", + ClientID: "", + ClientSecret: "", + ClientRealm: "", + UserRealm: "", + ExecuteActions: []string{"UPDATE_PASSWORD", "VERIFY_EMAIL"}, }, } } diff --git a/services/invitations/pkg/service/v0/service.go b/services/invitations/pkg/service/v0/service.go index 791c8c39d2e..319bec20369 100644 --- a/services/invitations/pkg/service/v0/service.go +++ b/services/invitations/pkg/service/v0/service.go @@ -57,6 +57,7 @@ func New(opts ...Option) (Service, error) { options.Config.Keycloak.ClientRealm, options.Config.Keycloak.UserRealm, options.Config.Keycloak.InsecureSkipVerify, + options.Config.Keycloak.ExecuteActions, ) return svc{