forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
104 lines (87 loc) · 2.15 KB
/
request.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
package fosite
import (
"net/url"
"time"
"github.com/pborman/uuid"
)
// Request is an implementation of Requester
type Request struct {
ID string `json:"id" gorethink:"id"`
RequestedAt time.Time `json:"requestedAt" gorethink:"requestedAt"`
Client Client `json:"client" gorethink:"client"`
Scopes Arguments `json:"scopes" gorethink:"scopes"`
GrantedScopes Arguments `json:"grantedScopes" gorethink:"grantedScopes"`
Form url.Values `json:"form" gorethink:"form"`
Session Session `json:"session" gorethink:"session"`
}
func NewRequest() *Request {
return &Request{
Client: &DefaultClient{},
Scopes: Arguments{},
GrantedScopes: Arguments{},
Form: url.Values{},
RequestedAt: time.Now(),
}
}
func (a *Request) GetID() string {
if a.ID == "" {
a.ID = uuid.New()
}
return a.ID
}
func (a *Request) GetRequestForm() url.Values {
return a.Form
}
func (a *Request) GetRequestedAt() time.Time {
return a.RequestedAt
}
func (a *Request) GetClient() Client {
return a.Client
}
func (a *Request) GetRequestedScopes() Arguments {
return a.Scopes
}
func (a *Request) SetRequestedScopes(s Arguments) {
for _, scope := range s {
a.AppendRequestedScope(scope)
}
}
func (a *Request) AppendRequestedScope(scope string) {
for _, has := range a.Scopes {
if scope == has {
return
}
}
a.Scopes = append(a.Scopes, scope)
}
func (a *Request) GetGrantedScopes() Arguments {
return a.GrantedScopes
}
func (a *Request) GrantScope(scope string) {
for _, has := range a.GrantedScopes {
if scope == has {
return
}
}
a.GrantedScopes = append(a.GrantedScopes, scope)
}
func (a *Request) SetSession(session Session) {
a.Session = session
}
func (a *Request) GetSession() Session {
return a.Session
}
func (a *Request) Merge(request Requester) {
for _, scope := range request.GetRequestedScopes() {
a.AppendRequestedScope(scope)
}
for _, scope := range request.GetGrantedScopes() {
a.GrantScope(scope)
}
a.RequestedAt = request.GetRequestedAt()
a.Client = request.GetClient()
a.Session = request.GetSession()
for k, v := range request.GetRequestForm() {
a.Form[k] = v
}
}