github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/skymarshal/legacyserver/server.go (about)

     1  package legacyserver
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  
     7  	"code.cloudfoundry.org/lager"
     8  	"github.com/tedsuo/rata"
     9  )
    10  
    11  type LegacyConfig struct {
    12  	Logger lager.Logger
    13  }
    14  
    15  const (
    16  	LoginRoute    = "LoginRoute"
    17  	LogoutRoute   = "LogoutRoute"
    18  	CallbackRoute = "CallbackRoute"
    19  )
    20  
    21  func NewLegacyServer(config *LegacyConfig) (http.Handler, error) {
    22  
    23  	routes := rata.Routes([]rata.Route{
    24  		{Path: "/login", Method: "GET", Name: LoginRoute},
    25  		{Path: "/logout", Method: "GET", Name: LogoutRoute},
    26  		{Path: "/auth/:provider/callback", Method: "GET", Name: CallbackRoute},
    27  	})
    28  
    29  	handlers := map[string]http.Handler{
    30  
    31  		LoginRoute: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    32  			u := url.URL{
    33  				Scheme:   r.URL.Scheme,
    34  				Host:     r.URL.Host,
    35  				Path:     "/sky/login",
    36  				RawQuery: r.URL.RawQuery,
    37  			}
    38  
    39  			flyPort := r.FormValue("fly_port")
    40  			if flyPort != "" {
    41  				u.RawQuery = "redirect_uri=/fly_success%3Ffly_port=" + flyPort
    42  			}
    43  
    44  			http.Redirect(w, r, u.String(), http.StatusFound)
    45  		}),
    46  
    47  		LogoutRoute: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    48  			u := url.URL{
    49  				Scheme: r.URL.Scheme,
    50  				Host:   r.URL.Host,
    51  				Path:   "/sky/logout",
    52  			}
    53  
    54  			http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
    55  		}),
    56  
    57  		CallbackRoute: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    58  
    59  			q, _ := url.ParseQuery(r.URL.RawQuery)
    60  			q.Del(":provider")
    61  
    62  			u := url.URL{
    63  				Scheme:   r.URL.Scheme,
    64  				Host:     r.URL.Host,
    65  				Path:     "/sky/issuer/callback",
    66  				RawQuery: q.Encode(),
    67  			}
    68  
    69  			http.Redirect(w, r, u.String(), http.StatusMovedPermanently)
    70  		}),
    71  	}
    72  
    73  	return rata.NewRouter(routes, handlers)
    74  }