Skip to content

Commit 43bbf37

Browse files
TippyFlitsUKTippyFlits
andauthored
fix(pdptool): Skip JWT auth for public service (#566)
- Add getJWTTokenForService() wrapper that returns empty token for 'public' service - Update all commands to use the wrapper instead of direct JWT creation - Only set Authorization header when JWT token is not empty - Allows pdptool to work with public services without requiring pdpservice.json Fixes #558 Co-authored-by: TippyFlits <[email protected]>
1 parent 450438b commit 43bbf37

File tree

1 file changed

+45
-61
lines changed

1 file changed

+45
-61
lines changed

cmd/pdptool/main.go

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,10 @@ var pingCmd = &cli.Command{
185185
if serviceName == "" {
186186
return fmt.Errorf("either --jwt-token or --service-name must be provided")
187187
}
188-
privKey, err := loadPrivateKey()
188+
jwtToken, err := getJWTTokenForService(serviceName)
189189
if err != nil {
190190
return err
191191
}
192-
var errCreateToken error
193-
jwtToken, errCreateToken := createJWTToken(serviceName, privKey)
194-
if errCreateToken != nil {
195-
return errCreateToken
196-
}
197192

198193
// Append /pdp/ping to the service URL
199194
pingURL := serviceURL + "/pdp/ping"
@@ -203,7 +198,9 @@ var pingCmd = &cli.Command{
203198
if err != nil {
204199
return fmt.Errorf("failed to create request: %v", err)
205200
}
206-
req.Header.Set("Authorization", "Bearer "+jwtToken)
201+
if jwtToken != "" {
202+
req.Header.Set("Authorization", "Bearer "+jwtToken)
203+
}
207204

208205
// Send the request
209206
client := &http.Client{}
@@ -244,6 +241,19 @@ func createJWTToken(serviceName string, privateKey *ecdsa.PrivateKey) (string, e
244241
return tokenString, nil
245242
}
246243

244+
func getJWTTokenForService(serviceName string) (string, error) {
245+
if serviceName == "public" {
246+
return "", nil // No JWT needed for public service
247+
}
248+
249+
privKey, err := loadPrivateKey()
250+
if err != nil {
251+
return "", err
252+
}
253+
254+
return createJWTToken(serviceName, privKey)
255+
}
256+
247257
func loadPrivateKey() (*ecdsa.PrivateKey, error) {
248258
file, err := os.Open("pdpservice.json")
249259
if err != nil {
@@ -401,7 +411,9 @@ func uploadOnePiece(client *http.Client, serviceURL string, reqBody []byte, jwtT
401411
if err != nil {
402412
return fmt.Errorf("failed to create request: %v", err)
403413
}
404-
req.Header.Set("Authorization", "Bearer "+jwtToken)
414+
if jwtToken != "" {
415+
req.Header.Set("Authorization", "Bearer "+jwtToken)
416+
}
405417
req.Header.Set("Content-Type", "application/json")
406418

407419
resp, err := client.Do(req)
@@ -522,15 +534,11 @@ var pieceUploadCmd = &cli.Command{
522534
if serviceName == "" {
523535
return fmt.Errorf("either --jwt-token or --service-name must be provided")
524536
}
525-
privKey, err := loadPrivateKey()
537+
var err error
538+
jwtToken, err = getJWTTokenForService(serviceName)
526539
if err != nil {
527540
return err
528541
}
529-
var errCreateToken error
530-
jwtToken, errCreateToken = createJWTToken(serviceName, privKey)
531-
if errCreateToken != nil {
532-
return errCreateToken
533-
}
534542
}
535543

536544
if hashType != "sha256" && hashType != "commp" {
@@ -690,15 +698,11 @@ var uploadFileCmd = &cli.Command{
690698
if serviceName == "" {
691699
return fmt.Errorf("either --jwt-token or --service-name must be provided")
692700
}
693-
privKey, err := loadPrivateKey()
701+
var err error
702+
jwtToken, err = getJWTTokenForService(serviceName)
694703
if err != nil {
695704
return err
696705
}
697-
var errCreateToken error
698-
jwtToken, errCreateToken = createJWTToken(serviceName, privKey)
699-
if errCreateToken != nil {
700-
return errCreateToken
701-
}
702706
}
703707

704708
// Open input file
@@ -875,14 +879,8 @@ var createProofSetCmd = &cli.Command{
875879
return err
876880
}
877881

878-
// Load the private key
879-
privKey, err := loadPrivateKey()
880-
if err != nil {
881-
return fmt.Errorf("failed to load private key: %v", err)
882-
}
883-
884882
// Create the JWT token
885-
jwtToken, err := createJWTToken(serviceName, privKey)
883+
jwtToken, err := getJWTTokenForService(serviceName)
886884
if err != nil {
887885
return fmt.Errorf("failed to create JWT token: %v", err)
888886
}
@@ -908,7 +906,9 @@ var createProofSetCmd = &cli.Command{
908906
if err != nil {
909907
return fmt.Errorf("failed to create request: %v", err)
910908
}
911-
req.Header.Set("Authorization", "Bearer "+jwtToken)
909+
if jwtToken != "" {
910+
req.Header.Set("Authorization", "Bearer "+jwtToken)
911+
}
912912
req.Header.Set("Content-Type", "application/json")
913913

914914
// Send the request
@@ -964,14 +964,8 @@ var getProofSetStatusCmd = &cli.Command{
964964
serviceName := cctx.String("service-name")
965965
txHash := cctx.String("tx-hash")
966966

967-
// Load the private key
968-
privKey, err := loadPrivateKey()
969-
if err != nil {
970-
return fmt.Errorf("failed to load private key: %v", err)
971-
}
972-
973967
// Create the JWT token
974-
jwtToken, err := createJWTToken(serviceName, privKey)
968+
jwtToken, err := getJWTTokenForService(serviceName)
975969
if err != nil {
976970
return fmt.Errorf("failed to create JWT token: %v", err)
977971
}
@@ -990,7 +984,9 @@ var getProofSetStatusCmd = &cli.Command{
990984
if err != nil {
991985
return fmt.Errorf("failed to create request: %v", err)
992986
}
993-
req.Header.Set("Authorization", "Bearer "+jwtToken)
987+
if jwtToken != "" {
988+
req.Header.Set("Authorization", "Bearer "+jwtToken)
989+
}
994990

995991
// Send the request
996992
client := &http.Client{}
@@ -1074,14 +1070,8 @@ var getProofSetCmd = &cli.Command{
10741070
serviceURL := cctx.String("service-url")
10751071
serviceName := cctx.String("service-name")
10761072

1077-
// Load the private key
1078-
privKey, err := loadPrivateKey()
1079-
if err != nil {
1080-
return fmt.Errorf("failed to load private key: %v", err)
1081-
}
1082-
10831073
// Create the JWT token
1084-
jwtToken, err := createJWTToken(serviceName, privKey)
1074+
jwtToken, err := getJWTTokenForService(serviceName)
10851075
if err != nil {
10861076
return fmt.Errorf("failed to create JWT token: %v", err)
10871077
}
@@ -1094,7 +1084,9 @@ var getProofSetCmd = &cli.Command{
10941084
if err != nil {
10951085
return fmt.Errorf("failed to create request: %v", err)
10961086
}
1097-
req.Header.Set("Authorization", "Bearer "+jwtToken)
1087+
if jwtToken != "" {
1088+
req.Header.Set("Authorization", "Bearer "+jwtToken)
1089+
}
10981090

10991091
// Send the request
11001092
client := &http.Client{}
@@ -1188,14 +1180,8 @@ var addRootsCmd = &cli.Command{
11881180
return err
11891181
}
11901182

1191-
// Load the private key
1192-
privKey, err := loadPrivateKey()
1193-
if err != nil {
1194-
return fmt.Errorf("failed to load private key: %v", err)
1195-
}
1196-
11971183
// Create the JWT token
1198-
jwtToken, err := createJWTToken(serviceName, privKey)
1184+
jwtToken, err := getJWTTokenForService(serviceName)
11991185
if err != nil {
12001186
return fmt.Errorf("failed to create JWT token: %v", err)
12011187
}
@@ -1264,7 +1250,9 @@ var addRootsCmd = &cli.Command{
12641250
if err != nil {
12651251
return fmt.Errorf("failed to create request: %v", err)
12661252
}
1267-
req.Header.Set("Authorization", "Bearer "+jwtToken)
1253+
if jwtToken != "" {
1254+
req.Header.Set("Authorization", "Bearer "+jwtToken)
1255+
}
12681256
req.Header.Set("Content-Type", "application/json")
12691257

12701258
// Send the request
@@ -1426,14 +1414,8 @@ var removeRootsCmd = &cli.Command{
14261414
proofSetID := cctx.Uint64("proof-set-id")
14271415
rootID := cctx.Uint64("root-id")
14281416

1429-
// Load the private key (implement `loadPrivateKey` according to your setup)
1430-
privKey, err := loadPrivateKey()
1431-
if err != nil {
1432-
return fmt.Errorf("failed to load private key: %v", err)
1433-
}
1434-
1435-
// Create the JWT token (implement `createJWTToken` according to your setup)
1436-
jwtToken, err := createJWTToken(serviceName, privKey)
1417+
// Create the JWT token
1418+
jwtToken, err := getJWTTokenForService(serviceName)
14371419
if err != nil {
14381420
return fmt.Errorf("failed to create JWT token: %v", err)
14391421
}
@@ -1447,7 +1429,9 @@ var removeRootsCmd = &cli.Command{
14471429
if err != nil {
14481430
return fmt.Errorf("failed to create request: %v", err)
14491431
}
1450-
req.Header.Set("Authorization", "Bearer "+jwtToken)
1432+
if jwtToken != "" {
1433+
req.Header.Set("Authorization", "Bearer "+jwtToken)
1434+
}
14511435
req.Header.Set("Content-Type", "application/json")
14521436

14531437
// Send the request

0 commit comments

Comments
 (0)