Skip to content

Commit

Permalink
feat: allow the owner of a webservice did to fetch the token
Browse files Browse the repository at this point in the history
- added tests
  • Loading branch information
r-marques committed Jun 20, 2023
1 parent d05548f commit 3ac9626
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
27 changes: 23 additions & 4 deletions integration/subscriptions.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe('SubscriptionsController', () => {
})

it('should not allow expired subscription', async () => {
jest.spyOn(neverminedService, 'getDuration').mockImplementation(async () => 1)
jest.spyOn(neverminedService, 'getDuration').mockImplementationOnce(async () => 1)

const response = await request(app.getHttpServer())
.get(`/${ddoWebService.id}`)
Expand All @@ -275,7 +275,7 @@ describe('SubscriptionsController', () => {
})

it('should allow unlimited subscriptions', async () => {
jest.spyOn(neverminedService, 'getDuration').mockImplementation(async () => 0)
jest.spyOn(neverminedService, 'getDuration').mockImplementationOnce(async () => 0)
const spyGetExpirationTime = jest.spyOn(subscriptionsService, 'getExpirationTime')

const response = await request(app.getHttpServer())
Expand All @@ -297,7 +297,7 @@ describe('SubscriptionsController', () => {
})

it('should allow limited duration subscriptions', async () => {
jest.spyOn(neverminedService, 'getDuration').mockImplementation(async () => 1000)
jest.spyOn(neverminedService, 'getDuration').mockImplementationOnce(async () => 1000)

const response = await request(app.getHttpServer())
.get(`/${ddoWebService.id}`)
Expand All @@ -306,7 +306,7 @@ describe('SubscriptionsController', () => {
expect(response.statusCode).toEqual(200)
})

it('should throw 403 if any event is found', async () => {
it('should throw 403 if no event is found', async () => {
jest
.spyOn(
neverminedService.nevermined.keeper.conditions.transferNft721Condition.events,
Expand All @@ -320,5 +320,24 @@ describe('SubscriptionsController', () => {

expect(response.statusCode).toEqual(403)
})

it('should allow the owner to retrieve the token', async () => {
const signer = await nevermined.accounts.findSigner(ownerAddress)
const ownerToken = await authService.createToken({}, signer)

const response = await request(app.getHttpServer())
.get(`/${ddoWebService.id}`)
.set('Authorization', `Bearer ${ownerToken}`)

expect(response.statusCode).toEqual(200)

const { accessToken } = response.body
const { jwtSecret } = configService.subscriptionsConfig()
const { payload } = await jose.jwtDecrypt(accessToken, jwtSecret)

expect(payload.did).toEqual(ddoWebService.id)
expect(payload.owner).toEqual(ownerAddress)
expect(payload.userId).toEqual(ownerAddress)
})
})
})
39 changes: 22 additions & 17 deletions src/subscriptions/subscriptions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,32 @@ export class SubscriptionsController {
await this.subscriptionService.validateDid(did)

// validate that the subscription is valid
const isValid = await this.subscriptionService.isSubscriptionValid(
contractAddress,
numberNfts,
req.user.address,
)

if (!isValid) {
Logger.debug(
`[GET /subscriptions] ${did}: user ${req.user.address} does not have access to subscription`,
let expiryTime: string
if (req.user.address !== owner) {
const isValid = await this.subscriptionService.isSubscriptionValid(
contractAddress,
numberNfts,
req.user.address,
)
throw new ForbiddenException(
`user ${req.user.address} does not have access to subscription ${did}`,

if (!isValid) {
Logger.debug(
`[GET /subscriptions] ${did}: user ${req.user.address} does not have access to subscription`,
)
throw new ForbiddenException(
`user ${req.user.address} does not have access to subscription ${did}`,
)
}

// get expiry time
expiryTime = await this.subscriptionService.getExpirationTime(
contractAddress,
req.user.address,
)
} else {
expiryTime = this.subscriptionService.defaultExpiryTime
}

// get expiry time
const expiryTime = await this.subscriptionService.getExpirationTime(
contractAddress,
req.user.address,
)

Logger.debug(`Generating access token with expiration time: ${expiryTime}`)
// get access token
const accessToken = await this.subscriptionService.generateToken(
Expand Down

0 comments on commit 3ac9626

Please sign in to comment.