@@ -44,9 +44,12 @@ import (
4444// Options are requires parameters for Coder to start.
4545type Options struct {
4646 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
5053
5154 // CacheDir is used for caching files served by the API.
5255 CacheDir string
@@ -158,7 +161,20 @@ func New(options *Options) *API {
158161 Github : options .GithubOAuth2Config ,
159162 OIDC : options .OIDCConfig ,
160163 }
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+ })
162178
163179 r .Use (
164180 httpmw .AttachRequestID ,
@@ -170,18 +186,14 @@ func New(options *Options) *API {
170186 api .handleSubdomainApplications (
171187 // Middleware to impose on the served application.
172188 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+ }),
185197 httpmw .ExtractUserParam (api .Database ),
186198 httpmw .ExtractWorkspaceAndAgentParam (api .Database ),
187199 ),
@@ -199,7 +211,7 @@ func New(options *Options) *API {
199211 r .Use (
200212 tracing .Middleware (api .TracerProvider ),
201213 httpmw .RateLimitPerMinute (options .APIRateLimit ),
202- httpmw . ExtractAPIKey ( options . Database , oauthConfigs , true ) ,
214+ apiKeyMiddlewareRedirect ,
203215 httpmw .ExtractUserParam (api .Database ),
204216 // Extracts the <workspace.agent> from the url
205217 httpmw .ExtractWorkspaceAndAgentParam (api .Database ),
@@ -384,8 +396,6 @@ func New(options *Options) *API {
384396 r .Put ("/roles" , api .putUserRoles )
385397 r .Get ("/roles" , api .userRoles )
386398
387- r .Post ("/authorization" , api .checkPermissions )
388-
389399 r .Route ("/keys" , func (r chi.Router ) {
390400 r .Post ("/" , api .postAPIKey )
391401 r .Get ("/{keyid}" , api .apiKey )
@@ -481,6 +491,25 @@ func New(options *Options) *API {
481491 r .Get ("/resources" , api .workspaceBuildResources )
482492 r .Get ("/state" , api .workspaceBuildState )
483493 })
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+ })
484513 })
485514
486515 r .NotFound (compressHandler (http .HandlerFunc (api .siteHandler .ServeHTTP )).ServeHTTP )
0 commit comments