This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
capabilities.go
69 lines (61 loc) · 1.98 KB
/
capabilities.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
// Copyright (C) 2013-2017, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
// Use of this source code is governed by GPLv3 found in the LICENSE file
//----------------------------------------------------------------------------------------
// implements a general way for recording capabilities that can be stored, confirmed and revoked
//
// Used by various parts of the system, like for api keys for bridging between apps, etc.
package holochain
import (
"errors"
"fmt"
"github.com/tidwall/buntdb"
"math/rand"
)
type Capability struct {
Token string
db *buntdb.DB
//Who list of public keys for whom this it valid
}
var CapabilityInvalidErr = errors.New("invalid capability")
func makeToken(capability string) (token string) {
return fmt.Sprintf("%d", rand.Int63())
}
// NewCapability returns and registers a capability of a type, for a specific or anyone if who is nil
func NewCapability(db *buntdb.DB, capability string, who interface{}) (c *Capability, err error) {
c = &Capability{db: db}
c.Token = makeToken(capability)
err = db.Update(func(tx *buntdb.Tx) error {
Debugf("NewCapability: save token:%s\n", c.Token)
_, _, err = tx.Set("tok:"+c.Token, capability, nil)
if err != nil {
return err
}
return nil
})
return
}
// Validate checks to see if the token has been registered and returns the capability it represent
func (c *Capability) Validate(who interface{}) (capability string, err error) {
err = c.db.View(func(tx *buntdb.Tx) (e error) {
Debugf("Validate: get token:%s\n", c.Token)
capability, e = tx.Get("tok:" + c.Token)
if e == buntdb.ErrNotFound {
e = CapabilityInvalidErr
}
return
})
return
}
// Revoke unregisters the capability for a peer
func (c *Capability) Revoke(who interface{}) (err error) {
err = c.db.Update(func(tx *buntdb.Tx) (e error) {
_, e = tx.Get("tok:" + c.Token)
if e == buntdb.ErrNotFound {
e = CapabilityInvalidErr
} else if e == nil {
_, e = tx.Delete("tok:" + c.Token)
}
return e
})
return
}