Skip to content

Commit

Permalink
fix: add compose functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vivshankar committed Sep 1, 2024
1 parent 2840d26 commit ab318ff
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 17 deletions.
63 changes: 63 additions & 0 deletions compose/compose_rfc8693.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package compose

import (
"github.com/ory/fosite"
"github.com/ory/fosite/handler/oauth2"
"github.com/ory/fosite/handler/openid"
"github.com/ory/fosite/handler/rfc8693"
"github.com/ory/fosite/token/jwt"
)

// RFC8693AccessTokenTypeHandlerFactory creates a access token type handler.
func RFC8693AccessTokenTypeHandlerFactory(config fosite.Configurator, storage interface{}, strategy interface{}) interface{} {
return &rfc8693.AccessTokenTypeHandler{
CoreStrategy: strategy.(oauth2.CoreStrategy),
Storage: storage.(rfc8693.Storage),
Config: config,
}
}

// RFC8693RefreshTokenTypeHandlerFactory creates a refresh token type handler.
func RFC8693RefreshTokenTypeHandlerFactory(config fosite.Configurator, storage interface{}, strategy interface{}) interface{} {
return &rfc8693.RefreshTokenTypeHandler{
CoreStrategy: strategy.(oauth2.CoreStrategy),
Storage: storage.(rfc8693.Storage),
Config: config,
}
}

// RFC8693ActorTokenValidationHandlerFactory creates a actor token validation handler.
func RFC8693ActorTokenValidationHandlerFactory(config fosite.Configurator, storage interface{}, strategy interface{}) interface{} {
return &rfc8693.ActorTokenValidationHandler{}
}

// RFC8693CustomJWTTypeHandlerFactory creates a custom JWT token type handler.
func RFC8693CustomJWTTypeHandlerFactory(config fosite.Configurator, storage interface{}, strategy interface{}) interface{} {
return &rfc8693.CustomJWTTypeHandler{
JWTStrategy: strategy.(jwt.Signer),
Storage: storage.(rfc8693.Storage),
Config: config,
}
}

// RFC8693TokenExchangeGrantHandlerFactory creates the request validation handler for token exchange. This should be the first
// in the list.
func RFC8693TokenExchangeGrantHandlerFactory(config fosite.Configurator, storage interface{}, strategy interface{}) interface{} {
return &rfc8693.TokenExchangeGrantHandler{
Config: config,
}
}

// RFC8693IDTokenTypeHandlerFactory creates a ID token type handler.
func RFC8693IDTokenTypeHandlerFactory(config fosite.Configurator, storage interface{}, strategy interface{}) interface{} {
return &rfc8693.IDTokenTypeHandler{
JWTStrategy: strategy.(jwt.Signer),
Storage: storage.(rfc8693.Storage),
Config: config,
IssueStrategy: strategy.(openid.OpenIDConnectTokenStrategy),
ValidationStrategy: strategy.(openid.OpenIDConnectTokenValidationStrategy),
}
}
2 changes: 1 addition & 1 deletion handler/rfc8693/access_token_type_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/actor_token_validation_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
22 changes: 16 additions & 6 deletions handler/rfc8693/custom_jwt_type_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand All @@ -16,7 +16,7 @@ import (
)

type CustomJWTTypeHandler struct {
Config fosite.RFC8693ConfigProvider
Config fosite.Configurator
JWTStrategy jwt.Signer
Storage
}
Expand All @@ -32,8 +32,13 @@ func (c *CustomJWTTypeHandler) HandleTokenEndpointRequest(ctx context.Context, r
return errorsx.WithStack(fosite.ErrServerError.WithDebug("Failed to perform token exchange because the session is not of the right type."))
}

teConfig, _ := c.Config.(fosite.RFC8693ConfigProvider)
if teConfig == nil {
return errorsx.WithStack(fosite.ErrServerError.WithDebug("Failed to perform token exchange because the config is not of the right type."))
}

form := request.GetRequestForm()
tokenTypes := c.Config.GetTokenTypes(ctx)
tokenTypes := teConfig.GetTokenTypes(ctx)
actorTokenType := tokenTypes[form.Get("actor_token_type")]
subjectTokenType := tokenTypes[form.Get("subject_token_type")]
if actorTokenType != nil && actorTokenType.GetType(ctx) == JWTTokenType {
Expand Down Expand Up @@ -75,13 +80,18 @@ func (c *CustomJWTTypeHandler) PopulateTokenEndpointResponse(ctx context.Context
return errorsx.WithStack(fosite.ErrServerError.WithDebug("Failed to perform token exchange because the session is not of the right type."))
}

teConfig, _ := c.Config.(fosite.RFC8693ConfigProvider)
if teConfig == nil {
return errorsx.WithStack(fosite.ErrServerError.WithDebug("Failed to perform token exchange because the config is not of the right type."))
}

form := request.GetRequestForm()
requestedTokenType := form.Get("requested_token_type")
if requestedTokenType == "" {
requestedTokenType = c.Config.GetDefaultRequestedTokenType(ctx)
requestedTokenType = teConfig.GetDefaultRequestedTokenType(ctx)
}

tokenTypes := c.Config.GetTokenTypes(ctx)
tokenTypes := teConfig.GetTokenTypes(ctx)
tokenType := tokenTypes[requestedTokenType]
if tokenType == nil || tokenType.GetType(ctx) != JWTTokenType {
return nil
Expand All @@ -106,7 +116,7 @@ func (c *CustomJWTTypeHandler) CanHandleTokenEndpointRequest(ctx context.Context
return requester.GetGrantTypes().ExactOne("urn:ietf:params:oauth:grant-type:token-exchange")
}

func (c *CustomJWTTypeHandler) validate(ctx context.Context, request fosite.AccessRequester, tokenType fosite.RFC8693TokenType, token string) (map[string]interface{}, error) {
func (c *CustomJWTTypeHandler) validate(ctx context.Context, _ fosite.AccessRequester, tokenType fosite.RFC8693TokenType, token string) (map[string]interface{}, error) {

jwtType, _ := tokenType.(*JWTType)
if jwtType == nil {
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/flow_token_exchange.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/id_token_type_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/refresh_token_type_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/session.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/storage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/token_exchange_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693_test
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/token_type.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down
2 changes: 1 addition & 1 deletion handler/rfc8693/token_type_jwt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package rfc8693
Expand Down

0 comments on commit ab318ff

Please sign in to comment.