-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
139 lines (115 loc) · 3.65 KB
/
handler.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
package oauth2
import (
"net/http"
"net/url"
"github.com/go-errors/errors"
"github.com/julienschmidt/httprouter"
"github.com/ory-am/fosite"
"github.com/ory-am/hydra/pkg"
)
const (
OpenIDConnectKeyName = "hydra.openid.connect"
)
type Handler struct {
OAuth2 fosite.OAuth2Provider
Consent ConsentStrategy
ForcedHTTP bool
ConsentURL url.URL
}
func (h *Handler) SetRoutes(r *httprouter.Router) {
r.POST("/oauth2/token", h.TokenHandler)
r.GET("/oauth2/auth", h.AuthHandler)
r.POST("/oauth2/auth", h.AuthHandler)
r.GET("/oauth2/consent", h.DefaultConsentHandler)
}
func (o *Handler) TokenHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var session = NewSession("")
var ctx = fosite.NewContext()
accessRequest, err := o.OAuth2.NewAccessRequest(ctx, r, session)
if err != nil {
pkg.LogError(err)
o.OAuth2.WriteAccessError(w, accessRequest, err)
return
}
if accessRequest.GetGrantTypes().Exact("client_credentials") {
session.Subject = accessRequest.GetClient().GetID()
for _, scope := range accessRequest.GetRequestedScopes() {
if fosite.HierarchicScopeStrategy(accessRequest.GetClient().GetScopes(), scope) {
accessRequest.GrantScope(scope)
}
}
}
accessResponse, err := o.OAuth2.NewAccessResponse(ctx, r, accessRequest)
if err != nil {
pkg.LogError(err)
o.OAuth2.WriteAccessError(w, accessRequest, err)
return
}
o.OAuth2.WriteAccessResponse(w, accessRequest, accessResponse)
}
func (o *Handler) AuthHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var ctx = fosite.NewContext()
authorizeRequest, err := o.OAuth2.NewAuthorizeRequest(ctx, r)
if err != nil {
pkg.LogError(err)
o.writeAuthorizeError(w, authorizeRequest, err)
return
}
// A session_token will be available if the user was authenticated an gave consent
consentToken := authorizeRequest.GetRequestForm().Get("consent")
if consentToken == "" {
// otherwise redirect to log in endpoint
if err := o.redirectToConsent(w, r, authorizeRequest); err != nil {
pkg.LogError(err)
o.writeAuthorizeError(w, authorizeRequest, err)
return
}
return
}
// decode consent_token claims
// verify anti-CSRF (inject state) and anti-replay token (expiry time, good value would be 10 seconds)
session, err := o.Consent.ValidateResponse(authorizeRequest, consentToken)
if err != nil {
pkg.LogError(err)
o.writeAuthorizeError(w, authorizeRequest, errors.New(fosite.ErrAccessDenied))
return
}
// done
response, err := o.OAuth2.NewAuthorizeResponse(ctx, r, authorizeRequest, session)
if err != nil {
pkg.LogError(err)
o.writeAuthorizeError(w, authorizeRequest, err)
return
}
o.OAuth2.WriteAuthorizeResponse(w, authorizeRequest, response)
}
func (o *Handler) redirectToConsent(w http.ResponseWriter, r *http.Request, authorizeRequest fosite.AuthorizeRequester) error {
schema := "https"
if o.ForcedHTTP {
schema = "http"
}
challenge, err := o.Consent.IssueChallenge(authorizeRequest, schema+"://"+r.Host+r.URL.String())
if err != nil {
return err
}
p := o.ConsentURL
q := p.Query()
q.Set("challenge", challenge)
p.RawQuery = q.Encode()
http.Redirect(w, r, p.String(), http.StatusFound)
return nil
}
func (o *Handler) writeAuthorizeError(w http.ResponseWriter, ar fosite.AuthorizeRequester, err error) {
if !ar.IsRedirectURIValid() {
var rfcerr = fosite.ErrorToRFC6749Error(err)
redirectURI := o.ConsentURL
query := redirectURI.Query()
query.Add("error", rfcerr.Name)
query.Add("error_description", rfcerr.Description)
redirectURI.RawQuery = query.Encode()
w.Header().Add("Location", redirectURI.String())
w.WriteHeader(http.StatusFound)
return
}
o.OAuth2.WriteAuthorizeError(w, ar, err)
}