...
1
21
22 package fosite
23
24 import (
25 "context"
26 "encoding/json"
27 "fmt"
28 "net/http"
29
30 "github.com/ory/x/errorsx"
31
32 "github.com/pkg/errors"
33 )
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 func (f *Fosite) NewRevocationRequest(ctx context.Context, r *http.Request) error {
53 ctx = context.WithValue(ctx, RequestContextKey, r)
54
55 if r.Method != "POST" {
56 return errorsx.WithStack(ErrInvalidRequest.WithHintf("HTTP method is '%s' but expected 'POST'.", r.Method))
57 } else if err := r.ParseMultipartForm(1 << 20); err != nil && err != http.ErrNotMultipart {
58 return errorsx.WithStack(ErrInvalidRequest.WithHint("Unable to parse HTTP body, make sure to send a properly formatted form request body.").WithWrap(err).WithDebug(err.Error()))
59 } else if len(r.PostForm) == 0 {
60 return errorsx.WithStack(ErrInvalidRequest.WithHint("The POST body can not be empty."))
61 }
62
63 client, err := f.AuthenticateClient(ctx, r, r.PostForm)
64 if err != nil {
65 return err
66 }
67
68 token := r.PostForm.Get("token")
69 tokenTypeHint := TokenType(r.PostForm.Get("token_type_hint"))
70
71 var found = false
72 for _, loader := range f.RevocationHandlers {
73 if err := loader.RevokeToken(ctx, token, tokenTypeHint, client); err == nil {
74 found = true
75 } else if errors.Is(err, ErrUnknownRequest) {
76
77 } else if err != nil {
78 return err
79 }
80 }
81
82 if !found {
83 return errorsx.WithStack(ErrInvalidRequest)
84 }
85
86 return nil
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100 func (f *Fosite) WriteRevocationResponse(rw http.ResponseWriter, err error) {
101 rw.Header().Set("Cache-Control", "no-store")
102 rw.Header().Set("Pragma", "no-cache")
103
104 if err == nil {
105 rw.WriteHeader(http.StatusOK)
106 return
107 }
108
109 if errors.Is(err, ErrInvalidRequest) {
110 rw.Header().Set("Content-Type", "application/json;charset=UTF-8")
111
112 js, err := json.Marshal(ErrInvalidRequest)
113 if err != nil {
114 http.Error(rw, fmt.Sprintf(`{"error": "%s"}`, err.Error()), http.StatusInternalServerError)
115 return
116 }
117
118 rw.WriteHeader(ErrInvalidRequest.CodeField)
119 _, _ = rw.Write(js)
120 } else if errors.Is(err, ErrInvalidClient) {
121 rw.Header().Set("Content-Type", "application/json;charset=UTF-8")
122
123 js, err := json.Marshal(ErrInvalidClient)
124 if err != nil {
125 http.Error(rw, fmt.Sprintf(`{"error": "%s"}`, err.Error()), http.StatusInternalServerError)
126 return
127 }
128
129 rw.WriteHeader(ErrInvalidClient.CodeField)
130 _, _ = rw.Write(js)
131 } else {
132
133 rw.WriteHeader(http.StatusOK)
134 }
135 }
136
View as plain text