store.go 3.33 KB
Newer Older
Sietse Ringers's avatar
Sietse Ringers committed
1
2
3
4
5
6
7
package irmago

import (
	"encoding/xml"
	"io/ioutil"
	"os"
	"path/filepath"
Sietse Ringers's avatar
Sietse Ringers committed
8
	"strconv"
Sietse Ringers's avatar
Sietse Ringers committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

	"github.com/mhe/gabi"
)

// MetaStore is the global instance of ConfigurationStore
var MetaStore = ConfigurationStore{
	make(map[string]*SchemeManagerDescription),
	make(map[string]*IssuerDescription),
	make(map[string]*CredentialDescription),
	make(map[string]*gabi.PublicKey),
}

// ConfigurationStore ...
type ConfigurationStore struct {
	managers    map[string]*SchemeManagerDescription
	issuers     map[string]*IssuerDescription
	credentials map[string]*CredentialDescription
	publickeys  map[string]*gabi.PublicKey
}

Sietse Ringers's avatar
Sietse Ringers committed
29
30
// ParseFolder populates the current store by parsing the specified irma_configuration folder,
// listing the containing scheme managers, issuers, credential types and public keys.
Sietse Ringers's avatar
Sietse Ringers committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
func (store *ConfigurationStore) ParseFolder(path string) error {
	return iterateSubfolders(path, func(dir string) error {
		manager := &SchemeManagerDescription{}
		exists, err := pathToDescription(dir+"/description.xml", manager)
		if err != nil {
			return err
		}
		if exists {
			MetaStore.managers[manager.Name] = manager
			return store.parseIssuerFolders(dir)
		}
		return nil
	})
}

func (store *ConfigurationStore) parseIssuerFolders(path string) error {
	return iterateSubfolders(path, func(dir string) error {
		issuer := &IssuerDescription{}
		exists, err := pathToDescription(dir+"/description.xml", issuer)
		if err != nil {
			return err
		}
		if exists {
			store.issuers[issuer.Identifier().string] = issuer
Sietse Ringers's avatar
Sietse Ringers committed
55
56
57
58
			if err = store.parseCredentialsFolder(dir + "/Issues/"); err != nil {
				return err
			}
			return store.parseKeysFolder(issuer.Identifier(), dir+"/PublicKeys/")
Sietse Ringers's avatar
Sietse Ringers committed
59
60
61
62
63
		}
		return nil
	})
}

Sietse Ringers's avatar
Sietse Ringers committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
func (store *ConfigurationStore) parseKeysFolder(issuer *IssuerIdentifier, path string) error {
	for i := 0; ; i++ {
		file := path + strconv.Itoa(i) + ".xml"
		if _, err := os.Stat(file); err != nil {
			break
		}
		pk, err := gabi.NewPublicKeyFromFile(file)
		if err != nil {
			return err
		}
		MetaStore.publickeys[issuer.string] = pk
	}
	return nil
}

Sietse Ringers's avatar
Sietse Ringers committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
func (store *ConfigurationStore) parseCredentialsFolder(path string) error {
	return iterateSubfolders(path, func(dir string) error {
		cred := &CredentialDescription{}
		exists, err := pathToDescription(dir+"/description.xml", cred)
		if err != nil {
			return err
		}
		if exists {
			store.credentials[cred.Identifier().string] = cred
		}
		return nil
	})
}

Sietse Ringers's avatar
Sietse Ringers committed
93
94
95
// iterateSubfolders iterates over the subfolders of the specified path,
// calling the specified handler each time. If anything goes wrong, or
// if the caller returns a non-nil error, an error is immediately returned.
Sietse Ringers's avatar
Sietse Ringers committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
func iterateSubfolders(path string, handler func(string) error) error {
	dirs, err := filepath.Glob(path + "/*")
	if err != nil {
		return err
	}

	for _, dir := range dirs {
		stat, err := os.Stat(dir)
		if err != nil {
			return err
		}
		if !stat.IsDir() {
			continue
		}
		err = handler(dir)
		if err != nil {
			return err
		}
	}

	return nil
}

func pathToDescription(path string, description interface{}) (bool, error) {
	if _, err := os.Stat(path); err != nil {
		return false, nil
	}

	file, err := os.Open(path)
	if err != nil {
		return true, err
	}
	defer file.Close()

	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		return true, err
	}

	err = xml.Unmarshal(bytes, description)
	if err != nil {
		return true, err
	}

	return true, nil
}