store.go 3.48 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

	"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),
18
	make(map[string][]*gabi.PublicKey),
Sietse Ringers's avatar
Sietse Ringers committed
19
20
}

Sietse Ringers's avatar
Sietse Ringers committed
21
22
// ConfigurationStore keeps track of scheme managers, issuers, credential types and public keys.
// Use the global MetaStore instance.
Sietse Ringers's avatar
Sietse Ringers committed
23
24
25
26
type ConfigurationStore struct {
	managers    map[string]*SchemeManagerDescription
	issuers     map[string]*IssuerDescription
	credentials map[string]*CredentialDescription
27
	publickeys  map[string][]*gabi.PublicKey
Sietse Ringers's avatar
Sietse Ringers committed
28
29
}

Sietse Ringers's avatar
Sietse Ringers committed
30
31
// 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
56
57
58
59
			if err = store.parseCredentialsFolder(dir + "/Issues/"); err != nil {
				return err
			}
			return store.parseKeysFolder(issuer.Identifier(), dir+"/PublicKeys/")
Sietse Ringers's avatar
Sietse Ringers committed
60
61
62
63
64
		}
		return nil
	})
}

Sietse Ringers's avatar
Sietse Ringers committed
65
66
67
68
69
70
71
72
73
74
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
		}
75
		MetaStore.publickeys[issuer.string] = append(MetaStore.publickeys[issuer.string], pk)
Sietse Ringers's avatar
Sietse Ringers committed
76
77
78
79
	}
	return nil
}

Sietse Ringers's avatar
Sietse Ringers committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
94
95
96
// 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
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
142
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
}