decoders.go (3892B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package trans 19 20 import ( 21 "crypto/x509" 22 "encoding/pem" 23 "errors" 24 "fmt" 25 "time" 26 27 "github.com/mitchellh/mapstructure" 28 transmodel "github.com/superseriousbusiness/gotosocial/internal/trans/model" 29 ) 30 31 func newDecoder(target interface{}) (*mapstructure.Decoder, error) { 32 decoderConfig := &mapstructure.DecoderConfig{ 33 DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339), // this is needed to decode time.Time entries serialized as string 34 Result: target, 35 } 36 return mapstructure.NewDecoder(decoderConfig) 37 } 38 39 func (i *importer) accountDecode(e transmodel.Entry) (*transmodel.Account, error) { 40 a := &transmodel.Account{} 41 if err := i.simpleDecode(e, a); err != nil { 42 return nil, err 43 } 44 45 // extract public key 46 publicKeyBlock, _ := pem.Decode([]byte(a.PublicKeyString)) 47 if publicKeyBlock == nil { 48 return nil, errors.New("accountDecode: error decoding account public key") 49 } 50 publicKey, err := x509.ParsePKCS1PublicKey(publicKeyBlock.Bytes) 51 if err != nil { 52 return nil, fmt.Errorf("accountDecode: error parsing account public key: %s", err) 53 } 54 a.PublicKey = publicKey 55 56 if a.Domain == "" { 57 // extract private key (local account) 58 privateKeyBlock, _ := pem.Decode([]byte(a.PrivateKeyString)) 59 if privateKeyBlock == nil { 60 return nil, errors.New("accountDecode: error decoding account private key") 61 } 62 privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes) 63 if err != nil { 64 return nil, fmt.Errorf("accountDecode: error parsing account private key: %s", err) 65 } 66 a.PrivateKey = privateKey 67 } 68 69 return a, nil 70 } 71 72 func (i *importer) blockDecode(e transmodel.Entry) (*transmodel.Block, error) { 73 b := &transmodel.Block{} 74 if err := i.simpleDecode(e, b); err != nil { 75 return nil, err 76 } 77 78 return b, nil 79 } 80 81 func (i *importer) domainBlockDecode(e transmodel.Entry) (*transmodel.DomainBlock, error) { 82 b := &transmodel.DomainBlock{} 83 if err := i.simpleDecode(e, b); err != nil { 84 return nil, err 85 } 86 87 return b, nil 88 } 89 90 func (i *importer) followDecode(e transmodel.Entry) (*transmodel.Follow, error) { 91 f := &transmodel.Follow{} 92 if err := i.simpleDecode(e, f); err != nil { 93 return nil, err 94 } 95 96 return f, nil 97 } 98 99 func (i *importer) followRequestDecode(e transmodel.Entry) (*transmodel.FollowRequest, error) { 100 f := &transmodel.FollowRequest{} 101 if err := i.simpleDecode(e, f); err != nil { 102 return nil, err 103 } 104 105 return f, nil 106 } 107 108 func (i *importer) instanceDecode(e transmodel.Entry) (*transmodel.Instance, error) { 109 inst := &transmodel.Instance{} 110 if err := i.simpleDecode(e, inst); err != nil { 111 return nil, err 112 } 113 114 return inst, nil 115 } 116 117 func (i *importer) userDecode(e transmodel.Entry) (*transmodel.User, error) { 118 u := &transmodel.User{} 119 if err := i.simpleDecode(e, u); err != nil { 120 return nil, err 121 } 122 123 return u, nil 124 } 125 126 func (i *importer) simpleDecode(entry transmodel.Entry, target interface{}) error { 127 decoder, err := newDecoder(target) 128 if err != nil { 129 return fmt.Errorf("simpleDecode: error creating decoder: %s", err) 130 } 131 132 if err := decoder.Decode(&entry); err != nil { 133 return fmt.Errorf("simpleDecode: error decoding: %s", err) 134 } 135 136 return nil 137 }