mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
authenticate in suspended mode
This commit is contained in:
parent
a032ca55f5
commit
c76f49d037
@ -14,13 +14,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/senorprogrammer/wtf/cfg"
|
||||||
"github.com/senorprogrammer/wtf/wtf"
|
"github.com/senorprogrammer/wtf/wtf"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
@ -115,6 +113,30 @@ func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
|
|||||||
return config.Client(ctx, tok)
|
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.
|
// getTokenFromWeb uses Config to request a Token.
|
||||||
// It returns the retrieved Token.
|
// It returns the retrieved Token.
|
||||||
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
|
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
|
||||||
@ -137,14 +159,7 @@ func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
|
|||||||
// tokenCacheFile generates credential file path/filename.
|
// tokenCacheFile generates credential file path/filename.
|
||||||
// It returns the generated credential path/filename.
|
// It returns the generated credential path/filename.
|
||||||
func tokenCacheFile() (string, error) {
|
func tokenCacheFile() (string, error) {
|
||||||
usr, err := user.Current()
|
return cfg.CreateFile("gcal-auth.json")
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tokenFromFile retrieves a Token from a given file path.
|
// tokenFromFile retrieves a Token from a given file path.
|
||||||
|
@ -14,12 +14,14 @@ type Widget struct {
|
|||||||
calEvents []*CalEvent
|
calEvents []*CalEvent
|
||||||
ch chan struct{}
|
ch chan struct{}
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
|
app *tview.Application
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWidget(app *tview.Application) *Widget {
|
func NewWidget(app *tview.Application) *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
TextWidget: wtf.NewTextWidget(app, "Calendar", "gcal", true),
|
TextWidget: wtf.NewTextWidget(app, "Calendar", "gcal", true),
|
||||||
ch: make(chan struct{}),
|
ch: make(chan struct{}),
|
||||||
|
app: app,
|
||||||
}
|
}
|
||||||
|
|
||||||
go updateLoop(&widget)
|
go updateLoop(&widget)
|
||||||
@ -35,19 +37,27 @@ func (widget *Widget) Disable() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) Refresh() {
|
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()
|
calEvents, err := Fetch()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
widget.calEvents = []*CalEvent{}
|
widget.calEvents = []*CalEvent{}
|
||||||
} else {
|
} else {
|
||||||
widget.calEvents = calEvents
|
widget.calEvents = calEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.UpdateRefreshedAt()
|
|
||||||
widget.display()
|
widget.display()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
|
||||||
|
|
||||||
func updateLoop(widget *Widget) {
|
func updateLoop(widget *Widget) {
|
||||||
interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30)
|
interval := wtf.Config.UInt("wtf.mods.gcal.textInterval", 30)
|
||||||
if interval == 0 {
|
if interval == 0 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user