github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/assertion_info.go (about)

     1  /*
     2  Copyright 2022 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"github.com/gravitational/trace"
    23  	saml2 "github.com/russellhaering/gosaml2"
    24  )
    25  
    26  // AssertionInfo is an alias for saml2.AssertionInfo with additional methods, required for serialization to/from protobuf.
    27  // With those we can reference it with an option like so: `(gogoproto.customtype) = "AssertionInfo"`
    28  type AssertionInfo saml2.AssertionInfo
    29  
    30  func (a *AssertionInfo) Size() int {
    31  	bytes, err := json.Marshal(a)
    32  	if err != nil {
    33  		return 0
    34  	}
    35  	return len(bytes)
    36  }
    37  
    38  func (a *AssertionInfo) Unmarshal(bytes []byte) error {
    39  	return trace.Wrap(json.Unmarshal(bytes, a))
    40  }
    41  
    42  func (a *AssertionInfo) MarshalTo(bytes []byte) (int, error) {
    43  	out, err := json.Marshal(a)
    44  	if err != nil {
    45  		return 0, trace.Wrap(err)
    46  	}
    47  
    48  	if len(out) > cap(bytes) {
    49  		return 0, trace.BadParameter("capacity too low: %v, need %v", cap(bytes), len(out))
    50  	}
    51  
    52  	copy(bytes, out)
    53  
    54  	return len(out), nil
    55  }