mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-02-17 18:41:28 -05:00
Rename existing `authSource` to `authService` through deprecation.
`AuthService` more clearly distinguishes it from `Sources` objects.
`authSources` will be converted into `authServices` after the
unmarshalling process. A warning log is shown if `authSources` are used
(for both within tools parameters and defining auth services):
```
2025-02-20T13:57:51.156025-08:00 WARN "`authSources` is deprecated, use `authServices` for parameters instead"
2025-02-20T13:57:51.156569-08:00 WARN "`authSources` is deprecated, use `authServices` instead"
2025-02-20T13:57:52.047584-08:00 INFO "Initialized 1 sources."
...
```
The manifest generated will continue to use `authSources` to keep
compatibility with the sdks:
```
{
"serverVersion":"0.1.0",
"tools":{
"test_tool2":{
"description":"Use this tool to test\n",
"parameters":[{
"name":"user_id",
"type":"string",
"description":"Auto-populated from Google login",
"authSources":["my-google-auth"]
}]
}
}
}
```
Test cases with `authSources` are kept for compatibility. Will be
removed when `authSources` are no longer supported.
83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
// Copyright 2024 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package google
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/googleapis/genai-toolbox/internal/auth"
|
|
"google.golang.org/api/idtoken"
|
|
)
|
|
|
|
const AuthServiceKind string = "google"
|
|
|
|
// validate interface
|
|
var _ auth.AuthServiceConfig = Config{}
|
|
|
|
// Auth service configuration
|
|
type Config struct {
|
|
Name string `yaml:"name" validate:"required"`
|
|
Kind string `yaml:"kind" validate:"required"`
|
|
ClientID string `yaml:"clientId" validate:"required"`
|
|
}
|
|
|
|
// Returns the auth service kind
|
|
func (cfg Config) AuthServiceConfigKind() string {
|
|
return AuthServiceKind
|
|
}
|
|
|
|
// Initialize a Google auth service
|
|
func (cfg Config) Initialize() (auth.AuthService, error) {
|
|
a := &AuthService{
|
|
Name: cfg.Name,
|
|
Kind: AuthServiceKind,
|
|
ClientID: cfg.ClientID,
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
var _ auth.AuthService = AuthService{}
|
|
|
|
// struct used to store auth service info
|
|
type AuthService struct {
|
|
Name string `yaml:"name"`
|
|
Kind string `yaml:"kind"`
|
|
ClientID string `yaml:"clientId"`
|
|
}
|
|
|
|
// Returns the auth service kind
|
|
func (a AuthService) AuthServiceKind() string {
|
|
return AuthServiceKind
|
|
}
|
|
|
|
// Returns the name of the auth service
|
|
func (a AuthService) GetName() string {
|
|
return a.Name
|
|
}
|
|
|
|
// Verifies Google ID token and return claims
|
|
func (a AuthService) GetClaimsFromHeader(h http.Header) (map[string]any, error) {
|
|
if token := h.Get(a.Name + "_token"); token != "" {
|
|
payload, err := idtoken.Validate(context.Background(), token, a.ClientID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Google ID token verification failure: %w", err)
|
|
}
|
|
return payload.Claims, nil
|
|
}
|
|
return nil, nil
|
|
}
|