Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishikant181 committed Sep 2, 2024
1 parent f206c0a commit 3e06816
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/helper/CliUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/helper/JsonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export function findByFilter<T>(data: NonNullable<unknown>, 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<unknown>, value: string): string | undefined {
// Finding the key-value pairs that have the given value
Expand Down
9 changes: 0 additions & 9 deletions src/helper/StringUtils.ts

This file was deleted.

52 changes: 51 additions & 1 deletion src/services/public/AuthService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Auth } from 'rettiwt-auth';

import { EApiErrors } from '../../enums/Api';
import { IRettiwtConfig } from '../../types/RettiwtConfig';

import { FetcherService } from './FetcherService';
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down
30 changes: 4 additions & 26 deletions src/services/public/FetcherService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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' });
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 3e06816

Please sign in to comment.