Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes .ROBLOSECURITY= requirement when binding cookies #21

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
._*
src/
tests/
buildDocs/
docs/
docs_site/
node_modules/
./assets/
bun.lockb
tsconfig.tsbuildinfo
./build.ts
./build.ts
.env
Binary file modified bun.lockb
Binary file not shown.
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: "node",
setupFilesAfterEnv: ['dotenv/config'],
transform: {
"^.+.tsx?$": ["ts-jest", {}],
},
};
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@
"parse-roblox-errors": "^1.1.10"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/lodash": "^4.17.0",
"@types/node": "^22.1.0",
"delete-cli": "^0.1.3",
"dotenv": "^16.4.7",
"esbuild": "^0.21.5",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"tablemark": "^3.1.0",
"ts-arithmetic": "^0.1.1",
"ts-morph": "^22.0.0",
"delete-cli": "^0.1.3"
"ts-jest": "^29.2.5",
"ts-morph": "^22.0.0"
},
"peerDependencies": {
"typescript": "^5.0.0",
Expand All @@ -109,6 +113,7 @@
"scripts": {
"build:code": "delete dist && bun run ./build.ts && tsc --emitDeclarationOnly",
"build:docs": "bun run ./docs/buildDocs.ts",
"build": "bun run build:docs && bun run build:code"
"build": "bun run build:docs && bun run build:code",
"test": "bunx jest"
}
}
2 changes: 1 addition & 1 deletion src/apis/apiGroup/apiGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const createApiGroup: CreateApiGroupFn = ({ name:groupName, baseUrl, defa

const createCallApiMethod = (_baseUrl: UrlSecure = baseUrl): CallApiMethod<any, any, boolean> => async function(args) {
const overrides = this
const cookie = overrides?.cookie || config?.cookie
const cookie = overrides?.cookie ? `.ROBLOSECURITY=${overrides.cookie}` : config?.cookie;
const cloudKey = overrides?.cloudKey || config?.cloudKey
const oauthToken = overrides?.oauthToken

Expand Down
3 changes: 2 additions & 1 deletion src/apis/classic/badges/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./badges"
export * from "./badges"
export * from "./badges.types"
3 changes: 2 additions & 1 deletion src/apis/classic/catalog/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./catalog"
export * from "./catalog"
export * from "./catalog.types"
3 changes: 2 additions & 1 deletion src/apis/classic/develop/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./develop"
export * from "./develop"
export * from "./develop.types"
3 changes: 2 additions & 1 deletion src/apis/classic/developerProducts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./developerProducts"
export * from "./developerProducts"
export * from "./developerProducts.types"
3 changes: 2 additions & 1 deletion src/apis/classic/economy/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./economy"
export * from "./economy"
export * from "./economy.types"
3 changes: 2 additions & 1 deletion src/apis/classic/friends/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./friends"
export * from "./friends"
export * from "./friends.types"
3 changes: 2 additions & 1 deletion src/apis/classic/gamePasses/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./gamePasses"
export * from "./gamePasses"
export * from "./gamePasses.types"
3 changes: 2 additions & 1 deletion src/apis/classic/games/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./games"
export * from "./games"
export * from "./games.types"
3 changes: 2 additions & 1 deletion src/apis/classic/groups/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./groups"
export * from "./groups"
export * from "./groups.types"
3 changes: 2 additions & 1 deletion src/apis/classic/organizations/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./organizations"
export * from "./organizations"
export * from "./organizations.types"
3 changes: 2 additions & 1 deletion src/apis/classic/premiumFeatures/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./premiumFeatures"
export * from "./premiumFeatures"
export * from "./premiumFeatures.types"
3 changes: 2 additions & 1 deletion src/apis/classic/shareLinks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./shareLinks"
export * from "./shareLinks"
export * from "./shareLinks.types"
3 changes: 2 additions & 1 deletion src/apis/classic/subscriptions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./subscriptions"
export * from "./subscriptions"
export * from "./subscriptions.types"
3 changes: 2 additions & 1 deletion src/apis/classic/talent/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./talent"
export * from "./talent"
export * from "./talent.types"
3 changes: 2 additions & 1 deletion src/apis/classic/thumbnails/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./thumbnails"
export * from "./thumbnails"
export * from "./thumbnails.types"
3 changes: 2 additions & 1 deletion src/apis/classic/users/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./users"
export * from "./users"
export * from "./users.types"
3 changes: 2 additions & 1 deletion src/apis/classic/virtualEvents/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./virtualEvents"
export * from "./virtualEvents"
export * from "./virtualEvents.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/assets/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./assets"
export * from "./assets"
export * from "./assets.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/creatorStore/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./creatorStore"
export * from "./creatorStore"
export * from "./creatorStore.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/engine/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./engine"
export * from "./engine"
export * from "./engine.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/experiences/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./experiences"
export * from "./experiences"
export * from "./experiences.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/groups/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./groups"
export * from "./groups"
export * from "./groups.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/inventory/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./inventory"
export * from "./inventory"
export * from "./inventory.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/legacyFollowings/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./legacyFollowings"
export * from "./legacyFollowings"
export * from "./legacyFollowings.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/luauExecution/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./luauExecution"
export * from "./luauExecution"
export * from "./luauExecution.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/memoryStores/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./memoryStores"
export * from "./memoryStores"
export * from "./memoryStores.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/oauth/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./oauth"
export * from "./oauth"
export * from "./oauth.types"
2 changes: 1 addition & 1 deletion src/apis/cloud/orderedDataStores_V1/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./orderedDataStores_V1"

export * from "./orderedDataStores_V1.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/orderedDataStores_V2/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./orderedDataStores_V2"
export * from "./orderedDataStores_V2"
export * from "./orderedDataStores_V2.types"
2 changes: 1 addition & 1 deletion src/apis/cloud/standardDataStores_V1/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./standardDataStores_V1"

export * from "./standardDataStores_V1.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/standardDataStores_V2/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./standardDataStores_V2"
export * from "./standardDataStores_V2"
export * from "./standardDataStores_V2.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/subscriptions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./subscriptions"
export * from "./subscriptions"
export * from "./subscriptions.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/userRestrictions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./userRestrictions"
export * from "./userRestrictions"
export * from "./userRestrictions.types"
3 changes: 2 additions & 1 deletion src/apis/cloud/users/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./users"
export * from "./users"
export * from "./users.types"
3 changes: 3 additions & 0 deletions src/http/http.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ export class HttpResponse<Body extends any = any> {
}

export class HttpError {
name = "OpenBlox HttpError"
// message: string
type: "Csrf" | "Generic"
errors: AnyRobloxError[]
response: HttpResponse

constructor(type: "Csrf" | "Generic", response: HttpResponse, errors: AnyRobloxError[]) {
// this.message = `A ${type} HttpError occurred when making a request to: ${response.url.toString()}`
this.type = type
this.errors = errors
this.response = response
Expand Down
3 changes: 2 additions & 1 deletion src/http/httpHandler/httpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export const HttpHandler = async <RawData extends any = any>(

} else {
// Only Changes the saved csrf token to be that of the response csrf token if the cookie used was not an override.
if (headerCookie == config?.cookie) savedCsrfToken = responseCsrfToken
// Well, we do want to allow overrides
/* if (headerCookie == config?.cookie) */savedCsrfToken = responseCsrfToken

// retries the request with the new csrf token.
currentCsrfAttempt++
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as ClassicAPI from "./apis/classic/index"
export * as CloudAPI from "./apis/cloud/index"
40 changes: 40 additions & 0 deletions tests/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as OpenBlox from "../src/index";

// Set a ROBLOX_COOKIE environment variable to test authenticated requests.
// Replace parameters with valid objects that your account has access to.

describe('OpenBlox Cookie Auth', () => {
it('should use .env as default', async () => {
const result = await OpenBlox.ClassicAPI.ClassicUsersApi.authenticatedUserInfo();
expect(result.response.success).toBe(true);
});

it('should use context binded cookie', async () => {
const cookie = process.env.ROBLOX_COOKIE;
process.env.ROBLOX_COOKIE = undefined;

const result = await OpenBlox.ClassicAPI.ClassicUsersApi.authenticatedUserInfo.bind({ cookie })();
expect(result.response.success).toBe(true);

// Restores original state for other tests.
process.env.ROBLOX_COOKIE = cookie;
});

it('should use a csrf validated request', async () => {
const result = await OpenBlox.ClassicAPI.ClassicGroupsApi.updateGroupMemberRole({ groupId: 101010, roleId: 101010, userId: 101010 });
expect(result.response.success).toBe(true);
});

it('should use a csrf validated request using context binded cookie', async () => {
const cookie = process.env.ROBLOX_COOKIE;
process.env.ROBLOX_COOKIE = undefined;

const result = await OpenBlox.ClassicAPI.ClassicGroupsApi.updateGroupMemberRole.bind({ cookie })({ groupId: 101010, roleId: 101010, userId: 101010 });
expect(result.response.success).toBe(true);

// Restores original state for other tests.
process.env.ROBLOX_COOKIE = cookie;
});

test.todo("open cloud auth");
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"module": "NodeNext",
"moduleResolution": "NodeNext",

"types": ["jest", "node"],
"typeRoots": [ "./node_modules/@types" ]
},
"include": [ "./src" ],
Expand Down