...

Source file src/github.com/ory/fosite/compose/compose_strategy.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/ecdsa"
    26  	"crypto/rsa"
    27  
    28  	"github.com/ory/fosite/handler/oauth2"
    29  	"github.com/ory/fosite/handler/openid"
    30  	"github.com/ory/fosite/token/hmac"
    31  	"github.com/ory/fosite/token/jwt"
    32  )
    33  
    34  type CommonStrategy struct {
    35  	oauth2.CoreStrategy
    36  	openid.OpenIDConnectTokenStrategy
    37  	jwt.JWTStrategy
    38  }
    39  
    40  func NewOAuth2HMACStrategy(config *Config, secret []byte, rotatedSecrets [][]byte) *oauth2.HMACSHAStrategy {
    41  	return &oauth2.HMACSHAStrategy{
    42  		Enigma: &hmac.HMACStrategy{
    43  			GlobalSecret:         secret,
    44  			RotatedGlobalSecrets: rotatedSecrets,
    45  			TokenEntropy:         config.GetTokenEntropy(),
    46  		},
    47  		AccessTokenLifespan:   config.GetAccessTokenLifespan(),
    48  		AuthorizeCodeLifespan: config.GetAuthorizeCodeLifespan(),
    49  		RefreshTokenLifespan:  config.GetRefreshTokenLifespan(),
    50  	}
    51  }
    52  
    53  func NewOAuth2JWTStrategy(key *rsa.PrivateKey, strategy *oauth2.HMACSHAStrategy) *oauth2.DefaultJWTStrategy {
    54  	return &oauth2.DefaultJWTStrategy{
    55  		JWTStrategy: &jwt.RS256JWTStrategy{
    56  			PrivateKey: key,
    57  		},
    58  		HMACSHAStrategy: strategy,
    59  	}
    60  }
    61  
    62  func NewOAuth2JWTECDSAStrategy(key *ecdsa.PrivateKey, strategy *oauth2.HMACSHAStrategy) *oauth2.DefaultJWTStrategy {
    63  	return &oauth2.DefaultJWTStrategy{
    64  		JWTStrategy: &jwt.ES256JWTStrategy{
    65  			PrivateKey: key,
    66  		},
    67  		HMACSHAStrategy: strategy,
    68  	}
    69  }
    70  
    71  // Deprecated: Use NewOAuth2JWTStrategy(key, strategy).WithIssuer(issuer) instead.
    72  func NewOAuth2JWTStrategyWithIssuer(key *rsa.PrivateKey, strategy *oauth2.HMACSHAStrategy, issuer string) *oauth2.DefaultJWTStrategy {
    73  	return NewOAuth2JWTStrategy(key, strategy).WithIssuer(issuer)
    74  }
    75  
    76  // Deprecated: Use NewOAuth2JWTECDSAStrategy(key, strategy).WithIssuer(issuer) instead.
    77  func NewOAuth2JWTECDSAStrategyWithIssuer(key *ecdsa.PrivateKey, strategy *oauth2.HMACSHAStrategy, issuer string) *oauth2.DefaultJWTStrategy {
    78  	return NewOAuth2JWTECDSAStrategy(key, strategy).WithIssuer(issuer)
    79  }
    80  
    81  func NewOpenIDConnectStrategy(config *Config, key *rsa.PrivateKey) *openid.DefaultStrategy {
    82  	return &openid.DefaultStrategy{
    83  		JWTStrategy: &jwt.RS256JWTStrategy{
    84  			PrivateKey: key,
    85  		},
    86  		Expiry:              config.GetIDTokenLifespan(),
    87  		Issuer:              config.IDTokenIssuer,
    88  		MinParameterEntropy: config.GetMinParameterEntropy(),
    89  	}
    90  }
    91  
    92  func NewOpenIDConnectECDSAStrategy(config *Config, key *ecdsa.PrivateKey) *openid.DefaultStrategy {
    93  	return &openid.DefaultStrategy{
    94  		JWTStrategy: &jwt.ES256JWTStrategy{
    95  			PrivateKey: key,
    96  		},
    97  		Expiry:              config.GetIDTokenLifespan(),
    98  		Issuer:              config.IDTokenIssuer,
    99  		MinParameterEntropy: config.GetMinParameterEntropy(),
   100  	}
   101  }
   102  

View as plain text