mirror of
https://github.com/netbirdio/netbird.git
synced 2025-05-01 06:44:37 +02:00
Close PKCE Listening Port After Authorization (#1110)
Addresses the issue of an open listening port persisting after the PKCE authorization flow is completed.
This commit is contained in:
parent
80d9b5fca5
commit
1a9301b684
@ -204,6 +204,7 @@ func openURL(cmd *cobra.Command, verificationURIComplete, userCode string) {
|
|||||||
|
|
||||||
authenticateUsingBrowser := func() {
|
authenticateUsingBrowser := func() {
|
||||||
cmd.Println(browserAuthMsg)
|
cmd.Println(browserAuthMsg)
|
||||||
|
cmd.Println("")
|
||||||
if err := open.Run(verificationURIComplete); err != nil {
|
if err := open.Run(verificationURIComplete); err != nil {
|
||||||
cmd.Println(setupKeyAuthMsg)
|
cmd.Println(setupKeyAuthMsg)
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,14 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -125,21 +127,25 @@ func (p *PKCEAuthorizationFlow) WaitToken(ctx context.Context, _ AuthFlowInfo) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *PKCEAuthorizationFlow) startServer(tokenChan chan<- *oauth2.Token, errChan chan<- error) {
|
func (p *PKCEAuthorizationFlow) startServer(tokenChan chan<- *oauth2.Token, errChan chan<- error) {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
parsedURL, err := url.Parse(p.oAuthConfig.RedirectURL)
|
parsedURL, err := url.Parse(p.oAuthConfig.RedirectURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- fmt.Errorf("failed to parse redirect URL: %v", err)
|
errChan <- fmt.Errorf("failed to parse redirect URL: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
port := parsedURL.Port()
|
|
||||||
|
|
||||||
server := http.Server{Addr: fmt.Sprintf(":%s", port)}
|
server := http.Server{Addr: fmt.Sprintf(":%s", parsedURL.Port())}
|
||||||
defer func() {
|
go func() {
|
||||||
if err := server.Shutdown(context.Background()); err != nil {
|
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
log.Errorf("error while shutting down pkce flow server: %v", err)
|
errChan <- err
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
wg.Add(1)
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
tokenValidatorFunc := func() (*oauth2.Token, error) {
|
tokenValidatorFunc := func() (*oauth2.Token, error) {
|
||||||
query := req.URL.Query()
|
query := req.URL.Query()
|
||||||
|
|
||||||
@ -176,8 +182,9 @@ func (p *PKCEAuthorizationFlow) startServer(tokenChan chan<- *oauth2.Token, errC
|
|||||||
tokenChan <- token
|
tokenChan <- token
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := server.ListenAndServe(); err != nil {
|
wg.Wait()
|
||||||
errChan <- err
|
if err := server.Shutdown(context.Background()); err != nil {
|
||||||
|
log.Errorf("error while shutting down pkce flow server: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user