credinfo.go 2.29 KB
Newer Older
1
package irma
2

Sietse Ringers's avatar
Sietse Ringers committed
3
import (
4
	"fmt"
5
	"strings"
6
	"time"
7
8

	"github.com/mhe/gabi/big"
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
	ID              string                                       // e.g., "studentCard"
	IssuerID        string                                       // e.g., "RU"
	SchemeManagerID string                                       // e.g., "irma-demo"
	SignedOn        Timestamp                                    // Unix timestamp
	Expires         Timestamp                                    // Unix timestamp
	Attributes      map[AttributeTypeIdentifier]TranslatedString // Human-readable rendered attributes
	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
	attrs := NewAttributeListFromInts(ints, conf)
33
34
	id := credtype.Identifier()
	issid := id.IssuerIdentifier()
35
	return &CredentialInfo{
36
37
38
39
40
41
42
		ID:              id.Name(),
		IssuerID:        issid.Name(),
		SchemeManagerID: issid.SchemeManagerIdentifier().Name(),
		SignedOn:        Timestamp(meta.SigningDate()),
		Expires:         Timestamp(meta.Expiry()),
		Attributes:      attrs.Map(conf),
		Hash:            attrs.Hash(),
43
44
45
46
	}
}

func (ci CredentialInfo) GetCredentialType(conf *Configuration) *CredentialType {
47
	return conf.CredentialTypes[NewCredentialTypeIdentifier(fmt.Sprintf("%s.%s.%s", ci.SchemeManagerID, ci.IssuerID, ci.ID))]
48
49
}

50
51
52
53
54
// 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
55
// Len implements sort.Interface.
56
func (cl CredentialInfoList) Len() int {
Sietse Ringers's avatar
Sietse Ringers committed
57
58
59
60
	return len(cl)
}

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

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