Skip to content

Commit 07323e5

Browse files
committed
Path based redirects redirect to dashboardurl
1 parent ec04552 commit 07323e5

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

coderd/httpmw/apikey.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
149149
// like workspace applications.
150150
write := func(code int, response codersdk.Response) (*database.APIKey, *Authorization, bool) {
151151
if cfg.RedirectToLogin {
152-
RedirectToLogin(rw, r, response.Message)
152+
RedirectToLogin(rw, r, nil, response.Message)
153153
return nil, nil, false
154154
}
155155

@@ -440,7 +440,11 @@ func SplitAPIToken(token string) (id string, secret string, err error) {
440440

441441
// RedirectToLogin redirects the user to the login page with the `message` and
442442
// `redirect` query parameters set.
443-
func RedirectToLogin(rw http.ResponseWriter, r *http.Request, message string) {
443+
//
444+
// If dashboardURL is nil, the redirect will be relative to the current
445+
// request's host. If it is not nil, the redirect will be absolute with dashboard
446+
// url as the host.
447+
func RedirectToLogin(rw http.ResponseWriter, r *http.Request, dashboardURL *url.URL, message string) {
444448
path := r.URL.Path
445449
if r.URL.RawQuery != "" {
446450
path += "?" + r.URL.RawQuery
@@ -454,6 +458,14 @@ func RedirectToLogin(rw http.ResponseWriter, r *http.Request, message string) {
454458
Path: "/login",
455459
RawQuery: q.Encode(),
456460
}
461+
// If dashboardURL is provided, we want to redirect to the dashboard
462+
// login page.
463+
if dashboardURL != nil {
464+
cpy := *dashboardURL
465+
cpy.Path = u.Path
466+
cpy.RawQuery = u.RawQuery
467+
u = &cpy
468+
}
457469

458470
// See other forces a GET request rather than keeping the current method
459471
// (like temporary redirect does).

coderd/httpmw/userparam.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func ExtractUserParam(db database.Store, redirectToLoginOnMe bool) func(http.Han
6060
apiKey, ok := APIKeyOptional(r)
6161
if !ok {
6262
if redirectToLoginOnMe {
63-
RedirectToLogin(rw, r, SignedOutErrorMessage)
63+
RedirectToLogin(rw, r, nil, SignedOutErrorMessage)
6464
return
6565
}
6666

coderd/workspaceapps/db.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ func (p *DBTokenProvider) IssueToken(ctx context.Context, rw http.ResponseWriter
130130
// and they aren't signed in.
131131
switch appReq.AccessMethod {
132132
case AccessMethodPath:
133-
// TODO(@deansheather): this doesn't work on moons so will need to
134-
// be updated to include the access URL as a param
135-
httpmw.RedirectToLogin(rw, r, httpmw.SignedOutErrorMessage)
133+
httpmw.RedirectToLogin(rw, r, p.DashboardURL, httpmw.SignedOutErrorMessage)
136134
case AccessMethodSubdomain:
137135
// Redirect to the app auth redirect endpoint with a valid redirect
138136
// URI.

enterprise/cli/workspaceproxy.go

Whitespace-only changes.

0 commit comments

Comments
 (0)