credinfo.go 2.42 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/credentials/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
31
32
33
34
35
36
37
	credtype := meta.CredentialType()

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

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

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

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