mirror of
https://github.com/livekit/livekit.git
synced 2026-10-22 19:43:01 +00:00
basic integration test
This commit is contained in:
1 parent
09e5a37c75
commit
233306d83a
6 files changed
+114
-24
No files matched your search
+11
-8
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/twitchtv/twirp"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/auth"
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -40,8 +41,7 @@ func (m *APIKeyAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request,
|
||||
|
||||
if authHeader != "" {
|
||||
if !strings.HasPrefix(authHeader, bearerPrefix) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("invalid authorization header. Must start with " + bearerPrefix))
|
||||
handleError(w, http.StatusUnauthorized, "invalid authorization header. Must start with "+bearerPrefix)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -54,22 +54,19 @@ func (m *APIKeyAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request,
|
||||
if authToken != "" {
|
||||
v, err := auth.ParseAPIToken(authToken)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("invalid authorization token"))
|
||||
handleError(w, http.StatusUnauthorized, "invalid authorization token")
|
||||
return
|
||||
}
|
||||
|
||||
secret := m.provider.GetSecret(v.APIKey())
|
||||
if secret == "" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("invalid API key"))
|
||||
handleError(w, http.StatusUnauthorized, "invalid API key")
|
||||
return
|
||||
}
|
||||
|
||||
grants, err := v.Verify(secret)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("invalid token: " + err.Error()))
|
||||
handleError(w, http.StatusUnauthorized, "invalid token: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -130,3 +127,9 @@ func EnsureCreatePermission(ctx context.Context) error {
|
||||
func twirpAuthError(err error) error {
|
||||
return twirp.NewError(twirp.Unauthenticated, err.Error())
|
||||
}
|
||||
|
||||
func handleError(w http.ResponseWriter, status int, msg string) {
|
||||
logger.GetLogger().Debugw("error handling request", "error", msg, "status", status)
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(msg))
|
||||
}
|
||||
+7
-7
@@ -46,7 +46,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
claims := GetGrants(r.Context())
|
||||
// require a claim
|
||||
if claims == nil || claims.Video == nil {
|
||||
writeJSONError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error())
|
||||
handleError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error())
|
||||
}
|
||||
pName = claims.Identity
|
||||
}
|
||||
@@ -54,14 +54,14 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
onlyName, err := EnsureJoinPermission(r.Context())
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusUnauthorized, err.Error())
|
||||
handleError(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
room, err := s.manager.GetRoomWithConstraint(roomId, onlyName)
|
||||
if err != nil {
|
||||
// TODO: return errors/status correctly
|
||||
writeJSONError(w, http.StatusNotFound, err.Error())
|
||||
handleError(w, http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
logger.GetLogger().Warnw("could not upgrade to WS",
|
||||
"err", err,
|
||||
)
|
||||
writeJSONError(w, http.StatusInternalServerError, err.Error())
|
||||
handleError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
conn.SetCloseHandler(func(code int, text string) error {
|
||||
@@ -82,12 +82,12 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
pc, err := rtc.NewPeerConnection(s.manager.Config())
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "could not create peerConnection", err.Error())
|
||||
handleError(w, http.StatusInternalServerError, "could not create peerConnection: "+err.Error())
|
||||
return
|
||||
}
|
||||
participant, err := rtc.NewParticipant(pc, signalConn, pName, s.manager.Config().Receiver)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "could not create participant", err.Error())
|
||||
handleError(w, http.StatusInternalServerError, "could not create participant: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
)
|
||||
|
||||
if err := room.Join(participant); err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "could not join room", err.Error())
|
||||
handleError(w, http.StatusInternalServerError, "could not join room: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user