Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suspend app to make authenticating for google calendar easier #327

Merged
merged 2 commits into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions gcal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"sort"
"time"

"github.com/senorprogrammer/wtf/cfg"
"github.com/senorprogrammer/wtf/wtf"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
Expand Down Expand Up @@ -115,12 +113,36 @@ func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
return config.Client(ctx, tok)
}

func isAuthenticated() bool {
cacheFile, err := tokenCacheFile()
if err != nil {
log.Fatalf("Unable to get path to cached credential file. %v", err)
}
_, err = tokenFromFile(cacheFile)
return err == nil
}

func authenticate() {
filename := wtf.Config.UString("wtf.mods.gcal.secretFile")
secretPath, _ := wtf.ExpandHomeDir(filename)

b, err := ioutil.ReadFile(secretPath)
if err != nil {
log.Fatalf("Unable to read secret file. %v", filename)
}

config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope)
tok := getTokenFromWeb(config)
cacheFile, err := tokenCacheFile()
saveToken(cacheFile, tok)
}

// getTokenFromWeb uses Config to request a Token.
// It returns the retrieved Token.
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: \n%v\n", authURL)
"authorization code: \n%v (press 'return' before inserting the code)", authURL)

var code string
if _, err := fmt.Scan(&code); err != nil {
Expand All @@ -137,14 +159,7 @@ func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
// tokenCacheFile generates credential file path/filename.
// It returns the generated credential path/filename.
func tokenCacheFile() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
tokenCacheDir := filepath.Join(usr.HomeDir, ".credentials")
os.MkdirAll(tokenCacheDir, 0700)
return filepath.Join(tokenCacheDir,
url.QueryEscape("calendar-go-quickstart.json")), err
return cfg.CreateFile("gcal-auth.json")
}

// tokenFromFile retrieves a Token from a given file path.
Expand Down
18 changes: 14 additions & 4 deletions gcal/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ type Widget struct {
calEvents []*CalEvent
ch chan struct{}
mutex sync.Mutex
app *tview.Application
}

func NewWidget(app *tview.Application) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(app, "Calendar", "gcal", true),
ch: make(chan struct{}),
app: app,
}

go updateLoop(&widget)
Expand All @@ -35,19 +37,27 @@ func (widget *Widget) Disable() {
}

func (widget *Widget) Refresh() {
defer widget.UpdateRefreshedAt()
if isAuthenticated() {
widget.fetchAndDisplayEvents()
return
}
widget.app.Suspend(authenticate)
widget.Refresh()
}

/* -------------------- Unexported Functions -------------------- */

func (widget *Widget) fetchAndDisplayEvents() {
calEvents, err := Fetch()
if err != nil {
widget.calEvents = []*CalEvent{}
} else {
widget.calEvents = calEvents
}

widget.UpdateRefreshedAt()
widget.display()
}

/* -------------------- Unexported Functions -------------------- */

func updateLoop(widget *Widget) {
interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30)
if interval == 0 {
Expand Down