This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use viper configs instead of file
- Loading branch information
1 parent
453f76c
commit e664365
Showing
2 changed files
with
58 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |