credinfo.go 2.46 KB
Newer Older
1
package irma
2

Sietse Ringers's avatar
Sietse Ringers committed
3
import (
4
	"fmt"
5
6
	"math/big"
	"strings"
7

8
	"github.com/privacybydesign/irmago/internal/fs"
Sietse Ringers's avatar
Sietse Ringers committed
9
)
10

11
12
// CredentialInfo contains all information of an IRMA credential.
type CredentialInfo struct {
13
14
15
16
17
18
19
20
21
	CredentialTypeID string             // e.g., "irma-demo.RU.studentCard"
	Name             string             // e.g., "studentCard"
	IssuerID         string             // e.g., "RU"
	SchemeManagerID  string             // e.g., "irma-demo"
	Index            int                // This is the Index-th credential instance of this type
	SignedOn         Timestamp          // Unix timestamp
	Expires          Timestamp          // Unix timestamp
	Attributes       []TranslatedString // Human-readable rendered attributes
	Logo             string             // Path to logo on storage
22
	Hash             string             // SHA256 hash over the attributes
23
24
}

25
26
// A CredentialInfoList is a list of credentials (implements sort.Interface).
type CredentialInfoList []*CredentialInfo
Sietse Ringers's avatar
Sietse Ringers committed
27

28
29
func NewCredentialInfo(ints []*big.Int, conf *Configuration) *CredentialInfo {
	meta := MetadataFromInt(ints[0], conf)
30
	credtype := meta.CredentialType()
31
32
33
	if credtype == nil {
		return nil
	}
34
35
36
37
38
39
40

	attrs := make([]TranslatedString, len(credtype.Attributes))
	for i := range credtype.Attributes {
		val := string(ints[i+1].Bytes())
		attrs[i] = TranslatedString(map[string]string{"en": val, "nl": val})
	}

41
	path := fmt.Sprintf("%s/%s/%s/Issues/%s/logo.png", conf.path, credtype.SchemeManagerID, credtype.IssuerID, credtype.ID)
42
	exists, err := fs.PathExists(path)
43
44
45
46
47
48
	if err != nil {
		return nil
	}
	if !exists {
		path = ""
	}
49
50
	id := credtype.Identifier()
	issid := id.IssuerIdentifier()
51
	return &CredentialInfo{
52
53
54
55
		CredentialTypeID: id.String(),
		Name:             id.Name(),
		IssuerID:         issid.Name(),
		SchemeManagerID:  issid.SchemeManagerIdentifier().String(),
56
57
58
59
		SignedOn:         Timestamp(meta.SigningDate()),
		Expires:          Timestamp(meta.Expiry()),
		Attributes:       attrs,
		Logo:             path,
60
		Hash:             NewAttributeListFromInts(ints, conf).Hash(),
61
62
63
	}
}

Sietse Ringers's avatar
Sietse Ringers committed
64
// Len implements sort.Interface.
65
func (cl CredentialInfoList) Len() int {
Sietse Ringers's avatar
Sietse Ringers committed
66
67
68
69
	return len(cl)
}

// Swap implements sort.Interface.
70
func (cl CredentialInfoList) Swap(i, j int) {
Sietse Ringers's avatar
Sietse Ringers committed
71
72
73
74
	cl[i], cl[j] = cl[j], cl[i]
}

// Less implements sort.Interface.
75
func (cl CredentialInfoList) Less(i, j int) bool {
76
	// TODO Decide on sorting, and if it depends on a irmago.TranslatedString, allow language choosing
77
	return strings.Compare(cl[i].Name, cl[j].Name) > 0
Sietse Ringers's avatar
Sietse Ringers committed
78
}