| 1 |
package cli |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"net" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/zitadel/oidc/v3/pkg/client/rp" |
| 13 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 14 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 15 |
) |
| 16 |
|
| 17 |
const ( |
| 18 |
loginPath = "/login" |
| 19 |
) |
| 20 |
|
| 21 |
func CodeFlow[C oidc.IDClaims](ctx context.Context, relyingParty rp.RelyingParty, callbackPath, port string, stateProvider func() string) *oidc.Tokens[C] { |
| 22 |
codeflowCtx, codeflowCancel := context.WithCancel(ctx) |
| 23 |
defer codeflowCancel() |
| 24 |
|
| 25 |
tokenChan := make(chan *oidc.Tokens[C], 1) |
| 26 |
|
| 27 |
callback := func(w http.ResponseWriter, r *http.Request, tokens *oidc.Tokens[C], state string, rp rp.RelyingParty) { |
| 28 |
tokenChan <- tokens |
| 29 |
msg := "<p><strong>Success!</strong></p>" |
| 30 |
msg = msg + "<p>You are authenticated and can now return to the CLI.</p>" |
| 31 |
w.Write([]byte(msg)) |
| 32 |
} |
| 33 |
http.Handle(loginPath, rp.AuthURLHandler(stateProvider, relyingParty)) |
| 34 |
http.Handle(callbackPath, rp.CodeExchangeHandler(callback, relyingParty)) |
| 35 |
|
| 36 |
listenAddress, loginURL, err := callbackServerConfig(relyingParty.OAuthConfig().RedirectURL, callbackPath, port) |
| 37 |
if err != nil { |
| 38 |
log.Fatalf("invalid redirect URI: %v", err) |
| 39 |
} |
| 40 |
httphelper.StartServer(codeflowCtx, listenAddress) |
| 41 |
|
| 42 |
OpenBrowser(loginURL) |
| 43 |
|
| 44 |
return <-tokenChan |
| 45 |
} |
| 46 |
|
| 47 |
func callbackServerConfig(redirectURI, callbackPath, port string) (listenAddress, loginURL string, err error) { |
| 48 |
redirect, err := url.Parse(redirectURI) |
| 49 |
if err != nil { |
| 50 |
return "", "", err |
| 51 |
} |
| 52 |
if !strings.EqualFold(redirect.Scheme, "http") { |
| 53 |
return "", "", fmt.Errorf("scheme must be http") |
| 54 |
} |
| 55 |
if redirect.User != nil { |
| 56 |
return "", "", fmt.Errorf("user information is not allowed") |
| 57 |
} |
| 58 |
if redirect.Fragment != "" { |
| 59 |
return "", "", fmt.Errorf("fragment is not allowed") |
| 60 |
} |
| 61 |
if redirect.Path != callbackPath { |
| 62 |
return "", "", fmt.Errorf("callback path %q does not match redirect URI path %q", callbackPath, redirect.Path) |
| 63 |
} |
| 64 |
|
| 65 |
host := redirect.Hostname() |
| 66 |
if host == "" { |
| 67 |
return "", "", fmt.Errorf("host is required") |
| 68 |
} |
| 69 |
redirectPort := redirect.Port() |
| 70 |
if redirectPort == "" { |
| 71 |
return "", "", fmt.Errorf("port is required") |
| 72 |
} |
| 73 |
if port != redirectPort { |
| 74 |
return "", "", fmt.Errorf("port %q does not match redirect URI port %q", port, redirectPort) |
| 75 |
} |
| 76 |
|
| 77 |
listenHost := host |
| 78 |
if strings.EqualFold(host, "localhost") { |
| 79 |
// Preserve the existing dual-stack behavior for localhost. Callers should |
| 80 |
// prefer an explicit loopback IP to avoid address-family ambiguity. |
| 81 |
listenHost = "" |
| 82 |
} else if ip := net.ParseIP(host); ip == nil || !ip.IsLoopback() { |
| 83 |
return "", "", fmt.Errorf("host %q is not a loopback address", host) |
| 84 |
} |
| 85 |
|
| 86 |
listenAddress = net.JoinHostPort(listenHost, redirectPort) |
| 87 |
login := url.URL{ |
| 88 |
Scheme: "http", |
| 89 |
Host: net.JoinHostPort(host, redirectPort), |
| 90 |
Path: loginPath, |
| 91 |
} |
| 92 |
return listenAddress, login.String(), nil |
| 93 |
} |