2023-02-16 12:00:41 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-28 15:01:24 +01:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-02-16 12:00:41 +01:00
|
|
|
"github.com/gorilla/mux"
|
2023-02-28 15:01:24 +01:00
|
|
|
|
2023-02-16 12:00:41 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2023-11-28 15:15:51 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/account"
|
2023-02-16 12:00:41 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// AccountsHandler is a handler that handles the server.Account HTTP endpoints
|
|
|
|
type AccountsHandler struct {
|
2023-02-16 12:00:41 +01:00
|
|
|
accountManager server.AccountManager
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// NewAccountsHandler creates a new AccountsHandler HTTP handler
|
|
|
|
func NewAccountsHandler(accountManager server.AccountManager, authCfg AuthCfg) *AccountsHandler {
|
|
|
|
return &AccountsHandler{
|
2023-02-16 12:00:41 +01:00
|
|
|
accountManager: accountManager,
|
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GetAllAccounts is HTTP GET handler that returns a list of accounts. Effectively returns just a single account.
|
|
|
|
func (h *AccountsHandler) GetAllAccounts(w http.ResponseWriter, r *http.Request) {
|
2023-02-16 12:00:41 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-27 16:10:50 +02:00
|
|
|
accountID, userID, err := h.accountManager.GetAccountIDFromToken(r.Context(), claims)
|
2023-02-16 12:00:41 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-02-16 12:00:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
settings, err := h.accountManager.GetAccountSettings(r.Context(), accountID, userID)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-02-16 12:00:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
resp := toAccountResponse(accountID, settings)
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, []*api.Account{resp})
|
2023-02-16 12:00:41 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// UpdateAccount is HTTP PUT handler that updates the provided account. Updates only account settings (server.Settings)
|
|
|
|
func (h *AccountsHandler) UpdateAccount(w http.ResponseWriter, r *http.Request) {
|
2023-02-16 12:00:41 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-27 16:10:50 +02:00
|
|
|
_, userID, err := h.accountManager.GetAccountIDFromToken(r.Context(), claims)
|
2023-02-16 12:00:41 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-02-16 12:00:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2023-05-03 00:15:25 +02:00
|
|
|
accountID := vars["accountId"]
|
2023-02-16 12:00:41 +01:00
|
|
|
if len(accountID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid accountID ID"), w)
|
2023-02-16 12:00:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-02 01:09:18 +02:00
|
|
|
var req api.PutApiAccountsAccountIdJSONRequestBody
|
2023-02-16 12:00:41 +01:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-27 16:51:05 +02:00
|
|
|
settings := &server.Settings{
|
2023-02-16 12:00:41 +01:00
|
|
|
PeerLoginExpirationEnabled: req.Settings.PeerLoginExpirationEnabled,
|
|
|
|
PeerLoginExpiration: time.Duration(float64(time.Second.Nanoseconds()) * float64(req.Settings.PeerLoginExpiration)),
|
2024-03-27 16:11:45 +01:00
|
|
|
RegularUsersViewBlocked: req.Settings.RegularUsersViewBlocked,
|
2024-10-13 14:52:43 +02:00
|
|
|
|
|
|
|
PeerInactivityExpirationEnabled: req.Settings.PeerInactivityExpirationEnabled,
|
|
|
|
PeerInactivityExpiration: time.Duration(float64(time.Second.Nanoseconds()) * float64(req.Settings.PeerInactivityExpiration)),
|
2023-06-27 16:51:05 +02:00
|
|
|
}
|
|
|
|
|
2023-11-28 11:44:08 +01:00
|
|
|
if req.Settings.Extra != nil {
|
2023-11-28 15:15:51 +01:00
|
|
|
settings.Extra = &account.ExtraSettings{PeerApprovalEnabled: *req.Settings.Extra.PeerApprovalEnabled}
|
2023-11-28 11:44:08 +01:00
|
|
|
}
|
|
|
|
|
2023-06-27 16:51:05 +02:00
|
|
|
if req.Settings.JwtGroupsEnabled != nil {
|
|
|
|
settings.JWTGroupsEnabled = *req.Settings.JwtGroupsEnabled
|
|
|
|
}
|
2023-08-07 17:44:51 +02:00
|
|
|
if req.Settings.GroupsPropagationEnabled != nil {
|
|
|
|
settings.GroupsPropagationEnabled = *req.Settings.GroupsPropagationEnabled
|
|
|
|
}
|
2023-06-27 16:51:05 +02:00
|
|
|
if req.Settings.JwtGroupsClaimName != nil {
|
|
|
|
settings.JWTGroupsClaimName = *req.Settings.JwtGroupsClaimName
|
|
|
|
}
|
2023-12-11 16:59:15 +01:00
|
|
|
if req.Settings.JwtAllowGroups != nil {
|
|
|
|
settings.JWTAllowGroups = *req.Settings.JwtAllowGroups
|
|
|
|
}
|
2023-06-27 16:51:05 +02:00
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
updatedAccount, err := h.accountManager.UpdateAccountSettings(r.Context(), accountID, userID, settings)
|
2023-02-16 12:00:41 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-02-16 12:00:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
resp := toAccountResponse(updatedAccount.Id, updatedAccount.Settings)
|
2023-02-16 12:00:41 +01:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, &resp)
|
2023-02-16 12:00:41 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 14:23:38 +01:00
|
|
|
// DeleteAccount is a HTTP DELETE handler to delete an account
|
|
|
|
func (h *AccountsHandler) DeleteAccount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
targetAccountID := vars["accountId"]
|
|
|
|
if len(targetAccountID) == 0 {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid account ID"), w)
|
2023-11-28 14:23:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err := h.accountManager.DeleteAccount(r.Context(), targetAccountID, claims.UserId)
|
2023-11-28 14:23:38 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-11-28 14:23:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, emptyObject{})
|
2023-11-28 14:23:38 +01:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
func toAccountResponse(accountID string, settings *server.Settings) *api.Account {
|
|
|
|
jwtAllowGroups := settings.JWTAllowGroups
|
2023-12-11 16:59:15 +01:00
|
|
|
if jwtAllowGroups == nil {
|
|
|
|
jwtAllowGroups = []string{}
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
apiSettings := api.AccountSettings{
|
2024-10-23 16:40:15 +02:00
|
|
|
PeerLoginExpiration: int(settings.PeerLoginExpiration.Seconds()),
|
|
|
|
PeerLoginExpirationEnabled: settings.PeerLoginExpirationEnabled,
|
|
|
|
PeerInactivityExpiration: int(settings.PeerInactivityExpiration.Seconds()),
|
|
|
|
PeerInactivityExpirationEnabled: settings.PeerInactivityExpirationEnabled,
|
|
|
|
GroupsPropagationEnabled: &settings.GroupsPropagationEnabled,
|
|
|
|
JwtGroupsEnabled: &settings.JWTGroupsEnabled,
|
|
|
|
JwtGroupsClaimName: &settings.JWTGroupsClaimName,
|
|
|
|
JwtAllowGroups: &jwtAllowGroups,
|
|
|
|
RegularUsersViewBlocked: settings.RegularUsersViewBlocked,
|
2023-11-28 11:44:08 +01:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
if settings.Extra != nil {
|
|
|
|
apiSettings.Extra = &api.AccountExtraSettings{PeerApprovalEnabled: &settings.Extra.PeerApprovalEnabled}
|
2023-11-28 11:44:08 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 12:00:41 +01:00
|
|
|
return &api.Account{
|
2024-09-27 16:10:50 +02:00
|
|
|
Id: accountID,
|
|
|
|
Settings: apiSettings,
|
2023-02-16 12:00:41 +01:00
|
|
|
}
|
|
|
|
}
|