-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
287 lines (247 loc) · 8.26 KB
/
provider.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//** OAuth2.0 authorization service provider code built on osin library.
//
// It supports simple oauth2 authorization cycle with userinfo endpoint,
// and currently does not support jwt and oidc.
//
// osin library: https://github.com/RangelReale/osin
package actions
// TODO CHECK RFC AND IMPLEMENT AGAIN BUT NOT NOW
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/RangelReale/osin"
"github.com/gobuffalo/buffalo"
"github.com/hyeoncheon/uart/models"
"github.com/hyeoncheon/uart/utils"
)
var svr *osin.Server
var logger buffalo.Logger
const (
privKeyFile = "files/jwt.private.pem"
pubKeyFile = "files/jwt.public.pem"
)
func initProvider(l buffalo.Logger) {
logger = l
conf := osin.NewServerConfig()
conf.AccessExpiration = 60
svr = osin.NewServer(conf, newProvider())
keyFile := uartHome + "/" + privKeyFile
if _, err := os.Stat(keyFile); err != nil {
logger.Error("Abort! cannot found key file: $UART_HOME/", privKeyFile)
fmt.Println("Abort! cannot found key file: $UART_HOME/" + privKeyFile)
os.Exit(1)
}
svr.AccessTokenGen = utils.NewRS256AccessTokenGen(brandName, keyFile)
logger.Info("oauth2 provider with jwt support initialized!")
return
}
func authorizeHandler(c buffalo.Context) error {
resp := svr.NewResponse()
defer resp.Close()
logger = c.Logger().WithField("category", "oauth2")
logger.Debugf("---- authorize params ---- %v", c.Params())
if c.Param("client_id") == "" || c.Param("response_type") == "" {
logger.Error("invalid request!")
return c.Redirect(http.StatusBadRequest, "/")
}
user := currentMember(c)
if !user.IsActive {
c.Flash().Add("danger", t(c, "no.perm.inactive.member"))
return c.Redirect(http.StatusFound, "/membership/me")
}
if ar := svr.HandleAuthorizeRequest(resp, c.Request()); ar != nil {
app := ar.Client.(*AppClient).GetApp()
if ar.Authorized = user.Granted(app.ID, ar.Scope); !ar.Authorized {
logger.Warnf("request for grant: %v, %v, %v", app, user, ar.Scope)
c.Set("app", app)
c.Set("scope", ar.Scope)
c.Set("appkey", ar.Client.GetId())
c.Session().Set("origin", c.Request().RequestURI)
return c.Render(http.StatusOK, r.HTML("oauth2/grant.html"))
}
logger.Infof("app %v has grant for %v with scope %v", app, user, ar.Scope)
if strings.Contains(ar.Scope, "profile") {
ar.UserData = map[string]interface{}{
"user_id": user.ID,
"name": user.Name,
"email": user.Email,
"picture": user.Icon,
"phone_number": user.Mobile,
"roles": user.GetAppRoleCodes(app.Code),
}
}
svr.FinishAuthorizeRequest(resp, c.Request(), ar)
}
logger.Debugf("authorization response: --- %v ---", resp.Output)
if resp.IsError && resp.InternalError != nil {
c.Logger().Error("internal error: ", resp.InternalError)
c.Logger().Error("error: ", resp.ErrorId)
}
if !resp.IsError {
resp.Output["xtra"] = "uart"
}
return osin.OutputJSON(resp, c.Response(), c.Request())
}
func tokenHandler(c buffalo.Context) error {
resp := svr.NewResponse()
defer resp.Close()
logger = c.Logger().WithField("category", "oauth2")
if ar := svr.HandleAccessRequest(resp, c.Request()); ar != nil {
ar.Authorized = true
svr.FinishAccessRequest(resp, c.Request(), ar)
}
logger.Debugf("token response: --- %v ---", resp.Output)
if resp.IsError && resp.InternalError != nil {
c.Logger().Error("internal error: ", resp.InternalError)
c.Logger().Error("error: ", resp.ErrorId)
}
if !resp.IsError {
resp.Output["xtra"] = "uart"
}
return osin.OutputJSON(resp, c.Response(), c.Request())
}
func userInfoHandler(c buffalo.Context) error {
resp := svr.NewResponse()
defer resp.Close()
logger = c.Logger().WithField("category", "oauth2")
if ir := svr.HandleInfoRequest(resp, c.Request()); ir != nil {
if userdata, ok := ir.AccessData.UserData.(map[string]interface{}); ok {
resp.Output = userdata
}
}
logger.Debugf("userinfo response: --- %v ---", resp.Output)
return osin.OutputJSON(resp, c.Response(), c.Request())
}
//** Provider implementation ----------------------------------------------
// Provider handles authentication statuses and connections.
//
type Provider struct {
clients map[string]osin.Client
authorize map[string]*osin.AuthorizeData
access map[string]*osin.AccessData
refresh map[string]string
}
// AppClient is struct for storing client information
// It implements osin.Client by osin.DefaultClient and store app additionally.
type AppClient struct {
*osin.DefaultClient
app *models.App
}
// GetApp returns app model of the client.
func (c *AppClient) GetApp() *models.App {
return c.app
}
func newProvider() *Provider {
stg := &Provider{
clients: make(map[string]osin.Client), // TODO: for cache, later
authorize: make(map[string]*osin.AuthorizeData),
access: make(map[string]*osin.AccessData),
refresh: make(map[string]string),
}
return stg
}
// Clone just return it self.
func (s *Provider) Clone() osin.Storage {
return s
}
// Close does nothing because there is no clone or additional resources.
func (s *Provider) Close() {
for k, v := range s.access {
if v.IsExpired() {
logger.Debugf("delete expired ak for %v", v.Client.GetId()[0:8])
s.RemoveAccess(k)
}
}
for k, v := range s.authorize {
if v.IsExpired() {
logger.Debugf("rarely, delete expired auth_code %v", v.Code)
s.RemoveAuthorize(k)
}
}
}
// GetClient searches and returns osin.Client instance with clientID.
// Called by HandleAuthorizeRequest, HandleAccessRequest, ...
func (s *Provider) GetClient(clientID string) (osin.Client, error) {
logger.Debug("op.getclient invoked for: ", clientID[0:9])
app := models.GetAppByKey(clientID)
if app != nil {
return &AppClient{
DefaultClient: &osin.DefaultClient{
Id: app.AppKey,
Secret: app.AppSecret,
RedirectUri: app.CallbackURL,
},
app: app,
}, nil
}
return nil, osin.ErrNotFound
}
//** Functions for phase #1, authorize request ----------------------------
// SaveAuthorize stores authorization information into provider storage.
// Called by FinishAuthorizeRequest
func (s *Provider) SaveAuthorize(data *osin.AuthorizeData) error {
logger.Debug("op.save authorize for ", data.Code[0:9])
s.authorize[data.Code] = data
return nil
}
//** Functions for phase #2, access token ---------------------------------
// LoadAuthorize read and returns authorize information from provider storage.
// Called by HandleAccessRequest
func (s *Provider) LoadAuthorize(code string) (*osin.AuthorizeData, error) {
logger.Debug("op.load authorize for ", code[0:9])
if d, ok := s.authorize[code]; ok {
return d, nil
}
return nil, osin.ErrNotFound
}
// SaveAccess stores given access data on the provider storage.
// Called by FinishAccessRequest, before call RemoveAuthorize
func (s *Provider) SaveAccess(data *osin.AccessData) error {
logger.Debug("save access for ", data.AuthorizeData.Code[0:9])
s.access[data.AccessToken] = data
// TODO: do not store refresh token until fully implemented
if false && data.RefreshToken != "" {
s.refresh[data.RefreshToken] = data.AccessToken
}
return nil
}
// RemoveAuthorize delete authorize information from provider storage.
// Called by FinishAccessRequest, after call SaveAccess
func (s *Provider) RemoveAuthorize(code string) error {
logger.Debug("op.remove authorize for ", code[0:9])
delete(s.authorize, code)
return nil
}
//** Functions for resource accessing -------------------------------------
// LoadAccess read and returns access information from provider storage.
// Called by HandleInfoRequest to validate access token
func (s *Provider) LoadAccess(code string) (*osin.AccessData, error) {
logger.Debug("op.load access invoked for ", code[0:9])
if d, ok := s.access[code]; ok {
return d, nil
}
return nil, osin.ErrNotFound
}
// Others: Refresh Token related things maybe.
// LoadRefresh is not used yet
func (s *Provider) LoadRefresh(code string) (*osin.AccessData, error) {
logger.Debug("provider.loadrefresh invoked")
if d, ok := s.refresh[code]; ok {
return s.LoadAccess(d)
}
return nil, osin.ErrNotFound
}
// RemoveAccess is not used yet
func (s *Provider) RemoveAccess(code string) error {
logger.Debug("provider.removeaccess invoked")
delete(s.access, code)
return nil
}
// RemoveRefresh is not used yet
func (s *Provider) RemoveRefresh(code string) error {
logger.Debug("provider.removerefresh invoked")
delete(s.refresh, code)
return nil
}