credinfo.go 2.2 KB
Newer Older
1
package irma
2

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

8
9
// CredentialInfo contains all information of an IRMA credential.
type CredentialInfo struct {
10
11
12
13
14
15
16
17
18
	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
19
	Hash             string             // SHA256 hash over the attributes
20
21
}

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

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

	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})
	}

38
39
	id := credtype.Identifier()
	issid := id.IssuerIdentifier()
40
	return &CredentialInfo{
41
42
43
44
		CredentialTypeID: id.String(),
		Name:             id.Name(),
		IssuerID:         issid.Name(),
		SchemeManagerID:  issid.SchemeManagerIdentifier().String(),
45
46
47
		SignedOn:         Timestamp(meta.SigningDate()),
		Expires:          Timestamp(meta.Expiry()),
		Attributes:       attrs,
Sietse Ringers's avatar
Sietse Ringers committed
48
		Logo:             credtype.Logo(conf),
49
		Hash:             NewAttributeListFromInts(ints, conf).Hash(),
50
51
52
	}
}

Sietse Ringers's avatar
Sietse Ringers committed
53
// Len implements sort.Interface.
54
func (cl CredentialInfoList) Len() int {
Sietse Ringers's avatar
Sietse Ringers committed
55
56
57
58
	return len(cl)
}

// Swap implements sort.Interface.
59
func (cl CredentialInfoList) Swap(i, j int) {
Sietse Ringers's avatar
Sietse Ringers committed
60
61
62
63
	cl[i], cl[j] = cl[j], cl[i]
}

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