2024-02-20 09:59:56 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-06-13 13:24:24 +02:00
|
|
|
"regexp"
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/geolocation"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
var (
|
|
|
|
countryCodeRegex = regexp.MustCompile("^[a-zA-Z]{2}$")
|
|
|
|
)
|
|
|
|
|
2024-02-20 09:59:56 +01:00
|
|
|
// GeolocationsHandler is a handler that returns locations.
|
|
|
|
type GeolocationsHandler struct {
|
|
|
|
accountManager server.AccountManager
|
|
|
|
geolocationManager *geolocation.Geolocation
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGeolocationsHandlerHandler creates a new Geolocations handler
|
|
|
|
func NewGeolocationsHandlerHandler(accountManager server.AccountManager, geolocationManager *geolocation.Geolocation, authCfg AuthCfg) *GeolocationsHandler {
|
|
|
|
return &GeolocationsHandler{
|
|
|
|
accountManager: accountManager,
|
|
|
|
geolocationManager: geolocationManager,
|
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllCountries retrieves a list of all countries
|
|
|
|
func (l *GeolocationsHandler) GetAllCountries(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := l.authenticateUser(r); err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.geolocationManager == nil {
|
|
|
|
// TODO: update error message to include geo db self hosted doc link when ready
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.PreconditionFailed, "Geo location database is not initialized"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
allCountries, err := l.geolocationManager.GetAllCountries()
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
countries := make([]api.Country, 0, len(allCountries))
|
|
|
|
for _, country := range allCountries {
|
|
|
|
countries = append(countries, toCountryResponse(country))
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, countries)
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCitiesByCountry retrieves a list of cities based on the given country code
|
|
|
|
func (l *GeolocationsHandler) GetCitiesByCountry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := l.authenticateUser(r); err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
countryCode := vars["country"]
|
|
|
|
if !countryCodeRegex.MatchString(countryCode) {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.InvalidArgument, "invalid country code"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.geolocationManager == nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), status.Errorf(status.PreconditionFailed, "Geo location database is not initialized. "+
|
2024-06-13 13:24:24 +02:00
|
|
|
"Check the self-hosted Geo database documentation at https://docs.netbird.io/selfhosted/geo-support"), w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
allCities, err := l.geolocationManager.GetCitiesByCountry(countryCode)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2024-02-20 09:59:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cities := make([]api.City, 0, len(allCities))
|
|
|
|
for _, city := range allCities {
|
|
|
|
cities = append(cities, toCityResponse(city))
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, cities)
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *GeolocationsHandler) authenticateUser(r *http.Request) error {
|
|
|
|
claims := l.claimsExtractor.FromRequestContext(r)
|
2024-09-17 22:15:54 +02:00
|
|
|
user, err := l.accountManager.GetUserByID(r.Context(), claims.UserId)
|
2024-02-20 09:59:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.HasAdminPower() {
|
|
|
|
return status.Errorf(status.PermissionDenied, "user is not allowed to perform this action")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toCountryResponse(country geolocation.Country) api.Country {
|
|
|
|
return api.Country{
|
|
|
|
CountryName: country.CountryName,
|
|
|
|
CountryCode: country.CountryISOCode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toCityResponse(city geolocation.City) api.City {
|
|
|
|
return api.City{
|
|
|
|
CityName: city.CityName,
|
|
|
|
GeonameId: city.GeoNameID,
|
|
|
|
}
|
|
|
|
}
|