Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/internal/oauth/oauth2/granthandlers/ciba.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/thunder-id/thunderid/internal/attributecache"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/ciba"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/constants"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/dpop"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/model"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/resourceindicators"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/tokenservice"
Expand Down Expand Up @@ -230,6 +231,7 @@ func (h *cibaGrantHandler) issueTokens(ctx context.Context, record *ciba.CIBAAut
GrantType: string(providers.GrantTypeCIBA),
OAuthApp: oauthApp,
ValidityPeriod: userSubConfig.ValidityPeriodOrZero(),
DPoPJkt: dpop.GetJkt(ctx),
})
if err != nil {
h.logger.Error(ctx, "Failed to generate access token", log.Error(err))
Expand Down
37 changes: 37 additions & 0 deletions backend/internal/oauth/oauth2/granthandlers/ciba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/thunder-id/thunderid/internal/attributecache"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/ciba"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/constants"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/dpop"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/model"
"github.com/thunder-id/thunderid/internal/oauth/oauth2/tokenservice"
"github.com/thunder-id/thunderid/tests/mocks/attributecachemock"
Expand Down Expand Up @@ -252,6 +253,42 @@ func (suite *CIBAGrantHandlerTestSuite) TestHandleGrant_Authenticated_IssuesToke
suite.Equal("id-token", resp.IDToken.Token)
}

// A CIBA access token is sender-constrained to the key the client proved possession of when polling
// the token endpoint, so a stolen token cannot be replayed without the matching DPoP proof.
func (suite *CIBAGrantHandlerTestSuite) TestHandleGrant_Authenticated_BindsDPoPJkt() {
record := suite.boundAuthenticatedRecord(testScopeRead)
suite.mockCIBAService.EXPECT().GetByAuthReqID(mock.Anything, "auth-req-1").Return(record, nil)
suite.expectResourceServer()
suite.mockTokenBuilder.EXPECT().BuildAccessToken(mock.Anything, mock.MatchedBy(
func(ctx *tokenservice.AccessTokenBuildContext) bool {
return ctx.DPoPJkt == "test-jkt"
})).Return(&model.TokenDTO{Token: "access-token", TokenType: "DPoP"}, nil)
suite.mockCIBAService.EXPECT().MarkConsumed(mock.Anything, "auth-req-1").Return(true, nil)

ctx := dpop.WithJkt(context.Background(), "test-jkt")
resp, errResp := suite.handler.HandleGrant(ctx, suite.tokenReq, suite.oauthApp)
suite.Nil(errResp)
suite.NotNil(resp)
suite.Equal("access-token", resp.AccessToken.Token)
}

// Without a verified DPoP proof the access token stays unbound, so non-DPoP clients are unaffected.
func (suite *CIBAGrantHandlerTestSuite) TestHandleGrant_Authenticated_NoProofLeavesTokenUnbound() {
record := suite.boundAuthenticatedRecord(testScopeRead)
suite.mockCIBAService.EXPECT().GetByAuthReqID(mock.Anything, "auth-req-1").Return(record, nil)
suite.expectResourceServer()
suite.mockTokenBuilder.EXPECT().BuildAccessToken(mock.Anything, mock.MatchedBy(
func(ctx *tokenservice.AccessTokenBuildContext) bool {
return ctx.DPoPJkt == ""
})).Return(&model.TokenDTO{Token: "access-token", TokenType: "Bearer"}, nil)
suite.mockCIBAService.EXPECT().MarkConsumed(mock.Anything, "auth-req-1").Return(true, nil)

resp, errResp := suite.handler.HandleGrant(context.Background(), suite.tokenReq, suite.oauthApp)
suite.Nil(errResp)
suite.NotNil(resp)
suite.Equal("access-token", resp.AccessToken.Token)
}

func (suite *CIBAGrantHandlerTestSuite) TestHandleGrant_Authenticated_NoOpenIDSkipsIDToken() {
record := suite.boundAuthenticatedRecord(testScopeRead)
suite.mockCIBAService.EXPECT().GetByAuthReqID(mock.Anything, "auth-req-1").Return(record, nil)
Expand Down
43 changes: 31 additions & 12 deletions backend/internal/oauth/oauth2/granthandlers/refresh_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,14 @@ func (h *refreshTokenGrantHandler) ValidateGrant(ctx context.Context, tokenReque
return nil
}

// HandleGrant processes the refresh token grant request and generates a new token response.
func (h *refreshTokenGrantHandler) HandleGrant(ctx context.Context, tokenRequest *model.TokenRequest,
oauthApp *providers.OAuthClient) (
*model.TokenResponseDTO, *model.ErrorResponse) {
logger := log.GetLogger().With(log.String(log.LoggerKeyComponentName, "RefreshTokenGrantHandler"))

// Validate refresh token using token validator
// ValidateRefreshToken verifies the token and enforces the RFC 7009 deny list. A revoked token is
// rejected as invalid_grant like any other invalid token; an unavailable deny list fails closed
// with a server_error.
refreshTokenClaims, err := h.tokenValidator.ValidateRefreshToken(
ctx, tokenRequest.RefreshToken, tokenRequest.ClientID)
// resolveRefreshToken validates the presented refresh token and confirms it was issued to the
// requesting client. ValidateRefreshToken enforces the RFC 7009 deny list, so a revoked token is
// rejected as invalid_grant like any other invalid token and an unavailable deny list fails closed
// with a server_error.
func (h *refreshTokenGrantHandler) resolveRefreshToken(ctx context.Context,
tokenRequest *model.TokenRequest, logger *log.Logger) (
*tokenservice.RefreshTokenClaims, *model.ErrorResponse) {
refreshTokenClaims, err := h.tokenValidator.ValidateRefreshToken(ctx, tokenRequest.RefreshToken)
if err != nil {
logger.Debug(ctx, "Failed to validate refresh token", log.Error(err))
if errors.Is(err, revocation.ErrEnforcementUnavailable) {
Expand All @@ -129,6 +125,29 @@ func (h *refreshTokenGrantHandler) HandleGrant(ctx context.Context, tokenRequest
}
}

// A client may only redeem refresh tokens issued to it.
if refreshTokenClaims.ClientID != tokenRequest.ClientID {
logger.Debug(ctx, "Refresh token does not belong to the requesting client")
return nil, &model.ErrorResponse{
Error: constants.ErrorInvalidGrant,
ErrorDescription: "Invalid refresh token",
}
}

return refreshTokenClaims, nil
}

// HandleGrant processes the refresh token grant request and generates a new token response.
func (h *refreshTokenGrantHandler) HandleGrant(ctx context.Context, tokenRequest *model.TokenRequest,
oauthApp *providers.OAuthClient) (
*model.TokenResponseDTO, *model.ErrorResponse) {
logger := log.GetLogger().With(log.String(log.LoggerKeyComponentName, "RefreshTokenGrantHandler"))

refreshTokenClaims, errResp := h.resolveRefreshToken(ctx, tokenRequest, logger)
if errResp != nil {
return nil, errResp
}

if errResp := dpop.VerifyProofBinding(ctx, refreshTokenClaims.DPoPJkt, "refresh token"); errResp != nil {
return nil, errResp
}
Expand Down
Loading
Loading