@@ -44,9 +44,12 @@ import (
44
44
// Options are requires parameters for Coder to start.
45
45
type Options struct {
46
46
AccessURL * url.URL
47
- Logger slog.Logger
48
- Database database.Store
49
- Pubsub database.Pubsub
47
+ // AppHostname should be the wildcard hostname to use for workspace
48
+ // applications without the asterisk or leading dot. E.g. "apps.coder.com".
49
+ AppHostname string
50
+ Logger slog.Logger
51
+ Database database.Store
52
+ Pubsub database.Pubsub
50
53
51
54
// CacheDir is used for caching files served by the API.
52
55
CacheDir string
@@ -158,7 +161,20 @@ func New(options *Options) *API {
158
161
Github : options .GithubOAuth2Config ,
159
162
OIDC : options .OIDCConfig ,
160
163
}
161
- apiKeyMiddleware := httpmw .ExtractAPIKey (options .Database , oauthConfigs , false )
164
+
165
+ apiKeyMiddleware := httpmw .ExtractAPIKey (httpmw.ExtractAPIKeyConfig {
166
+ DB : options .Database ,
167
+ OAuth2Configs : oauthConfigs ,
168
+ RedirectToLogin : false ,
169
+ Optional : false ,
170
+ })
171
+ // Same as above but it redirects to the login page.
172
+ apiKeyMiddlewareRedirect := httpmw .ExtractAPIKey (httpmw.ExtractAPIKeyConfig {
173
+ DB : options .Database ,
174
+ OAuth2Configs : oauthConfigs ,
175
+ RedirectToLogin : true ,
176
+ Optional : false ,
177
+ })
162
178
163
179
r .Use (
164
180
httpmw .AttachRequestID ,
@@ -170,18 +186,14 @@ func New(options *Options) *API {
170
186
api .handleSubdomainApplications (
171
187
// Middleware to impose on the served application.
172
188
httpmw .RateLimitPerMinute (options .APIRateLimit ),
173
- httpmw .UseLoginURL (func () * url.URL {
174
- if options .AccessURL == nil {
175
- return nil
176
- }
177
-
178
- u := * options .AccessURL
179
- u .Path = "/login"
180
- return & u
181
- }()),
182
- // This should extract the application specific API key when we
183
- // implement a scoped token.
184
- httpmw .ExtractAPIKey (options .Database , oauthConfigs , true ),
189
+ httpmw .ExtractAPIKey (httpmw.ExtractAPIKeyConfig {
190
+ DB : options .Database ,
191
+ OAuth2Configs : oauthConfigs ,
192
+ // The code handles the the case where the user is not
193
+ // authenticated automatically.
194
+ RedirectToLogin : false ,
195
+ Optional : true ,
196
+ }),
185
197
httpmw .ExtractUserParam (api .Database ),
186
198
httpmw .ExtractWorkspaceAndAgentParam (api .Database ),
187
199
),
@@ -199,7 +211,7 @@ func New(options *Options) *API {
199
211
r .Use (
200
212
tracing .Middleware (api .TracerProvider ),
201
213
httpmw .RateLimitPerMinute (options .APIRateLimit ),
202
- httpmw . ExtractAPIKey ( options . Database , oauthConfigs , true ) ,
214
+ apiKeyMiddlewareRedirect ,
203
215
httpmw .ExtractUserParam (api .Database ),
204
216
// Extracts the <workspace.agent> from the url
205
217
httpmw .ExtractWorkspaceAndAgentParam (api .Database ),
@@ -384,8 +396,6 @@ func New(options *Options) *API {
384
396
r .Put ("/roles" , api .putUserRoles )
385
397
r .Get ("/roles" , api .userRoles )
386
398
387
- r .Post ("/authorization" , api .checkPermissions )
388
-
389
399
r .Route ("/keys" , func (r chi.Router ) {
390
400
r .Post ("/" , api .postAPIKey )
391
401
r .Get ("/{keyid}" , api .apiKey )
@@ -481,6 +491,25 @@ func New(options *Options) *API {
481
491
r .Get ("/resources" , api .workspaceBuildResources )
482
492
r .Get ("/state" , api .workspaceBuildState )
483
493
})
494
+ r .Route ("/authcheck" , func (r chi.Router ) {
495
+ r .Use (apiKeyMiddleware )
496
+ r .Post ("/" , api .checkAuthorization )
497
+ })
498
+ r .Route ("/applications" , func (r chi.Router ) {
499
+ r .Route ("/host" , func (r chi.Router ) {
500
+ // Don't leak the hostname to unauthenticated users.
501
+ r .Use (apiKeyMiddleware )
502
+ r .Get ("/" , api .appHost )
503
+ })
504
+ r .Route ("/auth-redirect" , func (r chi.Router ) {
505
+ // We want to redirect to login if they are not authenticated.
506
+ r .Use (apiKeyMiddlewareRedirect )
507
+
508
+ // This is a GET request as it's redirected to by the subdomain app
509
+ // handler and the login page.
510
+ r .Get ("/" , api .workspaceApplicationAuth )
511
+ })
512
+ })
484
513
})
485
514
486
515
r .NotFound (compressHandler (http .HandlerFunc (api .siteHandler .ServeHTTP )).ServeHTTP )
0 commit comments