Skip to content
Merged
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
29 changes: 19 additions & 10 deletions apps/meteor/app/api/server/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,23 @@ type Options = (
}
) & {
validateParams?: ValidateFunction;
authOrAnonRequired?: true;
};

type PartialThis = {
readonly request: Request & { query: Record<string, string> };
readonly response: Response;
};

type UserInfo = IUser & {
email?: string;
settings: {
profile: {};
preferences: unknown;
};
avatarUrl: string;
};

type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptions> = {
readonly requestIp: string;
urlParams: UrlParams<TPathPattern>;
Expand Down Expand Up @@ -113,16 +123,9 @@ type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptio
/* @deprecated */
isUserFromParams(): boolean;
/* @deprecated */
getUserInfo(me: IUser): TOptions extends { authRequired: true }
? IUser & {
email?: string;
settings: {
profile: {};
preferences: unknown;
};
avatarUrl: string;
}
: undefined;
getUserInfo(
me: IUser,
): TOptions extends { authRequired: true } ? UserInfo : TOptions extends { authOrAnonRequired: true } ? UserInfo | undefined : undefined;
insertUserObject<T>({ object, userId }: { object: { [key: string]: unknown }; userId: string }): { [key: string]: unknown } & T;
composeRoomWithLastMessage(room: IRoom, userId: string): IRoom;
} & (TOptions extends { authRequired: true }
Expand All @@ -131,6 +134,12 @@ type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptio
readonly userId: string;
readonly token: string;
}
: TOptions extends { authOrAnonRequired: true }
? {
readonly user?: IUser;
readonly userId?: string;
readonly token?: string;
}
: {
readonly user: null;
readonly userId: undefined;
Expand Down
28 changes: 28 additions & 0 deletions apps/meteor/app/api/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,34 @@ export class APIClass extends Restivus {
...getRestPayload(this.request.body),
});

// If the endpoint requires authentication only if anonymous read is disabled, load the user info if it was provided
if (!options.authRequired && options.authOrAnonRequired) {
const { 'x-user-id': userId, 'x-auth-token': userToken } = this.request.headers;
if (userId && userToken) {
this.user = Meteor.users.findOne(
{
'services.resume.loginTokens.hashedToken': Accounts._hashLoginToken(userToken),
'_id': userId,
},
{
fields: getDefaultUserFields(),
},
);

this.userId = this.user?._id;
}

if (!this.user && !settings.get('Accounts_AllowAnonymousRead')) {
return {
statusCode: 401,
body: {
status: 'error',
message: 'You must be logged in to do this.',
},
};
}
}

const objectForRateLimitMatch = {
IPAddr: this.requestIp,
route: `${this.request.route}${this.request.method.toLowerCase()}`,
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/ee/server/api/licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ API.v1.addRoute(

API.v1.addRoute(
'licenses.isEnterprise',
{ authRequired: true },
Comment thread
casalsgh marked this conversation as resolved.
Resolved
{ authOrAnonRequired: true },
{
get() {
const isEnterpriseEdtion = isEnterprise();
Expand Down