...

Source file src/github.com/ory/fosite/handler/openid/flow_implicit.go

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

     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 openid
    23  
    24  import (
    25  	"context"
    26  
    27  	"github.com/ory/x/errorsx"
    28  
    29  	"github.com/ory/fosite"
    30  	"github.com/ory/fosite/handler/oauth2"
    31  	"github.com/ory/fosite/token/jwt"
    32  )
    33  
    34  type OpenIDConnectImplicitHandler struct {
    35  	AuthorizeImplicitGrantTypeHandler *oauth2.AuthorizeImplicitGrantTypeHandler
    36  	*IDTokenHandleHelper
    37  	ScopeStrategy                 fosite.ScopeStrategy
    38  	OpenIDConnectRequestValidator *OpenIDConnectRequestValidator
    39  
    40  	RS256JWTStrategy *jwt.RS256JWTStrategy
    41  
    42  	MinParameterEntropy int
    43  }
    44  
    45  func (c *OpenIDConnectImplicitHandler) HandleAuthorizeEndpointRequest(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
    46  	if !(ar.GetGrantedScopes().Has("openid") && (ar.GetResponseTypes().Has("token", "id_token") || ar.GetResponseTypes().ExactOne("id_token"))) {
    47  		return nil
    48  	} else if ar.GetResponseTypes().Has("code") {
    49  		// hybrid flow
    50  		return nil
    51  	}
    52  
    53  	ar.SetDefaultResponseMode(fosite.ResponseModeFragment)
    54  
    55  	if !ar.GetClient().GetGrantTypes().Has("implicit") {
    56  		return errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("The OAuth 2.0 Client is not allowed to use the authorization grant 'implicit'."))
    57  	}
    58  
    59  	// Disabled because this is already handled at the authorize_request_handler
    60  	//if ar.GetResponseTypes().ExactOne("id_token") && !ar.GetClient().GetResponseTypes().Has("id_token") {
    61  	//	return errorsx.WithStack(fosite.ErrInvalidGrant.WithDebug("The client is not allowed to use response type id_token"))
    62  	//} else if ar.GetResponseTypes().Matches("token", "id_token") && !ar.GetClient().GetResponseTypes().Has("token", "id_token") {
    63  	//	return errorsx.WithStack(fosite.ErrInvalidGrant.WithDebug("The client is not allowed to use response type token and id_token"))
    64  	//}
    65  
    66  	if nonce := ar.GetRequestForm().Get("nonce"); len(nonce) == 0 {
    67  		return errorsx.WithStack(fosite.ErrInvalidRequest.WithHint("Parameter 'nonce' must be set when using the OpenID Connect Implicit Flow."))
    68  	} else if len(nonce) < c.MinParameterEntropy {
    69  		return errorsx.WithStack(fosite.ErrInsufficientEntropy.WithHintf("Parameter 'nonce' is set but does not satisfy the minimum entropy of %d characters.", c.MinParameterEntropy))
    70  	}
    71  
    72  	client := ar.GetClient()
    73  	for _, scope := range ar.GetRequestedScopes() {
    74  		if !c.ScopeStrategy(client.GetScopes(), scope) {
    75  			return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope))
    76  		}
    77  	}
    78  
    79  	sess, ok := ar.GetSession().(Session)
    80  	if !ok {
    81  		return errorsx.WithStack(ErrInvalidSession)
    82  	}
    83  
    84  	if err := c.OpenIDConnectRequestValidator.ValidatePrompt(ctx, ar); err != nil {
    85  		return err
    86  	}
    87  
    88  	claims := sess.IDTokenClaims()
    89  	if ar.GetResponseTypes().Has("token") {
    90  		if err := c.AuthorizeImplicitGrantTypeHandler.IssueImplicitAccessToken(ctx, ar, resp); err != nil {
    91  			return errorsx.WithStack(err)
    92  		}
    93  
    94  		ar.SetResponseTypeHandled("token")
    95  		hash, err := c.ComputeHash(ctx, sess, resp.GetParameters().Get("access_token"))
    96  		if err != nil {
    97  			return err
    98  		}
    99  
   100  		claims.AccessTokenHash = hash
   101  	} else {
   102  		resp.AddParameter("state", ar.GetState())
   103  	}
   104  
   105  	if err := c.IssueImplicitIDToken(ctx, ar, resp); err != nil {
   106  		return errorsx.WithStack(err)
   107  	}
   108  
   109  	// there is no need to check for https, because implicit flow does not require https
   110  	// https://tools.ietf.org/html/rfc6819#section-4.4.2
   111  
   112  	ar.SetResponseTypeHandled("id_token")
   113  	return nil
   114  }
   115  

View as plain text