fix(invite): Back to application lands in the workspace #79

Merged
sharang merged 2 commits from fix/invite-redirect-to-workspace into main 2026-10-19 08:38:19 +00:00
6 changed files with 39 additions and 12 deletions

No files matched your search

+2 -2
View File
@@ -4,7 +4,7 @@ go 1.26.0
require (
github.com/coreos/go-oidc/v3 v3.20.0
github.com/getkin/kin-openapi v0.144.0
github.com/getkin/kin-openapi v0.149.0
github.com/go-jose/go-jose/v4 v4.1.4
github.com/golang-migrate/migrate/v4 v4.19.1
github.com/google/uuid v1.6.0
@@ -68,7 +68,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 // indirect
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 // indirect
github.com/shirou/gopsutil/v4 v4.26.3 // indirect
github.com/sirupsen/logrus v1.9.4 // indirect
github.com/stretchr/testify v1.11.1 // indirect
+4 -4
View File
@@ -45,8 +45,8 @@ github.com/ebitengine/purego v0.10.0 h1:QIw4xfpWT6GWTzaW5XEKy3HXoqrJGx1ijYHzTF0/
github.com/ebitengine/purego v0.10.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/getkin/kin-openapi v0.144.0 h1:hIRcTH+KjLfkLpYU6bSSfdFpi0fZi1fp+hSPi4aQu9Y=
github.com/getkin/kin-openapi v0.144.0/go.mod h1:3BH9M9XDe/y9M5DSvEocVYAYq1w0qrhJHjC/vZi0AaY=
github.com/getkin/kin-openapi v0.149.0 h1:ZbhmVJ4yq5RZDUsyP8lcBcGMsjsaTqXEFt6isdtMDfA=
github.com/getkin/kin-openapi v0.149.0/go.mod h1:1+BHDzstro+P5CKtPy1X4PfofnFgmRe6uvMy9+r9fKY=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-jose/go-jose/v4 v4.1.4 h1:moDMcTHmvE6Groj34emNPLs/qtYXRVcd6S7NHbHz3kA=
@@ -151,8 +151,8 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 h1:KRzFb2m7YtdldCEkzs6KqmJw4nqEVZGK7IN2kJkjTuQ=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 h1:1EYB5IzjZawrrnELUi78f9fPu57HuXjmddZPjrls/28=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
github.com/shirou/gopsutil/v4 v4.26.3 h1:2ESdQt90yU3oXF/CdOlRCJxrP+Am1aBYubTMTfxJ1qc=
github.com/shirou/gopsutil/v4 v4.26.3/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ=
github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w=
+4 -1
View File
@@ -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 /<slug>/dashboard; Keycloak only allows /start*).
InviteClientID string
InviteRedirectURI string
}
+2 -2
View File
@@ -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
}
}
+20 -2
View File
@@ -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)
}
}
+7 -1
View File
@@ -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)),