Skip to content

Commit

Permalink
make accesstoken store not s2s-specific
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Dec 8, 2023
1 parent 379ff01 commit 8154e60
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
10 changes: 9 additions & 1 deletion auth/api/iam/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const apiPath = "iam"
const apiModuleName = auth.ModuleName + "/" + apiPath
const httpRequestContextKey = "http-request"

// accessTokenValidity defines how long access tokens are valid.
// TODO: Might want to make this configurable at some point
const accessTokenValidity = 15 * time.Minute

//go:embed assets
var assets embed.FS

Expand Down Expand Up @@ -167,7 +171,7 @@ func (r Wrapper) IntrospectAccessToken(ctx context.Context, request IntrospectAc
}

token := AccessToken{}
if err := r.s2sAccessTokenStore().Get(request.Body.Token, &token); err != nil {
if err := r.accessTokenStore().Get(request.Body.Token, &token); err != nil {
// Return 200 + 'Active = false' when token is invalid or malformed
return IntrospectAccessToken200JSONResponse{}, err
}
Expand Down Expand Up @@ -391,3 +395,7 @@ func (r Wrapper) idToDID(id string) did.DID {
result, _ := didweb.URLToDID(*r.auth.PublicURL().JoinPath("iam", id))
return *result
}

func (r *Wrapper) accessTokenStore() storage.SessionStore {
return r.storageEngine.GetSessionDatabase().GetStore(accessTokenValidity, "accesstoken")
}
6 changes: 3 additions & 3 deletions auth/api/iam/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func TestWrapper_IntrospectAccessToken(t *testing.T) {
})
t.Run("error - expired token", func(t *testing.T) {
token := AccessToken{Expiration: time.Now().Add(-time.Second)}
require.NoError(t, ctx.client.s2sAccessTokenStore().Put("token", token))
require.NoError(t, ctx.client.accessTokenStore().Put("token", token))

res, err := ctx.client.IntrospectAccessToken(context.Background(), IntrospectAccessTokenRequestObject{Body: &TokenIntrospectionRequest{Token: "token"}})

Expand All @@ -256,7 +256,7 @@ func TestWrapper_IntrospectAccessToken(t *testing.T) {
})
t.Run("ok", func(t *testing.T) {
token := AccessToken{Expiration: time.Now().Add(time.Second)}
require.NoError(t, ctx.client.s2sAccessTokenStore().Put("token", token))
require.NoError(t, ctx.client.accessTokenStore().Put("token", token))

res, err := ctx.client.IntrospectAccessToken(context.Background(), IntrospectAccessTokenRequestObject{Body: &TokenIntrospectionRequest{Token: "token"}})

Expand Down Expand Up @@ -287,7 +287,7 @@ func TestWrapper_IntrospectAccessToken(t *testing.T) {
PresentationDefinition: &pe.PresentationDefinition{},
}

require.NoError(t, ctx.client.s2sAccessTokenStore().Put(token.Token, token))
require.NoError(t, ctx.client.accessTokenStore().Put(token.Token, token))
expectedResponse, err := json.Marshal(IntrospectAccessToken200JSONResponse{
Active: true,
ClientId: ptrTo("client"),
Expand Down
10 changes: 1 addition & 9 deletions auth/api/iam/s2s_vptoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ import (
"github.com/nuts-foundation/nuts-node/vdr/resolver"
)

// accessTokenValidity defines how long access tokens are valid.
// TODO: Might want to make this configurable at some point
const accessTokenValidity = 15 * time.Minute

// s2sMaxPresentationValidity defines the maximum validity of a presentation.
// This is to prevent replay attacks. The value is specified by Nuts RFC021, and excludes max. clock skew.
const s2sMaxPresentationValidity = 5 * time.Second
Expand Down Expand Up @@ -165,7 +161,7 @@ func (r *Wrapper) createS2SAccessToken(issuer did.DID, issueTime time.Time, pres
PresentationDefinition: &definition,
PresentationSubmission: &submission,
}
err = r.s2sAccessTokenStore().Put(accessToken.Token, accessToken)
err = r.accessTokenStore().Put(accessToken.Token, accessToken)
if err != nil {
return nil, fmt.Errorf("unable to store access token: %w", err)
}
Expand Down Expand Up @@ -312,10 +308,6 @@ func (r *Wrapper) validatePresentationAudience(presentation vc.VerifiablePresent
}
}

func (r *Wrapper) s2sAccessTokenStore() storage.SessionStore {
return r.storageEngine.GetSessionDatabase().GetStore(accessTokenValidity, "s2s", "accesstoken")
}

type AccessToken struct {
Token string
// Issuer and Subject of a token are always the same.
Expand Down
2 changes: 1 addition & 1 deletion auth/api/iam/s2s_vptoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func TestWrapper_createAccessToken(t *testing.T) {
assert.Equal(t, "everything", *accessToken.Scope)

var storedToken AccessToken
err = ctx.client.s2sAccessTokenStore().Get(accessToken.AccessToken, &storedToken)
err = ctx.client.accessTokenStore().Get(accessToken.AccessToken, &storedToken)
require.NoError(t, err)
assert.Equal(t, accessToken.AccessToken, storedToken.Token)
assert.Equal(t, submission, *storedToken.PresentationSubmission)
Expand Down

0 comments on commit 8154e60

Please sign in to comment.