...

Source file src/github.com/ory/fosite/handler/oauth2/flow_authorize_code_auth.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  	"net/url"
    27  	"strings"
    28  	"time"
    29  
    30  	"github.com/ory/x/errorsx"
    31  
    32  	"github.com/ory/fosite"
    33  )
    34  
    35  // AuthorizeExplicitGrantTypeHandler is a response handler for the Authorize Code grant using the explicit grant type
    36  // as defined in https://tools.ietf.org/html/rfc6749#section-4.1
    37  type AuthorizeExplicitGrantHandler struct {
    38  	AccessTokenStrategy   AccessTokenStrategy
    39  	RefreshTokenStrategy  RefreshTokenStrategy
    40  	AuthorizeCodeStrategy AuthorizeCodeStrategy
    41  	CoreStorage           CoreStorage
    42  	//TokenRevocationStorage TokenRevocationStorage
    43  
    44  	// AuthCodeLifespan defines the lifetime of an authorize code.
    45  	AuthCodeLifespan time.Duration
    46  
    47  	// AccessTokenLifespan defines the lifetime of an access token.
    48  	AccessTokenLifespan time.Duration
    49  
    50  	// RefreshTokenLifespan defines the lifetime of a refresh token. Leave to 0 for unlimited lifetime.
    51  	RefreshTokenLifespan time.Duration
    52  
    53  	ScopeStrategy            fosite.ScopeStrategy
    54  	AudienceMatchingStrategy fosite.AudienceMatchingStrategy
    55  
    56  	// SanitationWhiteList is a whitelist of form values that are required by the token endpoint. These values
    57  	// are safe for storage in a database (cleartext).
    58  	SanitationWhiteList []string
    59  
    60  	TokenRevocationStorage TokenRevocationStorage
    61  
    62  	IsRedirectURISecure func(*url.URL) bool
    63  
    64  	RefreshTokenScopes []string
    65  
    66  	// OmitRedirectScopeParam must be set to true if the scope query param is to be omitted
    67  	// in the authorization's redirect URI
    68  	OmitRedirectScopeParam bool
    69  }
    70  
    71  func (c *AuthorizeExplicitGrantHandler) secureChecker() func(*url.URL) bool {
    72  	if c.IsRedirectURISecure == nil {
    73  		c.IsRedirectURISecure = fosite.IsRedirectURISecure
    74  	}
    75  	return c.IsRedirectURISecure
    76  }
    77  
    78  func (c *AuthorizeExplicitGrantHandler) HandleAuthorizeEndpointRequest(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
    79  	// This let's us define multiple response types, for example open id connect's id_token
    80  	if !ar.GetResponseTypes().ExactOne("code") {
    81  		return nil
    82  	}
    83  
    84  	ar.SetDefaultResponseMode(fosite.ResponseModeQuery)
    85  
    86  	// Disabled because this is already handled at the authorize_request_handler
    87  	// if !ar.GetClient().GetResponseTypes().Has("code") {
    88  	// 	 return errorsx.WithStack(fosite.ErrInvalidGrant)
    89  	// }
    90  
    91  	if !c.secureChecker()(ar.GetRedirectURI()) {
    92  		return errorsx.WithStack(fosite.ErrInvalidRequest.WithHint("Redirect URL is using an insecure protocol, http is only allowed for hosts with suffix `localhost`, for example: http://myapp.localhost/."))
    93  	}
    94  
    95  	client := ar.GetClient()
    96  	for _, scope := range ar.GetRequestedScopes() {
    97  		if !c.ScopeStrategy(client.GetScopes(), scope) {
    98  			return errorsx.WithStack(fosite.ErrInvalidScope.WithHintf("The OAuth 2.0 Client is not allowed to request scope '%s'.", scope))
    99  		}
   100  	}
   101  
   102  	if err := c.AudienceMatchingStrategy(client.GetAudience(), ar.GetRequestedAudience()); err != nil {
   103  		return err
   104  	}
   105  
   106  	return c.IssueAuthorizeCode(ctx, ar, resp)
   107  }
   108  
   109  func (c *AuthorizeExplicitGrantHandler) IssueAuthorizeCode(ctx context.Context, ar fosite.AuthorizeRequester, resp fosite.AuthorizeResponder) error {
   110  	code, signature, err := c.AuthorizeCodeStrategy.GenerateAuthorizeCode(ctx, ar)
   111  	if err != nil {
   112  		return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
   113  	}
   114  
   115  	ar.GetSession().SetExpiresAt(fosite.AuthorizeCode, time.Now().UTC().Add(c.AuthCodeLifespan))
   116  	if err := c.CoreStorage.CreateAuthorizeCodeSession(ctx, signature, ar.Sanitize(c.GetSanitationWhiteList())); err != nil {
   117  		return errorsx.WithStack(fosite.ErrServerError.WithWrap(err).WithDebug(err.Error()))
   118  	}
   119  
   120  	resp.AddParameter("code", code)
   121  	resp.AddParameter("state", ar.GetState())
   122  	if !c.OmitRedirectScopeParam {
   123  		resp.AddParameter("scope", strings.Join(ar.GetGrantedScopes(), " "))
   124  	}
   125  
   126  	ar.SetResponseTypeHandled("code")
   127  	return nil
   128  }
   129  
   130  func (c *AuthorizeExplicitGrantHandler) GetSanitationWhiteList() []string {
   131  	if len(c.SanitationWhiteList) > 0 {
   132  		return c.SanitationWhiteList
   133  	}
   134  	return []string{
   135  		"code",
   136  		"redirect_uri",
   137  	}
   138  }
   139  

View as plain text