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