-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpushed_authorize_response_writer.go
91 lines (73 loc) · 3.15 KB
/
pushed_authorize_response_writer.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"context"
"encoding/json"
"fmt"
"net/http"
"authelia.com/provider/oauth2/internal/consts"
"authelia.com/provider/oauth2/x/errorsx"
)
// NewPushedAuthorizeResponse executes the handlers and builds the response
func (f *Fosite) NewPushedAuthorizeResponse(ctx context.Context, requester AuthorizeRequester, session Session) (responder PushedAuthorizeResponder, err error) {
// Get handlers. If no handlers are defined, this is considered a misconfigured Fosite instance.
provider, ok := f.Config.(PushedAuthorizeRequestHandlersProvider)
if !ok {
return nil, errorsx.WithStack(ErrServerError.WithHint(ErrorPARNotSupported).WithDebug(DebugPARRequestsHandlerMissing))
}
var response = &PushedAuthorizeResponse{
Header: http.Header{},
Extra: map[string]any{},
}
ctx = context.WithValue(ctx, AuthorizeRequestContextKey, requester)
ctx = context.WithValue(ctx, PushedAuthorizeResponseContextKey, response)
requester.SetSession(session)
for _, h := range provider.GetPushedAuthorizeEndpointHandlers(ctx) {
if err = h.HandlePushedAuthorizeEndpointRequest(ctx, requester, response); err != nil {
return nil, err
}
}
return response, nil
}
// WritePushedAuthorizeResponse writes the PAR response
func (f *Fosite) WritePushedAuthorizeResponse(ctx context.Context, rw http.ResponseWriter, requester AuthorizeRequester, responder PushedAuthorizeResponder) {
// Set custom headers, e.g. "X-MySuperCoolCustomHeader" or "X-DONT-CACHE-ME"...
wh := rw.Header()
rh := responder.GetHeader()
for k := range rh {
wh.Set(k, rh.Get(k))
}
wh.Set(consts.HeaderCacheControl, consts.CacheControlNoStore)
wh.Set(consts.HeaderPragma, consts.PragmaNoCache)
wh.Set(consts.HeaderContentType, consts.ContentTypeApplicationJSON)
js, err := json.Marshal(responder.ToMap())
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.Header().Set(consts.HeaderContentType, consts.ContentTypeApplicationJSON)
rw.WriteHeader(http.StatusCreated)
_, _ = rw.Write(js)
}
// WritePushedAuthorizeError writes the PAR error
func (f *Fosite) WritePushedAuthorizeError(ctx context.Context, rw http.ResponseWriter, requester AuthorizeRequester, err error) {
rw.Header().Set(consts.HeaderCacheControl, consts.CacheControlNoStore)
rw.Header().Set(consts.HeaderPragma, consts.PragmaNoCache)
rw.Header().Set(consts.HeaderContentType, consts.ContentTypeApplicationJSON)
sendDebugMessagesToClient := f.Config.GetSendDebugMessagesToClients(ctx)
rfcerr := ErrorToRFC6749Error(err).WithLegacyFormat(f.Config.GetUseLegacyErrorFormat(ctx)).
WithExposeDebug(sendDebugMessagesToClient).WithLocalizer(f.Config.GetMessageCatalog(ctx), getLangFromRequester(requester))
js, err := json.Marshal(rfcerr)
if err != nil {
if sendDebugMessagesToClient {
errorMessage := EscapeJSONString(err.Error())
http.Error(rw, fmt.Sprintf(`{"error":"server_error","error_description":"%s"}`, errorMessage), http.StatusInternalServerError)
} else {
http.Error(rw, `{"error":"server_error"}`, http.StatusInternalServerError)
}
return
}
rw.WriteHeader(rfcerr.CodeField)
_, _ = rw.Write(js)
}