Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
fix: use viper configs instead of file
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed Dec 10, 2019
1 parent 453f76c commit e664365
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
34 changes: 12 additions & 22 deletions firebase/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,61 @@ 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
projectId string
}

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
}

Expand Down
46 changes: 46 additions & 0 deletions firebase/token.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e664365

Please sign in to comment.