github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/oidc_external.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  	"time"
    22  
    23  	"github.com/gravitational/trace"
    24  )
    25  
    26  // OIDCClaims is a redefinition of jose.Claims with additional methods, required for serialization to/from protobuf.
    27  // With those we can reference it with an option like so: `(gogoproto.customtype) = "OIDCClaims"`
    28  type OIDCClaims map[string]interface{}
    29  
    30  // Size returns size of the object when marshaled
    31  func (a *OIDCClaims) Size() int {
    32  	bytes, err := json.Marshal(a)
    33  	if err != nil {
    34  		return 0
    35  	}
    36  	return len(bytes)
    37  }
    38  
    39  // Unmarshal the object from provided buffer.
    40  func (a *OIDCClaims) Unmarshal(bytes []byte) error {
    41  	return trace.Wrap(json.Unmarshal(bytes, a))
    42  }
    43  
    44  // MarshalTo marshals the object to sized buffer
    45  func (a *OIDCClaims) MarshalTo(bytes []byte) (int, error) {
    46  	out, err := json.Marshal(a)
    47  	if err != nil {
    48  		return 0, trace.Wrap(err)
    49  	}
    50  
    51  	if len(out) > cap(bytes) {
    52  		return 0, trace.BadParameter("capacity too low: %v, need %v", cap(bytes), len(out))
    53  	}
    54  
    55  	copy(bytes, out)
    56  
    57  	return len(out), nil
    58  }
    59  
    60  // OIDCIdentity is a redefinition of oidc.Identity with additional methods, required for serialization to/from protobuf.
    61  // With those we can reference it with an option like so: `(gogoproto.customtype) = "OIDCIdentity"`
    62  type OIDCIdentity struct {
    63  	// ID is populated from "subject" claim.
    64  	ID string
    65  	// Name of user. Empty in current version of library.
    66  	Name string
    67  	// Email is populated from "email" claim.
    68  	Email string
    69  	// ExpiresAt populated from "exp" claim, represents expiry time.
    70  	ExpiresAt time.Time
    71  }
    72  
    73  // Size returns size of the object when marshaled
    74  func (a *OIDCIdentity) Size() int {
    75  	bytes, err := json.Marshal(a)
    76  	if err != nil {
    77  		return 0
    78  	}
    79  	return len(bytes)
    80  }
    81  
    82  // Unmarshal the object from provided buffer.
    83  func (a *OIDCIdentity) Unmarshal(bytes []byte) error {
    84  	return trace.Wrap(json.Unmarshal(bytes, a))
    85  }
    86  
    87  // MarshalTo marshals the object to sized buffer
    88  func (a *OIDCIdentity) MarshalTo(bytes []byte) (int, error) {
    89  	out, err := json.Marshal(a)
    90  	if err != nil {
    91  		return 0, trace.Wrap(err)
    92  	}
    93  
    94  	if len(out) > cap(bytes) {
    95  		return 0, trace.BadParameter("capacity too low: %v, need %v", cap(bytes), len(out))
    96  	}
    97  
    98  	copy(bytes, out)
    99  
   100  	return len(out), nil
   101  }