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),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
})
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
Loading