Skip to content

Commit

Permalink
Add timeout to local OAuth server. Closes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jul 20, 2020
1 parent 7683563 commit cd3271d
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 201 deletions.
13 changes: 13 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net/http"
"os/exec"
"sync"
"time"

"github.com/pkg/errors"
"golang.org/x/net/context"
Expand Down Expand Up @@ -253,6 +254,17 @@ func (a *Authenticator) codeFromLocalServer() (string, error) {
}
}()

// automatically close server after 3 minutes
timeout := time.AfterFunc(time.Minute*3, func() {
log.Println("[auth] automatically stopping server after timeout")
if err := srv.Shutdown(context.Background()); err != nil && err != http.ErrServerClosed {
log.Printf("[error] shutdown: %v", err)
c <- response{err: err}
return
}
c <- response{err: errors.New("OAuth server timeout exceeded")}
})

mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
vars := req.URL.Query()
code := vars.Get("code")
Expand Down Expand Up @@ -296,6 +308,7 @@ func (a *Authenticator) codeFromLocalServer() (string, error) {
})

r := <-c
timeout.Stop()

// log.Printf("srv=%+v, response=%+v", srv, r)
if err := srv.Shutdown(context.Background()); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

const (
previewServerURL = "localhost:61433"
quitAfter = 60 * time.Second
quitAfter = 90 * time.Second
)

// previewURL returns a preview server URL.
Expand Down Expand Up @@ -62,7 +62,7 @@ func doStartServer() error {
}()

go func() {
c := time.Tick(10 * time.Second)
c := time.Tick(30 * time.Second)
for now := range c {
mu.Lock()
d := now.Sub(lastRequest)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module github.com/deanishe/alfred-gcal

require (
cloud.google.com/go v0.61.0 // indirect
github.com/bmatcuk/doublestar v1.3.1 // indirect
github.com/deanishe/awgo v0.24.1
github.com/deanishe/awgo v0.25.0
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/magefile/mage v1.10.0
github.com/pkg/errors v0.9.1
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
google.golang.org/api v0.29.0
google.golang.org/genproto v0.0.0-20200720141249-1244ee217b7e // indirect
)

replace github.com/golang/lint => golang.org/x/lint v0.0.0-20190409202823-959b441ac422
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/bmatcuk/doublestar v1.3.0/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/bmatcuk/doublestar v1.3.1 h1:rT8rxDPsavp9G+4ZULzqhhUSaI/OPsTZNG88Z3i0xvY=
github.com/bmatcuk/doublestar v1.3.1/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand All @@ -48,8 +47,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deanishe/awgo v0.24.1 h1:yLQuT7dMlV986b87o8u4ank2tPIIRMh4Ff+2KRbZk8s=
github.com/deanishe/awgo v0.24.1/go.mod h1:M2gP2LpidyU4vpy5E6/eztFnrECIWTO2eQ8LF1u2he4=
github.com/deanishe/awgo v0.25.0 h1:r6/eJFETRB6Zex10XbjixSASGlc2a484b+FgRRAYY9g=
github.com/deanishe/awgo v0.25.0/go.mod h1:reRZEwXsMuJISYxmdqkn24KrHN+WV/ZQzRBXTtKDPHY=
github.com/deanishe/go-env v0.4.0 h1:tpu14o16gJGTN/w2gxntwxu2l5Eby30jSrnlgOfjzwk=
github.com/deanishe/go-env v0.4.0/go.mod h1:RgEcGAqdRnt8ybQteAbv1Ys2lWIRE7TlgON/sbdjuaY=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
Expand Down Expand Up @@ -376,6 +375,8 @@ google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEY
google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA=
google.golang.org/genproto v0.0.0-20200711021454-869866162049 h1:YFTFpQhgvrLrmxtiIncJxFXeCyq84ixuKWVCaCAi9Oc=
google.golang.org/genproto v0.0.0-20200711021454-869866162049/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20200720141249-1244ee217b7e h1:KoSCpgvphmtnpVycBCtfV9hNdHbInsqdx4gqnDPQCkg=
google.golang.org/genproto v0.0.0-20200720141249-1244ee217b7e/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand Down
Loading

0 comments on commit cd3271d

Please sign in to comment.