Skip to content

Commit 3796657

Browse files
committed
[public-api] Extract BearerToken from request
1 parent e0452ea commit 3796657

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

components/public-api-server/integration_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
"google.golang.org/grpc"
1313
"google.golang.org/grpc/codes"
1414
"google.golang.org/grpc/credentials/insecure"
15+
"google.golang.org/grpc/metadata"
1516
"google.golang.org/grpc/status"
1617
"testing"
1718
)
1819

1920
func TestPublicAPIServer_v1_WorkspaceService(t *testing.T) {
20-
ctx := context.Background()
21+
ctx := metadata.AppendToOutgoingContext(context.Background(), "authorization", "some-token")
2122
srv := baseserver.NewForTests(t)
2223

2324
require.NoError(t, register(srv))

components/public-api-server/pkg/apiv1/workspace.go

+26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package apiv1
77
import (
88
"context"
99
v1 "github.com/gitpod-io/gitpod/public-api/v1"
10+
"google.golang.org/grpc/codes"
11+
"google.golang.org/grpc/metadata"
12+
"google.golang.org/grpc/status"
1013
)
1114

1215
func NewWorkspaceService() *WorkspaceService {
@@ -20,6 +23,11 @@ type WorkspaceService struct {
2023
}
2124

2225
func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceRequest) (*v1.GetWorkspaceResponse, error) {
26+
_, err := bearerTokenFromContext(ctx)
27+
if err != nil {
28+
return nil, err
29+
}
30+
2331
return &v1.GetWorkspaceResponse{
2432
ResponseStatus: nil,
2533
Result: &v1.Workspace{
@@ -34,3 +42,21 @@ func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceR
3442
},
3543
}, nil
3644
}
45+
46+
func bearerTokenFromContext(ctx context.Context) (string, error) {
47+
md, ok := metadata.FromIncomingContext(ctx)
48+
if !ok {
49+
return "", status.Error(codes.Unauthenticated, "no credentials provided")
50+
}
51+
52+
values := md.Get("authorization")
53+
if len(values) == 0 {
54+
return "", status.Error(codes.Unauthenticated, "no authorization header specified")
55+
}
56+
if len(values) > 1 {
57+
return "", status.Error(codes.Unauthenticated, "more than one authorization header specified, exactly one is required")
58+
}
59+
60+
token := values[0]
61+
return token, nil
62+
}

components/public-api-server/pkg/apiv1/workspace_test.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,34 @@ package apiv1
66

77
import (
88
"context"
9+
"github.com/gitpod-io/gitpod/common-go/baseserver"
910
v1 "github.com/gitpod-io/gitpod/public-api/v1"
1011
"github.com/stretchr/testify/require"
12+
"google.golang.org/grpc"
13+
"google.golang.org/grpc/credentials/insecure"
14+
"google.golang.org/grpc/metadata"
15+
"google.golang.org/protobuf/proto"
1116
"testing"
1217
)
1318

1419
func TestWorkspaceService_GetWorkspace(t *testing.T) {
15-
svc := NewWorkspaceService()
20+
srv := baseserver.NewForTests(t)
21+
v1.RegisterWorkspacesServiceServer(srv.GRPC(), NewWorkspaceService())
22+
baseserver.StartServerForTests(t, srv)
23+
24+
conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
25+
require.NoError(t, err)
26+
27+
client := v1.NewWorkspacesServiceClient(conn)
28+
29+
ctx := metadata.AppendToOutgoingContext(context.Background(), "authorization", "some-token")
1630

1731
workspaceID := "some-workspace-id"
18-
resp, err := svc.GetWorkspace(context.Background(), &v1.GetWorkspaceRequest{
32+
resp, err := client.GetWorkspace(ctx, &v1.GetWorkspaceRequest{
1933
WorkspaceId: workspaceID,
2034
})
2135
require.NoError(t, err)
22-
require.Equal(t, &v1.GetWorkspaceResponse{
36+
require.True(t, proto.Equal(&v1.GetWorkspaceResponse{
2337
ResponseStatus: nil,
2438
Result: &v1.Workspace{
2539
WorkspaceId: workspaceID,
@@ -31,5 +45,5 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) {
3145
},
3246
Description: "This is a mock response",
3347
},
34-
}, resp)
48+
}, resp))
3549
}

0 commit comments

Comments
 (0)