forked from motiv-labs/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_repository.go
55 lines (45 loc) · 1.36 KB
/
file_repository.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
package oauth2
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"sync"
log "github.com/sirupsen/logrus"
)
// FileSystemRepository represents a mongodb repository
type FileSystemRepository struct {
*InMemoryRepository
sync.Mutex
}
// NewFileSystemRepository creates a mongo OAuth Server repo
func NewFileSystemRepository(dir string) (*FileSystemRepository, error) {
repo := &FileSystemRepository{InMemoryRepository: NewInMemoryRepository()}
// Grab json files from directory
files, err := ioutil.ReadDir(dir)
if nil != err {
return nil, err
}
for _, f := range files {
if filepath.Ext(f.Name()) == ".json" {
filePath := filepath.Join(dir, f.Name())
oauthServerRaw, err := ioutil.ReadFile(filePath)
if err != nil {
log.WithError(err).WithField("path", filePath).Error("Couldn't load the oauth server file")
return nil, err
}
oauthServer := repo.parseOAuthServer(oauthServerRaw)
if err = repo.Add(oauthServer); err != nil {
log.WithError(err).Error("Can't add the definition to the repository")
return nil, err
}
}
}
return repo, nil
}
func (r *FileSystemRepository) parseOAuthServer(oauthServerRaw []byte) *OAuth {
oauthServer := NewOAuth()
if err := json.Unmarshal(oauthServerRaw, oauthServer); err != nil {
log.WithError(err).Error("[RPC] --> Couldn't unmarshal oauth server configuration")
}
return oauthServer
}