github.com/nats-io/jwt/v2@v2.5.6/decoder_activation.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 package jwt 16 17 import ( 18 "encoding/json" 19 "fmt" 20 ) 21 22 // Migration adds GenericFields 23 type v1NatsActivation struct { 24 ImportSubject Subject `json:"subject,omitempty"` 25 ImportType ExportType `json:"type,omitempty"` 26 // Limit values deprecated inv v2 27 Max int64 `json:"max,omitempty"` 28 Payload int64 `json:"payload,omitempty"` 29 Src string `json:"src,omitempty"` 30 Times []TimeRange `json:"times,omitempty"` 31 } 32 33 type v1ActivationClaims struct { 34 ClaimsData 35 v1ClaimsDataDeletedFields 36 v1NatsActivation `json:"nats,omitempty"` 37 } 38 39 func loadActivation(data []byte, version int) (*ActivationClaims, error) { 40 switch version { 41 case 1: 42 var v1a v1ActivationClaims 43 v1a.Max = NoLimit 44 v1a.Payload = NoLimit 45 if err := json.Unmarshal(data, &v1a); err != nil { 46 return nil, err 47 } 48 return v1a.Migrate() 49 case 2: 50 var v2a ActivationClaims 51 if err := json.Unmarshal(data, &v2a); err != nil { 52 return nil, err 53 } 54 return &v2a, nil 55 default: 56 return nil, fmt.Errorf("library supports version %d or less - received %d", libVersion, version) 57 } 58 } 59 60 func (oa v1ActivationClaims) Migrate() (*ActivationClaims, error) { 61 return oa.migrateV1() 62 } 63 64 func (oa v1ActivationClaims) migrateV1() (*ActivationClaims, error) { 65 var a ActivationClaims 66 // copy the base claim 67 a.ClaimsData = oa.ClaimsData 68 // move the moved fields 69 a.Activation.Type = oa.v1ClaimsDataDeletedFields.Type 70 a.Activation.Tags = oa.v1ClaimsDataDeletedFields.Tags 71 a.Activation.IssuerAccount = oa.v1ClaimsDataDeletedFields.IssuerAccount 72 // copy the activation data 73 a.ImportSubject = oa.ImportSubject 74 a.ImportType = oa.ImportType 75 a.Version = 1 76 return &a, nil 77 }