Skip to content

Commit 67513a2

Browse files
authored
Merge pull request #3432 from SeedCompany/drop-session
2 parents 79c87eb + f887ce0 commit 67513a2

File tree

231 files changed

+1581
-3134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

231 files changed

+1581
-3134
lines changed

src/components/authentication/authentication.gel.repository.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,26 @@ export class AuthenticationGelRepository
165165
},
166166
);
167167

168-
async getCurrentPasswordHash(session: Session) {
169-
return await this.db.run(this.getCurrentPasswordHashQuery, {
170-
userId: session.userId,
171-
});
168+
async getCurrentPasswordHash() {
169+
return await this.db.run(this.getCurrentPasswordHashQuery, {});
172170
}
173-
private readonly getCurrentPasswordHashQuery = e.params(
174-
{ userId: e.uuid },
175-
({ userId }) => {
176-
const user = e.cast(e.User, userId);
177-
const identity = e.select(e.Auth.Identity, () => ({
178-
filter_single: { user },
179-
}));
180-
return identity.passwordHash;
181-
},
182-
);
171+
private readonly getCurrentPasswordHashQuery = e.params({}, () => {
172+
const user = e.global.currentUser;
173+
const identity = e.select(e.Auth.Identity, () => ({
174+
filter_single: { user },
175+
}));
176+
return identity.passwordHash;
177+
});
183178

184-
async updatePassword(newPasswordHash: string, session: Session) {
179+
async updatePassword(newPasswordHash: string) {
185180
await this.db.run(this.updatePasswordQuery, {
186-
userId: session.userId,
187181
passwordHash: newPasswordHash,
188182
});
189183
}
190184
private readonly updatePasswordQuery = e.params(
191-
{ userId: e.uuid, passwordHash: e.str },
192-
({ userId, passwordHash }) => {
193-
const user = e.cast(e.User, userId);
185+
{ passwordHash: e.str },
186+
({ passwordHash }) => {
187+
const user = e.global.currentUser;
194188
const identity = e.assert_exists(
195189
e.select(e.Auth.Identity, () => ({
196190
filter_single: { user },

src/components/authentication/authentication.repository.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class AuthenticationRepository {
222222
return result?.roles ?? [];
223223
}
224224

225-
async getCurrentPasswordHash(session: Session) {
225+
async getCurrentPasswordHash() {
226226
const result = await this.db
227227
.query()
228228
.match([
@@ -236,10 +236,7 @@ export class AuthenticationRepository {
236236
return result?.passwordHash ?? null;
237237
}
238238

239-
async updatePassword(
240-
newPasswordHash: string,
241-
session: Session,
242-
): Promise<void> {
239+
async updatePassword(newPasswordHash: string): Promise<void> {
243240
await this.db
244241
.query()
245242
.match([

src/components/authentication/authentication.service.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class AuthenticationService {
6969
const users = this.moduleRef.get(userMod.UserService, { strict: false });
7070
userId = await this.gel.usingOptions(
7171
disableAccessPolicies,
72-
async () => await users.create(input, session),
72+
async () => await users.create(input),
7373
);
7474
} catch (e) {
7575
// remap field prop as `email` field is at a different location in register() than createPerson()
@@ -85,14 +85,17 @@ export class AuthenticationService {
8585
return userId;
8686
}
8787

88-
async login(input: LoginInput, session: Session): Promise<ID> {
88+
async login(input: LoginInput): Promise<ID> {
8989
const hash = await this.repo.getPasswordHash(input);
9090

9191
if (!(await this.crypto.verify(hash, input.password))) {
9292
throw new UnauthenticatedException('Invalid credentials');
9393
}
9494

95-
const userId = await this.repo.connectSessionToUser(input, session);
95+
const userId = await this.repo.connectSessionToUser(
96+
input,
97+
this.sessionHost.current,
98+
);
9699

97100
if (!userId) {
98101
throw new ServerException('Login failed');
@@ -256,21 +259,20 @@ export class AuthenticationService {
256259
async changePassword(
257260
oldPassword: string,
258261
newPassword: string,
259-
session: Session,
260262
): Promise<void> {
261263
if (!oldPassword)
262264
throw new InputException('Old Password Required', 'oldPassword');
263265

264-
const hash = await this.repo.getCurrentPasswordHash(session);
266+
const hash = await this.repo.getCurrentPasswordHash();
265267

266268
if (!(await this.crypto.verify(hash, oldPassword))) {
267269
throw new UnauthenticatedException('Invalid credentials');
268270
}
269271

270272
const newPasswordHash = await this.crypto.hash(newPassword);
271-
await this.repo.updatePassword(newPasswordHash, session);
273+
await this.repo.updatePassword(newPasswordHash);
272274

273-
await this.repo.deactivateAllOtherSessions(session);
275+
await this.repo.deactivateAllOtherSessions(this.sessionHost.current);
274276
}
275277

276278
async forgotPassword(email: string): Promise<void> {
@@ -288,10 +290,7 @@ export class AuthenticationService {
288290
});
289291
}
290292

291-
async resetPassword(
292-
{ token, password }: ResetPasswordInput,
293-
session: Session,
294-
): Promise<void> {
293+
async resetPassword({ token, password }: ResetPasswordInput): Promise<void> {
295294
const emailToken = await this.repo.findEmailToken(token);
296295
if (!emailToken) {
297296
throw new InputException('Token is invalid', 'TokenInvalid');
@@ -306,7 +305,7 @@ export class AuthenticationService {
306305
await this.repo.updatePasswordViaEmailToken(emailToken, pash);
307306
await this.repo.deactivateAllOtherSessionsByEmail(
308307
emailToken.email,
309-
session,
308+
this.sessionHost.current,
310309
);
311310
await this.repo.removeAllEmailTokensForEmail(emailToken.email);
312311
}

src/components/authentication/extra-info.resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function AuthExtraInfoResolver(concreteClass: AbstractClassType<any>) {
1717

1818
@ResolveField(() => BetaFeatures)
1919
betaFeatures(@AnonSession() session: Session): BetaFeatures {
20-
const privileges = this.privileges.for(session, BetaFeatures);
20+
const privileges = this.privileges.for(BetaFeatures);
2121
const { props } = EnhancedResource.of(BetaFeatures);
2222
return mapValues.fromList([...props], (prop) =>
2323
privileges.can('edit', prop),

src/components/authentication/login.resolver.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ export class LoginResolver {
3030
`,
3131
})
3232
@Anonymous()
33-
async login(
34-
@Args('input') input: LoginInput,
35-
@AnonSession() session: Session,
36-
): Promise<LoginOutput> {
37-
const user = await this.authentication.login(input, session);
33+
async login(@Args('input') input: LoginInput): Promise<LoginOutput> {
34+
const user = await this.authentication.login(input);
3835
await this.authentication.refreshCurrentSession();
3936
return { user };
4037
}
@@ -61,7 +58,7 @@ export class LoginResolver {
6158
}
6259

6360
@ResolveField(() => [Power])
64-
async powers(@AnonSession() session: Session): Promise<Power[]> {
65-
return [...this.privileges.forUser(session).powers];
61+
async powers(): Promise<Power[]> {
62+
return [...this.privileges.powers];
6663
}
6764
}

src/components/authentication/password.resolver.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Args, Mutation, Resolver } from '@nestjs/graphql';
22
import { stripIndent } from 'common-tags';
3-
import { AnonSession, LoggedInSession, type Session } from '~/common';
43
import { Anonymous } from './anonymous.decorator';
54
import { AuthenticationService } from './authentication.service';
65
import {
@@ -24,9 +23,8 @@ export class PasswordResolver {
2423
})
2524
async changePassword(
2625
@Args() { oldPassword, newPassword }: ChangePasswordArgs,
27-
@LoggedInSession() session: Session,
2826
): Promise<ChangePasswordOutput> {
29-
await this.authentication.changePassword(oldPassword, newPassword, session);
27+
await this.authentication.changePassword(oldPassword, newPassword);
3028
return { success: true };
3129
}
3230

@@ -49,9 +47,8 @@ export class PasswordResolver {
4947
@Anonymous()
5048
async resetPassword(
5149
@Args('input') input: ResetPasswordInput,
52-
@AnonSession() session: Session,
5350
): Promise<ResetPasswordOutput> {
54-
await this.authentication.resetPassword(input, session);
51+
await this.authentication.resetPassword(input);
5552
return { success: true };
5653
}
5754
}

src/components/authentication/register.resolver.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Resolver,
77
} from '@nestjs/graphql';
88
import { stripIndent } from 'common-tags';
9-
import { AnonSession, type Session } from '~/common';
109
import { Loader, type LoaderOf } from '~/core';
1110
import { Privileges } from '../authorization';
1211
import { Power } from '../authorization/dto';
@@ -30,12 +29,9 @@ export class RegisterResolver {
3029
`,
3130
})
3231
@Anonymous()
33-
async register(
34-
@Args('input') input: RegisterInput,
35-
@AnonSession() session: Session,
36-
): Promise<RegisterOutput> {
37-
const user = await this.authentication.register(input, session);
38-
await this.authentication.login(input, session);
32+
async register(@Args('input') input: RegisterInput): Promise<RegisterOutput> {
33+
const user = await this.authentication.register(input);
34+
await this.authentication.login(input);
3935
await this.authentication.refreshCurrentSession();
4036
return { user };
4137
}
@@ -51,7 +47,7 @@ export class RegisterResolver {
5147
}
5248

5349
@ResolveField(() => [Power])
54-
async powers(@AnonSession() session: Session): Promise<Power[]> {
55-
return [...this.privileges.forUser(session).powers];
50+
async powers(): Promise<Power[]> {
51+
return [...this.privileges.powers];
5652
}
5753
}

src/components/authentication/session.resolver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ export class SessionResolver {
123123
// They should still be able to see their own props from this field.
124124
// Otherwise, it could be that the impersonatee can't see the impersonator's roles,
125125
// and now the UI can't stop impersonating because it doesn't know the impersonator's roles.
126-
return await this.authentication.asUser(impersonator, (_) =>
127-
this.users.readOne(impersonator.userId, _),
126+
return await this.authentication.asUser(impersonator, () =>
127+
this.users.readOne(impersonator.userId),
128128
);
129129
}
130130

131131
@ResolveField(() => [Power], { nullable: true })
132-
async powers(@Parent() output: SessionOutput): Promise<Power[]> {
133-
return [...this.privileges.forUser(output.session).powers];
132+
async powers(): Promise<Power[]> {
133+
return [...this.privileges.powers];
134134
}
135135
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Query, Resolver } from '@nestjs/graphql';
2-
import { AnonSession, type Session } from '~/common';
32
import { Power } from './dto';
43
import { Privileges } from './policy';
54

@@ -8,7 +7,7 @@ export class AuthorizationResolver {
87
constructor(private readonly privileges: Privileges) {}
98

109
@Query(() => [Power])
11-
async powers(@AnonSession() session: Session): Promise<Power[]> {
12-
return [...this.privileges.forUser(session).powers];
10+
async powers(): Promise<Power[]> {
11+
return [...this.privileges.powers];
1312
}
1413
}

src/components/authorization/policy/executor/edge-privileges.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ export class EdgePrivileges<
3030
this.resource = EnhancedResource.of(resource);
3131
}
3232

33-
/** @deprecated */
34-
get session() {
35-
return this.policyExecutor.sessionHost.current;
36-
}
37-
38-
/** @deprecated Use {@link forContext} instead */
39-
forUser(_session: unknown, object?: ResourceObjectContext<TResourceStatic>) {
40-
return object ? this.forContext(object) : this;
41-
}
42-
4333
get context() {
4434
return this.object;
4535
}

0 commit comments

Comments
 (0)