8
8
"context"
9
9
"errors"
10
10
"fmt"
11
+ "strings"
11
12
12
13
"github.com/bufbuild/connect-go"
13
14
"github.com/gitpod-io/gitpod/common-go/experiments"
@@ -17,6 +18,7 @@ import (
17
18
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
18
19
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
19
20
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
21
+ "github.com/google/uuid"
20
22
)
21
23
22
24
func NewTokensService (connPool proxy.ServerConnectionPool , expClient experiments.Client ) * TokensService {
@@ -35,24 +37,50 @@ type TokensService struct {
35
37
}
36
38
37
39
func (s * TokensService ) CreatePersonalAccessToken (ctx context.Context , req * connect.Request [v1.CreatePersonalAccessTokenRequest ]) (* connect.Response [v1.CreatePersonalAccessTokenResponse ], error ) {
40
+ conn , err := s .getConnection (ctx )
41
+ if err != nil {
42
+ return nil , err
43
+ }
44
+
45
+ _ , err = s .getUser (ctx , conn )
46
+ if err != nil {
47
+ return nil , err
48
+ }
49
+
50
+ return nil , connect .NewError (connect .CodeUnimplemented , errors .New ("gitpod.experimental.v1.TokensService.CreatePersonalAccessToken is not implemented" ))
51
+ }
52
+
53
+ func (s * TokensService ) GetPersonalAccessToken (ctx context.Context , req * connect.Request [v1.GetPersonalAccessTokenRequest ]) (* connect.Response [v1.GetPersonalAccessTokenResponse ], error ) {
54
+ tokenID , err := validateTokenID (req .Msg .GetId ())
55
+ if err != nil {
56
+ return nil , err
57
+ }
38
58
39
59
conn , err := s .getConnection (ctx )
40
60
if err != nil {
41
61
return nil , err
42
62
}
43
63
64
+ _ , err = s .getUser (ctx , conn )
65
+ if err != nil {
66
+ return nil , err
67
+ }
68
+
69
+ log .Infof ("Handling GetPersonalAccessToken request for Token ID '%s'" , tokenID .String ())
70
+ return nil , connect .NewError (connect .CodeUnimplemented , errors .New ("gitpod.experimental.v1.TokensService.GetPersonalAccessToken is not implemented" ))
71
+ }
72
+
73
+ func (s * TokensService ) getUser (ctx context.Context , conn protocol.APIInterface ) (* protocol.User , error ) {
44
74
user , err := conn .GetLoggedInUser (ctx )
45
75
if err != nil {
46
76
return nil , proxy .ConvertError (err )
47
77
}
48
78
49
- isEnabled := experiments .IsPersonalAccessTokensEnabled (ctx , s .expClient , experiments.Attributes {UserID : user .ID })
50
-
51
- if ! isEnabled {
79
+ if ! experiments .IsPersonalAccessTokensEnabled (ctx , s .expClient , experiments.Attributes {UserID : user .ID }) {
52
80
return nil , connect .NewError (connect .CodePermissionDenied , errors .New ("This feature is currently in beta. If you would like to be part of the beta, please contact us." ))
53
81
}
54
82
55
- return nil , connect . NewError ( connect . CodeUnimplemented , errors . New ( "gitpod.experimental.v1.TokensService.CreatePersonalAccessToken is not implemented" ))
83
+ return user , nil
56
84
}
57
85
58
86
func (s * TokensService ) getConnection (ctx context.Context ) (protocol.APIInterface , error ) {
@@ -69,3 +97,17 @@ func (s *TokensService) getConnection(ctx context.Context) (protocol.APIInterfac
69
97
70
98
return conn , nil
71
99
}
100
+
101
+ func validateTokenID (id string ) (uuid.UUID , error ) {
102
+ trimmed := strings .TrimSpace (id )
103
+ if trimmed == "" {
104
+ return uuid .Nil , connect .NewError (connect .CodeInvalidArgument , fmt .Errorf ("Token ID is a required argument." ))
105
+ }
106
+
107
+ tokenID , err := uuid .Parse (trimmed )
108
+ if err != nil {
109
+ return uuid .Nil , connect .NewError (connect .CodeInvalidArgument , fmt .Errorf ("Token ID must be a valid UUID" ))
110
+ }
111
+
112
+ return tokenID , nil
113
+ }
0 commit comments