github.com/nats-io/jwt/v2@v2.5.6/v1compat/server_claims.go (about)

     1  /*
     2   * Copyright 2018-2020 The NATS Authors
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package jwt
    17  
    18  import (
    19  	"errors"
    20  
    21  	"github.com/nats-io/nkeys"
    22  )
    23  
    24  // Deprecated: ServerClaims are not supported
    25  type Server struct {
    26  	Permissions
    27  	Cluster string `json:"cluster,omitempty"`
    28  }
    29  
    30  // Validate checks the cluster and permissions for a server JWT
    31  func (s *Server) Validate(vr *ValidationResults) {
    32  	if s.Cluster == "" {
    33  		vr.AddError("servers can't contain an empty cluster")
    34  	}
    35  }
    36  
    37  // Deprecated: ServerClaims are not supported
    38  type ServerClaims struct {
    39  	ClaimsData
    40  	Server `json:"nats,omitempty"`
    41  }
    42  
    43  // Deprecated: ServerClaims are not supported
    44  func NewServerClaims(subject string) *ServerClaims {
    45  	if subject == "" {
    46  		return nil
    47  	}
    48  	c := &ServerClaims{}
    49  	c.Subject = subject
    50  	return c
    51  }
    52  
    53  // Encode tries to turn the server claims into a JWT string
    54  func (s *ServerClaims) Encode(pair nkeys.KeyPair) (string, error) {
    55  	if !nkeys.IsValidPublicServerKey(s.Subject) {
    56  		return "", errors.New("expected subject to be a server public key")
    57  	}
    58  	s.ClaimsData.Type = ServerClaim
    59  	return s.ClaimsData.Encode(pair, s)
    60  }
    61  
    62  // Deprecated: ServerClaims are not supported
    63  func DecodeServerClaims(token string) (*ServerClaims, error) {
    64  	v := ServerClaims{}
    65  	if err := Decode(token, &v); err != nil {
    66  		return nil, err
    67  	}
    68  	return &v, nil
    69  }
    70  
    71  func (s *ServerClaims) String() string {
    72  	return s.ClaimsData.String(s)
    73  }
    74  
    75  // Payload returns the server specific data
    76  func (s *ServerClaims) Payload() interface{} {
    77  	return &s.Server
    78  }
    79  
    80  // Validate checks the generic and server data in the server claims
    81  func (s *ServerClaims) Validate(vr *ValidationResults) {
    82  	s.ClaimsData.Validate(vr)
    83  	s.Server.Validate(vr)
    84  }
    85  
    86  // ExpectedPrefixes defines the types that can encode a server JWT, operator or cluster
    87  func (s *ServerClaims) ExpectedPrefixes() []nkeys.PrefixByte {
    88  	return []nkeys.PrefixByte{nkeys.PrefixByteOperator, nkeys.PrefixByteCluster}
    89  }
    90  
    91  // Claims returns the generic data
    92  func (s *ServerClaims) Claims() *ClaimsData {
    93  	return &s.ClaimsData
    94  }