Commit c736be28 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Improve identifier (de)serialization

parent e49c7e6a
......@@ -87,16 +87,6 @@ func (al *AttributeList) Info() *Credential {
return al.info
}
// TODO maybe remove
func (al *AttributeList) hash() string {
bytes := make([]byte, 20)
for _, i := range al.Ints {
bytes = append(bytes, i.Bytes()...)
}
shasum := sha256.Sum256(bytes)
return string(shasum[:])
}
// Strings converts the current instance to human-readable strings.
func (al *AttributeList) Strings() []string {
if al.strings == nil {
......
package irmago
import (
"encoding/json"
"strings"
)
import "strings"
type metaObjectIdentifier string
......@@ -104,40 +101,46 @@ func (ai *AttributeIdentifier) CredentialIdentifier() CredentialIdentifier {
return CredentialIdentifier{Type: ai.Type.CredentialTypeIdentifier(), Index: ai.Index, Count: ai.Count}
}
// MarshalJSON marshals this instance to JSON as a string.
func (id AttributeTypeIdentifier) MarshalJSON() ([]byte, error) {
return json.Marshal(id.String())
// MarshalText implements encoding.TextMarshaler.
func (id SchemeManagerIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// MarshalJSON marshals this instance to JSON as a string.
func (id CredentialTypeIdentifier) MarshalJSON() ([]byte, error) {
return json.Marshal(id.String())
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *SchemeManagerIdentifier) UnmarshalText(text []byte) error {
*id = NewSchemeManagerIdentifier(string(text))
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (id IssuerIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalJSON unmarshals this instance from JSON.
func (id *AttributeTypeIdentifier) UnmarshalJSON(b []byte) error {
var val string
err := json.Unmarshal(b, &val)
if err != nil {
return err
}
id.metaObjectIdentifier = metaObjectIdentifier(val)
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *IssuerIdentifier) UnmarshalText(text []byte) error {
*id = NewIssuerIdentifier(string(text))
return nil
}
// UnmarshalJSON unmarshals this instance from JSON.
func (id *CredentialTypeIdentifier) UnmarshalJSON(b []byte) error {
var val string
err := json.Unmarshal(b, &val)
if err != nil {
return err
}
id.metaObjectIdentifier = metaObjectIdentifier(val)
// MarshalText implements encoding.TextMarshaler.
func (id CredentialTypeIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *CredentialTypeIdentifier) UnmarshalText(text []byte) error {
*id = NewCredentialTypeIdentifier(string(text))
return nil
}
// TODO this also for the other identifiers
func (id *IssuerIdentifier) UnmarshalText(text []byte) error {
*id = NewIssuerIdentifier(string(text))
// MarshalText implements encoding.TextMarshaler.
func (id AttributeTypeIdentifier) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *AttributeTypeIdentifier) UnmarshalText(text []byte) error {
*id = NewAttributeTypeIdentifier(string(text))
return nil
}
......@@ -110,7 +110,7 @@ func (cm *CredentialManager) ParseAndroidStorage() (err error) {
xml.Unmarshal(bytes, &parsedxml)
parsedjson := make(map[string][]*gabi.Credential)
parsedksses := make(map[string]*keyshareServer)
cm.keyshareServers = make(map[SchemeManagerIdentifier]*keyshareServer)
for _, xmltag := range parsedxml.Strings {
if xmltag.Name == "credentials" {
jsontag := html.UnescapeString(xmltag.Content)
......@@ -120,7 +120,7 @@ func (cm *CredentialManager) ParseAndroidStorage() (err error) {
}
if xmltag.Name == "keyshare" {
jsontag := html.UnescapeString(xmltag.Content)
if err = json.Unmarshal([]byte(jsontag), &parsedksses); err != nil {
if err = json.Unmarshal([]byte(jsontag), &cm.keyshareServers); err != nil {
return
}
}
......@@ -134,10 +134,6 @@ func (cm *CredentialManager) ParseAndroidStorage() (err error) {
}
}
for name, kss := range parsedksses {
cm.keyshareServers[NewSchemeManagerIdentifier(name)] = kss
}
for _, list := range parsedjson {
cm.secretkey = list[0].Attributes[0]
for i, gabicred := range list {
......@@ -261,13 +257,7 @@ func (cm *CredentialManager) storeSignature(cred *credential, counter int) (err
}
func (cm *CredentialManager) storeAttributes() (err error) {
// Unfortunately, the type of cm.attributes (map[CredentialTypeIdentifier][]*AttributeList)
// cannot be passed directly to json.Marshal(), so we copy it into a temp list.
temp := make(map[string][]*AttributeList)
for credid, list := range cm.attributes {
temp[credid.String()] = list
}
attrbytes, err := json.Marshal(temp)
attrbytes, err := json.Marshal(cm.attributes)
if err != nil {
return err
}
......@@ -278,11 +268,7 @@ func (cm *CredentialManager) storeAttributes() (err error) {
}
func (cm *CredentialManager) storeKeyshareServers() (err error) {
temp := make(map[string]*keyshareServer)
for name, kss := range cm.keyshareServers {
temp[name.String()] = kss
}
bts, err := json.Marshal(temp)
bts, err := json.Marshal(cm.keyshareServers)
if err != nil {
return
}
......@@ -345,32 +331,24 @@ func (cm *CredentialManager) loadSecretKey() (*big.Int, error) {
func (cm *CredentialManager) loadAttributes() (list map[CredentialTypeIdentifier][]*AttributeList, err error) {
list = make(map[CredentialTypeIdentifier][]*AttributeList)
temp := make(map[string][]*AttributeList)
exists, err := PathExists(cm.path(attributesFile))
if err != nil || !exists {
return
}
bytes, err := ioutil.ReadFile(cm.path(attributesFile))
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &temp)
err = json.Unmarshal(bytes, &list)
if err != nil {
return nil, err
}
for credid, attrs := range temp {
list[NewCredentialTypeIdentifier(credid)] = attrs
}
return list, nil
}
func (cm *CredentialManager) loadKeyshareServers() (ksses map[SchemeManagerIdentifier]*keyshareServer, err error) {
ksses = make(map[SchemeManagerIdentifier]*keyshareServer)
temp := make(map[string]*keyshareServer)
exists, err := PathExists(cm.path(kssFile))
if err != nil || !exists {
return
......@@ -379,13 +357,10 @@ func (cm *CredentialManager) loadKeyshareServers() (ksses map[SchemeManagerIdent
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &temp)
err = json.Unmarshal(bytes, &ksses)
if err != nil {
return nil, err
}
for name, kss := range temp {
ksses[NewSchemeManagerIdentifier(name)] = kss
}
return
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment