...
1
21
22 package oauth2
23
24 import (
25 "context"
26 "time"
27
28 "github.com/ory/x/errorsx"
29
30 "github.com/ory/fosite"
31 )
32
33 type ClientCredentialsGrantHandler struct {
34 *HandleHelper
35 ScopeStrategy fosite.ScopeStrategy
36 AudienceMatchingStrategy fosite.AudienceMatchingStrategy
37 }
38
39
40 func (c *ClientCredentialsGrantHandler) HandleTokenEndpointRequest(_ context.Context, request fosite.AccessRequester) error {
41 if !c.CanHandleTokenEndpointRequest(request) {
42 return errorsx.WithStack(fosite.ErrUnknownRequest)
43 }
44
45 client := request.GetClient()
46 for _, scope := range request.GetRequestedScopes() {
47 if !c.ScopeStrategy(client.GetScopes(), scope) {
48 return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope))
49 }
50 }
51
52 if err := c.AudienceMatchingStrategy(client.GetAudience(), request.GetRequestedAudience()); err != nil {
53 return err
54 }
55
56
57
58
59 if client.IsPublic() {
60 return errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("The OAuth 2.0 Client is marked as public and is thus not allowed to use authorization grant 'client_credentials'."))
61 }
62
63
64 request.GetSession().SetExpiresAt(fosite.AccessToken, time.Now().UTC().Add(c.AccessTokenLifespan))
65 return nil
66 }
67
68
69 func (c *ClientCredentialsGrantHandler) PopulateTokenEndpointResponse(ctx context.Context, request fosite.AccessRequester, response fosite.AccessResponder) error {
70 if !c.CanHandleTokenEndpointRequest(request) {
71 return errorsx.WithStack(fosite.ErrUnknownRequest)
72 }
73
74 if !request.GetClient().GetGrantTypes().Has("client_credentials") {
75 return errorsx.WithStack(fosite.ErrUnauthorizedClient.WithHint("The OAuth 2.0 Client is not allowed to use authorization grant 'client_credentials'."))
76 }
77
78 return c.IssueAccessToken(ctx, request, response)
79 }
80
81 func (c *ClientCredentialsGrantHandler) CanSkipClientAuth(requester fosite.AccessRequester) bool {
82 return false
83 }
84
85 func (c *ClientCredentialsGrantHandler) CanHandleTokenEndpointRequest(requester fosite.AccessRequester) bool {
86
87
88 return requester.GetGrantTypes().ExactOne("client_credentials")
89 }
90
View as plain text