Skip to content

Commit facd600

Browse files
easyCZroboquat
authored andcommitted
[public-api] Extract BearerToken from request
# Conflicts: # components/public-api-server/pkg/apiv1/workspace_test.go
1 parent c9c7fc3 commit facd600

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
Result: &v1.Workspace{
2533
WorkspaceId: r.GetWorkspaceId(),
@@ -33,3 +41,21 @@ func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceR
3341
},
3442
}, nil
3543
}
44+
45+
func bearerTokenFromContext(ctx context.Context) (string, error) {
46+
md, ok := metadata.FromIncomingContext(ctx)
47+
if !ok {
48+
return "", status.Error(codes.Unauthenticated, "no credentials provided")
49+
}
50+
51+
values := md.Get("authorization")
52+
if len(values) == 0 {
53+
return "", status.Error(codes.Unauthenticated, "no authorization header specified")
54+
}
55+
if len(values) > 1 {
56+
return "", status.Error(codes.Unauthenticated, "more than one authorization header specified, exactly one is required")
57+
}
58+
59+
token := values[0]
60+
return token, nil
61+
}

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
Result: &v1.Workspace{
2438
WorkspaceId: workspaceID,
2539
OwnerId: "mock_owner",
@@ -30,5 +44,5 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) {
3044
},
3145
Description: "This is a mock response",
3246
},
33-
}, resp)
47+
}, resp))
3448
}

0 commit comments

Comments
 (0)