...

Source file src/github.com/ory/fosite/handler/oauth2/flow_client_credentials.go

Documentation: github.com/ory/fosite/handler/oauth2

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    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  // IntrospectTokenEndpointRequest implements https://tools.ietf.org/html/rfc6749#section-4.4.2
    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  	// The client MUST authenticate with the authorization server as described in Section 3.2.1.
    57  	// This requirement is already fulfilled because fosite requires all token requests to be authenticated as described
    58  	// in https://tools.ietf.org/html/rfc6749#section-3.2.1
    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  	// if the client is not public, he has already been authenticated by the access request handler.
    63  
    64  	request.GetSession().SetExpiresAt(fosite.AccessToken, time.Now().UTC().Add(c.AccessTokenLifespan))
    65  	return nil
    66  }
    67  
    68  // PopulateTokenEndpointResponse implements https://tools.ietf.org/html/rfc6749#section-4.4.3
    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  	// grant_type REQUIRED.
    87  	// Value MUST be set to "client_credentials".
    88  	return requester.GetGrantTypes().ExactOne("client_credentials")
    89  }
    90  

View as plain text