...
1
21
22 package openid
23
24 import (
25 "context"
26
27 "github.com/ory/x/errorsx"
28
29 "github.com/pkg/errors"
30
31 "github.com/ory/fosite"
32 )
33
34 func (c *OpenIDConnectExplicitHandler) HandleTokenEndpointRequest(ctx context.Context, request fosite.AccessRequester) error {
35 return errorsx.WithStack(fosite.ErrUnknownRequest)
36 }
37
38 func (c *OpenIDConnectExplicitHandler) PopulateTokenEndpointResponse(ctx context.Context, requester fosite.AccessRequester, responder fosite.AccessResponder) error {
39 if !c.CanHandleTokenEndpointRequest(requester) {
40 return errorsx.WithStack(fosite.ErrUnknownRequest)
41 }
42
43 authorize, err := c.OpenIDConnectRequestStorage.GetOpenIDConnectSession(ctx, requester.GetRequestForm().Get("code"), requester)
44 if errors.Is(err, ErrNoSessionFound) {
45 return errorsx.WithStack(fosite.ErrUnknownRequest.WithWrap(err).WithDebug(err.Error()))
46 } else if err != nil {
47 return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
48 }
49
50 if !authorize.GetGrantedScopes().Has("openid") {
51 return errorsx.WithStack(fosite.ErrMisconfiguration.WithDebug("An OpenID Connect session was found but the openid scope is missing, probably due to a broken code configuration."))
52 }
53
54 if !requester.GetClient().GetGrantTypes().Has("authorization_code") {
55 return errorsx.WithStack(fosite.ErrUnauthorizedClient.WithHint("The OAuth 2.0 Client is not allowed to use the authorization grant \"authorization_code\"."))
56 }
57
58 sess, ok := requester.GetSession().(Session)
59 if !ok {
60 return errorsx.WithStack(fosite.ErrServerError.WithDebug("Failed to generate id token because session must be of type fosite/handler/openid.Session."))
61 }
62
63 claims := sess.IDTokenClaims()
64 if claims.Subject == "" {
65 return errorsx.WithStack(fosite.ErrServerError.WithDebug("Failed to generate id token because subject is an empty string."))
66 }
67
68 claims.AccessTokenHash = c.GetAccessTokenHash(ctx, requester, responder)
69
70
71
72
73
74
75
76
77 return c.IssueExplicitIDToken(ctx, authorize, responder)
78 }
79
80 func (c *OpenIDConnectExplicitHandler) CanSkipClientAuth(requester fosite.AccessRequester) bool {
81 return false
82 }
83
84 func (c *OpenIDConnectExplicitHandler) CanHandleTokenEndpointRequest(requester fosite.AccessRequester) bool {
85 return requester.GetGrantTypes().ExactOne("authorization_code")
86 }
87
View as plain text