basic integration test

This commit is contained in:
David Zhao
2021-01-13 13:41:34 -08:00
parent 09e5a37c75
commit 233306d83a
6 changed files with 114 additions and 24 deletions
+11 -8
View File
@@ -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))
}