Skip to content

Commit

Permalink
Merge pull request #290 from helixml/fix/subrouter_auth
Browse files Browse the repository at this point in the history
fix auth for openai api
  • Loading branch information
rusenask committed May 10, 2024
2 parents 25735e7 + 3b956ef commit bbb2f58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions api/pkg/server/auth_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,19 @@ func (auth *authMiddleware) extractMiddleware(next http.Handler) http.Handler {

return http.HandlerFunc(f)
}

func (auth *authMiddleware) auth(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, err := auth.getUserFromToken(r.Context(), getRequestToken(r))
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if user == nil {
user = &types.User{}
}
r = r.WithContext(setRequestUser(r.Context(), *user))

f(w, r)
}
}
2 changes: 1 addition & 1 deletion api/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (apiServer *HelixAPIServer) registerRoutes(_ context.Context) (*mux.Router,
}

// OpenAI API compatible routes
router.HandleFunc("/v1/chat/completions", apiServer.createChatCompletion).Methods("POST", "OPTIONS")
router.HandleFunc("/v1/chat/completions", apiServer.authMiddleware.auth(apiServer.createChatCompletion)).Methods("POST", "OPTIONS")

authRouter.HandleFunc("/sessions", system.DefaultWrapper(apiServer.getSessions)).Methods("GET")
authRouter.HandleFunc("/sessions", system.DefaultWrapper(apiServer.createSession)).Methods("POST")
Expand Down

0 comments on commit bbb2f58

Please sign in to comment.