This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathapiutil.go
More file actions
181 lines (141 loc) · 3.69 KB
/
apiutil.go
File metadata and controls
181 lines (141 loc) · 3.69 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package apiutil
import (
"sync"
"time"
"github.com/koding/kite"
"koding/api"
"koding/kites/config"
"koding/kites/kloud/stack"
"koding/klient/storage"
)
// Kite is an interface for kite.Kite, that is used
// to make RPC calls.
type Kite interface {
Call(method string, req, resp interface{}) error
}
// LazyKite is a wrapper for Client that dials the
// kite on the first request.
type LazyKite struct {
Client *kite.Client // kite client to use; required
DialTimeout time.Duration // max dial time; 30s by default
CallTimeout time.Duration // max call time; 60s by default
mu sync.Mutex
dialed bool
}
var _ Kite = (*LazyKite)(nil)
// Call implements the Kite interface.
func (lk *LazyKite) Call(method string, req, resp interface{}) error {
if err := lk.init(); err != nil {
return err
}
r, err := lk.Client.TellWithTimeout(method, lk.callTimeout(), req)
if err != nil {
return err
}
if resp != nil {
return r.Unmarshal(resp)
}
return nil
}
func (lk *LazyKite) init() error {
lk.mu.Lock()
defer lk.mu.Unlock()
if !lk.dialed {
if err := lk.Client.DialTimeout(lk.dialTimeout()); err != nil {
return err
}
lk.dialed = true
}
return nil
}
func (lk *LazyKite) dialTimeout() time.Duration {
if lk.DialTimeout != 0 {
return lk.DialTimeout
}
return 30 * time.Second
}
func (lk *LazyKite) callTimeout() time.Duration {
if lk.CallTimeout != 0 {
return lk.CallTimeout
}
return 60 * time.Second
}
// KloudAuth provides api.AuthFunc that is
// backed by kite RPC.
type KloudAuth struct {
Kite Kite // kite transport to use; required
Storage api.Storage // storage for cache to use; optional
once sync.Once
auth api.AuthFunc
}
var _ api.AuthFunc = (&KloudAuth{}).Auth
func (ka *KloudAuth) init() {
ka.once.Do(ka.initAuth)
}
func (ka *KloudAuth) initAuth() {
if ka.Storage != nil {
cache := api.NewCache(ka.rpcAuth)
cache.Storage = ka.Storage
ka.auth = cache.Auth
} else {
ka.auth = ka.rpcAuth
}
}
// Auth obtains user session by calling "auth.login" method over Kite transport.
func (ka *KloudAuth) Auth(opts *api.AuthOptions) (*api.Session, error) {
ka.init()
return ka.auth(opts)
}
func (ka *KloudAuth) rpcAuth(opts *api.AuthOptions) (*api.Session, error) {
var req = &stack.LoginRequest{GroupName: opts.User.Team}
var resp stack.LoginResponse
if err := ka.Kite.Call("auth.login", req, &resp); err != nil {
return nil, err
}
return &api.Session{
ClientID: resp.ClientID,
User: &api.User{
Username: resp.Username,
Team: resp.GroupName,
},
}, nil
}
// Storage is wrapper for config.Cache that implements api.Storage.
type Storage struct {
Cache *config.Cache
}
var _ api.Storage = (*Storage)(nil)
// Get implements the api.Storage interface.
func (st *Storage) Get(u *api.User) (*api.Session, error) {
var sessions map[string]api.Session
err := st.Cache.GetValue("auth.sessions", &sessions)
if err == storage.ErrKeyNotFound {
return nil, api.ErrSessionNotFound
}
if err != nil {
return nil, err
}
session, ok := sessions[u.String()]
if !ok {
return nil, api.ErrSessionNotFound
}
return &session, nil
}
// Set implements the api.Storage interface.
func (st *Storage) Set(s *api.Session) error {
sessions := make(map[string]api.Session)
if err := st.Cache.GetValue("auth.sessions", &sessions); err != nil {
return err
}
sessions[s.User.String()] = *s
return st.Cache.SetValue("auth.sessions", sessions)
}
// Delete implements the api.Storage interface.
func (st *Storage) Delete(s *api.Session) error {
sessions := make(map[string]api.Session)
if err := st.Cache.GetValue("auth.sessions", &sessions); err != nil {
return err
}
delete(sessions, s.User.String())
return st.Cache.SetValue("auth.sessions", sessions)
}