generated from resonatecoop/id-server-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
54 lines (43 loc) · 1.15 KB
/
context.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
package web
import (
"errors"
"net/http"
"github.com/gorilla/context"
"github.com/resonatecoop/id/session"
"github.com/resonatecoop/user-api/model"
)
type contextKey int
const (
sessionServiceKey contextKey = 0
clientKey contextKey = 1
)
var (
// ErrSessionServiceNotPresent ...
ErrSessionServiceNotPresent = errors.New("Session service not present in the request context")
// ErrClientNotPresent ...
ErrClientNotPresent = errors.New("Client not present in the request context")
)
// Returns *session.Service from the request context
func getSessionService(r *http.Request) (session.ServiceInterface, error) {
val, ok := context.GetOk(r, sessionServiceKey)
if !ok {
return nil, ErrSessionServiceNotPresent
}
sessionService, ok := val.(session.ServiceInterface)
if !ok {
return nil, ErrSessionServiceNotPresent
}
return sessionService, nil
}
// Returns *oauth.Client from the request context
func getClient(r *http.Request) (*model.Client, error) {
val, ok := context.GetOk(r, clientKey)
if !ok {
return nil, ErrClientNotPresent
}
client, ok := val.(*model.Client)
if !ok {
return nil, ErrClientNotPresent
}
return client, nil
}