Commit 8fa2ee5b authored by Loïck Bonniot's avatar Loïck Bonniot

[p][c] Use bytes instead of strings for hashes

parent a163a1cb
Pipeline #319 failed with stage
...@@ -2,7 +2,6 @@ package sign ...@@ -2,7 +2,6 @@ package sign
import ( import (
"crypto/sha512" "crypto/sha512"
"fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"time" "time"
...@@ -26,7 +25,7 @@ type CreateManager struct { ...@@ -26,7 +25,7 @@ type CreateManager struct {
filepath string filepath string
comment string comment string
signers []string signers []string
hash string hash []byte
filename string filename string
} }
...@@ -64,7 +63,7 @@ func (m *CreateManager) computeFile() error { ...@@ -64,7 +63,7 @@ func (m *CreateManager) computeFile() error {
} }
hash := sha512.Sum512(data) hash := sha512.Sum512(data)
m.hash = fmt.Sprintf("%x", hash) m.hash = hash[:]
m.filename = filepath.Base(m.filepath) m.filename = filepath.Base(m.filepath)
return nil return nil
......
package sign package sign
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
...@@ -55,7 +56,7 @@ func TestComputeFile(t *testing.T) { ...@@ -55,7 +56,7 @@ func TestComputeFile(t *testing.T) {
m := &CreateManager{filepath: fcontract} m := &CreateManager{filepath: fcontract}
err := m.computeFile() err := m.computeFile()
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
assert.Equal(t, "37fd29decfb2d689439478b1f64b60441534c1e373a7023676c94ac6772639edab46f80139d167a2741f159e62b3064eca58bb331d32cd10770f29064af2a9de", m.hash) assert.Equal(t, "37fd29decfb2d689439478b1f64b60441534c1e373a7023676c94ac6772639edab46f80139d167a2741f159e62b3064eca58bb331d32cd10770f29064af2a9de", fmt.Sprintf("%x", m.hash))
assert.Equal(t, "contract.txt", m.filename) assert.Equal(t, "contract.txt", m.filename)
} }
...@@ -132,7 +132,7 @@ func (*Empty) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } ...@@ -132,7 +132,7 @@ func (*Empty) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} }
// PostContractRequest message contains the contract as SHA-512 hash, its filename, // PostContractRequest message contains the contract as SHA-512 hash, its filename,
// the list of signers as an array of strings, and a comment // the list of signers as an array of strings, and a comment
type PostContractRequest struct { type PostContractRequest struct {
Hash string `protobuf:"bytes,1,opt,name=hash" json:"hash,omitempty"` Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"`
Filename string `protobuf:"bytes,2,opt,name=filename" json:"filename,omitempty"` Filename string `protobuf:"bytes,2,opt,name=filename" json:"filename,omitempty"`
Signer []string `protobuf:"bytes,3,rep,name=signer" json:"signer,omitempty"` Signer []string `protobuf:"bytes,3,rep,name=signer" json:"signer,omitempty"`
Comment string `protobuf:"bytes,4,opt,name=comment" json:"comment,omitempty"` Comment string `protobuf:"bytes,4,opt,name=comment" json:"comment,omitempty"`
...@@ -374,36 +374,36 @@ var _Platform_serviceDesc = grpc.ServiceDesc{ ...@@ -374,36 +374,36 @@ var _Platform_serviceDesc = grpc.ServiceDesc{
} }
var fileDescriptor0 = []byte{ var fileDescriptor0 = []byte{
// 489 bytes of a gzipped FileDescriptorProto // 490 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x94, 0x53, 0x5d, 0x8f, 0xd2, 0x40, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x94, 0x53, 0x5d, 0x8f, 0xd2, 0x40,
0x14, 0xdd, 0x42, 0x97, 0x8f, 0xbb, 0xbb, 0xd8, 0x0c, 0x68, 0x2a, 0x0f, 0x66, 0x33, 0x2f, 0x1a, 0x14, 0xdd, 0x42, 0x97, 0x8f, 0xbb, 0x2c, 0x36, 0x03, 0x9a, 0xca, 0x83, 0xd9, 0xcc, 0x8b, 0xc6,
0x1f, 0xd0, 0xa0, 0x31, 0xd1, 0xf8, 0x52, 0x2b, 0x59, 0xd7, 0x07, 0x42, 0xa6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x1e, 0xd6, 0x18, 0xfa, 0x53, 0xa1, 0x27, 0xe4, 0x19, 0xd8, 0xca, 0x37, 0xe2, 0xe8, 0xcc, 0x91, 0x85, 0x93, 0x51,
0x4a, 0x67, 0x8a, 0x86, 0xa7, 0x00, 0x61, 0x26, 0xcc, 0x18, 0x28, 0x09, 0x95, 0x19, 0x0d, 0xe4, 0x8d, 0xa1, 0x74, 0xa6, 0x68, 0x78, 0x0a, 0x10, 0x66, 0xc2, 0x8c, 0x81, 0x92, 0x50, 0x99, 0xd1,
0x6f, 0xe0, 0xfc, 0xd8, 0x1e, 0xe2, 0xea, 0x8a, 0x06, 0xc7, 0x1a, 0x7a, 0xdf, 0xc2, 0x45, 0x4d, 0x40, 0xfe, 0x06, 0x06, 0xc7, 0xf6, 0x10, 0x57, 0x57, 0x34, 0x38, 0xd6, 0xd0, 0xfb, 0x16, 0xce,
0x61, 0xf2, 0x50, 0x97, 0x34, 0xa9, 0xde, 0xd0, 0xfd, 0x12, 0xfa, 0x07, 0x3d, 0xc9, 0xfd, 0xea, 0x6b, 0x0a, 0x93, 0x87, 0xba, 0xa4, 0x49, 0xf5, 0x86, 0xee, 0x97, 0xd0, 0x3f, 0xe8, 0x49, 0xee,
0x26, 0x75, 0x7d, 0xff, 0xed, 0xfa, 0xd2, 0xd1, 0x7f, 0xda, 0x17, 0x7f, 0x02, 0x00, 0x00, 0xff, 0x57, 0x37, 0xa9, 0xeb, 0xfb, 0x6f, 0xd7, 0x97, 0x8e, 0xfe, 0xd3, 0xbe, 0xf8, 0x13, 0x00, 0x00,
0xff, 0x59, 0x0a, 0x30, 0x18, 0xca, 0x03, 0x00, 0x00, 0xff, 0xff, 0x35, 0x7d, 0xdf, 0xdc, 0xca, 0x03, 0x00, 0x00,
} }
...@@ -57,7 +57,7 @@ message Empty { ...@@ -57,7 +57,7 @@ message Empty {
// PostContractRequest message contains the contract as SHA-512 hash, its filename, // PostContractRequest message contains the contract as SHA-512 hash, its filename,
// the list of signers as an array of strings, and a comment // the list of signers as an array of strings, and a comment
message PostContractRequest { message PostContractRequest {
string hash = 1; bytes hash = 1;
string filename = 2; string filename = 2;
repeated string signer = 3; repeated string signer = 3;
string comment = 4; string comment = 4;
......
...@@ -56,8 +56,8 @@ func TestAddSigner(t *testing.T) { ...@@ -56,8 +56,8 @@ func TestAddSigner(t *testing.T) {
id := bson.NewObjectId() id := bson.NewObjectId()
c.AddSigner(nil, "mail1", "hash1") c.AddSigner(nil, "mail1", []byte{0xaa})
c.AddSigner(&id, "mail2", "hash2") c.AddSigner(&id, "mail2", []byte{})
signers := c.Signers signers := c.Signers
...@@ -66,10 +66,10 @@ func TestAddSigner(t *testing.T) { ...@@ -66,10 +66,10 @@ func TestAddSigner(t *testing.T) {
} }
assert.Equal(t, signers[0].Email, "mail1") assert.Equal(t, signers[0].Email, "mail1")
assert.Equal(t, signers[0].Hash, "hash1") assert.Equal(t, signers[0].Hash, []byte{0xaa})
assert.Equal(t, signers[0].UserID.Hex(), "000000000000000000000000") assert.Equal(t, signers[0].UserID.Hex(), "000000000000000000000000")
assert.Equal(t, signers[1].Email, "mail2") assert.Equal(t, signers[1].Email, "mail2")
assert.Equal(t, signers[1].Hash, "hash2") assert.Equal(t, signers[1].Hash, []byte{})
assert.Equal(t, signers[1].UserID.Hex(), id.Hex()) assert.Equal(t, signers[1].UserID.Hex(), id.Hex())
} }
...@@ -85,10 +85,10 @@ func assertContractEqual(t *testing.T, contract, fetched entities.Contract) { ...@@ -85,10 +85,10 @@ func assertContractEqual(t *testing.T, contract, fetched entities.Contract) {
func TestInsertContract(t *testing.T) { func TestInsertContract(t *testing.T) {
dropDataset() dropDataset()
c := entities.NewContract() c := entities.NewContract()
c.AddSigner(nil, "mail1", "hash1") c.AddSigner(nil, "mail1", []byte{0xaa})
c.AddSigner(nil, "mail1", "hash1") c.AddSigner(nil, "mail1", []byte{0xaa})
c.File.Name = "file" c.File.Name = "file"
c.File.Hash = "hashFile" c.File.Hash = []byte{0xff}
c.File.Hosted = false c.File.Hosted = false
c.Comment = "comment" c.Comment = "comment"
c.Ready = true c.Ready = true
...@@ -118,17 +118,17 @@ func TestGetWaitingForUser(t *testing.T) { ...@@ -118,17 +118,17 @@ func TestGetWaitingForUser(t *testing.T) {
dropDataset() dropDataset()
c1 := entities.NewContract() c1 := entities.NewContract()
c1.AddSigner(nil, "mail1", "") c1.AddSigner(nil, "mail1", []byte{})
c1.Ready = false c1.Ready = false
c2 := entities.NewContract() c2 := entities.NewContract()
c2.AddSigner(nil, "mail1", "") c2.AddSigner(nil, "mail1", []byte{})
c2.AddSigner(&knownID, "mail2", "hash") c2.AddSigner(&knownID, "mail2", []byte{0x12})
c2.Ready = false c2.Ready = false
c3 := entities.NewContract() c3 := entities.NewContract()
c3.AddSigner(nil, "mail2", "") c3.AddSigner(nil, "mail2", []byte{})
c3.AddSigner(&knownID, "mail1", "hash") c3.AddSigner(&knownID, "mail1", []byte{0xaa})
c3.Ready = false c3.Ready = false
_, _ = repository.Collection.Insert(c1) _, _ = repository.Collection.Insert(c1)
......
...@@ -70,7 +70,7 @@ func (c *Builder) checkInput() *api.ErrorCode { ...@@ -70,7 +70,7 @@ func (c *Builder) checkInput() *api.ErrorCode {
return &api.ErrorCode{Code: api.ErrorCode_INVARG, Message: "Expecting a valid filename"} return &api.ErrorCode{Code: api.ErrorCode_INVARG, Message: "Expecting a valid filename"}
} }
if len(c.in.Hash) != sha512.Size*2 { // *2 because string format doubles the length if len(c.in.Hash) != sha512.Size {
return &api.ErrorCode{Code: api.ErrorCode_INVARG, Message: "Expecting a valid sha512 hash"} return &api.ErrorCode{Code: api.ErrorCode_INVARG, Message: "Expecting a valid sha512 hash"}
} }
...@@ -117,7 +117,7 @@ func (c *Builder) addContract() error { ...@@ -117,7 +117,7 @@ func (c *Builder) addContract() error {
contract.AddSigner(&s.ID, s.Email, s.CertHash) contract.AddSigner(&s.ID, s.Email, s.CertHash)
} }
for _, s := range c.missingSigners { for _, s := range c.missingSigners {
contract.AddSigner(nil, s, "") contract.AddSigner(nil, s, nil)
} }
contract.Comment = c.in.Comment contract.Comment = c.in.Comment
......
...@@ -2,7 +2,6 @@ package contract_test ...@@ -2,7 +2,6 @@ package contract_test
import ( import (
"crypto/sha512" "crypto/sha512"
"fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"testing" "testing"
...@@ -18,7 +17,6 @@ import ( ...@@ -18,7 +17,6 @@ import (
var user1, user2, user3 *entities.User var user1, user2, user3 *entities.User
var defaultHash = sha512.Sum512([]byte{0}) var defaultHash = sha512.Sum512([]byte{0})
var defaultHashStr = fmt.Sprintf("%x", defaultHash)
func createDataset() { func createDataset() {
...@@ -29,17 +27,17 @@ func createDataset() { ...@@ -29,17 +27,17 @@ func createDataset() {
user1.Email = "user1@example.com" user1.Email = "user1@example.com"
user1.Expiration = time.Now().AddDate(1, 0, 0) user1.Expiration = time.Now().AddDate(1, 0, 0)
user1.Certificate = "Certificate1" user1.Certificate = "Certificate1"
user1.CertHash = "Hash1" user1.CertHash = []byte{0x01}
user2.Email = "user2@example.com" user2.Email = "user2@example.com"
user2.Expiration = time.Now().AddDate(1, 0, 0) user2.Expiration = time.Now().AddDate(1, 0, 0)
user2.Certificate = "Certificate2" user2.Certificate = "Certificate2"
user2.CertHash = "Hash2" user2.CertHash = []byte{0x02}
user3.Email = "user3@example.com" user3.Email = "user3@example.com"
user3.Expiration = time.Now().AddDate(0, 0, -1) user3.Expiration = time.Now().AddDate(0, 0, -1)
user3.Certificate = "Certificate3" user3.Certificate = "Certificate3"
user3.CertHash = "Hash3" user3.CertHash = []byte{0x03}
_, _ = manager.Get("users").Insert(user1) _, _ = manager.Get("users").Insert(user1)
_, _ = manager.Get("users").Insert(user2) _, _ = manager.Get("users").Insert(user2)
...@@ -91,7 +89,7 @@ func TestAddContract(t *testing.T) { ...@@ -91,7 +89,7 @@ func TestAddContract(t *testing.T) {
client := clientTest(t) client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{ errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr, Hash: defaultHash[:],
Filename: "ContractFilename", Filename: "ContractFilename",
Signer: []string{user1.Email, user2.Email}, Signer: []string{user1.Email, user2.Email},
Comment: "ContractComment", Comment: "ContractComment",
...@@ -107,7 +105,7 @@ func TestAddContract(t *testing.T) { ...@@ -107,7 +105,7 @@ func TestAddContract(t *testing.T) {
} }
assert.Equal(t, 1, len(contracts)) assert.Equal(t, 1, len(contracts))
assert.Equal(t, defaultHashStr, contracts[0].File.Hash) assert.Equal(t, defaultHash[:], contracts[0].File.Hash)
assert.Equal(t, "ContractFilename", contracts[0].File.Name) assert.Equal(t, "ContractFilename", contracts[0].File.Name)
assert.Equal(t, "ContractComment", contracts[0].Comment) assert.Equal(t, "ContractComment", contracts[0].Comment)
assert.T(t, contracts[0].Ready) assert.T(t, contracts[0].Ready)
...@@ -127,7 +125,7 @@ func TestAddContractMissingUser(t *testing.T) { ...@@ -127,7 +125,7 @@ func TestAddContractMissingUser(t *testing.T) {
client := clientTest(t) client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{ errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr, Hash: defaultHash[:],
Filename: "ContractFilename", Filename: "ContractFilename",
Signer: []string{user1.Email, user3.Email}, Signer: []string{user1.Email, user3.Email},
}) })
...@@ -142,7 +140,7 @@ func TestAddContractMissingUser(t *testing.T) { ...@@ -142,7 +140,7 @@ func TestAddContractMissingUser(t *testing.T) {
} }
assert.Equal(t, 1, len(contracts)) assert.Equal(t, 1, len(contracts))
assert.Equal(t, defaultHashStr, contracts[0].File.Hash) assert.Equal(t, defaultHash[:], contracts[0].File.Hash)
assert.Equal(t, "ContractFilename", contracts[0].File.Name) assert.Equal(t, "ContractFilename", contracts[0].File.Name)
assert.Equal(t, "", contracts[0].Comment) assert.Equal(t, "", contracts[0].Comment)
assert.T(t, !contracts[0].Ready) assert.T(t, !contracts[0].Ready)
...@@ -152,7 +150,7 @@ func TestAddContractMissingUser(t *testing.T) { ...@@ -152,7 +150,7 @@ func TestAddContractMissingUser(t *testing.T) {
assert.Equal(t, user1.CertHash, contracts[0].Signers[0].Hash) assert.Equal(t, user1.CertHash, contracts[0].Signers[0].Hash)
assert.Equal(t, user1.Email, contracts[0].Signers[0].Email) assert.Equal(t, user1.Email, contracts[0].Signers[0].Email)
assert.Equal(t, "000000000000000000000000", contracts[0].Signers[1].UserID.Hex()) assert.Equal(t, "000000000000000000000000", contracts[0].Signers[1].UserID.Hex())
assert.Equal(t, "", contracts[0].Signers[1].Hash) assert.Equal(t, []byte{}, contracts[0].Signers[1].Hash)
assert.Equal(t, user3.Email, contracts[0].Signers[1].Email) assert.Equal(t, user3.Email, contracts[0].Signers[1].Email)
} }
...@@ -162,7 +160,7 @@ func TestAddContractNoUser(t *testing.T) { ...@@ -162,7 +160,7 @@ func TestAddContractNoUser(t *testing.T) {
client := clientTest(t) client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{ errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr, Hash: defaultHash[:],
Filename: "ContractFilename", Filename: "ContractFilename",
Signer: []string{}, Signer: []string{},
}) })
...@@ -185,7 +183,7 @@ func TestAddContractDuplicatedUser(t *testing.T) { ...@@ -185,7 +183,7 @@ func TestAddContractDuplicatedUser(t *testing.T) {
client := clientTest(t) client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{ errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr, Hash: defaultHash[:],
Filename: "ContractFilename", Filename: "ContractFilename",
Signer: []string{user1.Email, user1.Email, user2.Email}, Signer: []string{user1.Email, user1.Email, user2.Email},
}) })
...@@ -209,7 +207,7 @@ func TestAddContractNoFilename(t *testing.T) { ...@@ -209,7 +207,7 @@ func TestAddContractNoFilename(t *testing.T) {
client := clientTest(t) client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{ errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr, Hash: defaultHash[:],
Signer: []string{user1.Email}, Signer: []string{user1.Email},
}) })
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
...@@ -231,7 +229,7 @@ func TestAddContractBadHash(t *testing.T) { ...@@ -231,7 +229,7 @@ func TestAddContractBadHash(t *testing.T) {
client := clientTest(t) client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{ errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: "aVeryBadHash", Hash: []byte{0xba, 0xd},
Filename: "ContractFilename", Filename: "ContractFilename",
Signer: []string{user1.Email}, Signer: []string{user1.Email},
}) })
......
...@@ -2,6 +2,7 @@ package contract ...@@ -2,6 +2,7 @@ package contract
import ( import (
"encoding/json" "encoding/json"
"fmt"
"time" "time"
"dfss/dfssp/entities" "dfss/dfssp/entities"
...@@ -46,7 +47,7 @@ func GetJSON(c *entities.Contract, ttp *TTPJSON) ([]byte, error) { ...@@ -46,7 +47,7 @@ func GetJSON(c *entities.Contract, ttp *TTPJSON) ([]byte, error) {
Comment: c.Comment, Comment: c.Comment,
File: &FileJSON{ File: &FileJSON{
Name: c.File.Name, Name: c.File.Name,
Hash: c.File.Hash, Hash: fmt.Sprintf("%x", c.File.Hash),
Hosted: c.File.Hosted, Hosted: c.File.Hosted,
}, },
Signers: make([]SignerJSON, len(c.Signers)), Signers: make([]SignerJSON, len(c.Signers)),
...@@ -55,7 +56,7 @@ func GetJSON(c *entities.Contract, ttp *TTPJSON) ([]byte, error) { ...@@ -55,7 +56,7 @@ func GetJSON(c *entities.Contract, ttp *TTPJSON) ([]byte, error) {
for i, s := range c.Signers { for i, s := range c.Signers {
data.Signers[i].Email = s.Email data.Signers[i].Email = s.Email
data.Signers[i].Hash = s.Hash data.Signers[i].Hash = fmt.Sprintf("%x", s.Hash)
} }
return json.MarshalIndent(data, "", " ") return json.MarshalIndent(data, "", " ")
......
...@@ -20,12 +20,12 @@ func TestGetJSON(t *testing.T) { ...@@ -20,12 +20,12 @@ func TestGetJSON(t *testing.T) {
allow multiline and accents: éÉ`, allow multiline and accents: éÉ`,
File: &entities.File{ File: &entities.File{
Name: "filename.pdf", Name: "filename.pdf",
Hash: "hash", Hash: []byte{0x01, 0x02},
Hosted: false, Hosted: false,
}, },
Signers: []entities.Signer{ Signers: []entities.Signer{
entities.Signer{Email: "a", Hash: "ha"}, entities.Signer{Email: "a", Hash: []byte{0xaa}},
entities.Signer{Email: "b", Hash: "hb"}, entities.Signer{Email: "b", Hash: []byte{0xbb}},
}, },
} }
...@@ -35,17 +35,17 @@ allow multiline and accents: éÉ`, ...@@ -35,17 +35,17 @@ allow multiline and accents: éÉ`,
"Comment": "A test comment\nallow multiline and accents: éÉ", "Comment": "A test comment\nallow multiline and accents: éÉ",
"File": { "File": {
"Name": "filename.pdf", "Name": "filename.pdf",
"Hash": "hash", "Hash": "0102",
"Hosted": false "Hosted": false
}, },
"Signers": [ "Signers": [
{ {
"Email": "a", "Email": "a",
"Hash": "ha" "Hash": "aa"
}, },
{ {
"Email": "b", "Email": "b",
"Hash": "hb" "Hash": "bb"
} }
], ],
"Sequence": null, "Sequence": null,
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
// File : Represents a file structure // File : Represents a file structure
type File struct { type File struct {
Name string `key:"name" bson:"name"` // Name of the File Name string `key:"name" bson:"name"` // Name of the File
Hash string `key:"hash" bson:"hash"` // Hash of the File Hash []byte `key:"hash" bson:"hash"` // Hash of the File
Hosted bool `key:"hosted" bson:"hosted"` // True if hosted on the platform, else false Hosted bool `key:"hosted" bson:"hosted"` // True if hosted on the platform, else false
} }
...@@ -18,7 +18,7 @@ type File struct { ...@@ -18,7 +18,7 @@ type File struct {
type Signer struct { type Signer struct {
UserID bson.ObjectId `key:"userId" bson:"userId"` UserID bson.ObjectId `key:"userId" bson:"userId"`
Email string `key:"email" bson:"email"`