Skip to content

Commit

Permalink
fix: remove un wanted breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
Juiced66 committed Oct 31, 2024
1 parent f3139c1 commit a351f96
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
19 changes: 10 additions & 9 deletions features/step_definitions/auth-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ Given(
const previousToken = this.sdk.jwt;
const token = _.get(this.props, "result._source.token") || this.props.token;

should(token).not.be.undefined();

this.sdk.jwt = token;

const { valid } = await this.sdk.auth.checkToken();

this.sdk.jwt = previousToken;

if (not) {
should(await this.sdk.auth.checkToken()).throwError({
id: "services.storage.not_found",
});
should(valid).be.false("Provided token is valid");
} else {
should(token).not.be.undefined();

this.sdk.jwt = token;
const { valid } = await this.sdk.auth.checkToken();
this.sdk.jwt = previousToken;
should(valid).be.true("Provided token is invalid");
}
},
);

Given("I save the created API key", function () {
this.props.token = this.props.result.token;
this.props.token = this.props.result._source.token;
});

Given(
Expand Down
18 changes: 17 additions & 1 deletion lib/core/security/tokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,24 @@ export class TokenRepository extends ObjectRepository<Token> {

if (isApiKey) {
const fingerprint = sha256(token);

Check failure on line 366 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Delete `······`
const userApiKeys = await ApiKey.search({ query : {

Check failure on line 367 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Replace `·query·` with `⏎········query`
term: {

Check failure on line 368 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Insert `··`
userId: decoded._id,

Check failure on line 369 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Insert `··`
},

Check failure on line 370 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Insert `··`
} });

Check failure on line 371 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Replace `······}` with `········},⏎·····`

if (userApiKeys.length === 0) {
throw securityError.get("invalid");
}

const targetApiKey = userApiKeys.find((apiKey) => apiKey.fingerprint === fingerprint);

Check failure on line 377 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Replace `(apiKey)·=>·apiKey.fingerprint·===·fingerprint);` with `⏎········(apiKey)·=>·apiKey.fingerprint·===·fingerprint,`

Check failure on line 378 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Insert `);⏎`
if(!targetApiKey) {

Check failure on line 379 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Expected space(s) after "if"

Check failure on line 379 in lib/core/security/tokenRepository.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js

Insert `·`
throw securityError.get("invalid");
}

const apiKey = await ApiKey.load(decoded._id, fingerprint);
const apiKey = await ApiKey.load(decoded._id, targetApiKey._id);

const userToken = new Token({
_id: `${decoded._id}#${token}`,
Expand Down
33 changes: 11 additions & 22 deletions lib/model/storage/apiKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ApiKey extends BaseModel {
},
apiKeyId || fingerprint,
);

console.log(apiKey)
await apiKey.save({ refresh, userId: creatorId });

apiKey.token = token.jwt;
Expand All @@ -128,29 +128,18 @@ class ApiKey extends BaseModel {
*
* @returns {ApiKey}
*/
static async load(userId, fingerprint) {
const query = {
term: {
userId,
},
};

const apiKeys = await super.search({ query });
if (apiKeys.length > 0) {
const apiKey = apiKeys.find((a) => a.fingerprint === fingerprint);
if (userId !== apiKey.userId) {
throw kerror.get("services", "storage", "not_found", fingerprint, {
message: `ApiKey "${fingerprint}" not found for user "${userId}".`,
});
}

return apiKey;
static async load(userId, id) {
const apiKey = await super.load(id);

if (userId !== apiKey.userId) {
throw kerror.get("services", "storage", "not_found", id, {
message: `ApiKey "${id}" not found for user "${userId}".`,
});
}
throw kerror.get("services", "storage", "not_found", fingerprint, {
message: `ApiKey "${fingerprint}" not found for user "${userId}".`,
});
}

return apiKey;
}

/**
* Deletes API keys for an user
*
Expand Down
13 changes: 3 additions & 10 deletions test/model/storage/apiKey.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,16 @@ describe("ApiKey", () => {

describe("ApiKey.load", () => {
it("should throw if the key does not belong to the provided user", async () => {
const searchStub = sinon
.stub(BaseModel, "search")
const loadStub = sinon
.stub(BaseModel, "load")
.resolves({ userId: "mylehuong" });

const promise = ApiKey.load("aschen", "api-key-id");

await should(promise).be.rejectedWith({
id: "services.storage.not_found",
});

should(searchStub).be.calledWith({
query: {
term: {
userId: "aschen",
},
},
});
should(loadStub).be.calledWith("api-key-id");
});
});

Expand Down

0 comments on commit a351f96

Please sign in to comment.