Skip to content

Commit acc88e1

Browse files
committed
Add option to set success and error pages for when calling AcquireInteractiveOption
1 parent 882b562 commit acc88e1

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

apps/internal/local/server.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var okPage = []byte(`
2828
</html>
2929
`)
3030

31-
const failPage = `
31+
var failPage = []byte(`
3232
<!DOCTYPE html>
3333
<html>
3434
<head>
@@ -40,7 +40,7 @@ const failPage = `
4040
<p>Error details: error %s error_description: %s</p>
4141
</body>
4242
</html>
43-
`
43+
`)
4444

4545
// Result is the result from the redirect.
4646
type Result struct {
@@ -60,7 +60,7 @@ type Server struct {
6060
}
6161

6262
// New creates a local HTTP server and starts it.
63-
func New(reqState string, port int) (*Server, error) {
63+
func New(reqState string, port int, successPage []byte, errorPage []byte) (*Server, error) {
6464
var l net.Listener
6565
var err error
6666
var portStr string
@@ -84,6 +84,14 @@ func New(reqState string, port int) (*Server, error) {
8484
return nil, err
8585
}
8686

87+
if len(successPage) > 0 {
88+
okPage = successPage
89+
}
90+
91+
if len(errorPage) > 0 {
92+
failPage = errorPage
93+
}
94+
8795
serv := &Server{
8896
Addr: fmt.Sprintf("http://localhost:%s", portStr),
8997
s: &http.Server{Addr: "localhost:0", ReadHeaderTimeout: time.Second},
@@ -145,7 +153,7 @@ func (s *Server) handler(w http.ResponseWriter, r *http.Request) {
145153
desc := html.EscapeString(q.Get("error_description"))
146154
// Note: It is a little weird we handle some errors by not going to the failPage. If they all should,
147155
// change this to s.error() and make s.error() write the failPage instead of an error code.
148-
_, _ = w.Write([]byte(fmt.Sprintf(failPage, headerErr, desc)))
156+
_, _ = w.Write([]byte(fmt.Sprintf(string(failPage), headerErr, desc)))
149157
s.putResult(Result{Err: fmt.Errorf(desc)})
150158
return
151159
}

apps/public/public.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,59 @@ type interactiveAuthOptions struct {
522522
claims, domainHint, loginHint, redirectURI, tenantID string
523523
openURL func(url string) error
524524
authnScheme AuthenticationScheme
525+
successPage []byte
526+
errorPage []byte
525527
}
526528

527529
// AcquireInteractiveOption is implemented by options for AcquireTokenInteractive
528530
type AcquireInteractiveOption interface {
529531
acquireInteractiveOption()
530532
}
531533

534+
func WithSuccessPage(successPage []byte) interface {
535+
AcquireInteractiveOption
536+
options.CallOption
537+
} {
538+
return struct {
539+
AcquireInteractiveOption
540+
options.CallOption
541+
}{
542+
CallOption: options.NewCallOption(
543+
func(a any) error {
544+
switch t := a.(type) {
545+
case *interactiveAuthOptions:
546+
t.successPage = successPage
547+
default:
548+
return fmt.Errorf("unexpected options type %T", a)
549+
}
550+
return nil
551+
},
552+
),
553+
}
554+
}
555+
556+
func WithErrorPage(errorPage []byte) interface {
557+
AcquireInteractiveOption
558+
options.CallOption
559+
} {
560+
return struct {
561+
AcquireInteractiveOption
562+
options.CallOption
563+
}{
564+
CallOption: options.NewCallOption(
565+
func(a any) error {
566+
switch t := a.(type) {
567+
case *interactiveAuthOptions:
568+
t.errorPage = errorPage
569+
default:
570+
return fmt.Errorf("unexpected options type %T", a)
571+
}
572+
return nil
573+
},
574+
),
575+
}
576+
}
577+
532578
// WithLoginHint pre-populates the login prompt with a username.
533579
func WithLoginHint(username string) interface {
534580
AcquireInteractiveOption
@@ -671,7 +717,7 @@ func (pca Client) AcquireTokenInteractive(ctx context.Context, scopes []string,
671717
if o.authnScheme != nil {
672718
authParams.AuthnScheme = o.authnScheme
673719
}
674-
res, err := pca.browserLogin(ctx, redirectURL, authParams, o.openURL)
720+
res, err := pca.browserLogin(ctx, redirectURL, authParams, o.openURL, o.successPage, o.successPage)
675721
if err != nil {
676722
return AuthResult{}, err
677723
}
@@ -709,13 +755,13 @@ func parsePort(u *url.URL) (int, error) {
709755
}
710756

711757
// browserLogin calls openURL and waits for a user to log in
712-
func (pca Client) browserLogin(ctx context.Context, redirectURI *url.URL, params authority.AuthParams, openURL func(string) error) (interactiveAuthResult, error) {
758+
func (pca Client) browserLogin(ctx context.Context, redirectURI *url.URL, params authority.AuthParams, openURL func(string) error, successPage []byte, errorPage []byte) (interactiveAuthResult, error) {
713759
// start local redirect server so login can call us back
714760
port, err := parsePort(redirectURI)
715761
if err != nil {
716762
return interactiveAuthResult{}, err
717763
}
718-
srv, err := local.New(params.State, port)
764+
srv, err := local.New(params.State, port, successPage, errorPage)
719765
if err != nil {
720766
return interactiveAuthResult{}, err
721767
}

0 commit comments

Comments
 (0)