forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_endpoints_test.go
172 lines (144 loc) · 5.12 KB
/
helper_endpoints_test.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
/*
* Copyright © 2015-2018 Aeneas Rekkas <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <[email protected]>
* @copyright 2015-2018 Aeneas Rekkas <[email protected]>
* @license Apache-2.0
*
*/
package integration_test
import (
"encoding/json"
"net/http"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/ory/fosite"
"github.com/ory/fosite/handler/oauth2"
)
type stackTracer interface {
StackTrace() errors.StackTrace
}
func tokenRevocationHandler(t *testing.T, oauth2 fosite.OAuth2Provider, session fosite.Session) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
ctx := fosite.NewContext()
err := oauth2.NewRevocationRequest(ctx, req)
if err != nil {
t.Logf("Revoke request failed because %+v", err)
}
oauth2.WriteRevocationResponse(rw, err)
}
}
func tokenIntrospectionHandler(t *testing.T, oauth2 fosite.OAuth2Provider, session fosite.Session) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
ctx := fosite.NewContext()
ar, err := oauth2.NewIntrospectionRequest(ctx, req, session)
if err != nil {
t.Logf("Introspection request failed because: %+v", err)
oauth2.WriteIntrospectionError(rw, err)
return
}
oauth2.WriteIntrospectionResponse(rw, ar)
}
}
func tokenInfoHandler(t *testing.T, oauth2 fosite.OAuth2Provider, session fosite.Session) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
ctx := fosite.NewContext()
_, resp, err := oauth2.IntrospectToken(ctx, fosite.AccessTokenFromRequest(req), fosite.AccessToken, session)
if err != nil {
t.Logf("Info request failed because: %+v", err)
http.Error(rw, errors.Cause(err).(*fosite.RFC6749Error).Description, errors.Cause(err).(*fosite.RFC6749Error).Code)
return
}
t.Logf("Introspecting caused: %+v", resp)
if err := json.NewEncoder(rw).Encode(resp); err != nil {
panic(err)
}
}
}
func authEndpointHandler(t *testing.T, oauth2 fosite.OAuth2Provider, session fosite.Session) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
ctx := fosite.NewContext()
ar, err := oauth2.NewAuthorizeRequest(ctx, req)
if err != nil {
t.Logf("Access request failed because: %+v", err)
t.Logf("Request: %+v", ar)
oauth2.WriteAuthorizeError(rw, ar, err)
return
}
if ar.GetRequestedScopes().Has("fosite") {
ar.GrantScope("fosite")
}
if ar.GetRequestedScopes().Has("offline") {
ar.GrantScope("offline")
}
if ar.GetRequestedScopes().Has("openid") {
ar.GrantScope("openid")
}
for _, a := range ar.GetRequestedAudience() {
ar.GrantAudience(a)
}
// Normally, this would be the place where you would check if the user is logged in and gives his consent.
// For this test, let's assume that the user exists, is logged in, and gives his consent...
response, err := oauth2.NewAuthorizeResponse(ctx, ar, session)
if err != nil {
t.Logf("Access request failed because: %+v", err)
t.Logf("Request: %+v", ar)
oauth2.WriteAuthorizeError(rw, ar, err)
return
}
oauth2.WriteAuthorizeResponse(rw, ar, response)
}
}
func authCallbackHandler(t *testing.T) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
if q.Get("code") == "" && q.Get("error") == "" {
assert.NotEmpty(t, q.Get("code"))
assert.NotEmpty(t, q.Get("error"))
}
if q.Get("code") != "" {
rw.Write([]byte("code: ok"))
}
if q.Get("error") != "" {
rw.WriteHeader(http.StatusNotAcceptable)
rw.Write([]byte("error: " + q.Get("error")))
}
}
}
func tokenEndpointHandler(t *testing.T, provider fosite.OAuth2Provider) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
req.ParseMultipartForm(1 << 20)
ctx := fosite.NewContext()
accessRequest, err := provider.NewAccessRequest(ctx, req, &oauth2.JWTSession{})
if err != nil {
t.Logf("Access request failed because: %+v", err)
t.Logf("Request: %+v", accessRequest)
provider.WriteAccessError(rw, accessRequest, err)
return
}
if accessRequest.GetRequestedScopes().Has("fosite") {
accessRequest.GrantScope("fosite")
}
response, err := provider.NewAccessResponse(ctx, accessRequest)
if err != nil {
t.Logf("Access request failed because: %+v", err)
t.Logf("Request: %+v", accessRequest)
provider.WriteAccessError(rw, accessRequest, err)
return
}
provider.WriteAccessResponse(rw, accessRequest, response)
}
}