77 "log"
88 "net/http"
99 "strings"
10+
11+ "modelproxy/types"
1012)
1113
1214// obfuscateToken returns a truncated identifier for safely logging tokens.
@@ -18,61 +20,57 @@ func obfuscateToken(token string) string {
1820}
1921
2022type RestServerAuthenticator struct {
21- // rest-server token => model names => model urls
22- tokenToModels map [string ]map [string ][]string
23- modelKey string
23+ // rest-server token => model names => model service list
24+ tokenToModels map [string ]map [string ][]* types.BaseSpec
2425}
2526
26- func NewRestServerAuthenticator (tokenToModels map [string ]map [string ][]string , modelKey string ) * RestServerAuthenticator {
27- if tokenToModels == nil {
28- tokenToModels = make (map [string ]map [string ][]string )
29- }
27+ func NewRestServerAuthenticator () * RestServerAuthenticator {
3028 return & RestServerAuthenticator {
31- tokenToModels : tokenToModels ,
32- modelKey : modelKey ,
29+ tokenToModels : make (map [string ]map [string ][]* types.BaseSpec ),
3330 }
3431}
3532
36- func (ra * RestServerAuthenticator ) UpdateTokenModels (token string , model2Url map [string ][]string ) {
33+ // UpdateTokenModels updates the model mapping for a given token
34+ func (ra * RestServerAuthenticator ) UpdateTokenModels (token string , model2Service map [string ][]* types.BaseSpec ) {
3735 if ra .tokenToModels == nil {
38- ra .tokenToModels = make (map [string ]map [string ][]string )
36+ ra .tokenToModels = make (map [string ]map [string ][]* types. BaseSpec )
3937 }
40- ra .tokenToModels [token ] = model2Url
38+ ra .tokenToModels [token ] = model2Service
4139}
4240
4341// Check if the request is authenticated and return the available model urls
44- func (ra * RestServerAuthenticator ) AuthenticateReq (req * http.Request , reqBody map [string ]interface {}) (bool , []string ) {
42+ func (ra * RestServerAuthenticator ) AuthenticateReq (req * http.Request , reqBody map [string ]interface {}) (bool , []* types. BaseSpec ) {
4543 token := req .Header .Get ("Authorization" )
4644 token = strings .Replace (token , "Bearer " , "" , 1 )
4745 // read request body
4846 model , ok := reqBody ["model" ].(string )
4947 if ! ok {
5048 log .Printf ("[-] Error: 'model' field missing or not a string in request body" )
51- return false , [] string {}
49+ return false , nil
5250 }
5351 availableModels , ok := ra .tokenToModels [token ]
5452 if ! ok {
5553 // request to RestServer to get the models
5654 log .Printf ("[-] Error: token %s not found in the authenticator\n " , obfuscateToken (token ))
57- availableModels , err := GetJobModelsMapping (req , ra . modelKey )
55+ availableModels , err := GetJobModelsMapping (req )
5856 if err != nil {
5957 log .Printf ("[-] Error: failed to get models for token %s: %v\n " , obfuscateToken (token ), err )
60- return false , [] string {}
58+ return false , nil
6159 }
6260 ra .tokenToModels [token ] = availableModels
6361 }
6462 if len (availableModels ) == 0 {
6563 log .Printf ("[-] Error: no models found" )
66- return false , [] string {}
64+ return false , nil
6765 }
6866 if model == "" {
6967 log .Printf ("[-] Error: model is empty" )
70- return false , [] string {}
68+ return false , nil
7169 }
7270 for m , v := range availableModels {
7371 if m == model {
7472 return true , v
7573 }
7674 }
77- return false , [] string {}
75+ return false , nil
7876}
0 commit comments