github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/api/user_service.go (about) 1 /* 2 * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved. 3 * This software is released under GPL3. 4 * The full license information can be found under: 5 * https://www.gnu.org/licenses/gpl-3.0.en.html 6 * 7 */ 8 9 package api 10 11 import ( 12 "crypto/ecdsa" 13 "crypto/sha256" 14 "encoding/base64" 15 sdk "github.com/vchain-us/ledger-compliance-go/grpcclient" 16 "github.com/vchain-us/vcn/pkg/meta" 17 "github.com/vchain-us/vcn/pkg/store" 18 "strconv" 19 "strings" 20 ) 21 22 // User represent a CodeNotary platform user. 23 type LcUser struct { 24 Client *sdk.LcClient 25 } 26 27 // NewLcUser returns a new User instance configured with provided parameters. 28 // LcLedger parameter is used when a cross-ledger key is provided in order to specify the ledger on which future operations will be directed. Empty string is accepted 29 func NewLcUser(lcApiKey, lcLedger, host, port, lcCert string, skipTlsVerify bool, noTls bool, signingPubKey *ecdsa.PublicKey) (*LcUser, error) { 30 31 client, err := NewLcClient(lcApiKey, lcLedger, host, port, lcCert, skipTlsVerify, noTls, signingPubKey) 32 if err != nil { 33 return nil, err 34 } 35 store.Config().NewLcUser(host, port, lcCert, skipTlsVerify, noTls) 36 37 return &LcUser{ 38 Client: client, 39 }, nil 40 } 41 42 // NewLcUserVolatile returns a new User instance without a backing cfg file. 43 func NewLcUserVolatile(lcApiKey, lcLedger string, host string, port string) *LcUser { 44 p, _ := strconv.Atoi(port) 45 return &LcUser{ 46 Client: sdk.NewLcClient( 47 sdk.ApiKey(lcApiKey), 48 sdk.MetadataPairs([]string{ 49 meta.VcnLCLedgerHeaderName, lcLedger, 50 meta.VcnLCVersionHeaderName, meta.Version(), 51 }), 52 sdk.Host(host), 53 sdk.Port(p), 54 sdk.Dir(store.CurrentConfigFilePath())), 55 } 56 } 57 58 // Config returns the User configuration object (see store.User), if any. 59 // It returns nil if the User is not properly initialized. 60 func (u User) User() *store.User { 61 if u.cfg != nil { 62 return u.cfg 63 } 64 return nil 65 } 66 67 // GetUserFromContext returns a new the correct user based on the context 68 func GetUserFromContext(context store.CurrentContext, lcApiKey string, lcLedger string, signingPubKey *ecdsa.PublicKey) (interface{}, error) { 69 if context.Email != "" { 70 return &User{ 71 cfg: store.Config().UserByMail(context.Email), 72 }, nil 73 } 74 if context.LcHost != "" { 75 76 client, err := NewLcClientByContext(context, lcApiKey, lcLedger, signingPubKey) 77 if err != nil { 78 return nil, err 79 } 80 return &LcUser{ 81 Client: client, 82 }, nil 83 } 84 return nil, nil 85 } 86 87 func GetSignerIDByApiKey(lcApiKey string) string { 88 ris := strings.Split(lcApiKey, ".") 89 // new apikey format {friendlySignerID}.{secret} 90 if len(ris) > 1 { 91 return strings.Join(ris[:len(ris)-1], ".") 92 } 93 // old apikey format {secret} 94 hash := sha256.Sum256([]byte(lcApiKey)) 95 return base64.URLEncoding.EncodeToString(hash[:]) 96 }