fix: cli client (#92)
* fix: cli client * fix: print shutdown error correctly
This commit is contained in:
parent
602592d5f3
commit
5cd7bae505
2 changed files with 14 additions and 9 deletions
|
@ -13,13 +13,14 @@ const (
|
||||||
loginPath = "/login"
|
loginPath = "/login"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CodeFlow(relyingParty rp.RelyingParty, callbackPath, port string, stateProvider func() string) *oidc.Tokens {
|
func CodeFlow(ctx context.Context, relyingParty rp.RelyingParty, callbackPath, port string, stateProvider func() string) *oidc.Tokens {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
codeflowCtx, codeflowCancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
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) {
|
callback := func(w http.ResponseWriter, r *http.Request, tokens *oidc.Tokens, state string) {
|
||||||
token = tokens
|
tokenChan <- tokens
|
||||||
msg := "<p><strong>Success!</strong></p>"
|
msg := "<p><strong>Success!</strong></p>"
|
||||||
msg = msg + "<p>You are authenticated and can now return to the CLI.</p>"
|
msg = msg + "<p>You are authenticated and can now return to the CLI.</p>"
|
||||||
w.Write([]byte(msg))
|
w.Write([]byte(msg))
|
||||||
|
@ -27,9 +28,9 @@ func CodeFlow(relyingParty rp.RelyingParty, callbackPath, port string, stateProv
|
||||||
http.Handle(loginPath, rp.AuthURLHandler(stateProvider, relyingParty))
|
http.Handle(loginPath, rp.AuthURLHandler(stateProvider, relyingParty))
|
||||||
http.Handle(callbackPath, rp.CodeExchangeHandler(callback, relyingParty))
|
http.Handle(callbackPath, rp.CodeExchangeHandler(callback, relyingParty))
|
||||||
|
|
||||||
utils.StartServer(ctx, port)
|
utils.StartServer(codeflowCtx, ":"+port)
|
||||||
|
|
||||||
utils.OpenBrowser("http://localhost:" + port + loginPath)
|
utils.OpenBrowser("http://localhost:" + port + loginPath)
|
||||||
|
|
||||||
return token
|
return <-tokenChan
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,7 +97,11 @@ func StartServer(ctx context.Context, port string) {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
err := server.Shutdown(ctx)
|
ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
log.Fatalf("Shutdown(): %v", err)
|
defer cancelShutdown()
|
||||||
|
err := server.Shutdown(ctxShutdown)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Shutdown(): %v", err)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue