-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization_request.go
74 lines (59 loc) · 1.8 KB
/
authorization_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
package auth
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/go-oauth2/oauth2/v4/errors"
)
func httpAuthError(w http.ResponseWriter, err error) {
http.Error(w, errors.Descriptions[err], errors.StatusCodes[err])
}
type authorizationRequestHandler struct {
clientByClientID clientByClientIDFn
createAuthorization func(ctx context.Context, client client, state string, codeChallenge string) error
loginUrl url.URL
}
func (auth authorizationRequestHandler) checkResponseType(response_type string) error {
if response_type != "code" {
return errors.ErrUnsupportedResponseType
}
return nil
}
func (auth authorizationRequestHandler) checkMethod(r *http.Request) error {
if !(r.Method == "GET" || r.Method == "POST") {
return errors.ErrInvalidRequest
}
return nil
}
func (auth authorizationRequestHandler) checkCodeChallengeMethod(r *http.Request) error {
if r.FormValue("code_challenge_method") != "S256" {
return errors.ErrInvalidRequest
}
return nil
}
func (auth authorizationRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// log.Printf("Query params: %#v", r.URL.Query())
if err := auth.checkMethod(r); err != nil {
httpAuthError(w, err)
return
}
if err := auth.checkResponseType(r.FormValue("response_type")); err != nil {
httpAuthError(w, err)
return
}
if err := auth.checkCodeChallengeMethod(r); err != nil {
httpAuthError(w, err)
return
}
client, err := auth.clientByClientID(r.Context(), r.FormValue("client_id"))
if err != nil {
httpAuthError(w, errors.ErrInvalidRequest)
return
}
if auth.createAuthorization(r.Context(), client, r.FormValue("state"), r.FormValue("code_challenge")); err != nil {
httpAuthError(w, errors.ErrInvalidRequest)
return
}
http.Redirect(w, r, fmt.Sprint(&auth.loginUrl), http.StatusSeeOther)
}