Skip to content

Commit

Permalink
fix: cli client (#92)
Browse files Browse the repository at this point in the history
* fix: cli client

* fix: print shutdown error correctly
  • Loading branch information
eliobischof authored Apr 8, 2021
1 parent 602592d commit 5cd7bae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
15 changes: 8 additions & 7 deletions pkg/client/rp/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ const (
loginPath = "/login"
)

func CodeFlow(relyingParty rp.RelyingParty, callbackPath, port string, stateProvider func() string) *oidc.Tokens {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
func CodeFlow(ctx context.Context, relyingParty rp.RelyingParty, callbackPath, port string, stateProvider func() string) *oidc.Tokens {
codeflowCtx, codeflowCancel := context.WithCancel(ctx)
defer codeflowCancel()

tokenChan := make(chan *oidc.Tokens, 1)

var token *oidc.Tokens
callback := func(w http.ResponseWriter, r *http.Request, tokens *oidc.Tokens, state string) {
token = tokens
tokenChan <- tokens
msg := "<p><strong>Success!</strong></p>"
msg = msg + "<p>You are authenticated and can now return to the CLI.</p>"
w.Write([]byte(msg))
}
http.Handle(loginPath, rp.AuthURLHandler(stateProvider, relyingParty))
http.Handle(callbackPath, rp.CodeExchangeHandler(callback, relyingParty))

utils.StartServer(ctx, port)
utils.StartServer(codeflowCtx, ":"+port)

utils.OpenBrowser("http://localhost:" + port + loginPath)

return token
return <-tokenChan
}
8 changes: 6 additions & 2 deletions pkg/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func StartServer(ctx context.Context, port string) {

go func() {
<-ctx.Done()
err := server.Shutdown(ctx)
log.Fatalf("Shutdown(): %v", err)
ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelShutdown()
err := server.Shutdown(ctxShutdown)
if err != nil {
log.Fatalf("Shutdown(): %v", err)
}
}()
}

0 comments on commit 5cd7bae

Please sign in to comment.