Skip to content

[public-api] Extract BearerToken from request #9703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion components/public-api-server/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"testing"
)

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

require.NoError(t, register(srv))
Expand Down
26 changes: 26 additions & 0 deletions components/public-api-server/pkg/apiv1/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package apiv1
import (
"context"
v1 "github.com/gitpod-io/gitpod/public-api/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

func NewWorkspaceService() *WorkspaceService {
Expand All @@ -20,6 +23,11 @@ type WorkspaceService struct {
}

func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceRequest) (*v1.GetWorkspaceResponse, error) {
_, err := bearerTokenFromContext(ctx)
if err != nil {
return nil, err
}

return &v1.GetWorkspaceResponse{
Result: &v1.Workspace{
WorkspaceId: r.GetWorkspaceId(),
Expand All @@ -33,3 +41,21 @@ func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceR
},
}, nil
}

func bearerTokenFromContext(ctx context.Context) (string, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", status.Error(codes.Unauthenticated, "no credentials provided")
}

values := md.Get("authorization")
if len(values) == 0 {
return "", status.Error(codes.Unauthenticated, "no authorization header specified")
}
if len(values) > 1 {
return "", status.Error(codes.Unauthenticated, "more than one authorization header specified, exactly one is required")
}

token := values[0]
return token, nil
}
22 changes: 18 additions & 4 deletions components/public-api-server/pkg/apiv1/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,34 @@ package apiv1

import (
"context"
"github.com/gitpod-io/gitpod/common-go/baseserver"
v1 "github.com/gitpod-io/gitpod/public-api/v1"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/proto"
"testing"
)

func TestWorkspaceService_GetWorkspace(t *testing.T) {
svc := NewWorkspaceService()
srv := baseserver.NewForTests(t)
v1.RegisterWorkspacesServiceServer(srv.GRPC(), NewWorkspaceService())
baseserver.StartServerForTests(t, srv)

conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)

client := v1.NewWorkspacesServiceClient(conn)

ctx := metadata.AppendToOutgoingContext(context.Background(), "authorization", "some-token")

workspaceID := "some-workspace-id"
resp, err := svc.GetWorkspace(context.Background(), &v1.GetWorkspaceRequest{
resp, err := client.GetWorkspace(ctx, &v1.GetWorkspaceRequest{
WorkspaceId: workspaceID,
})
require.NoError(t, err)
require.Equal(t, &v1.GetWorkspaceResponse{
require.True(t, proto.Equal(&v1.GetWorkspaceResponse{
Result: &v1.Workspace{
WorkspaceId: workspaceID,
OwnerId: "mock_owner",
Expand All @@ -30,5 +44,5 @@ func TestWorkspaceService_GetWorkspace(t *testing.T) {
},
Description: "This is a mock response",
},
}, resp)
}, resp))
}