Skip to content

Commit f8f6da6

Browse files
committed
[server] add Unauthenticated decorator for public-api
1 parent cb32240 commit f8f6da6

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

components/server/src/api/server.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { APITeamsService as TeamsServiceAPI } from "./teams";
4141
import { APIUserService as UserServiceAPI } from "./user";
4242
import { WorkspaceServiceAPI } from "./workspace-service-api";
4343
import { AuthProviderServiceAPI } from "./auth-provider-service-api";
44+
import { Unauthenticated } from "./unauthenticated";
4445

4546
decorate(injectable(), PublicAPIConverter);
4647

@@ -213,10 +214,14 @@ export class API {
213214
};
214215

215216
const apply = async <T>(): Promise<T> => {
216-
const subjectId = await self.verify(context);
217-
await rateLimit(subjectId);
218-
context.user = await self.ensureFgaMigration(subjectId);
219-
217+
const unauthenticated = Unauthenticated.get(target, prop);
218+
if (unauthenticated) {
219+
// TODO(at) add a low rate limit
220+
} else {
221+
const subjectId = await self.verify(context);
222+
await rateLimit(subjectId);
223+
context.user = await self.ensureFgaMigration(subjectId);
224+
}
220225
return Reflect.apply(target[prop as any], target, args);
221226
};
222227
if (grpc_type === "unary" || grpc_type === "client_stream") {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import * as chai from "chai";
8+
import { Unauthenticated } from "./unauthenticated";
9+
10+
const expect = chai.expect;
11+
12+
class Foo {
13+
@Unauthenticated()
14+
async fooUnauthenticated() {}
15+
16+
async foo() {}
17+
}
18+
19+
describe("Unauthenticated decorator", function () {
20+
const foo = new Foo();
21+
22+
it("function is decorated", function () {
23+
expect(Unauthenticated.get(foo, "fooUnauthenticated")).to.be.true;
24+
});
25+
it("function is not decorated", function () {
26+
expect(Unauthenticated.get(foo, "foo")).to.be.false;
27+
});
28+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
const UNAUTHENTICATED_METADATA_KEY = Symbol("Unauthenticated");
8+
9+
export function Unauthenticated() {
10+
return Reflect.metadata(UNAUTHENTICATED_METADATA_KEY, true);
11+
}
12+
13+
export namespace Unauthenticated {
14+
export function get(target: Object, properyKey: string | symbol): boolean {
15+
return !!Reflect.getMetadata(UNAUTHENTICATED_METADATA_KEY, target, properyKey);
16+
}
17+
}

0 commit comments

Comments
 (0)