Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Possibility to connect as another user account
Browse files Browse the repository at this point in the history
Includes code from Du-St
prasmussen#336
  • Loading branch information
hulet committed Feb 10, 2022
1 parent fb08fe2 commit 8ff5446
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Clie
)
}

func NewServiceAccountClient(serviceAccountFile string) (*http.Client, error) {
func NewServiceAccountClient(serviceAccountFile string, serviceAccountSubject string) (*http.Client, error) {
content, exists, err := ReadFile(serviceAccountFile)
if(!exists) {
return nil, fmt.Errorf("Service account filename %q not found", serviceAccountFile)
Expand All @@ -79,6 +79,11 @@ func NewServiceAccountClient(serviceAccountFile string) (*http.Client, error) {
if(err != nil) {
return nil, err
}

if (serviceAccountSubject != "") {
conf.Subject = serviceAccountSubject
}

return conf.Client(oauth2.NoContext), nil
}

Expand Down
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: "serviceAccountSubject",
Patterns: []string{"--service-account-subject"},
Description: "Connect to this user account GDrive instead of the service account GDrive. Useful only with the --service-account parameter. The service account must be domain-wide delegated.",
},
}

handlers := []*cli.Handler{
Expand Down
2 changes: 1 addition & 1 deletion handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func getOauthClient(args cli.Arguments) (*http.Client, error) {

if args.String("serviceAccount") != "" {
serviceAccountPath := ConfigFilePath(configDir, args.String("serviceAccount"))
serviceAccountClient, err := auth.NewServiceAccountClient(serviceAccountPath)
serviceAccountClient, err := auth.NewServiceAccountClient(serviceAccountPath,args.String("serviceAccountSubject"))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8ff5446

Please sign in to comment.