Skip to content

Commit

Permalink
Merge pull request #700 from nevermined-io/feat/support-download-with…
Browse files Browse the repository at this point in the history
…-sessionKey

feat: add auxiliar methods to support download with sessionKeys
  • Loading branch information
eruizgar91 authored Aug 8, 2024
2 parents a4c7baa + 107dd54 commit 264ad6d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nevermined-io/sdk",
"version": "3.0.23-rc0",
"version": "3.0.23",
"description": "Javascript SDK for connecting with Nevermined Data Platform ",
"main": "./dist/node/sdk.js",
"typings": "./dist/node/sdk.d.ts",
Expand All @@ -14,7 +14,7 @@
"jest:test": "jest --no-cache --coverage --rootDir test -w 1 ",
"test": "mocha --config test/.mocharc.json ",
"test:all": "mocha --config test/.mocharc.json ./test/**/*.test.ts",
"integration": "export NETWORK_NAME=geth-localnet ;mocha --config integration/.mocharc.json ",
"integration": "export NETWORK_NAME=${NETWORK_NAME} ;mocha --config integration/.mocharc.json ",
"integration:all": "export NETWORK_NAME=geth-localnet;mocha --config integration/.mocharc.json ./integration/nevermined/*.test.ts",
"integration:subgraph": "mocha --config integration/.mocharc.json ./integration/**/*.test.subgraph.ts",
"integration:aave": "mocha --config aave_integration/.mocharc.json ./aave_integration/**/*.test.ts",
Expand Down
15 changes: 9 additions & 6 deletions src/nevermined/utils/JwtUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { deflateSync, inflateSync } from 'fflate'
import { JWSHeaderParameters, SignJWT, decodeJwt, importJWK } from 'jose'
import { Account, Hash, LocalAccount, hexToBytes, toHex } from 'viem'
import { urlSafeBase64Decode, urlSafeBase64Encode } from '../../common/helpers'
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract'
import { NvmAccount } from '../../models/NvmAccount'
import { getChecksumAddress } from '../../nevermined/utils/BlockchainViemUtils'
import { SignatureUtils } from '../../nevermined/utils/SignatureUtils'
import { Babysig, Eip712Data } from '../../types/GeneralTypes'
import { deflateSync, inflateSync } from 'fflate'
import { urlSafeBase64Decode, urlSafeBase64Encode } from '../../common/helpers'

export class EthSignJWT extends SignJWT {
protectedHeader: JWSHeaderParameters
Expand Down Expand Up @@ -63,14 +63,12 @@ export class EthSignJWT extends SignJWT {
} else {
sign = await signatureUtils.signText(decoder.decode(data), account)
}

const input = hexToBytes(sign)

const signed = this.base64url(input)
const grantToken = `${decoder.decode(encodedHeader)}.${decoder.decode(
encodedPayload,
)}.${signed}`

return grantToken
}

Expand Down Expand Up @@ -329,6 +327,7 @@ export class JwtUtils extends Instantiable {
account: NvmAccount,
buyer?: string,
babysig?: Babysig,
nvmApiKey?: string,
): Promise<string> {
const cacheKey = this.generateCacheKey(agreementId, account.getId(), did)

Expand All @@ -341,10 +340,14 @@ export class JwtUtils extends Instantiable {
buyer,
babysig,
)
const accessToken = await this.nevermined.services.node.fetchToken(grantToken, 1)

const accessToken =
account.getType() === 'sessionKey'
? await this.nevermined.services.node.fetchToken(grantToken, 1, nvmApiKey)
: await this.nevermined.services.node.fetchToken(grantToken, 1)

// TODO: enable the cache back when this issue is fixed in the Node: https://github.com/nevermined-io/node/issues/225
// this.tokenCache.set(cacheKey, accessToken)

return accessToken
} else {
return this.nevermined.utils.jwt.tokenCache.get(cacheKey) as string
Expand Down
30 changes: 21 additions & 9 deletions src/nevermined/utils/WebServiceConnector.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BodyInit, RequestInit, Response } from 'node-fetch'
import fs, { ReadStream } from 'fs'
import { InstantiableConfig } from '../../Instantiable.abstract'
import FormData from 'form-data'
import * as path from 'path'
import fs, { ReadStream } from 'fs'
import fileDownload from 'js-file-download'
import { BodyInit, RequestInit, Response } from 'node-fetch'
import * as path from 'path'
import { URL } from 'whatwg-url'
import { JwtUtils } from '../../nevermined/utils/JwtUtils'
import { HttpError } from '../../errors/NeverminedErrors'
import { InstantiableConfig } from '../../Instantiable.abstract'
import { JwtUtils } from '../../nevermined/utils/JwtUtils'

let fetch
if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -162,14 +162,26 @@ export class WebServiceConnector {
return this.fetch(url, { method: 'POST', body: form })
}

public async fetchToken(url: string, grantToken: string, numberTries = 1): Promise<Response> {
public async fetchToken(
url: string,
grantToken: string,
numberTries = 1,
nvmApiKey?: string,
): Promise<Response> {
const bodyParams = new URLSearchParams({
client_assertion_type: JwtUtils.CLIENT_ASSERTION_TYPE,
client_assertion: grantToken,
})

if (nvmApiKey) {
bodyParams.append('nevermined_api_key', nvmApiKey)
}

return await fetch(
url,
{
method: 'POST',
body: `client_assertion_type=${encodeURI(
JwtUtils.CLIENT_ASSERTION_TYPE,
)}&client_assertion=${encodeURI(grantToken)}`,
body: bodyParams.toString(),
headers: {
'Content-type': 'application/x-www-form-urlencoded',
},
Expand Down
24 changes: 17 additions & 7 deletions src/services/node/NeverminedNode.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract'
import { ReadStream } from 'fs'
import { Babysig, ERCType } from '../../types/GeneralTypes'
import { PublishMetadataOptions } from '../../types/MetadataTypes'
import { ImmutableBackends, MetaDataExternalResource, ServiceType } from '../../types/DDOTypes'
import { HttpError, NeverminedNodeError } from '../../errors/NeverminedErrors'
import { DDO } from '../../ddo/DDO'
import { HttpError, NeverminedNodeError } from '../../errors/NeverminedErrors'
import { Instantiable, InstantiableConfig } from '../../Instantiable.abstract'
import { NvmAccount } from '../../models/NvmAccount'
import { ImmutableBackends, MetaDataExternalResource, ServiceType } from '../../types/DDOTypes'
import { Babysig, ERCType } from '../../types/GeneralTypes'
import { PublishMetadataOptions } from '../../types/MetadataTypes'
import { noZeroX } from '../../utils/ConversionTypeHelpers'

const apiPath = '/api/v1/node/services'
Expand Down Expand Up @@ -85,6 +85,10 @@ export class NeverminedNode extends Instantiable {
return `${this.url}${apiPath}/oauth/token`
}

public getFetchTokenWithNvmApiKeyEndpoint() {
return `${this.url}${apiPath}/oauth/nvmApiKey`
}

public getUploadFilecoinEndpoint() {
return `${this.url}${apiPath}/upload/filecoin`
}
Expand Down Expand Up @@ -425,11 +429,17 @@ export class NeverminedNode extends Instantiable {
}
}

public async fetchToken(grantToken: string, numberTries = 3): Promise<string> {
public async fetchToken(
grantToken: string,
numberTries = 3,
nvmApiKey?: string,
): Promise<string> {
const url = nvmApiKey ? this.getFetchTokenWithNvmApiKeyEndpoint() : this.getFetchTokenEndpoint()
const response = await this.nevermined.utils.fetch.fetchToken(
this.getFetchTokenEndpoint(),
url,
grantToken,
numberTries,
nvmApiKey,
)

if (!response.ok) {
Expand Down
2 changes: 1 addition & 1 deletion webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
assert: require.resolve('assert/'),
path: require.resolve('path-browserify'),
stream: require.resolve('stream-browserify'),
vm: require.resolve("vm-browserify"),
vm: require.resolve('vm-browserify'),
zlib: false,
fs: false,
},
Expand Down

0 comments on commit 264ad6d

Please sign in to comment.