-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
helper_endpoints_test.go
177 lines (146 loc) · 5.3 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
173
174
175
176
177
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package integration_test
import (
"encoding/json"
"net/http"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/fosite"
"github.com/ory/fosite/handler/oauth2"
)
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(req.Context(), 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(req.Context(), rw, err)
return
}
oauth2.WriteIntrospectionResponse(req.Context(), 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)
var e *fosite.RFC6749Error
require.True(t, errors.As(err, &e))
http.Error(rw, e.DescriptionField, e.CodeField)
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(req.Context(), 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(req.Context(), rw, ar, err)
return
}
oauth2.WriteAuthorizeResponse(req.Context(), 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(req.Context(), 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(req.Context(), rw, accessRequest, err)
return
}
provider.WriteAccessResponse(req.Context(), rw, accessRequest, response)
}
}
func pushedAuthorizeRequestHandler(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.NewPushedAuthorizeRequest(ctx, req)
if err != nil {
t.Logf("PAR request failed because: %+v", err)
t.Logf("Request: %+v", ar)
oauth2.WritePushedAuthorizeError(ctx, rw, ar, err)
return
}
response, err := oauth2.NewPushedAuthorizeResponse(ctx, ar, session)
if err != nil {
t.Logf("PAR response failed because: %+v", err)
t.Logf("Request: %+v", ar)
oauth2.WritePushedAuthorizeError(ctx, rw, ar, err)
return
}
oauth2.WritePushedAuthorizeResponse(ctx, rw, ar, response)
}
}