credinfo.go 2.52 KB
Newer Older
1
package irma
2

Sietse Ringers's avatar
Sietse Ringers committed
3
import (
4
5
	"math/big"
	"strings"
6
7
	"github.com/privacybydesign/irmago/internal/fs"
	"time"
Sietse Ringers's avatar
Sietse Ringers committed
8
)
9

10
11
// CredentialInfo contains all information of an IRMA credential.
type CredentialInfo struct {
12
13
14
15
16
17
18
19
20
21
	CredentialTypeID CredentialTypeIdentifier // e.g., "irma-demo.RU.studentCard"
	Name             string                   // e.g., "studentCard"
	IssuerID         IssuerIdentifier         // e.g., "RU"
	SchemeManagerID  SchemeManagerIdentifier  // 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
	Hash             string                   // SHA256 hash over the attributes
22
23
}

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

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

34
	attrs := NewAttributeListFromInts(ints, conf)
35

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

func (ci CredentialInfo) GetCredentialType(conf *Configuration) *CredentialType {
	return conf.CredentialTypes[ci.CredentialTypeID]
53
54
}

55
56
57
58
59
// Returns true if credential is expired at moment of calling this function
func (ci CredentialInfo) IsExpired() bool {
	return ci.Expires.Before(Timestamp(time.Now()))
}

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

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

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