@subql/apollo-links
Advanced tools
Comparing version 0.3.4 to 0.3.5-0
import { AuthMessage } from './eip712'; | ||
export declare function POST<T>(url: string, body: Record<string, string | number | undefined>): Promise<T>; | ||
export declare function GET<T>(url: string): Promise<T>; | ||
export declare function isTokenExpired(token: string): boolean; | ||
export declare function signMessage(msg: AuthMessage, sk: string, chainId: number): string; | ||
export declare function requestAuthToken(authUrl: string, msg: AuthMessage, sk: string, chainId: number): Promise<string>; |
@@ -8,27 +8,20 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.requestAuthToken = exports.signMessage = exports.isTokenExpired = exports.GET = exports.POST = void 0; | ||
exports.requestAuthToken = exports.signMessage = exports.isTokenExpired = void 0; | ||
const eth_sig_util_1 = require("@metamask/eth-sig-util"); | ||
const jwt_decode_1 = __importDefault(require("jwt-decode")); | ||
const buffer_1 = __importDefault(require("buffer")); | ||
const axios_1 = __importDefault(require("axios")); | ||
const eip712_1 = require("./eip712"); | ||
const query_1 = require("../query"); | ||
const Buffer = buffer_1.default.Buffer; | ||
async function POST(url, body) { | ||
const headers = { 'Content-Type': 'application/json' }; | ||
const res = await axios_1.default.post(url, body, { headers }); | ||
return res.data; | ||
} | ||
exports.POST = POST; | ||
async function GET(url) { | ||
const headers = { 'Content-Type': 'application/json' }; | ||
const res = await axios_1.default.get(url, { headers }); | ||
return res.data; | ||
} | ||
exports.GET = GET; | ||
function isTokenExpired(token) { | ||
if (!token) | ||
return true; | ||
const { exp } = (0, jwt_decode_1.default)(token); | ||
const currentDate = new Date().getTime(); | ||
return exp < currentDate; | ||
try { | ||
const { exp } = (0, jwt_decode_1.default)(token); | ||
const currentDate = new Date().getTime(); | ||
return exp < currentDate; | ||
} | ||
catch (_a) { | ||
return true; | ||
} | ||
} | ||
@@ -55,3 +48,3 @@ exports.isTokenExpired = isTokenExpired; | ||
const body = (0, eip712_1.createAuthRequestBody)(msg, signature, chainId); | ||
const res = await POST(authUrl, body); | ||
const res = await (0, query_1.POST)(authUrl, body); | ||
return res.token; | ||
@@ -58,0 +51,0 @@ } |
import { ApolloLink, FetchResult, NextLink, Observable, Operation } from '@apollo/client/core'; | ||
import { Message } from './eip712'; | ||
export interface AuthOptions extends Message { | ||
authUrl: string; | ||
import { Logger } from '../logger'; | ||
interface AuthOptions extends Message { | ||
indexerUrl: string; | ||
chainId: number; | ||
sk?: string; | ||
sk: string; | ||
} | ||
export declare class AuthLink extends ApolloLink { | ||
private _options; | ||
private _logger; | ||
private _token; | ||
constructor(options: AuthOptions); | ||
constructor(options: AuthOptions, logger: Logger); | ||
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null; | ||
get queryEndpoint(): string; | ||
private generateMessage; | ||
private requestToken; | ||
private getUrlAndToken; | ||
} | ||
export {}; |
@@ -9,5 +9,6 @@ "use strict"; | ||
class AuthLink extends core_1.ApolloLink { | ||
constructor(options) { | ||
constructor(options, logger) { | ||
super(); | ||
this._options = options; | ||
this._logger = logger; | ||
this._token = ''; | ||
@@ -20,9 +21,20 @@ } | ||
let sub; | ||
this.requestToken().then((token) => { | ||
operation.setContext({ headers: { authorization: `Bearer ${token}` } }); | ||
this.getUrlAndToken().then((data) => { | ||
if (data) { | ||
const { token, url } = data; | ||
const headers = { authorization: `Bearer ${token}` }; | ||
operation.setContext({ url, headers }); | ||
} | ||
}) | ||
.catch((error) => observer.error(error)) | ||
.finally(() => { | ||
sub = forward(operation).subscribe(observer); | ||
}).catch((error) => observer.error(error)); | ||
return () => sub.unsubscribe(); | ||
}); | ||
return () => sub === null || sub === void 0 ? void 0 : sub.unsubscribe(); | ||
}); | ||
} | ||
get queryEndpoint() { | ||
const url = new URL('/query', this._options.indexerUrl); | ||
return url.toString(); | ||
} | ||
generateMessage() { | ||
@@ -33,15 +45,13 @@ const { indexer, consumer, agreement, deploymentId } = this._options; | ||
} | ||
async requestToken() { | ||
async getUrlAndToken() { | ||
if (!(0, authHelper_1.isTokenExpired)(this._token)) | ||
return this._token; | ||
const { indexer, deploymentId, sk, chainId, authUrl } = this._options; | ||
if (!sk) { | ||
const host = authUrl === null || authUrl === void 0 ? void 0 : authUrl.trim().replace(/\/+$/, ''); | ||
const res = await (0, authHelper_1.POST)(`${host}/token`, { deploymentId, indexer }); | ||
this._token = res.token; | ||
return this._token; | ||
} | ||
return { token: this._token, url: this.queryEndpoint }; | ||
const { sk, chainId, agreement, indexerUrl } = this._options; | ||
if (!chainId || !agreement) | ||
throw new Error('chainId and agreement are required'); | ||
const message = this.generateMessage(); | ||
this._token = await (0, authHelper_1.requestAuthToken)(authUrl, message, sk, chainId); | ||
return this._token; | ||
const tokenUrl = new URL('/token', indexerUrl); | ||
const authToken = await (0, authHelper_1.requestAuthToken)(tokenUrl.toString(), message, sk, chainId); | ||
this._token = authToken; | ||
return { token: authToken, url: this.queryEndpoint }; | ||
} | ||
@@ -48,0 +58,0 @@ } |
@@ -36,3 +36,3 @@ "use strict"; | ||
exports.buildTypedMessage = buildTypedMessage; | ||
function createAuthRequestBody(message, signature, chainId = 1287) { | ||
function createAuthRequestBody(message, signature, chainId = 137) { | ||
const { consumer, indexer, agreement, deploymentId, timestamp } = message; | ||
@@ -39,0 +39,0 @@ const baseBody = { |
export * from './authHelper'; | ||
export * from './eip712'; | ||
export * from './clusterAuthLink'; | ||
export * from './authLink'; |
@@ -21,3 +21,4 @@ "use strict"; | ||
__exportStar(require("./eip712"), exports); | ||
__exportStar(require("./clusterAuthLink"), exports); | ||
__exportStar(require("./authLink"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
export * from './auth-http-link/authHttpLink'; | ||
export * from './authHttpLink'; | ||
export * from './auth-link'; |
@@ -19,4 +19,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./auth-http-link/authHttpLink"), exports); | ||
__exportStar(require("./authHttpLink"), exports); | ||
__exportStar(require("./auth-link"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@subql/apollo-links", | ||
"version": "0.3.4", | ||
"version": "0.3.5-0", | ||
"description": "SubQuery Network - graphql links", | ||
@@ -13,2 +13,3 @@ "main": "dist/index.js", | ||
"@metamask/eth-sig-util": "4.0.1", | ||
"apollo-link-error": "^1.1.13", | ||
"axios": "^0.27.2", | ||
@@ -21,2 +22,3 @@ "buffer": "^6.0.3", | ||
"apollo": "^2.34.0", | ||
"pino": "^8.14.1", | ||
"typescript": "^4.6.4" | ||
@@ -28,3 +30,3 @@ }, | ||
}, | ||
"stableVersion": "0.3.3-1" | ||
"stableVersion": "0.3.4" | ||
} |
@@ -7,29 +7,18 @@ // Copyright 2020-2022 SubQuery Pte Ltd authors & contributors | ||
import buffer from 'buffer'; | ||
import axios from 'axios'; | ||
import { AuthMessage, buildTypedMessage, createAuthRequestBody } from './eip712'; | ||
import { POST } from '../query'; | ||
const Buffer = buffer.Buffer; | ||
export async function POST<T>(url: string, body: Record<string, string | number | undefined>) { | ||
const headers = { 'Content-Type': 'application/json' }; | ||
const res = await axios.post<T>(url, body, { headers }); | ||
return res.data; | ||
} | ||
export async function GET<T>(url: string) { | ||
const headers = { 'Content-Type': 'application/json' }; | ||
const res = await axios.get<T>(url, { headers }); | ||
return res.data; | ||
} | ||
export function isTokenExpired(token: string): boolean { | ||
if (!token) return true; | ||
const { exp } = jwt_decode(token) as { exp: number }; | ||
const currentDate = new Date().getTime(); | ||
return exp < currentDate; | ||
try { | ||
const { exp } = jwt_decode(token) as { exp: number }; | ||
const currentDate = new Date().getTime(); | ||
return exp < currentDate; | ||
} catch { | ||
return true; | ||
} | ||
} | ||
@@ -36,0 +25,0 @@ |
@@ -7,9 +7,10 @@ // Copyright 2020-2022 SubQuery Pte Ltd authors & contributors | ||
import { isTokenExpired, POST, requestAuthToken } from './authHelper'; | ||
import { isTokenExpired, requestAuthToken } from './authHelper'; | ||
import { Message } from './eip712'; | ||
import { Logger } from '../logger'; | ||
export interface AuthOptions extends Message { | ||
authUrl: string; // the url for geting token | ||
chainId: number; // chainId for the network | ||
sk?: string; // `sk` of the consumer or corresponding controller account | ||
interface AuthOptions extends Message { | ||
indexerUrl: string; // indexer url | ||
chainId: number; // chainId for the network | ||
sk: string; // `sk` of the consumer or corresponding controller account | ||
} | ||
@@ -19,7 +20,9 @@ | ||
private _options: AuthOptions; | ||
private _logger: Logger; | ||
private _token: string; | ||
constructor(options: AuthOptions) { | ||
constructor(options: AuthOptions, logger: Logger) { | ||
super(); | ||
this._options = options; | ||
this._logger = logger; | ||
this._token = ''; | ||
@@ -33,11 +36,23 @@ } | ||
let sub: Subscription; | ||
this.requestToken().then((token) => { | ||
operation.setContext({ headers: { authorization: `Bearer ${token}` } }); | ||
this.getUrlAndToken().then((data) => { | ||
if (data) { | ||
const { token, url } = data; | ||
const headers = { authorization: `Bearer ${token}` }; | ||
operation.setContext({ url, headers }); | ||
} | ||
}) | ||
.catch((error) => observer.error(error)) | ||
.finally(() => { | ||
sub = forward(operation).subscribe(observer); | ||
}).catch((error) => observer.error(error)); | ||
}); | ||
return () => sub.unsubscribe(); | ||
return () => sub?.unsubscribe(); | ||
}); | ||
} | ||
get queryEndpoint() { | ||
const url = new URL('/query', this._options.indexerUrl); | ||
return url.toString(); | ||
} | ||
private generateMessage() { | ||
@@ -49,19 +64,16 @@ const { indexer, consumer, agreement, deploymentId } = this._options; | ||
private async requestToken(): Promise<string> { | ||
if (!isTokenExpired(this._token)) return this._token; | ||
private async getUrlAndToken(): Promise<{ url: string; token: string } | undefined> { | ||
if (!isTokenExpired(this._token)) return { token: this._token, url: this.queryEndpoint }; | ||
const { indexer, deploymentId, sk, chainId, authUrl } = this._options; | ||
const { sk, chainId, agreement, indexerUrl } = this._options; | ||
if (!sk) { | ||
const host = authUrl?.trim().replace(/\/+$/, ''); | ||
const res = await POST<{ token: string }>(`${host}/token`, { deploymentId, indexer }); | ||
this._token = res.token; | ||
return this._token; | ||
} | ||
if (!chainId || !agreement) throw new Error('chainId and agreement are required'); | ||
const message = this.generateMessage(); | ||
this._token = await requestAuthToken(authUrl, message, sk, chainId) | ||
const tokenUrl = new URL('/token', indexerUrl); | ||
const authToken = await requestAuthToken(tokenUrl.toString(), message, sk, chainId) | ||
this._token = authToken; | ||
return this._token; | ||
return { token: authToken, url: this.queryEndpoint }; | ||
} | ||
} |
@@ -55,3 +55,3 @@ // Copyright 2020-2022 SubQuery Pte Ltd authors & contributors | ||
export function createAuthRequestBody(message: AuthMessage, signature: string, chainId = 1287) { | ||
export function createAuthRequestBody(message: AuthMessage, signature: string, chainId = 137) { | ||
const { consumer, indexer, agreement, deploymentId, timestamp } = message; | ||
@@ -58,0 +58,0 @@ const baseBody = { |
@@ -6,2 +6,3 @@ // Copyright 2020-2022 SubQuery Pte Ltd authors & contributors | ||
export * from './eip712'; | ||
export * from './clusterAuthLink'; | ||
export * from './authLink'; |
// Copyright 2020-2022 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
export * from './auth-http-link/authHttpLink'; | ||
export * from './authHttpLink'; | ||
export * from './auth-link'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115441
65
1039
8
3
1
+ Addedapollo-link-error@^1.1.13
+ Added@wry/equality@0.1.11(transitive)
+ Addedapollo-link@1.2.14(transitive)
+ Addedapollo-link-error@1.1.13(transitive)
+ Addedapollo-link-http-common@0.2.16(transitive)
+ Addedapollo-utilities@1.3.4(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedgraphql@15.9.0(transitive)
+ Addedts-invariant@0.4.4(transitive)
+ Addedtslib@1.14.1(transitive)
+ Addedzen-observable-ts@0.8.21(transitive)