github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/verifier.go (about) 1 /* 2 * Copyright (C) 2017 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package identity 19 20 // VerifierFactory callback returning Verifier 21 type VerifierFactory func(id Identity) Verifier 22 23 // Verifier checks message's sanity 24 type Verifier interface { 25 Verify(message []byte, signature Signature) (bool, Identity) 26 } 27 28 // NewVerifierSigned constructs Verifier which: 29 // - checks signature's sanity 30 // - checks if message was unchanged by middleman 31 func NewVerifierSigned() *verifierSigned { 32 return &verifierSigned{NewExtractor()} 33 } 34 35 // NewVerifierIdentity constructs Verifier which: 36 // - checks signature's sanity 37 // - checks if message was unchanged by middleman 38 // - checks if message is from exact identity 39 func NewVerifierIdentity(peerID Identity) *verifierIdentity { 40 return &verifierIdentity{NewExtractor(), peerID} 41 } 42 43 type verifierSigned struct { 44 extractor Extractor 45 } 46 47 func (verifier *verifierSigned) Verify(message []byte, signature Signature) (bool, Identity) { 48 identity, err := verifier.extractor.Extract(message, signature) 49 return err == nil, identity 50 } 51 52 type verifierIdentity struct { 53 extractor Extractor 54 peerID Identity 55 } 56 57 func (verifier *verifierIdentity) Verify(message []byte, signature Signature) (bool, Identity) { 58 identity, err := verifier.extractor.Extract(message, signature) 59 if err != nil { 60 return false, identity 61 } 62 63 return identity == verifier.peerID, identity 64 }