mirror of
https://github.com/docker/compose.git
synced 2025-07-06 05:14:25 +02:00
get an available port for login localhost server, instead of hardcoded port
This commit is contained in:
parent
d49773e348
commit
8b116b7c73
@ -7,6 +7,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -97,20 +98,13 @@ type azureAPIHelper struct{}
|
|||||||
//Login perform azure login through browser
|
//Login perform azure login through browser
|
||||||
func (login AzureLoginService) Login(ctx context.Context) error {
|
func (login AzureLoginService) Login(ctx context.Context) error {
|
||||||
queryCh := make(chan url.Values, 1)
|
queryCh := make(chan url.Values, 1)
|
||||||
mux := http.NewServeMux()
|
serverPort, err := startLoginServer(queryCh)
|
||||||
mux.HandleFunc("/", queryHandler(queryCh))
|
if err != nil {
|
||||||
server := &http.Server{Addr: ":8401", Handler: mux}
|
return err
|
||||||
go func() {
|
|
||||||
if err := server.ListenAndServe(); err != nil {
|
|
||||||
queryCh <- url.Values{
|
|
||||||
"error": []string{fmt.Sprintf("error starting http server with: %v", err)},
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
state := randomString("", 10)
|
redirectURL := "http://localhost:" + strconv.Itoa(serverPort)
|
||||||
authURL := fmt.Sprintf(authorizeFormat, clientID, "http://localhost:8401", state, scopes)
|
openAzureLoginPage(redirectURL)
|
||||||
openbrowser(authURL)
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@ -129,7 +123,7 @@ func (login AzureLoginService) Login(ctx context.Context) error {
|
|||||||
"client_id": []string{clientID},
|
"client_id": []string{clientID},
|
||||||
"code": code,
|
"code": code,
|
||||||
"scope": []string{scopes},
|
"scope": []string{scopes},
|
||||||
"redirect_uri": []string{"http://localhost:8401"},
|
"redirect_uri": []string{redirectURL},
|
||||||
}
|
}
|
||||||
token, err := login.apiHelper.queryToken(data, "organizations")
|
token, err := login.apiHelper.queryToken(data, "organizations")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -183,6 +177,32 @@ func (login AzureLoginService) Login(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func startLoginServer(queryCh chan url.Values) (int, error) {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", queryHandler(queryCh))
|
||||||
|
listener, err := net.Listen("tcp", ":0")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
availablePort := listener.Addr().(*net.TCPAddr).Port
|
||||||
|
server := &http.Server{Handler: mux}
|
||||||
|
go func() {
|
||||||
|
if err := server.Serve(listener); err != nil {
|
||||||
|
queryCh <- url.Values{
|
||||||
|
"error": []string{fmt.Sprintf("error starting http server with: %v", err)},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return availablePort, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func openAzureLoginPage(redirectURL string) {
|
||||||
|
state := randomString("", 10)
|
||||||
|
authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes)
|
||||||
|
openbrowser(authURL)
|
||||||
|
}
|
||||||
|
|
||||||
func queryHandler(queryCh chan url.Values) func(w http.ResponseWriter, r *http.Request) {
|
func queryHandler(queryCh chan url.Values) func(w http.ResponseWriter, r *http.Request) {
|
||||||
queryHandler := func(w http.ResponseWriter, r *http.Request) {
|
queryHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||||
_, hasCode := r.URL.Query()["code"]
|
_, hasCode := r.URL.Query()["code"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user