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

Commit

Permalink
allow user specified oauth credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
colemickens committed Nov 19, 2019
1 parent 315af06 commit 64e252f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func main() {
Patterns: []string{"--service-account"},
Description: "Oauth service account filename, used for server to server communication without user interaction (filename path is relative to config dir)",
},
cli.StringFlag{
Name: "oauthCredentials",
Patterns: []string{"--oauth-credentials"},
Description: "Oauth Credentials file as downloaded from https://console.cloud.google.com/apis/credentials",
},
}

handlers := []*cli.Handler{
Expand Down
46 changes: 41 additions & 5 deletions handlers_drive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand All @@ -17,6 +18,7 @@ import (
const ClientId = "367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com"
const ClientSecret = "1qsNodXNaWq1mQuBjUjmvhoO"
const TokenFilename = "token_v2.json"
const OauthCredentialsFilename = "oauthClient.json"
const DefaultCacheFileName = "file_cache.json"

func listHandler(ctx cli.Context) {
Expand Down Expand Up @@ -340,21 +342,55 @@ func aboutExportHandler(ctx cli.Context) {
checkErr(err)
}

func getOauthAppCredentials(credsPath string) (clientId, clientSecret string, err error) {
clientId = ClientId
clientSecret = ClientSecret
if _, err := os.Stat(credsPath); os.IsNotExist(err) {
return "", "", err
}
var oauthCredentials struct {
Installed struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
} `json:"installed"`
}
content, err := ioutil.ReadFile(credsPath)
if err != nil {
return "", "", err
}
json.Unmarshal(content, &oauthCredentials)
clientId = oauthCredentials.Installed.ClientId
clientSecret = oauthCredentials.Installed.ClientSecret

return clientId, clientSecret, nil
}

func getOauthClient(args cli.Arguments) (*http.Client, error) {
clientId := ClientId
clientSecret := ClientSecret
configDir := getConfigDir(args)

credsPath := ConfigFilePath(configDir, OauthCredentialsFilename)
if args.String("oauthCredentials") != "" {
credsPath = args.String("oauthCrendentials")
}
clientId, clientSecret, err := getOauthAppCredentials(credsPath)
if err != nil {
ExitF("Failed to load oauth app credentials:")
}

if args.String("refreshToken") != "" && args.String("accessToken") != "" {
ExitF("Access token not needed when refresh token is provided")
}

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

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

configDir := getConfigDir(args)

if args.String("serviceAccount") != "" {
serviceAccountPath := ConfigFilePath(configDir, args.String("serviceAccount"))
serviceAccountClient, err := auth.NewServiceAccountClient(serviceAccountPath)
Expand All @@ -365,7 +401,7 @@ func getOauthClient(args cli.Arguments) (*http.Client, error) {
}

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

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

0 comments on commit 64e252f

Please sign in to comment.