2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
import (
|
2021-08-20 22:33:43 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-06-14 10:32:54 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2022-10-10 11:06:54 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-08-12 12:49:10 +02:00
|
|
|
"net/http"
|
2021-08-20 22:33:43 +02:00
|
|
|
"time"
|
2021-08-12 12:49:10 +02:00
|
|
|
)
|
|
|
|
|
2022-10-10 11:06:54 +02:00
|
|
|
// writeJSONObject simply writes object to the HTTP reponse in JSON format
|
2021-08-23 21:43:05 +02:00
|
|
|
func writeJSONObject(w http.ResponseWriter, obj interface{}) {
|
2022-02-22 18:18:05 +01:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-08-23 21:43:05 +02:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
err := json.NewEncoder(w).Encode(obj)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "failed handling request", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:06:54 +02:00
|
|
|
// Duration is used strictly for JSON requests/responses due to duration marshalling issues
|
2021-08-20 22:33:43 +02:00
|
|
|
type Duration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(d.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch value := v.(type) {
|
|
|
|
case float64:
|
|
|
|
d.Duration = time.Duration(value)
|
|
|
|
return nil
|
|
|
|
case string:
|
|
|
|
var err error
|
|
|
|
d.Duration, err = time.ParseDuration(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("invalid duration")
|
|
|
|
}
|
|
|
|
}
|
2022-06-14 10:32:54 +02:00
|
|
|
|
|
|
|
func getJWTAccount(accountManager server.AccountManager,
|
|
|
|
jwtExtractor jwtclaims.ClaimsExtractor,
|
2022-11-05 10:24:50 +01:00
|
|
|
authAudience string, r *http.Request) (*server.Account, *server.User, error) {
|
2022-06-14 10:32:54 +02:00
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
claims := jwtExtractor.ExtractClaimsFromRequestContext(r, authAudience)
|
2022-06-14 10:32:54 +02:00
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
account, err := accountManager.GetAccountFromToken(claims)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
2022-11-05 10:24:50 +01:00
|
|
|
return nil, nil, fmt.Errorf("failed getting account of a user %s: %v", claims.UserId, err)
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
user := account.Users[claims.UserId]
|
|
|
|
if user == nil {
|
|
|
|
// this is not really possible because we got an account by user ID
|
|
|
|
return nil, nil, fmt.Errorf("user %s not found", claims.UserId)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, user, nil
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
2022-10-10 11:06:54 +02:00
|
|
|
|
|
|
|
func toHTTPError(err error, w http.ResponseWriter) {
|
|
|
|
errStatus, ok := status.FromError(err)
|
|
|
|
if ok && errStatus.Code() == codes.Internal {
|
|
|
|
http.Error(w, errStatus.String(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok && errStatus.Code() == codes.NotFound {
|
|
|
|
http.Error(w, errStatus.String(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok && errStatus.Code() == codes.InvalidArgument {
|
|
|
|
http.Error(w, errStatus.String(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
unhandledMSG := fmt.Sprintf("got unhandled error code, error: %s", errStatus.String())
|
|
|
|
log.Error(unhandledMSG)
|
|
|
|
http.Error(w, unhandledMSG, http.StatusInternalServerError)
|
|
|
|
}
|