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

     1  /*
     2  Copyright 2021 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  )
    24  
    25  var certExtensionTypeName = map[CertExtensionType]string{
    26  	CertExtensionType_SSH: "ssh",
    27  }
    28  
    29  var certExtensionTypeValue = map[string]CertExtensionType{
    30  	"ssh": CertExtensionType_SSH,
    31  }
    32  
    33  func (t CertExtensionType) MarshalJSON() ([]byte, error) {
    34  	name, ok := certExtensionTypeName[t]
    35  	if !ok {
    36  		return nil, trace.Errorf("invalid certificate extension type: %q", t)
    37  	}
    38  	return json.Marshal(name)
    39  }
    40  
    41  func (t *CertExtensionType) UnmarshalJSON(b []byte) error {
    42  	var anyVal any
    43  	if err := json.Unmarshal(b, &anyVal); err != nil {
    44  		return err
    45  	}
    46  
    47  	switch val := anyVal.(type) {
    48  	case string:
    49  		enumVal, ok := certExtensionTypeValue[val]
    50  		if !ok {
    51  			return trace.Errorf("invalid certificate extension type: %q", string(b))
    52  		}
    53  		*t = enumVal
    54  		return nil
    55  	case int32:
    56  		return t.setFromEnum(val)
    57  	case int:
    58  		return t.setFromEnum(int32(val))
    59  	case int64:
    60  		return t.setFromEnum(int32(val))
    61  	case float64:
    62  		return trace.Wrap(t.setFromEnum(int32(val)))
    63  	case float32:
    64  		return trace.Wrap(t.setFromEnum(int32(val)))
    65  	default:
    66  		return trace.BadParameter("unexpected type %T", val)
    67  	}
    68  }
    69  
    70  // setFromEnum sets the value from enum value as int32.
    71  func (t *CertExtensionType) setFromEnum(val int32) error {
    72  	if _, ok := CertExtensionType_name[val]; !ok {
    73  		return trace.BadParameter("invalid cert extension mode %v", val)
    74  	}
    75  	*t = CertExtensionType(val)
    76  	return nil
    77  }
    78  
    79  var certExtensionModeName = map[CertExtensionMode]string{
    80  	CertExtensionMode_EXTENSION: "extension",
    81  }
    82  
    83  var certExtensionModeValue = map[string]CertExtensionMode{
    84  	"extension": CertExtensionMode_EXTENSION,
    85  }
    86  
    87  func (t CertExtensionMode) MarshalJSON() ([]byte, error) {
    88  	name, ok := certExtensionModeName[t]
    89  	if !ok {
    90  		return nil, trace.Errorf("invalid certificate extension mode: %q", t)
    91  	}
    92  	return json.Marshal(name)
    93  }
    94  
    95  func (t *CertExtensionMode) UnmarshalJSON(b []byte) error {
    96  	var anyVal any
    97  	if err := json.Unmarshal(b, &anyVal); err != nil {
    98  		return err
    99  	}
   100  	switch val := anyVal.(type) {
   101  	case string:
   102  		enumVal, ok := certExtensionModeValue[val]
   103  		if !ok {
   104  			return trace.Errorf("invalid certificate extension mode: %q", string(b))
   105  		}
   106  		*t = enumVal
   107  		return nil
   108  	case int32:
   109  		return t.setFromEnum(val)
   110  	case int:
   111  		return t.setFromEnum(int32(val))
   112  	case int64:
   113  		return t.setFromEnum(int32(val))
   114  	case float64:
   115  		return trace.Wrap(t.setFromEnum(int32(val)))
   116  	case float32:
   117  		return trace.Wrap(t.setFromEnum(int32(val)))
   118  	default:
   119  		return trace.BadParameter("unexpected type %T", val)
   120  	}
   121  }
   122  
   123  // setFromEnum sets the value from enum value as int32.
   124  func (t *CertExtensionMode) setFromEnum(val int32) error {
   125  	if _, ok := CertExtensionMode_name[val]; !ok {
   126  		return trace.BadParameter("invalid cert extension mode %v", val)
   127  	}
   128  	*t = CertExtensionMode(val)
   129  	return nil
   130  }