-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest.handler.go
More file actions
39 lines (30 loc) · 787 Bytes
/
request.handler.go
File metadata and controls
39 lines (30 loc) · 787 Bytes
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
package main
import (
"github.com/gin-gonic/gin"
"github.com/RangelReale/osin"
"errors"
)
type RequestHandler interface {
GetUser(c *gin.Context) (*User, error)
}
type APIRequestHandler struct{}
func NewRequestHandler() RequestHandler {
return &APIRequestHandler{}
}
func (h *APIRequestHandler) GetUser(c *gin.Context) (*User, error) {
var isToken bool
var token *osin.AccessData
t, exists := c.Get("token")
if !exists {
return nil, errors.New("Token does not exist")
}
if token, isToken = t.(*osin.AccessData); !isToken {
return nil, errors.New("Could not assert token is *osin.AccessData")
}
var isUser bool
var user *User
if user, isUser = token.UserData.(*User); !isUser {
return nil, errors.New("Could not assert user is *User")
}
return user, nil
}