diff --git a/firebase/client.go b/firebase/client.go index 9a25836..ad81524 100644 --- a/firebase/client.go +++ b/firebase/client.go @@ -3,17 +3,19 @@ package firebase import ( "context" "encoding/json" + "fmt" + "log" + firebase "firebase.google.com/go" "firebase.google.com/go/auth" - "fmt" s "github.com/bitfield/script" - "github.com/spf13/viper" "google.golang.org/api/option" - "log" ) -const firebaseProjectConfig string = "./.firebaserc" -const defaultProject = "default" +const ( + firebaseProjectConfig = "./.firebaserc" + defaultProject = "default" +) type Firebase struct { App *firebase.App @@ -21,53 +23,41 @@ type Firebase struct { } func (f *Firebase) setProjectID(projectId string) error { - configFileContent, err := s.File(firebaseProjectConfig).Bytes() - if err != nil { return fmt.Errorf("An error occurred while reading config file: %w", err) } - var decodedConfigs *FirebaseProjectConfigs - err = json.Unmarshal(configFileContent, &decodedConfigs) - if err != nil { return fmt.Errorf("An error occurred while reading config file: %w", err) } - f.projectId = decodedConfigs.Projects[projectId] - return nil } func (f *Firebase) InitializeFirbeaseApp(ctx context.Context, projectId string) error { - if projectId == "" { projectId = defaultProject } - err := f.setProjectID(projectId) - if err != nil { return fmt.Errorf("An error occurred while reading config file: %w", err) } - configs := &firebase.Config{ ProjectID: f.projectId, } - + credentials, err := constructToken() + if err != nil { + return fmt.Errorf("Error getting credentials: %w", err) + } // replace this with something better - opt := option.WithCredentialsFile(viper.GetString("refreshTokenFilePath")) - + opt := option.WithCredentialsJSON(credentials) app, err := firebase.NewApp(ctx, configs, opt) - if err != nil { log.Fatalf("error initializing app: %v\n", err) } - f.App = app - return nil } diff --git a/firebase/token.go b/firebase/token.go new file mode 100644 index 0000000..98475b5 --- /dev/null +++ b/firebase/token.go @@ -0,0 +1,46 @@ +// Package oauth get refresh token and save for future use +package firebase + +import ( + "encoding/json" + "fmt" + + "github.com/spf13/viper" +) + +type RefreshToken struct { + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` + RefreshToken string `json:"refresh_token"` + Type string `json:"type"` +} + +func (r RefreshToken) validate() error { + if r.ClientID == "" { + return fmt.Errorf("client id can not be empty") + } + if r.ClientSecret == "" { + return fmt.Errorf("client secret can not be empty") + } + if r.RefreshToken == "" { + return fmt.Errorf("refresh token can not be empty") + } + if r.Type != "authorized_user" { + return fmt.Errorf("token type is not valid") + } + return nil +} + +// constructToken create a json refresh token byte for use by a firebase client +func constructToken() ([]byte, error) { + refreshToken := RefreshToken{ + ClientID: viper.GetString("GOOGLE_OAUTH_CLIENT_ID"), + ClientSecret: viper.GetString("GOOGLE_OAUTH_CLIENT_SECRET"), + RefreshToken: viper.GetString("FirebaseRefreshToken"), + Type: "authorized_user", + } + if err := refreshToken.validate(); err != nil { + return nil, fmt.Errorf("Error validating token: %w", err) + } + return json.Marshal(refreshToken) +}