Skip to content

Commit

Permalink
Merge pull request #126 from nevermined-io/fix/jwt_expiration
Browse files Browse the repository at this point in the history
Using string for JWT expiration
  • Loading branch information
aaitor authored Mar 14, 2023
2 parents f3e6981 + 3b8d796 commit 399a5f0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-ts",
"version": "1.1.1",
"version": "1.1.2",
"description": "Nevermined Node",
"main": "main.ts",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions src/shared/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface ComputeConfig {
export interface SubscriptionsConfig {
jwtSecret: Uint8Array
neverminedProxyUri: string
defaultExpiryTime: number
defaultExpiryTime: string
averageBlockTime: number
}

Expand All @@ -48,7 +48,7 @@ const DOTENV_SCHEMA = Joi.object({
.required()
.error(new Error('JWT_SUBSCRIPTION_SECRET_KEY is required!')),
// defaults to 2 years in seconds
SUBSCRIPTION_DEFAULT_EXPIRY_TIME: Joi.number().default(60 * 60 * 24 * 365 * 2),
SUBSCRIPTION_DEFAULT_EXPIRY_TIME: Joi.string().default('100 years'),
// Used to calculate expiry time of subscriptions in milliseconds
NETWORK_AVERAGE_BLOCK_TIME: Joi.number().default(2100),
server: Joi.object({
Expand Down Expand Up @@ -158,7 +158,7 @@ export class ConfigService {
.map((x) => parseInt(x)),
),
neverminedProxyUri: this.get<string>('NEVERMINED_PROXY_URI'),
defaultExpiryTime: this.get<number>('SUBSCRIPTION_DEFAULT_EXPIRY_TIME'),
defaultExpiryTime: this.get<string>('SUBSCRIPTION_DEFAULT_EXPIRY_TIME'),
averageBlockTime: this.get<number>('NETWORK_AVERAGE_BLOCK_TIME'),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/subscriptions/subscriptions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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'
import { Logger } from '../shared/logger/logger.service'

@ApiTags('Subscriptions')
@Controller()
Expand Down Expand Up @@ -44,6 +45,7 @@ export class SubscriptionsController {
req.user.address,
)

Logger.debug(`Generating access token with expiration time: ${expiryTime}`)
// get access token
const accessToken = await this.subscriptionService.generateToken(
did,
Expand Down
14 changes: 8 additions & 6 deletions src/subscriptions/subscriptions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface SubscriptionData {
export class SubscriptionsService {
private readonly jwtSecret: Uint8Array
public readonly neverminedProxyUri: string
private readonly defaultExpiryTime: number
private readonly defaultExpiryTime: string
private readonly averageBlockTime: number

constructor(private nvmService: NeverminedService, private config: ConfigService) {
Expand Down Expand Up @@ -123,7 +123,7 @@ export class SubscriptionsService {
did: string,
userAddress: string,
endpoints: any,
expiryTime: number,
expiryTime: number | string,
headers?: any,
): Promise<string> {
return await new jose.EncryptJWT({
Expand All @@ -139,15 +139,17 @@ export class SubscriptionsService {
}

/**
* Get the expiration time in seconds for a subscription
* Get the expiration time for a subscription.
* The expiration time is generated in string format using common abbreviations:
* `seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y`
*
* @param contractAddress - The NFT-721 contract address of the subscription
* @param userAddress - The address of the user requesting a JWT token
*
* @throws {@link BadRequestException}
* @returns {@link Promise<number>} The expiration time in seconds
* @returns {@link Promise<string>} The expiration time
*/
public async getExpirationTime(contractAddress: string, userAddress: string): Promise<number> {
public async getExpirationTime(contractAddress: string, userAddress: string): Promise<string> {
// get subscription DDO
const subscriptionDdo = await this.getSubscriptionDdo(contractAddress)
// get duration
Expand Down Expand Up @@ -180,7 +182,7 @@ export class SubscriptionsService {
const currentDate = new Date()
const expiryTime = Math.floor(currentDate.getTime() / 1000) + secondsLeft

return expiryTime
return `${expiryTime} secs`
}

/**
Expand Down

0 comments on commit 399a5f0

Please sign in to comment.