10000 Move type def to codersdk · coder/coder@1f0b34c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f0b34c

Browse files
committed
Move type def to codersdk
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 65f984f commit 1f0b34c

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

coderd/workspaceapps/apptest/setup.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/coder/coder/v2/coderd/coderdtest"
2323
"github.com/coder/coder/v2/coderd/workspaceapps"
2424
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
25-
"github.com/coder/coder/v2/coderd/workspaceapps/cors"
2625
"github.com/coder/coder/v2/codersdk"
2726
"github.com/coder/coder/v2/codersdk/agentsdk"
2827
"github.com/coder/coder/v2/cryptorand"
@@ -102,7 +101,7 @@ type App struct {
102101
Path string
103102

104103
// Control the behavior of CORS handling.
105-
CORSBehavior cors.AppCORSBehavior
104+
CORSBehavior codersdk.AppCORSBehavior
106105
}
107106

108107
// Details are the full test details returned from setupProxyTestWithFactory.
@@ -271,15 +270,15 @@ func setupProxyTestWithFactory(t *testing.T, factory DeploymentFactory, opts *De
271270
WorkspaceName: workspace.Name,
272271
AgentName: agnt.Name,
273272
AppSlugOrPort: proxyTestAppNamePublicCORSPassthru,
274-
CORSBehavior: cors.AppCORSBehaviorPassthru,
273+
CORSBehavior: codersdk.AppCORSBehaviorPassthru,
275274
Query: proxyTestAppQuery,
276275
}
277276
details.Apps.AuthenticatedCORSPassthru = App{
278277
Username: me.Username,
279278
WorkspaceName: workspace.Name,
280279
AgentName: agnt.Name,
281280
AppSlugOrPort: proxyTestAppNameAuthenticatedCORSPassthru,
282-
CORSBehavior: cors.AppCORSBehaviorPassthru,
281+
CORSBehavior: codersdk.AppCORSBehaviorPassthru,
283282
Query: proxyTestAppQuery,
284283
}
285284
details.Apps.PublicCORSDefault = App{

coderd/workspaceapps/cors/cors.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
package cors
22

3-
import "context"
3+
import (
4+
"context"
45

5-
type AppCORSBehavior string
6-
7-
const (
8-
AppCORSBehaviorSimple AppCORSBehavior = "simple"
9-
AppCORSBehaviorPassthru AppCORSBehavior = "passthru"
6+
"github.com/coder/coder/v2/codersdk"
107
)
118

129
type contextKeyBehavior struct{}
1310

1411
// WithBehavior sets the CORS behavior for the given context.
15-
func WithBehavior(ctx context.Context, behavior AppCORSBehavior) context.Context {
12+
func WithBehavior(ctx context.Context, behavior codersdk.AppCORSBehavior) context.Context {
1613
return context.WithValue(ctx, contextKeyBehavior{}, behavior)
1714
}
1815

1916
// HasBehavior returns true if the given context has the specified CORS behavior.
20-
func HasBehavior(ctx context.Context, behavior AppCORSBehavior) bool {
17+
func HasBehavior(ctx context.Context, behavior codersdk.AppCORSBehavior) bool {
2118
val := ctx.Value(contextKeyBehavior{})
22-
b, ok := val.(AppCORSBehavior)
19+
b, ok := val.(codersdk.AppCORSBehavior)
2320
return ok && b == behavior
2421
}

coderd/workspaceapps/db.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/coder/coder/v2/coderd/jwtutils"
2626
"github.com/coder/coder/v2/coderd/rbac"
2727
"github.com/coder/coder/v2/coderd/rbac/policy"
28-
"github.com/coder/coder/v2/coderd/workspaceapps/cors"
2928
"github.com/coder/coder/v2/codersdk"
3029
)
3130

@@ -132,7 +131,7 @@ func (p *DBTokenProvider) Issue(ctx context.Context, rw http.ResponseWriter, r *
132131
if dbReq.AppURL != nil {
133132
token.AppURL = dbReq.AppURL.String()
134133
}
135-
token.CORSBehavior = cors.AppCORSBehavior(dbReq.AppCORSBehavior)
134+
token.CORSBehavior = codersdk.AppCORSBehavior(dbReq.AppCORSBehavior)
136135

137136
// Verify the user has access to the app.
138137
authed, warnings, err := p.authorizeRequest(r.Context(), authz, dbReq)

coderd/workspaceapps/proxy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func (s *Server) determineCORSBehavior(token *SignedToken, app appurl.Applicatio
441441
corsHandler := httpmw.WorkspaceAppCors(s.HostnameRegex, app)(next)
442442

443443
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
444-
var behavior cors.AppCORSBehavior
444+
var behavior codersdk.AppCORSBehavior
445445
if token != nil {
446446
behavior = token.CORSBehavior
447447
}
@@ -452,7 +452,7 @@ func (s *Server) determineCORSBehavior(token *SignedToken, app appurl.Applicatio
452452
r = r.WithContext(cors.WithBehavior(r.Context(), behavior))
453453

454454
switch behavior {
455-
case cors.AppCORSBehaviorPassthru:
455+
case codersdk.AppCORSBehaviorPassthru:
456456
// Bypass the CORS middleware.
457457
next.ServeHTTP(rw, r)
458458
return
@@ -595,7 +595,7 @@ func (s *Server) proxyWorkspaceApp(rw http.ResponseWriter, r *http.Request, appT
595595

596596
proxy.ModifyResponse = func(r *http.Response) error {
597597
// If passthru behavior is set, disable our CORS header stripping.
598-
if cors.HasBehavior(r.Request.Context(), cors.AppCORSBehaviorPassthru) {
598+
if cors.HasBehavior(r.Request.Context(), codersdk.AppCORSBehaviorPassthru) {
599599
return nil
600600
}
601601

coderd/workspaceapps/token.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/coder/coder/v2/coderd/cryptokeys"
1313
"github.com/coder/coder/v2/coderd/jwtutils"
14-
"github.com/coder/coder/v2/coderd/workspaceapps/cors"
1514
"github.com/coder/coder/v2/codersdk"
1615
)
1716

@@ -27,7 +26,7 @@ type SignedToken struct {
2726
WorkspaceID uuid.UUID `json:"workspace_id"`
2827
AgentID uuid.UUID `json:"agent_id"`
2928
AppURL string `json:"app_url"`
30-
CORSBehavior cors.AppCORSBehavior `json:"cors_behavior"`
29+
CORSBehavior codersdk.AppCORSBehavior `json:"cors_behavior"`
3130
}
3231

3332
// MatchesRequest returns true if the token matches the request. Any token that

codersdk/cors_behavior.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package codersdk
2+
3+
import "golang.org/x/xerrors"
4+
5+
type AppCORSBehavior string
6+
7+
const (
8+
AppCORSBehaviorSimple AppCORSBehavior = "simple"
9+
AppCORSBehaviorPassthru AppCORSBehavior = "passthru"
10+
)
11+
12+
func (c AppCORSBehavior) Validate() error {
13+
if c != AppCORSBehaviorSimple && c != AppCORSBehaviorPassthru {
14+
return xerrors.New("Invalid CORS behavior.")
15+
}
16+
return nil
17+
}

0 commit comments

Comments
 (0)
0