@@ -18,26 +18,33 @@ import (
1818 "encoding/json"
1919 "fmt"
2020 "os"
21+ "path/filepath"
2122 "strings"
2223)
2324
2425const (
26+ // KeyFileTypeServiceAccount identifies service account key files.
2527 KeyFileTypeServiceAccount = "sn_service_account"
26- FILE = "file://"
27- DATA = "data://"
28+ // FILE indicates a file:// key file reference.
29+ FILE = "file://"
30+ // DATA indicates a data:// inline key file reference.
31+ DATA = "data://"
2832)
2933
34+ // KeyFileProvider provides client credentials from a key file path.
3035type KeyFileProvider struct {
3136 KeyFile string
3237}
3338
39+ // KeyFile holds service account credentials from a JSON key file.
3440type KeyFile struct {
3541 Type string `json:"type"`
3642 ClientID string `json:"client_id"`
3743 ClientSecret string `json:"client_secret"`
3844 ClientEmail string `json:"client_email"`
3945}
4046
47+ // NewClientCredentialsProviderFromKeyFile creates a provider from a key file path.
4148func NewClientCredentialsProviderFromKeyFile (keyFile string ) * KeyFileProvider {
4249 return & KeyFileProvider {
4350 KeyFile : keyFile ,
@@ -46,13 +53,14 @@ func NewClientCredentialsProviderFromKeyFile(keyFile string) *KeyFileProvider {
4653
4754var _ ClientCredentialsProvider = & KeyFileProvider {}
4855
56+ // GetClientCredentials loads client credentials from the configured key file source.
4957func (k * KeyFileProvider ) GetClientCredentials () (* KeyFile , error ) {
5058 var keyFile []byte
5159 var err error
5260 switch {
5361 case strings .HasPrefix (k .KeyFile , FILE ):
5462 filename := strings .TrimPrefix (k .KeyFile , FILE )
55- keyFile , err = os .ReadFile (filename )
63+ keyFile , err = os .ReadFile (filepath . Clean ( filename ) )
5664 case strings .HasPrefix (k .KeyFile , DATA ):
5765 keyFile = []byte (strings .TrimPrefix (k .KeyFile , DATA ))
5866 case strings .HasPrefix (k .KeyFile , "data:" ):
@@ -80,17 +88,20 @@ func (k *KeyFileProvider) GetClientCredentials() (*KeyFile, error) {
8088 return & v , nil
8189}
8290
91+ // KeyFileStructProvider provides client credentials from an in-memory KeyFile struct.
8392type KeyFileStructProvider struct {
8493 KeyFile * KeyFile
8594}
8695
96+ // GetClientCredentials returns the client credentials from the in-memory KeyFile.
8797func (k * KeyFileStructProvider ) GetClientCredentials () (* KeyFile , error ) {
8898 if k .KeyFile == nil {
8999 return nil , fmt .Errorf ("key file is nil" )
90100 }
91101 return k .KeyFile , nil
92102}
93103
104+ // NewClientCredentialsProviderFromKeyFileStruct creates a provider from an in-memory KeyFile.
94105func NewClientCredentialsProviderFromKeyFileStruct (keyFile * KeyFile ) * KeyFileStructProvider {
95106 return & KeyFileStructProvider {
96107 KeyFile : keyFile ,
0 commit comments