@@ -36,6 +36,7 @@ import (
3636
3737 "github.com/coreos/go-oidc/v3/oidc"
3838 "github.com/go-jose/go-jose/v3/jwt"
39+ "github.com/spf13/pflag"
3940 "golang.org/x/oauth2"
4041
4142 "github.com/unikorn-cloud/core/pkg/server/errors"
@@ -50,6 +51,13 @@ import (
5051)
5152
5253type Options struct {
54+ loginRedirectURL string
55+ errorRedirectURL string
56+ }
57+
58+ func (o * Options ) AddFlags (f * pflag.FlagSet ) {
59+ f .StringVar (& o .loginRedirectURL , "login-redirect-url" , "" , "External page to handle login requests" )
60+ f .StringVar (& o .errorRedirectURL , "error-redirect-url" , "" , "External page to handle errors" )
5361}
5462
5563// Authenticator provides Keystone authentication functionality.
@@ -384,14 +392,26 @@ func (a *Authenticator) Authorization(w http.ResponseWriter, r *http.Request) {
384392 }
385393 }
386394
395+ loginQuery := url.Values {}
396+
397+ loginQuery .Set ("state" , query .Encode ())
398+ loginQuery .Set ("callback" , "https://" + r .Host + "/oauth2/v2/login" )
399+
400+ // Redirect to an external login handler, if you have chosen to.
401+ if a .options .loginRedirectURL != "" {
402+ http .Redirect (w , r , fmt .Sprintf ("%s?%s" , a .options .loginRedirectURL , loginQuery .Encode ()), http .StatusFound )
403+ return
404+ }
405+
406+ // Otherwise use the internal version.
387407 tmpl , err := template .New ("login" ).Parse (loginTemplate )
388408 if err != nil {
389409 log .Info ("oauth2: failed to parse template" , "error" , err )
390410 return
391411 }
392412
393413 templateContext := map [string ]interface {}{
394- "query " : query .Encode (),
414+ "state " : query .Encode (),
395415 }
396416
397417 var buffer bytes.Buffer
@@ -566,27 +586,18 @@ func (a *Authenticator) Login(w http.ResponseWriter, r *http.Request) {
566586 return
567587 }
568588
569- if ! r .Form .Has ("email" ) {
570- log .Info ("email doesn't exist in form" )
571- return
572- }
573-
574- if ! r .Form .Has ("query" ) {
575- log .Info ("query doesn't exist in form" )
589+ if ! r .Form .Has ("state" ) {
590+ log .Info ("state doesn't exist in form" )
576591 return
577592 }
578593
579- if ! r .Form .Has ("provider" ) {
580- log .Info ("provider doesn't exist in form" )
581- }
582-
583- query , err := url .ParseQuery (r .Form .Get ("query" ))
594+ query , err := url .ParseQuery (r .Form .Get ("state" ))
584595 if err != nil {
585596 log .Error (err , "failed to parse query" )
586597 return
587598 }
588599
589- if providerType := r .Form .Get ("provider" ); providerType != "dynamic " {
600+ if providerType := r .Form .Get ("provider" ); providerType != "" {
590601 provider , err := a .lookupProviderByType (r .Context (), unikornv1 .IdentityProviderType (providerType ))
591602 if err != nil {
592603 authorizationError (w , r , query .Get ("redirect_uri" ), ErrorServerError , err .Error ())
@@ -608,6 +619,20 @@ func (a *Authenticator) Login(w http.ResponseWriter, r *http.Request) {
608619 return
609620 }
610621
622+ // Remove the cookie otherwise.
623+ cookie := & http.Cookie {
624+ Name : ProviderCookie ,
625+ Value : "undefined" ,
626+ MaxAge : - 1 ,
627+ }
628+
629+ w .Header ().Add ("Set-Cookie" , cookie .String ())
630+
631+ if ! r .Form .Has ("email" ) {
632+ log .Info ("email doesn't exist in form" )
633+ return
634+ }
635+
611636 organization , err := a .lookupOrganization (r .Context (), r .Form .Get ("email" ))
612637 if err != nil {
613638 authorizationError (w , r , query .Get ("redirect_uri" ), ErrorServerError , err .Error ())
0 commit comments