forked from LFDT-Panurus/panurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorization.go
More file actions
148 lines (123 loc) · 5.28 KB
/
Copy pathauthorization.go
File metadata and controls
148 lines (123 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package common
import (
"context"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-token-sdk/token"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/identity"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/logging"
token2 "github.com/hyperledger-labs/fabric-token-sdk/token/token"
)
// Authorization defines an interface for checking the ownership, issuance, and auditor status of tokens.
type Authorization interface {
// IsMine returns true if the passed token is owned by an owner wallet.
// It returns the ID of the owner wallet and any additional owner identifier, if supported.
// It is possible that the wallet ID is empty and the additional owner identifier list is not.
IsMine(ctx context.Context, tok *token2.Token) (string, []string, bool)
// AmIAnAuditor return true if the passed TMS contains an auditor wallet for any of the auditor identities
// defined in the public parameters of the passed TMS.
AmIAnAuditor() bool
// Issued returns true if the passed issuer issued the passed token
Issued(ctx context.Context, issuer token.Identity, tok *token2.Token) bool
}
// WalletBasedAuthorization is a wallet-based authorization implementation
type WalletBasedAuthorization struct {
Logger logging.Logger
PublicParameters driver.PublicParameters
WalletService driver.WalletService
amIAnAuditor bool
}
// NewTMSAuthorization returns a new WalletBasedAuthorization for the passed public parameters and wallet service.
func NewTMSAuthorization(logger logging.Logger, publicParameters driver.PublicParameters, walletService driver.WalletService) *WalletBasedAuthorization {
amIAnAuditor := false
var errs []error
for _, identity := range publicParameters.Auditors() {
_, err := walletService.AuditorWallet(context.Background(), identity)
if err == nil {
amIAnAuditor = true
break
}
errs = append(errs, errors.Wrapf(err, "I'm not this auditor identity [%s]", identity))
}
logger.Debugf("am I an auditor? [%v], with errs [%v]", amIAnAuditor, errs)
return &WalletBasedAuthorization{Logger: logger, PublicParameters: publicParameters, WalletService: walletService, amIAnAuditor: amIAnAuditor}
}
// IsMine returns true if the passed token is owned by an owner wallet.
// It returns the ID of the owner wallet and no additional owner identifiers.
func (w *WalletBasedAuthorization) IsMine(ctx context.Context, tok *token2.Token) (string, []string, bool) {
wallet, err := w.WalletService.OwnerWallet(ctx, tok.Owner)
if err != nil {
return "", nil, false
}
return wallet.ID(), nil, true
}
// AmIAnAuditor return true if the passed TMS contains an auditor wallet for any of the auditor identities
// defined in the public parameters of the passed TMS.
func (w *WalletBasedAuthorization) AmIAnAuditor() bool {
return w.amIAnAuditor
}
// Issued returns true if the passed issuer issued the passed token
func (w *WalletBasedAuthorization) Issued(ctx context.Context, issuer token.Identity, tok *token2.Token) bool {
_, err := w.WalletService.IssuerWallet(ctx, issuer)
if err == nil {
return true
}
// In some setups (notably HSM-backed identities), the issuer identity serialized in
// transfer metadata may not byte-match the locally registered wallet identity.
// Fall back to checking if this node has a default issuer wallet configured.
if issuer.IsNone() {
return false
}
_, err = w.WalletService.IssuerWallet(ctx, "")
return err == nil
}
// AuthorizationMultiplexer iterates over multiple authorization checker
type AuthorizationMultiplexer struct {
authorizations []Authorization
}
// NewAuthorizationMultiplexer returns a new AuthorizationMultiplexer for the passed ownership checkers
func NewAuthorizationMultiplexer(ownerships ...Authorization) *AuthorizationMultiplexer {
return &AuthorizationMultiplexer{authorizations: ownerships}
}
// IsMine returns true it there exists an authorization checker that returns true
func (o *AuthorizationMultiplexer) IsMine(ctx context.Context, tok *token2.Token) (string, []string, bool) {
for _, authorization := range o.authorizations {
walletID, ids, mine := authorization.IsMine(ctx, tok)
if mine {
return walletID, ids, true
}
}
return "", nil, false
}
// AmIAnAuditor returns true it there exists an authorization checker that returns true
func (o *AuthorizationMultiplexer) AmIAnAuditor() bool {
for _, authorization := range o.authorizations {
yes := authorization.AmIAnAuditor()
if yes {
return true
}
}
return false
}
// Issued returns true it there exists an authorization checker that returns true
func (o *AuthorizationMultiplexer) Issued(ctx context.Context, issuer token.Identity, tok *token2.Token) bool {
for _, authorization := range o.authorizations {
yes := authorization.Issued(ctx, issuer, tok)
if yes {
return true
}
}
return false
}
// OwnerType returns the type of owner (e.g. 'idemix' or 'htlc') and the identity bytes.
func (o *AuthorizationMultiplexer) OwnerType(raw []byte) (driver.IdentityType, []byte, error) {
owner, err := identity.UnmarshalTypedIdentity(raw)
if err != nil {
return driver.ZeroIdentityType, nil, err
}
return owner.Type, owner.Identity, nil
}