...

Source file src/github.com/ory/fosite/handler/oauth2/flow_authorize_implicit.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  	"strconv"
    27  	"strings"
    28  	"time"
    29  
    30  	"github.com/ory/x/errorsx"
    31  
    32  	"github.com/ory/fosite"
    33  )
    34  
    35  // AuthorizeImplicitGrantTypeHandler is a response handler for the Authorize Code grant using the implicit grant type
    36  // as defined in https://tools.ietf.org/html/rfc6749#section-4.2
    37  type AuthorizeImplicitGrantTypeHandler struct {
    38  	AccessTokenStrategy AccessTokenStrategy
    39  
    40  	// AccessTokenStorage is used to persist session data across requests.
    41  	AccessTokenStorage AccessTokenStorage
    42  
    43  	// AccessTokenLifespan defines the lifetime of an access token.
    44  	AccessTokenLifespan time.Duration
    45  
    46  	ScopeStrategy            fosite.ScopeStrategy
    47  	AudienceMatchingStrategy fosite.AudienceMatchingStrategy
    48  }
    49  
    50  func (c *AuthorizeImplicitGrantTypeHandler) HandleAuthorizeEndpointRequest(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
    51  	// This let's us define multiple response types, for example open id connect's id_token
    52  	if !ar.GetResponseTypes().ExactOne("token") {
    53  		return nil
    54  	}
    55  
    56  	ar.SetDefaultResponseMode(fosite.ResponseModeFragment)
    57  
    58  	// Disabled because this is already handled at the authorize_request_handler
    59  	// if !ar.GetClient().GetResponseTypes().Has("token") {
    60  	// 	 return errorsx.WithStack(fosite.ErrInvalidGrant.WithDebug("The client is not allowed to use response type token"))
    61  	// }
    62  
    63  	if !ar.GetClient().GetGrantTypes().Has("implicit") {
    64  		return errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("The OAuth 2.0 Client is not allowed to use the authorization grant 'implicit'."))
    65  	}
    66  
    67  	client := ar.GetClient()
    68  	for _, scope := range ar.GetRequestedScopes() {
    69  		if !c.ScopeStrategy(client.GetScopes(), scope) {
    70  			return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope))
    71  		}
    72  	}
    73  
    74  	if err := c.AudienceMatchingStrategy(client.GetAudience(), ar.GetRequestedAudience()); err != nil {
    75  		return err
    76  	}
    77  
    78  	// there is no need to check for https, because implicit flow does not require https
    79  	// https://tools.ietf.org/html/rfc6819#section-4.4.2
    80  
    81  	return c.IssueImplicitAccessToken(ctx, ar, resp)
    82  }
    83  
    84  func (c *AuthorizeImplicitGrantTypeHandler) IssueImplicitAccessToken(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
    85  	// Only override expiry if none is set.
    86  	if ar.GetSession().GetExpiresAt(fosite.AccessToken).IsZero() {
    87  		ar.GetSession().SetExpiresAt(fosite.AccessToken, time.Now().UTC().Add(c.AccessTokenLifespan).Round(time.Second))
    88  	}
    89  
    90  	// Generate the code
    91  	token, signature, err := c.AccessTokenStrategy.GenerateAccessToken(ctx, ar)
    92  	if err != nil {
    93  		return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
    94  	}
    95  
    96  	if err := c.AccessTokenStorage.CreateAccessTokenSession(ctx, signature, ar.Sanitize([]string{})); err != nil {
    97  		return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
    98  	}
    99  	resp.AddParameter("access_token", token)
   100  	resp.AddParameter("expires_in", strconv.FormatInt(int64(getExpiresIn(ar, fosite.AccessToken, c.AccessTokenLifespan, time.Now().UTC())/time.Second), 10))
   101  	resp.AddParameter("token_type", "bearer")
   102  	resp.AddParameter("state", ar.GetState())
   103  	resp.AddParameter("scope", strings.Join(ar.GetGrantedScopes(), " "))
   104  
   105  	ar.SetResponseTypeHandled("token")
   106  
   107  	return nil
   108  }
   109  

View as plain text