diff --git a/src/helper/CliUtils.ts b/src/helper/CliUtils.ts index 389c0987..675436bb 100644 --- a/src/helper/CliUtils.ts +++ b/src/helper/CliUtils.ts @@ -2,6 +2,8 @@ * Outputs the given JSON data. * * @param data - The data to be output. + * + * @internal */ export function output(data: unknown): void { // If data is string, output as is diff --git a/src/helper/JsonUtils.ts b/src/helper/JsonUtils.ts index b26d11a9..af9ac393 100644 --- a/src/helper/JsonUtils.ts +++ b/src/helper/JsonUtils.ts @@ -52,6 +52,8 @@ export function findByFilter(data: NonNullable, key: string, value: * @param data - The data on which search is to be performed. * @param value - The value to search. * @returns The key with the given value. + * + * @internal */ export function findKeyByValue(data: NonNullable, value: string): string | undefined { // Finding the key-value pairs that have the given value diff --git a/src/helper/StringUtils.ts b/src/helper/StringUtils.ts deleted file mode 100644 index b9d4d8ac..00000000 --- a/src/helper/StringUtils.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Converts the given API key to it's equivalent cookie string. - * - * @param apiKey - The API key representation of the cookie string. - * @returns - The cookie string retrieved from the API key. - */ -export function keyToCookie(apiKey: string): string { - return Buffer.from(apiKey, 'base64').toString('ascii'); -} diff --git a/src/services/public/AuthService.ts b/src/services/public/AuthService.ts index a6a8d01d..e8ae44d3 100644 --- a/src/services/public/AuthService.ts +++ b/src/services/public/AuthService.ts @@ -1,5 +1,6 @@ import { Auth } from 'rettiwt-auth'; +import { EApiErrors } from '../../enums/Api'; import { IRettiwtConfig } from '../../types/RettiwtConfig'; import { FetcherService } from './FetcherService'; @@ -19,6 +20,55 @@ export class AuthService extends FetcherService { super(config); } + /** + * Decodes the encoded cookie string. + * + * @param encodedCookies - The encoded cookie string to decode. + * @returns The decoded cookie string. + */ + public static decodeCookie(encodedCookies: string): string { + // Decoding the encoded cookie string + const decodedCookies: string = Buffer.from(encodedCookies, 'base64').toString('ascii'); + + return decodedCookies; + } + + /** + * Encodes the given cookie string. + * + * @param cookieString - The cookie string to encode. + * @returns The encoded cookie string. + */ + public static encodeCookie(cookieString: string): string { + // Encoding the cookie string to base64 + const encodedCookies: string = Buffer.from(cookieString).toString('base64'); + + return encodedCookies; + } + + /** + * Gets the user's id from the given API key. + * + * @param apiKey - The API key. + * @returns The user id associated with the API key. + */ + public static getUserId(apiKey: string): string { + // Getting the cookie string from the API key + const cookieString: string = AuthService.decodeCookie(apiKey); + + // Searching for the user id in the cookie string + const searchResults: string[] | null = cookieString.match(/((?<=twid="u=)(.*)(?="))|((?<=twid=u%3D)(.*)(?=;))/); + + // If user id was found + if (searchResults) { + return searchResults[0]; + } + // If user id was not found + else { + throw new Error(EApiErrors.BAD_AUTHENTICATION); + } + } + /** * Login to twitter as guest. * @@ -91,7 +141,7 @@ export class AuthService extends FetcherService { ).toHeader().cookie as string) ?? ''; // Converting the credentials to base64 string - apiKey = Buffer.from(apiKey).toString('base64'); + apiKey = AuthService.encodeCookie(apiKey); return apiKey; } diff --git a/src/services/public/FetcherService.ts b/src/services/public/FetcherService.ts index b66acb9a..cbb10917 100644 --- a/src/services/public/FetcherService.ts +++ b/src/services/public/FetcherService.ts @@ -9,7 +9,6 @@ import { requests } from '../../collections/Requests'; import { EApiErrors } from '../../enums/Api'; import { ELogActions } from '../../enums/Logging'; import { EResourceType } from '../../enums/Resource'; -import { keyToCookie } from '../../helper/StringUtils'; import { FetchArgs } from '../../models/args/FetchArgs'; import { PostArgs } from '../../models/args/PostArgs'; import { IErrorHandler } from '../../types/ErrorHandler'; @@ -18,6 +17,8 @@ import { IRettiwtConfig } from '../../types/RettiwtConfig'; import { ErrorService } from '../internal/ErrorService'; import { LogService } from '../internal/LogService'; +import { AuthService } from './AuthService'; + /** * The base service that handles all HTTP requests. * @@ -52,7 +53,7 @@ export class FetcherService { LogService.enabled = config?.logging ?? false; this.apiKey = config?.apiKey; this.guestKey = config?.guestKey; - this.userId = config?.apiKey ? this.getUserId(config.apiKey) : undefined; + this.userId = config?.apiKey ? AuthService.getUserId(config.apiKey) : undefined; this.authProxyUrl = config?.authProxyUrl ?? config?.proxyUrl; this.proxyUrl = config?.proxyUrl; this.timeout = config?.timeout ?? 0; @@ -86,7 +87,7 @@ export class FetcherService { // Logging LogService.log(ELogActions.GET, { target: 'USER_CREDENTIAL' }); - return new AuthCredential(keyToCookie(this.apiKey).split(';')); + return new AuthCredential(AuthService.decodeCookie(this.apiKey).split(';')); } else if (this.guestKey) { // Logging LogService.log(ELogActions.GET, { target: 'GUEST_CREDENTIAL' }); @@ -121,29 +122,6 @@ export class FetcherService { } } - /** - * Gets the authenticated user's id from the given API key. - * - * @param apiKey - The API key provided by the user. - * @returns The user id associated with the API key. - */ - private getUserId(apiKey: string): string { - // Getting the cookie string from the API key - const cookieString: string = keyToCookie(apiKey); - - // Searching for the user id in the cookie string - const searchResults: string[] | null = cookieString.match(/((?<=twid="u=)(.*)(?="))|((?<=twid=u%3D)(.*)(?=;))/); - - // If user id was found - if (searchResults) { - return searchResults[0]; - } - // If user id was not found - else { - throw new Error(EApiErrors.BAD_AUTHENTICATION); - } - } - /** * Validates the given args against the given resource. *