diff --git a/internal/keycloak/client.go b/internal/keycloak/client.go index 64289fd..42d4cae 100644 --- a/internal/keycloak/client.go +++ b/internal/keycloak/client.go @@ -38,7 +38,10 @@ type HTTPConfig struct { // Invite mail: which client + redirect the execute-actions email uses. // Defaults keep the dev realm working; stage/prod set portal-{env} and - // the portal's public URL. + // the portal's public URL. "{slug}" in the redirect is replaced by the + // workspace slug, so "Back to application" lands in the workspace + // (e.g. https://app.breakpilot.com/start?invited=1&ws={slug}, which the + // portal forwards to //dashboard; Keycloak only allows /start*). InviteClientID string InviteRedirectURI string } diff --git a/internal/keycloak/members.go b/internal/keycloak/members.go index 598dccc..5519f10 100644 --- a/internal/keycloak/members.go +++ b/internal/keycloak/members.go @@ -148,7 +148,7 @@ func (a *HTTPAdapter) InviteMember(ctx context.Context, in MemberInviteInput) (* _ = resp.Body.Close() res := &MemberInviteResult{UserID: userID, Status: InviteStatusInvited, Created: created} - if _, merr := a.executeActionsEmail(ctx, userID, []string{"VERIFY_EMAIL", "UPDATE_PASSWORD"}, a.cfg.InviteRedirectURI); merr != nil { + if _, merr := a.executeActionsEmail(ctx, userID, []string{"VERIFY_EMAIL", "UPDATE_PASSWORD"}, a.inviteRedirect(in.Slug)); merr != nil { res.MailError = merr } return res, nil @@ -204,7 +204,7 @@ func (a *HTTPAdapter) ResendInvite(ctx context.Context, slug, email string) erro } for _, m := range members { if strings.EqualFold(m.Email, email) { - _, err := a.executeActionsEmail(ctx, m.UserID, []string{"VERIFY_EMAIL", "UPDATE_PASSWORD"}, a.cfg.InviteRedirectURI) + _, err := a.executeActionsEmail(ctx, m.UserID, []string{"VERIFY_EMAIL", "UPDATE_PASSWORD"}, a.inviteRedirect(slug)) return err } } diff --git a/internal/keycloak/members_test.go b/internal/keycloak/members_test.go index b96b0fe..c9dec4d 100644 --- a/internal/keycloak/members_test.go +++ b/internal/keycloak/members_test.go @@ -22,6 +22,7 @@ type membersKC struct { users map[string]memberRepresentation // user id → user brief bool // member list omits attributes mails []string // user ids that got the execute-actions mail + redirects []string // redirect_uri of each execute-actions mail created []userCreate puts map[string]map[string][]string // user id → attributes PUT createErr int // non-zero: POST /users answers this status @@ -181,11 +182,12 @@ func newMembersKC(t *testing.T) (*membersKC, *HTTPAdapter) { mux.HandleFunc("PUT /admin/realms/test-realm/users/{id}/execute-actions-email", func(w http.ResponseWriter, r *http.Request) { k.mu.Lock() defer k.mu.Unlock() - if r.URL.Query().Get("client_id") != "portal-test" || r.URL.Query().Get("redirect_uri") != "https://portal.test/onboard" { + if r.URL.Query().Get("client_id") != "portal-test" { http.Error(w, "bad invite client", http.StatusBadRequest) return } k.mails = append(k.mails, r.PathValue("id")) + k.redirects = append(k.redirects, r.URL.Query().Get("redirect_uri")) w.WriteHeader(http.StatusNoContent) }) mux.HandleFunc("GET /admin/realms/test-realm/users/{id}/credentials", func(w http.ResponseWriter, r *http.Request) { @@ -261,7 +263,7 @@ func newMembersKC(t *testing.T) (*membersKC, *HTTPAdapter) { t.Cleanup(srv.Close) a := NewHTTPAdapter(HTTPConfig{ BaseURL: srv.URL, Realm: "test-realm", ClientID: "c", ClientSecret: "s", Timeout: 5 * time.Second, - InviteClientID: "portal-test", InviteRedirectURI: "https://portal.test/onboard", + InviteClientID: "portal-test", InviteRedirectURI: "https://portal.test/{slug}/dashboard", }) return k, a } @@ -326,6 +328,10 @@ func TestHTTPAdapter_InviteMember_newUser(t *testing.T) { if len(k.mails) != 1 || k.mails[0] != res.UserID { t.Errorf("execute-actions mail: %v", k.mails) } + // "Back to application" in the invite must land in the workspace, not on /start. + if len(k.redirects) != 1 || k.redirects[0] != "https://portal.test/acme/dashboard" { + t.Errorf("invite redirect: %v", k.redirects) + } if got := k.members["org-acme"]; len(got) != 2 || got[1] != res.UserID { t.Errorf("membership: %v", got) } @@ -441,3 +447,15 @@ func TestListOrgMembers_findsOrgByAliasNotName(t *testing.T) { t.Fatalf("unknown alias: want ErrOrgNotFound, got %v", err) } } + +func TestHTTPAdapter_InviteRedirect(t *testing.T) { + a := NewHTTPAdapter(HTTPConfig{InviteRedirectURI: "https://app.test/{slug}/dashboard"}) + if got := a.inviteRedirect("acme"); got != "https://app.test/acme/dashboard" { + t.Errorf("slug placeholder: %s", got) + } + // A fixed redirect without the placeholder stays as configured. + b := NewHTTPAdapter(HTTPConfig{InviteRedirectURI: "https://app.test/start"}) + if got := b.inviteRedirect("acme"); got != "https://app.test/start" { + t.Errorf("fixed redirect: %s", got) + } +} diff --git a/internal/keycloak/orgs.go b/internal/keycloak/orgs.go index 451b3e4..70b46f9 100644 --- a/internal/keycloak/orgs.go +++ b/internal/keycloak/orgs.go @@ -167,7 +167,7 @@ func (a *HTTPAdapter) CreateOrgAndInvite(ctx context.Context, in InviteInput) (* if in.EmailVerified { actions = []string{"UPDATE_PASSWORD"} } - inviteURL, err := a.executeActionsEmail(ctx, userID, actions, a.cfg.InviteRedirectURI) + inviteURL, err := a.executeActionsEmail(ctx, userID, actions, a.inviteRedirect(in.Slug)) if err != nil { // Non-fatal: the account exists, the mail can be resent. return &InviteResult{OrganizationID: orgID, UserID: userID, MailError: err}, nil @@ -296,6 +296,12 @@ func (a *HTTPAdapter) findUserByEmail(ctx context.Context, email string) (string return "", errUserLookupMiss } +// inviteRedirect is the configured invite redirect with "{slug}" replaced by +// the workspace slug. +func (a *HTTPAdapter) inviteRedirect(slug string) string { + return strings.ReplaceAll(a.cfg.InviteRedirectURI, "{slug}", url.PathEscape(slug)) +} + func (a *HTTPAdapter) executeActionsEmail(ctx context.Context, userID string, actions []string, redirectURI string) (string, error) { resp, err := a.adminCall(ctx, http.MethodPut, fmt.Sprintf("/users/%s/execute-actions-email?client_id=%s&redirect_uri=%s", userID, url.QueryEscape(a.cfg.InviteClientID), url.QueryEscape(redirectURI)),