forked from go-macaroon-bakery/macaroon-bakery
/
rendezvous.go
165 lines (151 loc) · 4.62 KB
/
rendezvous.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package bakerytest
import (
"bytes"
"fmt"
"sync"
"time"
errgo "gopkg.in/errgo.v1"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon-bakery.v2/bakery/checkers"
"gopkg.in/macaroon-bakery.v2/httpbakery"
)
// Rendezvous implements a place where discharge information
// can be stored, recovered and waited for.
type Rendezvous struct {
mu sync.Mutex
maxId int
waiting map[string]*dischargeFuture
}
func NewRendezvous() *Rendezvous {
return &Rendezvous{
waiting: make(map[string]*dischargeFuture),
}
}
type dischargeFuture struct {
info *bakery.ThirdPartyCaveatInfo
done chan struct{}
caveats []checkers.Caveat
err error
}
// NewDischarge creates a new discharge in the rendezvous
// associated with the given caveat information.
// It returns an identifier for the discharge that can
// later be used to complete the discharge or find
// out the information again.
func (r *Rendezvous) NewDischarge(cav *bakery.ThirdPartyCaveatInfo) string {
r.mu.Lock()
defer r.mu.Unlock()
dischargeId := fmt.Sprintf("%d", r.maxId)
r.maxId++
r.waiting[dischargeId] = &dischargeFuture{
info: cav,
done: make(chan struct{}),
}
return dischargeId
}
// Info returns information on the given discharge id
// and reports whether the information has been found.
func (r *Rendezvous) Info(dischargeId string) (*bakery.ThirdPartyCaveatInfo, bool) {
r.mu.Lock()
defer r.mu.Unlock()
d := r.waiting[dischargeId]
if d == nil {
return nil, false
}
return d.info, true
}
// DischargeComplete marks the discharge with the given id
// as completed with the given caveats,
// which will be associated with the given discharge id
// and returned from Await.
func (r *Rendezvous) DischargeComplete(dischargeId string, caveats []checkers.Caveat) {
r.dischargeDone(dischargeId, caveats, nil)
}
// DischargeFailed marks the discharge with the given id
// as failed with the given error, which will be
// returned from Await or CheckToken when they're
// called with that id.
func (r *Rendezvous) DischargeFailed(dischargeId string, err error) {
r.dischargeDone(dischargeId, nil, err)
}
func (r *Rendezvous) dischargeDone(dischargeId string, caveats []checkers.Caveat, err error) {
r.mu.Lock()
defer r.mu.Unlock()
d := r.waiting[dischargeId]
if d == nil {
panic(errgo.Newf("invalid discharge id %q", dischargeId))
}
select {
case <-d.done:
panic(errgo.Newf("DischargeComplete called twice"))
default:
}
d.caveats, d.err = caveats, err
close(d.done)
}
// Await waits for DischargeComplete or DischargeFailed to be called,
// and returns either the caveats passed to DischargeComplete
// or the error passed to DischargeFailed.
//
// It waits for at least the given duration. If timeout is zero,
// it returns the information only if it is already available.
func (r *Rendezvous) Await(dischargeId string, timeout time.Duration) ([]checkers.Caveat, error) {
r.mu.Lock()
d := r.waiting[dischargeId]
r.mu.Unlock()
if d == nil {
return nil, errgo.Newf("invalid discharge id %q", dischargeId)
}
if timeout == 0 {
select {
case <-d.done:
default:
return nil, errgo.New("rendezvous has not completed")
}
} else {
select {
case <-d.done:
case <-time.After(timeout):
return nil, errgo.New("timeout waiting for rendezvous to complete")
}
}
if d.err != nil {
return nil, errgo.Mask(d.err, errgo.Any)
}
return d.caveats, nil
}
func (r *Rendezvous) DischargeToken(dischargeId string) *httpbakery.DischargeToken {
_, err := r.Await(dischargeId, 0)
if err != nil {
panic(errgo.Notef(err, "cannot obtain discharge token for %q", dischargeId))
}
return &httpbakery.DischargeToken{
Kind: "discharge-id",
Value: []byte(dischargeId),
}
}
// CheckToken checks that the given token is valid for discharging the
// given caveat, and returns any caveats passed to DischargeComplete
// if it is.
func (r *Rendezvous) CheckToken(token *httpbakery.DischargeToken, cav *bakery.ThirdPartyCaveatInfo) ([]checkers.Caveat, error) {
if token.Kind != "discharge-id" {
return nil, errgo.Newf("invalid discharge token kind %q", token.Kind)
}
info, ok := r.Info(string(token.Value))
if !ok {
return nil, errgo.Newf("discharge token %q not found", token.Value)
}
if !bytes.Equal(info.Caveat, cav.Caveat) {
return nil, errgo.Newf("caveat provided to CheckToken does not match original")
}
if !bytes.Equal(info.Id, cav.Id) {
return nil, errgo.Newf("caveat id provided to CheckToken does not match original")
}
caveats, err := r.Await(string(token.Value), 0)
if err != nil {
// Don't mask the error because we want the cause to remain
// unchanged if it was passed to DischargeFailed.
return nil, errgo.Mask(err, errgo.Any)
}
return caveats, nil
}