Debian Codegraph @

pkg/client/rp/cli/cli.go

raw
1 package cli
2
3 import (
4 "context"
5 "net/http"
6
7 "github.com/zitadel/oidc/v3/pkg/client/rp"
8 httphelper "github.com/zitadel/oidc/v3/pkg/http"
9 "github.com/zitadel/oidc/v3/pkg/oidc"
10 )
11
12 const (
13 loginPath = "/login"
14 )
15
16 func CodeFlow[C oidc.IDClaims](ctx context.Context, relyingParty rp.RelyingParty, callbackPath, port string, stateProvider func() string) *oidc.Tokens[C] {
17 codeflowCtx, codeflowCancel := context.WithCancel(ctx)
18 defer codeflowCancel()
19
20 tokenChan := make(chan *oidc.Tokens[C], 1)
21
22 callback := func(w http.ResponseWriter, r *http.Request, tokens *oidc.Tokens[C], state string, rp rp.RelyingParty) {
23 tokenChan <- tokens
24 msg := "<p><strong>Success!</strong></p>"
25 msg = msg + "<p>You are authenticated and can now return to the CLI.</p>"
26 w.Write([]byte(msg))
27 }
28 http.Handle(loginPath, rp.AuthURLHandler(stateProvider, relyingParty))
29 http.Handle(callbackPath, rp.CodeExchangeHandler(callback, relyingParty))
30
31 httphelper.StartServer(codeflowCtx, ":"+port)
32
33 OpenBrowser("http://localhost:" + port + loginPath)
34
35 return <-tokenChan
36 }