-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
77 lines (60 loc) · 1.41 KB
/
data.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
// Copyright (c) Bluebeam Inc. All rights reserved.
//
// Licensed under the MIT License. See LICENSE in the project root for license information.
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/boltdb/bolt"
"golang.org/x/oauth2"
)
type DataStore interface {
New()
Close()
StoreToken(userID string, token *oauth2.Token) error
GetToken(userID string) (*oauth2.Token, error)
}
type BoltDBStore struct {
DB *bolt.DB
}
func (s *BoltDBStore) New() {
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("Tokens"))
if err != nil {
return fmt.Errorf("Create bucket: %s", err)
}
return nil
})
s.DB = db
}
func (s *BoltDBStore) Close() {
s.DB.Close()
}
func (s *BoltDBStore) StoreToken(userID string, token *oauth2.Token) error {
return s.DB.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Tokens"))
data, err := json.Marshal(token)
if err != nil {
return err
}
err = b.Put([]byte(userID), data)
return err
})
}
func (s *BoltDBStore) GetToken(userID string) (*oauth2.Token, error) {
token := &oauth2.Token{}
err := s.DB.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Tokens"))
data := b.Get([]byte(userID))
if data == nil {
return fmt.Errorf("Token Not Found: %s", userID)
}
return json.Unmarshal(data, token)
})
return token, err
}