-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from nevermined-io/feature/subscriptions
feat: added new subscriptions module
- Loading branch information
Showing
15 changed files
with
708 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { IsString } from 'class-validator' | ||
|
||
export class SubscriptionTokenDto { | ||
@ApiProperty({ | ||
description: 'The Authorization Bearer token', | ||
example: 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjIyIn0.eyJpc3Mi[...omitted for brevity...]', | ||
}) | ||
@IsString() | ||
accessToken: string | ||
|
||
@ApiProperty({ | ||
description: 'The proxy uri that should be used with the provided token', | ||
example: 'https://proxy.nevermined.one', | ||
}) | ||
@IsString() | ||
neverminedProxyUri: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { ConfigModule } from '../shared/config/config.module' | ||
import { NeverminedModule } from '../shared/nevermined/nvm.module' | ||
import { SubscriptionsController } from './subscriptions.controller' | ||
import { SubscriptionsService } from './subscriptions.service' | ||
|
||
describe('SubscriptionsController', () => { | ||
let controller: SubscriptionsController | ||
|
||
beforeEach(async () => { | ||
const moduleRef: TestingModule = await Test.createTestingModule({ | ||
controllers: [SubscriptionsController], | ||
providers: [SubscriptionsService], | ||
imports: [NeverminedModule, ConfigModule], | ||
}).compile() | ||
|
||
controller = moduleRef.get<SubscriptionsController>(SubscriptionsController) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Controller, ForbiddenException, Get, Param, Req } from '@nestjs/common' | ||
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger' | ||
import { SubscriptionTokenDto } from './dto/token.dto' | ||
import { SubscriptionsService } from './subscriptions.service' | ||
|
||
@ApiTags('Subscriptions') | ||
@Controller() | ||
export class SubscriptionsController { | ||
constructor(private subscriptionService: SubscriptionsService) {} | ||
|
||
@Get(':did') | ||
@ApiOperation({ | ||
description: 'Get and access token for a subscription', | ||
}) | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Returns the access token', | ||
type: SubscriptionTokenDto, | ||
}) | ||
@ApiResponse({ | ||
status: 401, | ||
description: 'Unauthorized access', | ||
}) | ||
@ApiBearerAuth('Authorization') | ||
async getAccessToken(@Req() req, @Param('did') did: string): Promise<SubscriptionTokenDto> { | ||
// get subscription data | ||
const { contractAddress, numberNfts, endpoints, headers } = | ||
await this.subscriptionService.validateDid(did) | ||
|
||
// validate that the subscription is valid | ||
const isValid = await this.subscriptionService.isSubscriptionValid( | ||
contractAddress, | ||
numberNfts, | ||
req.user.address, | ||
) | ||
|
||
if (!isValid) { | ||
throw new ForbiddenException(`user ${req.user.iss} has not access to subscription ${did}`) | ||
} | ||
|
||
// get expiry time | ||
const expiryTime = await this.subscriptionService.getExpirationTime( | ||
contractAddress, | ||
req.user.address, | ||
) | ||
|
||
// get access token | ||
const accessToken = await this.subscriptionService.generateToken( | ||
did, | ||
req.user.iss, | ||
endpoints, | ||
expiryTime, | ||
headers, | ||
) | ||
|
||
return { | ||
accessToken: accessToken, | ||
neverminedProxyUri: this.subscriptionService.neverminedProxyUri, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common' | ||
import { ConfigModule } from '../shared/config/config.module' | ||
import { NeverminedModule } from '../shared/nevermined/nvm.module' | ||
import { SubscriptionsController } from './subscriptions.controller' | ||
import { SubscriptionsService } from './subscriptions.service' | ||
|
||
@Module({ | ||
controllers: [SubscriptionsController], | ||
providers: [SubscriptionsService], | ||
imports: [NeverminedModule, ConfigModule], | ||
}) | ||
export class SubscriptionsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { ConfigModule } from '../shared/config/config.module' | ||
import { NeverminedModule } from '../shared/nevermined/nvm.module' | ||
import { SubscriptionsService } from './subscriptions.service' | ||
|
||
describe('SubscriptionsService', () => { | ||
let service: SubscriptionsService | ||
|
||
beforeEach(async () => { | ||
const moduleRef: TestingModule = await Test.createTestingModule({ | ||
providers: [SubscriptionsService], | ||
imports: [NeverminedModule, ConfigModule], | ||
}).compile() | ||
|
||
service = moduleRef.get<SubscriptionsService>(SubscriptionsService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
}) |
Oops, something went wrong.