github.com/google/osv-scalibr@v0.4.1/veles/secrets/openai/validator.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package openai 16 17 import ( 18 "net/http" 19 "time" 20 21 sv "github.com/google/osv-scalibr/veles/secrets/common/simplevalidate" 22 ) 23 24 const ( 25 // OpenAI API base URL. 26 openaiAPIBaseURL = "https://api.openai.com" 27 // Timeout for API validation requests. 28 validationTimeout = 10 * time.Second 29 // ModelsEndpoint is the OpenAI models API endpoint. 30 ModelsEndpoint = "/v1/models" 31 ) 32 33 // NewProjectValidator creates a new ProjectValidator that validates API keys by 34 // making a test request to the OpenAI API /v1/models endpoint. 35 func NewProjectValidator() *sv.Validator[APIKey] { 36 v := &sv.Validator[APIKey]{ 37 Endpoint: openaiAPIBaseURL + ModelsEndpoint, 38 HTTPMethod: http.MethodGet, 39 HTTPHeaders: func(k APIKey) map[string]string { 40 return map[string]string{"Authorization": "Bearer " + k.Key} 41 }, 42 ValidResponseCodes: []int{http.StatusOK, http.StatusTooManyRequests}, 43 InvalidResponseCodes: []int{http.StatusUnauthorized}, 44 HTTPC: &http.Client{ 45 Timeout: validationTimeout, 46 }, 47 } 48 49 return v 50 }