github.com/pion/webrtc/v4@v4.0.1/icecredentialtype.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webrtc
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  )
    10  
    11  // ICECredentialType indicates the type of credentials used to connect to
    12  // an ICE server.
    13  type ICECredentialType int
    14  
    15  const (
    16  	// ICECredentialTypePassword describes username and password based
    17  	// credentials as described in https://tools.ietf.org/html/rfc5389.
    18  	ICECredentialTypePassword ICECredentialType = iota
    19  
    20  	// ICECredentialTypeOauth describes token based credential as described
    21  	// in https://tools.ietf.org/html/rfc7635.
    22  	ICECredentialTypeOauth
    23  )
    24  
    25  // This is done this way because of a linter.
    26  const (
    27  	iceCredentialTypePasswordStr = "password"
    28  	iceCredentialTypeOauthStr    = "oauth"
    29  )
    30  
    31  func newICECredentialType(raw string) (ICECredentialType, error) {
    32  	switch raw {
    33  	case iceCredentialTypePasswordStr:
    34  		return ICECredentialTypePassword, nil
    35  	case iceCredentialTypeOauthStr:
    36  		return ICECredentialTypeOauth, nil
    37  	default:
    38  		return ICECredentialTypePassword, errInvalidICECredentialTypeString
    39  	}
    40  }
    41  
    42  func (t ICECredentialType) String() string {
    43  	switch t {
    44  	case ICECredentialTypePassword:
    45  		return iceCredentialTypePasswordStr
    46  	case ICECredentialTypeOauth:
    47  		return iceCredentialTypeOauthStr
    48  	default:
    49  		return ErrUnknownType.Error()
    50  	}
    51  }
    52  
    53  // UnmarshalJSON parses the JSON-encoded data and stores the result
    54  func (t *ICECredentialType) UnmarshalJSON(b []byte) error {
    55  	var val string
    56  	if err := json.Unmarshal(b, &val); err != nil {
    57  		return err
    58  	}
    59  
    60  	tmp, err := newICECredentialType(val)
    61  	if err != nil {
    62  		return fmt.Errorf("%w: (%s)", err, val)
    63  	}
    64  
    65  	*t = tmp
    66  	return nil
    67  }
    68  
    69  // MarshalJSON returns the JSON encoding
    70  func (t ICECredentialType) MarshalJSON() ([]byte, error) {
    71  	return json.Marshal(t.String())
    72  }