Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Support load Client Id and Secret from env
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayakumarb committed Dec 4, 2024
1 parent ab27085 commit 9b7914e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 16 additions & 5 deletions auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"fmt"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"os"
"net/http"
"time"
)

type authCodeFn func(string) func() string

func NewFileSourceClient(clientId, clientSecret, tokenFile string, authFn authCodeFn) (*http.Client, error) {
conf := getConfig(clientId, clientSecret)

func NewFileSourceClient(tokenFile string, authFn authCodeFn) (*http.Client, error) {

// Read cached token
token, exists, err := ReadToken(tokenFile)
if err != nil {
return nil, fmt.Errorf("Failed to read token: %s", err)
}
clientId, clientSecret := getOauthClientInfo()
conf := getConfig(clientId, clientSecret)

// Require auth code if token file does not exist
// or refresh token is missing
Expand All @@ -36,7 +38,8 @@ func NewFileSourceClient(clientId, clientSecret, tokenFile string, authFn authCo
), nil
}

func NewRefreshTokenClient(clientId, clientSecret, refreshToken string) *http.Client {
func NewRefreshTokenClient(refreshToken string) *http.Client {
clientId, clientSecret := getOauthClientInfo()
conf := getConfig(clientId, clientSecret)

token := &oauth2.Token{
Expand All @@ -51,7 +54,8 @@ func NewRefreshTokenClient(clientId, clientSecret, refreshToken string) *http.Cl
)
}

func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Client {
func NewAccessTokenClient(accessToken string) *http.Client {
clientId, clientSecret := getOauthClientInfo()
conf := getConfig(clientId, clientSecret)

token := &oauth2.Token{
Expand Down Expand Up @@ -94,3 +98,10 @@ func getConfig(clientId, clientSecret string) *oauth2.Config {
},
}
}

func getOauthClientInfo() (string, string) {
// Use Client Id and Secret from env.
clientId := os.Getenv("OAUTH_CLIENT_ID")
clientSecret := os.Getenv("OAUTH_CLIENT_SECRET")
return clientId, clientSecret
}
8 changes: 3 additions & 5 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/prasmussen/gdrive/drive"
)

const ClientId = "367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com"
const ClientSecret = "1qsNodXNaWq1mQuBjUjmvhoO"
const TokenFilename = "token_v2.json"
const DefaultCacheFileName = "file_cache.json"

Expand Down Expand Up @@ -346,11 +344,11 @@ func getOauthClient(args cli.Arguments) (*http.Client, error) {
}

if args.String("refreshToken") != "" {
return auth.NewRefreshTokenClient(ClientId, ClientSecret, args.String("refreshToken")), nil
return auth.NewRefreshTokenClient(args.String("refreshToken")), nil
}

if args.String("accessToken") != "" {
return auth.NewAccessTokenClient(ClientId, ClientSecret, args.String("accessToken")), nil
return auth.NewAccessTokenClient(args.String("accessToken")), nil
}

configDir := getConfigDir(args)
Expand All @@ -365,7 +363,7 @@ func getOauthClient(args cli.Arguments) (*http.Client, error) {
}

tokenPath := ConfigFilePath(configDir, TokenFilename)
return auth.NewFileSourceClient(ClientId, ClientSecret, tokenPath, authCodePrompt)
return auth.NewFileSourceClient(tokenPath, authCodePrompt)
}

func getConfigDir(args cli.Arguments) string {
Expand Down

0 comments on commit 9b7914e

Please sign in to comment.