Skip to content

Commit

Permalink
Allow multiple gcal widgets (#1112)
Browse files Browse the repository at this point in the history
* Allow multiple gcal widgets

Store cached credentials referenced by email
This solves #1016

* Rename existing gcal file if exists
  • Loading branch information
Seanstoppable authored Oct 27, 2021
1 parent 99e51bd commit 5f74b71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions modules/gcal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (widget *Widget) Fetch() ([]*CalEvent, error) {
if err != nil {
return nil, err
}
client := getClient(ctx, config)
client := getClient(ctx, config, widget.settings.email)

srv, err := calendar.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
Expand Down Expand Up @@ -103,8 +103,8 @@ func fromMidnight() time.Time {

// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
cacheFile, err := tokenCacheFile()
func getClient(ctx context.Context, config *oauth2.Config, name string) *http.Client {
cacheFile, err := tokenCacheFile(name)
if err != nil {
log.Fatalf("Unable to get path to cached credential file. %v", err)
}
Expand All @@ -116,8 +116,8 @@ func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
return config.Client(ctx, tok)
}

func isAuthenticated() bool {
cacheFile, err := tokenCacheFile()
func isAuthenticated(name string) bool {
cacheFile, err := tokenCacheFile(name)
if err != nil {
log.Fatalf("Unable to get path to cached credential file. %v", err)
}
Expand All @@ -135,7 +135,7 @@ func (widget *Widget) authenticate() {

config, _ := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope)
tok := getTokenFromWeb(config)
cacheFile, _ := tokenCacheFile()
cacheFile, _ := tokenCacheFile(widget.settings.email)
saveToken(cacheFile, tok)
}

Expand All @@ -160,8 +160,22 @@ func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {

// tokenCacheFile generates credential file path/filename.
// It returns the generated credential path/filename.
func tokenCacheFile() (string, error) {
return cfg.CreateFile("gcal-auth.json")
func tokenCacheFile(name string) (string, error) {
configDir, err := cfg.WtfConfigDir()
if err != nil {
return "", err
}
oldFile := configDir + "/gcal-auth.json"
newFileName := fmt.Sprintf("%s-gcal-auth.json", name)
if _, err := os.Stat(oldFile); err == nil {
renamedFile := configDir + "/" + newFileName
err := os.Rename(oldFile, renamedFile)
if err != nil {
return "", err
}
return renamedFile, nil
}
return cfg.CreateFile(newFileName)
}

// tokenFromFile retrieves a Token from a given file path.
Expand Down
2 changes: 1 addition & 1 deletion modules/gcal/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (widget *Widget) Disable() {
}

func (widget *Widget) Refresh() {
if isAuthenticated() {
if isAuthenticated(widget.settings.email) {
widget.fetchAndDisplayEvents()
return
}
Expand Down

0 comments on commit 5f74b71

Please sign in to comment.