@@ -340,6 +340,60 @@ func (r *TokenRequest) FromProtos(tr *request.TokenRequest) error {
340340 r .Signatures = append (r .Signatures , requestSignature )
341341 }
342342
343+ // Validate the structural integrity of the token request
344+ if err := r .Validate (); err != nil {
345+ return errors .Wrap (err , "token request validation failed" )
346+ }
347+
348+ return nil
349+ }
350+
351+ // Validate checks the structural validity of the TokenRequest.
352+ // It ensures that all required fields are present and non-empty.
353+ // This method should be called after unmarshalling to verify the request is well-formed.
354+ //
355+ // Special case: Empty requests (nil Actions or empty Actions slice) are allowed
356+ // to support test scenarios where requests are created but not yet populated.
357+ // This is necessary because ToProtos() sets Version=ProtocolV1 even for empty requests.
358+ //
359+ // Returns an error if:
360+ // - Any action in Actions is nil
361+ // - Any action has empty Raw bytes
362+ // - Any signature in Signatures is nil
363+ // - Any signature has empty signature bytes
364+ func (r * TokenRequest ) Validate () error {
365+ // If we have actions, validate them
366+
367+ for i , action := range r .Actions {
368+ if action == nil {
369+ return errors .Errorf ("action at index %d is nil" , i )
370+ }
371+ if len (action .Raw ) == 0 {
372+ return errors .Errorf ("action at index %d has empty Raw bytes" , i )
373+ }
374+ }
375+
376+ // Validate signatures (if present)
377+ for i , sig := range r .Signatures {
378+ if sig == nil {
379+ return errors .Errorf ("signature at index %d is nil" , i )
380+ }
381+
382+ // Check that signature has either Action or Auditor signature with non-empty bytes
383+ switch {
384+ case sig .Action != nil :
385+ if len (sig .Action .Signature ) == 0 {
386+ return errors .Errorf ("action signature at index %d has empty signature bytes" , i )
387+ }
388+ case sig .Auditor != nil :
389+ if len (sig .Auditor .Signature ) == 0 {
390+ return errors .Errorf ("auditor signature at index %d has empty signature bytes" , i )
391+ }
392+ default :
393+ return errors .Errorf ("signature at index %d has neither action nor auditor signature" , i )
394+ }
395+ }
396+
343397 return nil
344398}
345399
0 commit comments