Skip to content

Commit

Permalink
Add Revoke call & access to JWT token
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowbaq committed Feb 6, 2020
1 parent d84bff3 commit 71cdd3f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
35 changes: 34 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

retry "github.com/avast/retry-go"
"github.com/pkg/errors"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
drive "google.golang.org/api/drive/v3"
Expand All @@ -16,6 +17,8 @@ import (
)

type Client struct {
JWTConfig *jwt.Config

Sheets *sheets.Service
Drive *drive.Service
}
Expand Down Expand Up @@ -60,6 +63,31 @@ func (c *Client) shareFile(fileID, email string, notify bool) error {
})
}

func (c *Client) Revoke(fileID, email string) error {
var permissions *drive.PermissionList
err := googleRetry(func() error {
var rerr error
permissions, rerr = c.Drive.Permissions.List(fileID).Fields("nextPageToken, permissions(id, emailAddress, type, role)").Do()

return rerr
})
if err != nil {
return errors.Wrapf(err, "couldn't list permissions for %s", fileID)
}

for _, p := range permissions.Permissions {
if p.EmailAddress != email {
continue
}

return googleRetry(func() error {
return c.Drive.Permissions.Delete(fileID, p.Id).Do()
})
}

return nil
}

func (c *Client) ListFiles(query string) ([]*drive.File, error) {
var resp *drive.FileList
err := googleRetry(func() error {
Expand Down Expand Up @@ -231,7 +259,12 @@ func NewServiceAccountClient(credsReader io.Reader) (*Client, error) {
return nil, err
}

return &Client{sheetsSrv, driveSrv}, nil
return &Client{
JWTConfig: config,

Sheets: sheetsSrv,
Drive: driveSrv,
}, nil
}

func googleRetry(f func() error) error {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/avast/retry-go v2.5.0+incompatible
github.com/pkg/errors v0.9.1
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
google.golang.org/api v0.15.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down

0 comments on commit 71cdd3f

Please sign in to comment.