-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.go
More file actions
130 lines (111 loc) · 3.03 KB
/
storage.go
File metadata and controls
130 lines (111 loc) · 3.03 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"errors"
"fmt"
"github.com/RangelReale/osin"
"os"
)
type Storage struct {
clients map[string]osin.Client
authorize map[string]*osin.AuthorizeData
access map[string]*osin.AccessData
refresh map[string]string
}
func NewStorage(redirectHostname, redirectPort string) *Storage {
r := &Storage{
clients: make(map[string]osin.Client),
authorize: make(map[string]*osin.AuthorizeData),
access: make(map[string]*osin.AccessData),
refresh: make(map[string]string),
}
r.clients["1234"] = &osin.DefaultClient{
Id: "1234",
Secret: "aabbccdd",
RedirectUri: "http://localhost:14000/appauth",
}
r.clients["1"] = &osin.DefaultClient{
Id: "1",
Secret: "Cheesecake",
RedirectUri: getCheesecakeRedirectUrl(redirectHostname, redirectPort),
}
fmt.Printf("using %s as port for Cheesecake redirect\n", redirectPort)
return r
}
func (s *Storage) Clone() osin.Storage {
return s
}
func (s *Storage) Close() {
}
func (s *Storage) GetClient(id string) (osin.Client, error) {
fmt.Printf("GetClient: %s\n", id)
if c, ok := s.clients[id]; ok {
return c, nil
}
return nil, errors.New("Client not found")
}
func (s *Storage) SetClient(id string, client osin.Client) error {
fmt.Printf("SetClient: %s\n", id)
s.clients[id] = client
return nil
}
func (s *Storage) SaveAuthorize(data *osin.AuthorizeData) error {
fmt.Printf("SaveAuthorize: %#v\n", data)
s.authorize[data.Code] = data
return nil
}
func (s *Storage) LoadAuthorize(code string) (*osin.AuthorizeData, error) {
fmt.Printf("LoadAuthorize: %s\n", code)
if d, ok := s.authorize[code]; ok {
fmt.Printf("%#v\n", d)
return d, nil
}
return nil, errors.New("Authorize not found")
}
func (s *Storage) RemoveAuthorize(code string) error {
fmt.Printf("RemoveAuthorize: %s\n", code)
delete(s.authorize, code)
return nil
}
func (s *Storage) SaveAccess(data *osin.AccessData) error {
fmt.Printf("SaveAccess: %s\n", data.AccessToken)
s.access[data.AccessToken] = data
if data.RefreshToken != "" {
s.refresh[data.RefreshToken] = data.AccessToken
}
return nil
}
func (s *Storage) LoadAccess(code string) (*osin.AccessData, error) {
fmt.Printf("LoadAccess: %s\n", code)
if d, ok := s.access[code]; ok {
return d, nil
}
return nil, errors.New("Access not found")
}
func (s *Storage) RemoveAccess(code string) error {
fmt.Printf("RemoveAccess: %s\n", code)
delete(s.access, code)
return nil
}
func (s *Storage) LoadRefresh(code string) (*osin.AccessData, error) {
fmt.Printf("LoadRefresh: %s\n", code)
if d, ok := s.refresh[code]; ok {
return s.LoadAccess(d)
}
return nil, errors.New("Refresh not found")
}
func (s *Storage) RemoveRefresh(code string) error {
fmt.Printf("RemoveRefresh: %s\n", code)
delete(s.refresh, code)
return nil
}
func getCheesecakeRedirectUrl(hostname, port string) string {
fmt.Printf("Hostname is %s\n", hostname)
if hostname == "" {
if h, err := os.Hostname(); err == nil {
hostname = h
} else {
hostname = "localhost"
}
}
return fmt.Sprintf("http://%s:%s/login", hostname, port)
}