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

     1  /*
     2   * Copyright 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  	"encoding/json"
    20  	"fmt"
    21  )
    22  
    23  type v1NatsAccount struct {
    24  	Imports Imports `json:"imports,omitempty"`
    25  	Exports Exports `json:"exports,omitempty"`
    26  	Limits  struct {
    27  		NatsLimits
    28  		AccountLimits
    29  	} `json:"limits,omitempty"`
    30  	SigningKeys StringList     `json:"signing_keys,omitempty"`
    31  	Revocations RevocationList `json:"revocations,omitempty"`
    32  }
    33  
    34  func loadAccount(data []byte, version int) (*AccountClaims, error) {
    35  	switch version {
    36  	case 1:
    37  		var v1a v1AccountClaims
    38  		if err := json.Unmarshal(data, &v1a); err != nil {
    39  			return nil, err
    40  		}
    41  		return v1a.Migrate()
    42  	case 2:
    43  		var v2a AccountClaims
    44  		v2a.SigningKeys = make(SigningKeys)
    45  		if err := json.Unmarshal(data, &v2a); err != nil {
    46  			return nil, err
    47  		}
    48  		if len(v2a.Limits.JetStreamTieredLimits) > 0 {
    49  			v2a.Limits.JetStreamLimits = JetStreamLimits{}
    50  		}
    51  		return &v2a, nil
    52  	default:
    53  		return nil, fmt.Errorf("library supports version %d or less - received %d", libVersion, version)
    54  	}
    55  }
    56  
    57  type v1AccountClaims struct {
    58  	ClaimsData
    59  	v1ClaimsDataDeletedFields
    60  	v1NatsAccount `json:"nats,omitempty"`
    61  }
    62  
    63  func (oa v1AccountClaims) Migrate() (*AccountClaims, error) {
    64  	return oa.migrateV1()
    65  }
    66  
    67  func (oa v1AccountClaims) migrateV1() (*AccountClaims, error) {
    68  	var a AccountClaims
    69  	// copy the base claim
    70  	a.ClaimsData = oa.ClaimsData
    71  	// move the moved fields
    72  	a.Account.Type = oa.v1ClaimsDataDeletedFields.Type
    73  	a.Account.Tags = oa.v1ClaimsDataDeletedFields.Tags
    74  	// copy the account data
    75  	a.Account.Imports = oa.v1NatsAccount.Imports
    76  	a.Account.Exports = oa.v1NatsAccount.Exports
    77  	a.Account.Limits.AccountLimits = oa.v1NatsAccount.Limits.AccountLimits
    78  	a.Account.Limits.NatsLimits = oa.v1NatsAccount.Limits.NatsLimits
    79  	a.Account.Limits.JetStreamLimits = JetStreamLimits{}
    80  	a.Account.SigningKeys = make(SigningKeys)
    81  	for _, v := range oa.SigningKeys {
    82  		a.Account.SigningKeys.Add(v)
    83  	}
    84  	a.Account.Revocations = oa.v1NatsAccount.Revocations
    85  	a.Version = 1
    86  	return &a, nil
    87  }