/
store.go
73 lines (60 loc) · 1.81 KB
/
store.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
// Package token implements the filesystem based token-store for the
// Google Analytics API oAuth token.
package token
import (
"fmt"
"os"
"path/filepath"
"github.com/andreaskoch/ga-spam-control/api/apicredentials"
"github.com/andreaskoch/ga-spam-control/common/fsutil"
"golang.org/x/oauth2"
)
// NewTokenStore creates a new token store instance that uses
// the given file path to read and store API tokens.
func NewTokenStore(credentialFilePath string) apicredentials.TokenStorer {
return filesystemTokenStore{
credentialFilePath,
tokenSerializer{},
}
}
// filesystemTokenStore provides functions for reading and persisting
// oAuth tokens in a given file path.
type filesystemTokenStore struct {
filePath string
serializer tokenSerializer
}
// GetToken returns an oAuth token from disc. Returns an
// error if no token was found.
func (store filesystemTokenStore) GetToken() (*oauth2.Token, error) {
file, readErr := os.Open(store.filePath)
if readErr != nil {
return nil, readErr
}
token, deserializeErr := store.serializer.Deserialize(file)
if deserializeErr != nil {
return nil, deserializeErr
}
return token, nil
}
// SaveToken stores the given oAuth token to disc. Returns an
// error if the save failed.
func (store filesystemTokenStore) SaveToken(token *oauth2.Token) error {
folder := filepath.Dir(store.filePath)
if !fsutil.PathExists(folder) {
if err := os.MkdirAll(folder, 0700); err != nil {
return err
}
}
if !fsutil.IsDirectory(folder) {
return fmt.Errorf("Cannot create folder %q. A file with the same name already exists.", folder)
}
file, fileErr := os.OpenFile(store.filePath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
if fileErr != nil {
return fileErr
}
serializeErr := store.serializer.Serialize(file, token)
if serializeErr != nil {
return serializeErr
}
return nil
}