-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add user invite link feature for embedded IdP #5157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
f28b086
Add user invite link feature for embedded IdP
braginini c4cf97c
Add OpenAPI definitions for new endpoints
braginini 00306ea
Add best-effort IdP user rollback
braginini d02cc3c
Fix OpenAI definition
braginini 114c362
Add invited by to invite info and account invites endpoint
braginini 9c5d62d
Regenerate invite by ID
braginini 8e56427
Add invite delete
braginini 7ed5887
Add missing activities
braginini 2229f40
The bypass path for invite acceptance is now more restrictive
braginini 7e41d68
Don't swallow JSON malformed requests in invites
braginini 39698e7
Don't ignore store errors when looking up invites
braginini 32a8f5f
Add password constraints
braginini 05a895f
Fix checksum padding to avoid spaces in tokens.
braginini ea83e54
Add password validation
braginini 3806c33
Add invites handler tests
braginini 26d369a
Add invites manager test
braginini 59de054
Update invite instead of creating a new one when regenerating
braginini 54ae2a4
Fix lint
braginini 2de4c1a
Add rate limiter
braginini a5dd162
Rate limiter only considers remote addr
braginini 97b9010
Add rate limiter test
braginini e6ec1c0
Add Istance Version Endpoints (#5179)
braginini ab4d1e4
Remove unnecessary HTTP methods check
braginini b525382
Unify response handling
braginini f28561f
Rename invite_link to invite_token
braginini abb5363
Fix tests
braginini c6608bd
add store tests for invites
braginini d6606d5
Remove unnecessary transaction when regenerating invite
braginini 607ad52
Add user invite test
braginini d43f8af
Add minimum invite expiration
braginini a664640
Add DELETE invite endpoint definition
braginini 0af74f4
Make error handling consistent
braginini e44693a
Remove unused DELETE method check
braginini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
management/server/http/handlers/users/invites_handler.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| package users | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
|
|
||
| "github.com/gorilla/mux" | ||
|
|
||
| "github.com/netbirdio/netbird/management/server/account" | ||
| nbcontext "github.com/netbirdio/netbird/management/server/context" | ||
| "github.com/netbirdio/netbird/management/server/types" | ||
| "github.com/netbirdio/netbird/shared/management/http/api" | ||
| "github.com/netbirdio/netbird/shared/management/http/util" | ||
| "github.com/netbirdio/netbird/shared/management/status" | ||
| ) | ||
|
|
||
| // invitesHandler handles user invite operations | ||
| type invitesHandler struct { | ||
| accountManager account.Manager | ||
| } | ||
|
|
||
| // AddInvitesEndpoints registers invite-related endpoints | ||
| func AddInvitesEndpoints(accountManager account.Manager, router *mux.Router) { | ||
| h := &invitesHandler{accountManager: accountManager} | ||
|
|
||
| // Authenticated endpoints (require admin) | ||
| router.HandleFunc("/users/invites", h.listInvites).Methods("GET", "OPTIONS") | ||
| router.HandleFunc("/users/invites", h.createInvite).Methods("POST", "OPTIONS") | ||
| router.HandleFunc("/users/invites/{inviteId}/regenerate", h.regenerateInvite).Methods("POST", "OPTIONS") | ||
| } | ||
|
|
||
| // AddPublicInvitesEndpoints registers public (unauthenticated) invite endpoints | ||
| func AddPublicInvitesEndpoints(accountManager account.Manager, router *mux.Router) { | ||
| h := &invitesHandler{accountManager: accountManager} | ||
|
|
||
| // Public endpoints (no auth required, protected by token) | ||
| router.HandleFunc("/users/invites/{token}", h.getInviteInfo).Methods("GET", "OPTIONS") | ||
| router.HandleFunc("/users/invites/{token}/accept", h.acceptInvite).Methods("POST", "OPTIONS") | ||
| } | ||
|
|
||
| // listInvites handles GET /api/users/invites | ||
| func (h *invitesHandler) listInvites(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodGet { | ||
| util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w) | ||
| return | ||
| } | ||
|
|
||
| userAuth, err := nbcontext.GetUserAuthFromContext(r.Context()) | ||
| if err != nil { | ||
| util.WriteError(r.Context(), err, w) | ||
| return | ||
| } | ||
|
|
||
| invites, err := h.accountManager.ListUserInvites(r.Context(), userAuth.AccountId, userAuth.UserId) | ||
| if err != nil { | ||
| util.WriteError(r.Context(), err, w) | ||
| return | ||
| } | ||
|
|
||
| resp := make([]api.UserInviteListItem, 0, len(invites)) | ||
| for _, invite := range invites { | ||
| autoGroups := invite.AutoGroups | ||
| if autoGroups == nil { | ||
| autoGroups = []string{} | ||
| } | ||
| resp = append(resp, api.UserInviteListItem{ | ||
| Id: invite.ID, | ||
| Email: invite.Email, | ||
| Name: invite.Name, | ||
| Role: invite.Role, | ||
| AutoGroups: autoGroups, | ||
| ExpiresAt: invite.ExpiresAt.UTC(), | ||
| CreatedAt: invite.CreatedAt.UTC(), | ||
| Expired: invite.IsExpired(), | ||
| }) | ||
| } | ||
|
|
||
| util.WriteJSONObject(r.Context(), w, resp) | ||
| } | ||
|
|
||
| // createInvite handles POST /api/users/invites | ||
| func (h *invitesHandler) createInvite(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPost { | ||
| util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w) | ||
| return | ||
| } | ||
|
|
||
| userAuth, err := nbcontext.GetUserAuthFromContext(r.Context()) | ||
| if err != nil { | ||
| util.WriteError(r.Context(), err, w) | ||
| return | ||
| } | ||
|
|
||
| var req api.UserInviteCreateRequest | ||
| if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
| util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w) | ||
| return | ||
| } | ||
|
|
||
| invite := &types.UserInfo{ | ||
| Email: req.Email, | ||
| Name: req.Name, | ||
| Role: req.Role, | ||
| AutoGroups: req.AutoGroups, | ||
| } | ||
|
|
||
| expiresIn := 0 | ||
| if req.ExpiresIn != nil { | ||
| expiresIn = *req.ExpiresIn | ||
| } | ||
|
|
||
| result, err := h.accountManager.CreateUserInvite(r.Context(), userAuth.AccountId, userAuth.UserId, invite, expiresIn) | ||
| if err != nil { | ||
| util.WriteError(r.Context(), err, w) | ||
| return | ||
| } | ||
|
|
||
| autoGroups := result.UserInfo.AutoGroups | ||
| if autoGroups == nil { | ||
| autoGroups = []string{} | ||
| } | ||
|
|
||
| expiresAt := result.InviteExpiresAt.UTC() | ||
| util.WriteJSONObject(r.Context(), w, &api.UserInviteCreateResponse{ | ||
|
braginini marked this conversation as resolved.
Outdated
|
||
| Id: result.UserInfo.ID, | ||
| Email: result.UserInfo.Email, | ||
| Name: result.UserInfo.Name, | ||
| Role: result.UserInfo.Role, | ||
| AutoGroups: autoGroups, | ||
| Status: result.UserInfo.Status, | ||
| InviteLink: result.InviteLink, | ||
| InviteExpiresAt: expiresAt, | ||
| }) | ||
| } | ||
|
|
||
| // getInviteInfo handles GET /api/users/invites/{token} | ||
| func (h *invitesHandler) getInviteInfo(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodGet { | ||
| util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w) | ||
| return | ||
| } | ||
|
|
||
| vars := mux.Vars(r) | ||
| token := vars["token"] | ||
| if token == "" { | ||
| util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "token is required"), w) | ||
| return | ||
| } | ||
|
|
||
| info, err := h.accountManager.GetUserInviteInfo(r.Context(), token) | ||
| if err != nil { | ||
| util.WriteError(r.Context(), err, w) | ||
| return | ||
| } | ||
|
|
||
| expiresAt := info.ExpiresAt.UTC() | ||
| util.WriteJSONObject(r.Context(), w, &api.UserInviteInfo{ | ||
| Email: info.Email, | ||
| Name: info.Name, | ||
| ExpiresAt: expiresAt, | ||
| Valid: info.Valid, | ||
| InvitedBy: info.InvitedBy, | ||
| }) | ||
| } | ||
|
|
||
| // acceptInvite handles POST /api/users/invites/{token}/accept | ||
| func (h *invitesHandler) acceptInvite(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPost { | ||
| util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w) | ||
| return | ||
| } | ||
|
|
||
| vars := mux.Vars(r) | ||
| token := vars["token"] | ||
| if token == "" { | ||
| util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "token is required"), w) | ||
| return | ||
| } | ||
|
|
||
| var req api.UserInviteAcceptRequest | ||
| if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
| util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w) | ||
| return | ||
| } | ||
|
|
||
| err := h.accountManager.AcceptUserInvite(r.Context(), token, req.Password) | ||
| if err != nil { | ||
| util.WriteError(r.Context(), err, w) | ||
| return | ||
| } | ||
|
braginini marked this conversation as resolved.
|
||
|
|
||
| util.WriteJSONObject(r.Context(), w, &api.UserInviteAcceptResponse{Success: true}) | ||
| } | ||
|
|
||
| // regenerateInvite handles POST /api/users/invites/{inviteId}/regenerate | ||
| func (h *invitesHandler) regenerateInvite(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPost { | ||
| util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w) | ||
| return | ||
| } | ||
|
Comment on lines
+195
to
+198
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OPTIONS is registered but always rejected here. This handler returns 405 for any non‑POST request, so registered OPTIONS requests will fail. Either handle OPTIONS explicitly or remove it from the route. 🐛 Suggested fix (handle OPTIONS explicitly)- if r.Method != http.MethodPost {
+ if r.Method == http.MethodOptions {
+ w.WriteHeader(http.StatusOK)
+ return
+ }
+ if r.Method != http.MethodPost {
util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w)
return
}🤖 Prompt for AI Agents |
||
|
|
||
| userAuth, err := nbcontext.GetUserAuthFromContext(r.Context()) | ||
| if err != nil { | ||
| util.WriteError(r.Context(), err, w) | ||
| return | ||
| } | ||
|
|
||
| vars := mux.Vars(r) | ||
| inviteID := vars["inviteId"] | ||
| if inviteID == "" { | ||
| util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invite ID is required"), w) | ||
| return | ||
| } | ||
|
|
||
| var req api.UserInviteRegenerateRequest | ||
| if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
| // Allow empty body - expiresIn is optional | ||
| req = api.UserInviteRegenerateRequest{} | ||
| } | ||
|
|
||
| expiresIn := 0 | ||
| if req.ExpiresIn != nil { | ||
| expiresIn = *req.ExpiresIn | ||
| } | ||
|
|
||
| result, err := h.accountManager.RegenerateUserInvite(r.Context(), userAuth.AccountId, userAuth.UserId, inviteID, expiresIn) | ||
| if err != nil { | ||
| util.WriteError(r.Context(), err, w) | ||
| return | ||
| } | ||
|
|
||
| expiresAt := result.InviteExpiresAt.UTC() | ||
| util.WriteJSONObject(r.Context(), w, &api.UserInviteRegenerateResponse{ | ||
| InviteLink: result.InviteLink, | ||
| InviteExpiresAt: expiresAt, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.