/
redir.go
42 lines (32 loc) · 1.08 KB
/
redir.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package x
import (
"net/http"
"path"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/ory/kratos/driver/config"
)
func RedirectToAdminRoute(reg config.Provider) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
admin := reg.Config().SelfAdminURL(r.Context())
dest := *r.URL
dest.Host = admin.Host
dest.Scheme = admin.Scheme
dest.Path = strings.TrimPrefix(dest.Path, AdminPrefix)
dest.Path = path.Join(admin.Path, AdminPrefix, dest.Path)
http.Redirect(w, r, dest.String(), http.StatusTemporaryRedirect)
}
}
func RedirectToPublicRoute(reg config.Provider) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
public := reg.Config().SelfPublicURL(r.Context())
dest := *r.URL
dest.Host = public.Host
dest.Scheme = public.Scheme
dest.Path = strings.TrimPrefix(dest.Path, AdminPrefix)
dest.Path = path.Join(public.Path, dest.Path)
http.Redirect(w, r, dest.String(), http.StatusTemporaryRedirect)
}
}