credinfo.go 3.38 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
19
	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
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
35
	id := credtype.Identifier()
	issid := id.IssuerIdentifier()
36
	return &CredentialInfo{
37
		CredentialTypeID: NewCredentialTypeIdentifier(id.String()),
38
		Name:             id.Name(),
39
40
		IssuerID:         NewIssuerIdentifier(issid.Name()),
		SchemeManagerID:  NewSchemeManagerIdentifier(issid.SchemeManagerIdentifier().String()),
41
42
		SignedOn:         Timestamp(meta.SigningDate()),
		Expires:          Timestamp(meta.Expiry()),
43
		Attributes:       attrs.Strings(),
Sietse Ringers's avatar
Sietse Ringers committed
44
		Logo:             credtype.Logo(conf),
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
		Hash:             attrs.Hash(),
	}
}

// Convert proof responses to Ints, adding nils for undislosed attributes
func convertProofResponsesToInts(aResponses map[int]*big.Int, aDisclosed map[int]*big.Int) ([]*big.Int, error) {
	var ints []*big.Int

	length := len(aResponses) + len(aDisclosed)

	for i := 1; i < length; i++ {
		if aResponses[i] == nil {
			if aDisclosed[i] == nil {
				// If index not found in aResponses it must be in aDisclosed
				return nil, &SessionError{
					ErrorType: ErrorCrypto,
					Info:      fmt.Sprintf("Missing attribute index: %v", i),
				} // TODO: error type?
			}

			ints = append(ints, aDisclosed[i])
		} else {
			// Don't include value of hidden attributes
			ints = append(ints, nil)
		}
70
	}
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	return ints, nil
}

// NewAttributeListFromInts initializes a new AttributeList from disclosed attributes of a prooflist
func NewCredentialInfoFromADisclosed(aResponses map[int]*big.Int, aDisclosed map[int]*big.Int, conf *Configuration) (*CredentialInfo, error) {
	ints, err := convertProofResponsesToInts(aResponses, aDisclosed)
	if err != nil {
		return nil, err
	}

	return NewCredentialInfo(ints, conf), nil
}

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

Sietse Ringers's avatar
Sietse Ringers committed
88
// Len implements sort.Interface.
89
func (cl CredentialInfoList) Len() int {
Sietse Ringers's avatar
Sietse Ringers committed
90
91
92
93
	return len(cl)
}

// Swap implements sort.Interface.
94
func (cl CredentialInfoList) Swap(i, j int) {
Sietse Ringers's avatar
Sietse Ringers committed
95
96
97
98
	cl[i], cl[j] = cl[j], cl[i]
}

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