forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_hybrid.go
105 lines (85 loc) · 3.61 KB
/
flow_hybrid.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
package openid
import (
"net/http"
"fmt"
"github.com/ory-am/fosite"
"github.com/ory-am/fosite/handler/oauth2"
"github.com/ory-am/fosite/token/jwt"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
type OpenIDConnectHybridHandler struct {
AuthorizeImplicitGrantTypeHandler *oauth2.AuthorizeImplicitGrantTypeHandler
AuthorizeExplicitGrantHandler *oauth2.AuthorizeExplicitGrantHandler
IDTokenHandleHelper *IDTokenHandleHelper
ScopeStrategy fosite.ScopeStrategy
Enigma *jwt.RS256JWTStrategy
}
func (c *OpenIDConnectHybridHandler) HandleAuthorizeEndpointRequest(ctx context.Context, req *http.Request, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
if len(ar.GetResponseTypes()) < 2 {
return nil
}
if !(ar.GetResponseTypes().Matches("token", "id_token", "code") || ar.GetResponseTypes().Matches("token", "code")) {
return nil
}
if !ar.GetClient().GetResponseTypes().Has("token", "code") {
return errors.Wrap(fosite.ErrInvalidGrant, "The client is not allowed to use the code and token response type")
} else if ar.GetResponseTypes().Matches("id_token") && !ar.GetClient().GetResponseTypes().Has("id_token") {
return errors.Wrap(fosite.ErrInvalidGrant, "The client is not allowed to use the id_token response type")
}
sess, ok := ar.GetSession().(Session)
if !ok {
return errors.Wrap(ErrInvalidSession, "")
}
client := ar.GetClient()
for _, scope := range ar.GetRequestedScopes() {
if !c.ScopeStrategy(client.GetScopes(), scope) {
return errors.Wrap(fosite.ErrInvalidScope, fmt.Sprintf("The client is not allowed to request scope %s", scope))
}
}
claims := sess.IDTokenClaims()
if ar.GetResponseTypes().Has("code") {
if !ar.GetClient().GetGrantTypes().Has("authorization_code") {
return errors.Wrap(fosite.ErrInvalidGrant, "The client is not allowed to use the authorization_code grant type")
}
code, signature, err := c.AuthorizeExplicitGrantHandler.AuthorizeCodeStrategy.GenerateAuthorizeCode(ctx, ar)
if err != nil {
return errors.Wrap(fosite.ErrServerError, err.Error())
} else if err := c.AuthorizeExplicitGrantHandler.AuthorizeCodeGrantStorage.CreateAuthorizeCodeSession(ctx, signature, ar); err != nil {
return errors.Wrap(fosite.ErrServerError, err.Error())
}
resp.AddFragment("code", code)
resp.AddFragment("state", ar.GetState())
ar.SetResponseTypeHandled("code")
hash, err := c.Enigma.Hash([]byte(resp.GetFragment().Get("code")))
if err != nil {
return err
}
claims.CodeHash = hash[:c.Enigma.GetSigningMethodLength()/2]
}
if ar.GetResponseTypes().Has("token") {
if !ar.GetClient().GetGrantTypes().Has("implicit") {
return errors.Wrap(fosite.ErrInvalidGrant, "The client is not allowed to use the implicit grant type")
} else if err := c.AuthorizeImplicitGrantTypeHandler.IssueImplicitAccessToken(ctx, req, ar, resp); err != nil {
return errors.Wrap(err, err.Error())
}
ar.SetResponseTypeHandled("token")
hash, err := c.Enigma.Hash([]byte(resp.GetFragment().Get("access_token")))
if err != nil {
return err
}
claims.AccessTokenHash = hash[:c.Enigma.GetSigningMethodLength()/2]
}
if !ar.GetGrantedScopes().Has("openid") {
return nil
}
if err := c.IDTokenHandleHelper.IssueImplicitIDToken(ctx, req, ar, resp); err != nil {
return errors.Wrap(err, err.Error())
} else if err := c.IDTokenHandleHelper.IssueImplicitIDToken(ctx, req, ar, resp); err != nil {
return errors.Wrap(err, err.Error())
}
// there is no need to check for https, because implicit flow does not require https
// https://tools.ietf.org/html/rfc6819#section-4.4.2
ar.SetResponseTypeHandled("id_token")
return nil
}