...

Source file src/github.com/ory/fosite/compose/compose.go

Documentation: github.com/ory/fosite/compose

     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 compose
    23  
    24  import (
    25  	"crypto/rsa"
    26  
    27  	"github.com/ory/fosite"
    28  	"github.com/ory/fosite/token/jwt"
    29  )
    30  
    31  type Factory func(config *Config, storage interface{}, strategy interface{}) interface{}
    32  
    33  // Compose takes a config, a storage, a strategy and handlers to instantiate an OAuth2Provider:
    34  //
    35  //  import "github.com/ory/fosite/compose"
    36  //
    37  //  // var storage = new(MyFositeStorage)
    38  //  var config = Config {
    39  //  	AccessTokenLifespan: time.Minute * 30,
    40  // 	// check Config for further configuration options
    41  //  }
    42  //
    43  //  var strategy = NewOAuth2HMACStrategy(config)
    44  //
    45  //  var oauth2Provider = Compose(
    46  //  	config,
    47  // 	storage,
    48  // 	strategy,
    49  //	NewOAuth2AuthorizeExplicitHandler,
    50  //	OAuth2ClientCredentialsGrantFactory,
    51  // 	// for a complete list refer to the docs of this package
    52  //  )
    53  //
    54  // Compose makes use of interface{} types in order to be able to handle a all types of stores, strategies and handlers.
    55  func Compose(config *Config, storage interface{}, strategy interface{}, hasher fosite.Hasher, factories ...Factory) fosite.OAuth2Provider {
    56  	if hasher == nil {
    57  		hasher = &fosite.BCrypt{WorkFactor: config.GetHashCost()}
    58  	}
    59  
    60  	f := &fosite.Fosite{
    61  		Store:                        storage.(fosite.Storage),
    62  		AuthorizeEndpointHandlers:    fosite.AuthorizeEndpointHandlers{},
    63  		TokenEndpointHandlers:        fosite.TokenEndpointHandlers{},
    64  		TokenIntrospectionHandlers:   fosite.TokenIntrospectionHandlers{},
    65  		RevocationHandlers:           fosite.RevocationHandlers{},
    66  		Hasher:                       hasher,
    67  		ScopeStrategy:                config.GetScopeStrategy(),
    68  		AudienceMatchingStrategy:     config.GetAudienceStrategy(),
    69  		SendDebugMessagesToClients:   config.SendDebugMessagesToClients,
    70  		TokenURL:                     config.TokenURL,
    71  		JWKSFetcherStrategy:          config.GetJWKSFetcherStrategy(),
    72  		MinParameterEntropy:          config.GetMinParameterEntropy(),
    73  		UseLegacyErrorFormat:         config.UseLegacyErrorFormat,
    74  		ClientAuthenticationStrategy: config.GetClientAuthenticationStrategy(),
    75  		ResponseModeHandlerExtension: config.ResponseModeHandlerExtension,
    76  		MessageCatalog:               config.MessageCatalog,
    77  		FormPostHTMLTemplate:         config.FormPostHTMLTemplate,
    78  	}
    79  
    80  	for _, factory := range factories {
    81  		res := factory(config, storage, strategy)
    82  		if ah, ok := res.(fosite.AuthorizeEndpointHandler); ok {
    83  			f.AuthorizeEndpointHandlers.Append(ah)
    84  		}
    85  		if th, ok := res.(fosite.TokenEndpointHandler); ok {
    86  			f.TokenEndpointHandlers.Append(th)
    87  		}
    88  		if tv, ok := res.(fosite.TokenIntrospector); ok {
    89  			f.TokenIntrospectionHandlers.Append(tv)
    90  		}
    91  		if rh, ok := res.(fosite.RevocationHandler); ok {
    92  			f.RevocationHandlers.Append(rh)
    93  		}
    94  	}
    95  
    96  	return f
    97  }
    98  
    99  // ComposeAllEnabled returns a fosite instance with all OAuth2 and OpenID Connect handlers enabled.
   100  func ComposeAllEnabled(config *Config, storage interface{}, secret []byte, key *rsa.PrivateKey) fosite.OAuth2Provider {
   101  	return Compose(
   102  		config,
   103  		storage,
   104  		&CommonStrategy{
   105  			CoreStrategy:               NewOAuth2HMACStrategy(config, secret, nil),
   106  			OpenIDConnectTokenStrategy: NewOpenIDConnectStrategy(config, key),
   107  			JWTStrategy: &jwt.RS256JWTStrategy{
   108  				PrivateKey: key,
   109  			},
   110  		},
   111  		nil,
   112  
   113  		OAuth2AuthorizeExplicitFactory,
   114  		OAuth2AuthorizeImplicitFactory,
   115  		OAuth2ClientCredentialsGrantFactory,
   116  		OAuth2RefreshTokenGrantFactory,
   117  		OAuth2ResourceOwnerPasswordCredentialsFactory,
   118  		RFC7523AssertionGrantFactory,
   119  
   120  		OpenIDConnectExplicitFactory,
   121  		OpenIDConnectImplicitFactory,
   122  		OpenIDConnectHybridFactory,
   123  		OpenIDConnectRefreshFactory,
   124  
   125  		OAuth2TokenIntrospectionFactory,
   126  		OAuth2TokenRevocationFactory,
   127  
   128  		OAuth2PKCEFactory,
   129  	)
   130  }
   131  

View as plain text