/
vanadium.go
68 lines (56 loc) · 1.69 KB
/
vanadium.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
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package ticket
import (
"bytes"
"encoding/base64"
"fmt"
"strings"
"time"
"github.com/grailbio/base/common/log"
v23 "v.io/v23"
"v.io/v23/security"
"v.io/v23/vom"
)
const requiredSuffix = security.ChainSeparator + "_role"
func base64urlVomEncode(i interface{}) (string, error) {
buf := &bytes.Buffer{}
closer := base64.NewEncoder(base64.URLEncoding, buf)
enc := vom.NewEncoder(closer)
if err := enc.Encode(i); err != nil {
return "", err
}
// Must close the base64 encoder to flush out any partially written
// blocks.
if err := closer.Close(); err != nil {
return "", err
}
return buf.String(), nil
}
func (b *VanadiumBuilder) newVanadiumTicket(ctx *TicketContext) (TicketVanadiumTicket, error) {
empty := TicketVanadiumTicket{}
if !strings.HasSuffix(ctx.remoteBlessings.String(), requiredSuffix) {
return empty, fmt.Errorf("%q doesn't have the required %q suffix", ctx.remoteBlessings.String(), requiredSuffix)
}
pubKey := ctx.remoteBlessings.PublicKey()
expiryCaveat, err := security.NewExpiryCaveat(time.Now().Add(365 * 24 * time.Hour))
if err != nil {
return empty, err
}
blessing, _ := v23.GetPrincipal(ctx.ctx).BlessingStore().Default()
resultBlessings, err := v23.GetPrincipal(ctx.ctx).Bless(pubKey, blessing, b.BlessingName, expiryCaveat)
if err != nil {
return empty, err
}
log.Infof(ctx.ctx, "resultBlessings: %+v", resultBlessings)
s, err := base64urlVomEncode(resultBlessings)
if err != nil {
return empty, err
}
return TicketVanadiumTicket{
Value: VanadiumTicket{
Blessing: s,
},
}, nil
}