Fix local logins

pull/1101/head
Rory Doherty 2022-10-27 17:21:38 +01:00
parent 5982365c2d
commit 56645efb01
5 changed files with 26 additions and 18 deletions

View File

@ -1,13 +1,14 @@
package handlers package handlers
import ( import (
"net"
"net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/statping-ng/statping-ng/types/checkins" "github.com/statping-ng/statping-ng/types/checkins"
"github.com/statping-ng/statping-ng/types/errors" "github.com/statping-ng/statping-ng/types/errors"
"github.com/statping-ng/statping-ng/types/services" "github.com/statping-ng/statping-ng/types/services"
"github.com/statping-ng/statping-ng/utils" "github.com/statping-ng/statping-ng/utils"
"net"
"net/http"
) )
func findCheckin(r *http.Request) (*checkins.Checkin, string, error) { func findCheckin(r *http.Request) (*checkins.Checkin, string, error) {
@ -23,8 +24,9 @@ func findCheckin(r *http.Request) (*checkins.Checkin, string, error) {
return checkin, id, nil return checkin, id, nil
} }
func apiAllCheckinsHandler(w http.ResponseWriter, r *http.Request) { func apiAllCheckinsHandler(r *http.Request) interface{} {
returnJson(checkins.All(), w, r) checkins := checkins.All()
return checkins
} }
func apiCheckinHandler(w http.ResponseWriter, r *http.Request) { func apiCheckinHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -73,7 +73,11 @@ func IsReadAuthenticated(r *http.Request) bool {
if ok := hasAuthorizationHeader(r); ok { if ok := hasAuthorizationHeader(r); ok {
return true return true
} }
return IsFullAuthenticated(r) _, err := getJwtToken(r)
if err == nil {
return true
}
return false
} }
// IsFullAuthenticated returns true if the HTTP request is authenticated. You can set the environment variable GO_ENV=test // IsFullAuthenticated returns true if the HTTP request is authenticated. You can set the environment variable GO_ENV=test

View File

@ -1,29 +1,29 @@
package handlers package handlers
import ( import (
"net/http"
"sort"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/statping-ng/statping-ng/types/errors" "github.com/statping-ng/statping-ng/types/errors"
"github.com/statping-ng/statping-ng/types/failures" "github.com/statping-ng/statping-ng/types/failures"
"github.com/statping-ng/statping-ng/types/notifications" "github.com/statping-ng/statping-ng/types/notifications"
"github.com/statping-ng/statping-ng/types/services" "github.com/statping-ng/statping-ng/types/services"
"net/http"
"sort"
) )
func apiNotifiersHandler(w http.ResponseWriter, r *http.Request) { func apiAllNotifiersHandler(r *http.Request) interface{} {
var notifs []notifications.Notification var notifs []notifications.Notification
for _, n := range services.AllNotifiers() { for _, n := range services.AllNotifiers() {
notif := n.Select() notif := n.Select()
no, err := notifications.Find(notif.Method) no, err := notifications.Find(notif.Method)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
sendErrorJson(err, w, r)
} }
notif.UpdateFields(no) notif.UpdateFields(no)
notifs = append(notifs, *notif) notifs = append(notifs, *notif)
} }
sort.Sort(notifications.NotificationOrder(notifs)) sort.Sort(notifications.NotificationOrder(notifs))
returnJson(notifs, w, r) return notifs
} }
func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) { func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -1,13 +1,14 @@
package handlers package handlers
import ( import (
"net/http"
"net/http/pprof"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/statping-ng/statping-ng/source" "github.com/statping-ng/statping-ng/source"
"github.com/statping-ng/statping-ng/types/core" "github.com/statping-ng/statping-ng/types/core"
"github.com/statping-ng/statping-ng/utils" "github.com/statping-ng/statping-ng/utils"
"net/http"
"net/http/pprof"
_ "github.com/statping-ng/statping-ng/types/metrics" _ "github.com/statping-ng/statping-ng/types/metrics"
) )
@ -146,7 +147,7 @@ func Router() *mux.Router {
api.Handle("/api/incidents/{id}/updates/{uid}", authenticated(apiDeleteIncidentUpdateHandler, false)).Methods("DELETE") api.Handle("/api/incidents/{id}/updates/{uid}", authenticated(apiDeleteIncidentUpdateHandler, false)).Methods("DELETE")
// API USER Routes // API USER Routes
api.Handle("/api/users", authenticated(apiAllUsersHandler, false)).Methods("GET") api.Handle("/api/users", scoped(apiAllUsersHandler)).Methods("GET")
api.Handle("/api/users", authenticated(apiCreateUsersHandler, false)).Methods("POST") api.Handle("/api/users", authenticated(apiCreateUsersHandler, false)).Methods("POST")
api.Handle("/api/users/token", http.HandlerFunc(apiCheckUserTokenHandler)).Methods("POST") api.Handle("/api/users/token", http.HandlerFunc(apiCheckUserTokenHandler)).Methods("POST")
api.Handle("/api/users/{id}", authenticated(apiUserHandler, false)).Methods("GET") api.Handle("/api/users/{id}", authenticated(apiUserHandler, false)).Methods("GET")
@ -154,7 +155,7 @@ func Router() *mux.Router {
api.Handle("/api/users/{id}", authenticated(apiUserDeleteHandler, false)).Methods("DELETE") api.Handle("/api/users/{id}", authenticated(apiUserDeleteHandler, false)).Methods("DELETE")
// API NOTIFIER Routes // API NOTIFIER Routes
api.Handle("/api/notifiers", authenticated(apiNotifiersHandler, false)).Methods("GET") api.Handle("/api/notifiers", scoped(apiAllNotifiersHandler)).Methods("GET")
api.Handle("/api/notifier/{notifier}", authenticated(apiNotifierGetHandler, false)).Methods("GET") api.Handle("/api/notifier/{notifier}", authenticated(apiNotifierGetHandler, false)).Methods("GET")
api.Handle("/api/notifier/{notifier}", authenticated(apiNotifierUpdateHandler, false)).Methods("POST") api.Handle("/api/notifier/{notifier}", authenticated(apiNotifierUpdateHandler, false)).Methods("POST")
api.Handle("/api/notifier/{notifier}/test", authenticated(testNotificationHandler, false)).Methods("POST") api.Handle("/api/notifier/{notifier}/test", authenticated(testNotificationHandler, false)).Methods("POST")
@ -167,7 +168,7 @@ func Router() *mux.Router {
api.Handle("/api/messages/{id}", authenticated(apiMessageDeleteHandler, false)).Methods("DELETE") api.Handle("/api/messages/{id}", authenticated(apiMessageDeleteHandler, false)).Methods("DELETE")
// API CHECKIN Routes // API CHECKIN Routes
api.Handle("/api/checkins", authenticated(apiAllCheckinsHandler, false)).Methods("GET") api.Handle("/api/checkins", scoped(apiAllCheckinsHandler)).Methods("GET")
api.Handle("/api/checkins", authenticated(checkinCreateHandler, false)).Methods("POST") api.Handle("/api/checkins", authenticated(checkinCreateHandler, false)).Methods("POST")
api.Handle("/api/checkins/{api}", authenticated(apiCheckinHandler, false)).Methods("GET") api.Handle("/api/checkins/{api}", authenticated(apiCheckinHandler, false)).Methods("GET")
api.Handle("/api/checkins/{api}", authenticated(checkinDeleteHandler, false)).Methods("DELETE") api.Handle("/api/checkins/{api}", authenticated(checkinDeleteHandler, false)).Methods("DELETE")

View File

@ -2,11 +2,12 @@ package handlers
import ( import (
"fmt" "fmt"
"net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/statping-ng/statping-ng/types/errors" "github.com/statping-ng/statping-ng/types/errors"
"github.com/statping-ng/statping-ng/types/users" "github.com/statping-ng/statping-ng/types/users"
"github.com/statping-ng/statping-ng/utils" "github.com/statping-ng/statping-ng/utils"
"net/http"
) )
func findUser(r *http.Request) (*users.User, int64, error) { func findUser(r *http.Request) (*users.User, int64, error) {
@ -75,9 +76,9 @@ func apiUserDeleteHandler(w http.ResponseWriter, r *http.Request) {
sendJsonAction(user, "delete", w, r) sendJsonAction(user, "delete", w, r)
} }
func apiAllUsersHandler(w http.ResponseWriter, r *http.Request) { func apiAllUsersHandler(r *http.Request) interface{} {
allUsers := users.All() allUsers := users.All()
returnJson(allUsers, w, r) return allUsers
} }
func apiCheckUserTokenHandler(w http.ResponseWriter, r *http.Request) { func apiCheckUserTokenHandler(w http.ResponseWriter, r *http.Request) {