credential.go 1.99 KB
Newer Older
1
2
3
package irmaclient

import (
4
	"github.com/go-errors/errors"
5
	"github.com/privacybydesign/gabi"
6
	"github.com/privacybydesign/irmago"
7
8
9
10
11
12
)

// credential represents an IRMA credential, whose zeroth attribute
// is always the secret key and the first attribute the metadata attribute.
type credential struct {
	*gabi.Credential
13
14
	*irma.MetadataAttribute
	attrs *irma.AttributeList
15
16
}

17
18
func newCredential(gabicred *gabi.Credential, conf *irma.Configuration) (*credential, error) {
	meta := irma.MetadataFromInt(gabicred.Attributes[1], conf)
19
20
21
22
	cred := &credential{
		Credential:        gabicred,
		MetadataAttribute: meta,
	}
23
24
25
26
27
28

	if cred.CredentialType() == nil {
		// Unknown credtype, populate Pk field later
		return cred, nil
	}

29
	var err error
30
	cred.Pk, err = conf.PublicKey(meta.CredentialType().IssuerIdentifier(), cred.KeyCounter())
31
32
33
34
35
36
	if err != nil {
		return nil, err
	}
	return cred, nil
}

37
func (cred *credential) AttributeList() *irma.AttributeList {
38
	if cred.attrs == nil {
39
		cred.attrs = irma.NewAttributeListFromInts(cred.Credential.Attributes[1:], cred.MetadataAttribute.Conf)
40
41
42
	}
	return cred.attrs
}
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

func (cred *credential) PrepareNonrevocation(conf *irma.Configuration, request irma.SessionRequest) (bool, error) {
	// If the requestor wants us to include a nonrevocation proof,
	// it will have sent us the latest revocation update messages
	m := request.Base().RevocationUpdates
	credtype := cred.CredentialType().Identifier()
	if len(m) == 0 || len(m[credtype]) == 0 {
		return false, nil
	}

	revupdates := m[credtype]
	nonrev := len(revupdates) > 0
	keystore := conf.RevocationKeystore(credtype.IssuerIdentifier())
	if updated, err := cred.NonRevocationWitness.Update(revupdates, keystore); err != nil {
		return false, err
	} else if updated {
		cred.DiscardRevocationCache()
	}
	if nonrev && cred.NonRevocationWitness.Index < revupdates[len(revupdates)-1].EndIndex {
		return false, errors.New("failed to update nonrevocation witness")
		// TODO download missing update messages from issuer and retry
	}
	return nonrev, nil
}