Skip to content

Commit

Permalink
storage: throw error if user uses the static site token (#147)
Browse files Browse the repository at this point in the history
* storage: throw error if user uses the static site token

* CR: rename method to validateStorageOrganizationType
  • Loading branch information
shomix authored Sep 11, 2023
1 parent 396c100 commit 383cfdb
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 149 deletions.
204 changes: 102 additions & 102 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spheron/core",
"version": "2.0.0",
"version": "2.0.1",
"description": "Shared core package for all sdk packages",
"keywords": [
"Storage",
Expand Down Expand Up @@ -35,6 +35,7 @@
"dist/"
],
"dependencies": {
"async-lock": "^1.4.0",
"axios": "1.1.2",
"eventsource": "^2.0.2",
"p-limit": "^3.0.0"
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./spheron-api/interfaces";
export * from "./upload-manager";
export * from "./spheron-api/request-interfaces";
export * from "./spheron-api/response-interfaces";
export * from "./scope-extractor";
40 changes: 40 additions & 0 deletions packages/core/src/scope-extractor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import asyncLock from "async-lock";
import SpheronApi from "./spheron-api";
import { TokenScope } from "./spheron-api/interfaces";
import { AppTypeEnum } from "./spheron-api/enums";

abstract class ScopeExtractor {
private readonly api: SpheronApi;
private scope: TokenScope | null = null;
private lock: asyncLock;

constructor(spheronApi: SpheronApi) {
this.api = spheronApi;
this.lock = new asyncLock();
}

protected async getScopeFromToken(): Promise<TokenScope> {
if (!this.scope) {
await this.lock.acquire("getScopeFromToken", async () => {
if (!this.scope) {
this.scope = await this.api.getTokenScope();
}
});
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.scope!;
}

protected async getOrganizationIdFromToken(): Promise<string> {
const scope = await this.getScopeFromToken();
return scope.organizations[0].id;
}

protected async getOrganizationTypeFromToken(): Promise<AppTypeEnum> {
const scope = await this.getScopeFromToken();
return scope.organizations[0].appType;
}
}

export { ScopeExtractor };
1 change: 1 addition & 0 deletions packages/core/src/spheron-api/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ enum DeploymentStatusEnum {
enum AppTypeEnum {
WEB_APP = "app",
COMPUTE = "compute",
STORAGE = "storage",
}

enum ProviderEnum {
Expand Down
Loading

0 comments on commit 383cfdb

Please sign in to comment.