Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean code (context and hooks) #6

Merged
merged 2 commits into from
Jul 12, 2024
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
95 changes: 45 additions & 50 deletions packages/wibe-server/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type User {
email: Email
verifiedEmail: Boolean
role: Role
sessions: _SessionConnection
}

type UserACLObject {
Expand Down Expand Up @@ -62,14 +61,6 @@ type UserAuthenticationOtp {
code: String
}

type _SessionConnection {
edges: [_SessionEdge]
}

type _SessionEdge {
node: _Session!
}

"""User class"""
input UserInput {
name: String
Expand All @@ -82,7 +73,6 @@ input UserInput {
email: Email
verifiedEmail: Boolean
role: RolePointerInput
sessions: _SessionRelationInput
}

input UserACLObjectInput {
Expand Down Expand Up @@ -134,7 +124,6 @@ input UserCreateFieldsInput {
email: Email
verifiedEmail: Boolean
role: RolePointerInput
sessions: _SessionRelationInput
}

input UserACLObjectCreateFieldsInput {
Expand Down Expand Up @@ -492,7 +481,6 @@ input UserWhereInput {
email: EmailWhereInput
verifiedEmail: BooleanWhereInput
role: RoleWhereInput
sessions: _SessionWhereInput
OR: [UserWhereInput]
AND: [UserWhereInput]
}
Expand Down Expand Up @@ -634,43 +622,6 @@ input RoleACLObjectRolesACLWhereInput {
AND: [RoleACLObjectRolesACLWhereInput]
}

input _SessionWhereInput {
id: IdWhereInput
user: UserWhereInput
accessToken: StringWhereInput
accessTokenExpiresAt: DateWhereInput
refreshToken: StringWhereInput
refreshTokenExpiresAt: DateWhereInput
acl: _SessionACLObjectWhereInput
createdAt: DateWhereInput
updatedAt: DateWhereInput
OR: [_SessionWhereInput]
AND: [_SessionWhereInput]
}

input _SessionACLObjectWhereInput {
users: [_SessionACLObjectUsersACLWhereInput]
roles: [_SessionACLObjectRolesACLWhereInput]
OR: [_SessionACLObjectWhereInput]
AND: [_SessionACLObjectWhereInput]
}

input _SessionACLObjectUsersACLWhereInput {
userId: StringWhereInput
read: BooleanWhereInput
write: BooleanWhereInput
OR: [_SessionACLObjectUsersACLWhereInput]
AND: [_SessionACLObjectUsersACLWhereInput]
}

input _SessionACLObjectRolesACLWhereInput {
roleId: StringWhereInput
read: BooleanWhereInput
write: BooleanWhereInput
OR: [_SessionACLObjectRolesACLWhereInput]
AND: [_SessionACLObjectRolesACLWhereInput]
}

type PostConnection {
edges: [PostEdge]
}
Expand Down Expand Up @@ -720,6 +671,51 @@ input PostACLObjectRolesACLWhereInput {
AND: [PostACLObjectRolesACLWhereInput]
}

type _SessionConnection {
edges: [_SessionEdge]
}

type _SessionEdge {
node: _Session!
}

input _SessionWhereInput {
id: IdWhereInput
user: UserWhereInput
accessToken: StringWhereInput
accessTokenExpiresAt: DateWhereInput
refreshToken: StringWhereInput
refreshTokenExpiresAt: DateWhereInput
acl: _SessionACLObjectWhereInput
createdAt: DateWhereInput
updatedAt: DateWhereInput
OR: [_SessionWhereInput]
AND: [_SessionWhereInput]
}

input _SessionACLObjectWhereInput {
users: [_SessionACLObjectUsersACLWhereInput]
roles: [_SessionACLObjectRolesACLWhereInput]
OR: [_SessionACLObjectWhereInput]
AND: [_SessionACLObjectWhereInput]
}

input _SessionACLObjectUsersACLWhereInput {
userId: StringWhereInput
read: BooleanWhereInput
write: BooleanWhereInput
OR: [_SessionACLObjectUsersACLWhereInput]
AND: [_SessionACLObjectUsersACLWhereInput]
}

input _SessionACLObjectRolesACLWhereInput {
roleId: StringWhereInput
read: BooleanWhereInput
write: BooleanWhereInput
OR: [_SessionACLObjectRolesACLWhereInput]
AND: [_SessionACLObjectRolesACLWhereInput]
}

type RoleConnection {
edges: [RoleEdge]
}
Expand Down Expand Up @@ -811,7 +807,6 @@ input UserUpdateFieldsInput {
email: Email
verifiedEmail: Boolean
role: RolePointerInput
sessions: _SessionRelationInput
}

input UserACLObjectUpdateFieldsInput {
Expand Down
26 changes: 14 additions & 12 deletions packages/wibe-server/src/authentication/Session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('_Session', () => {

const res = await session.meFromAccessToken('accessToken', {
isRoot: true,
databaseController,
wibe: { databaseController },
} as any)

expect(res.user).toBeNull()
Expand All @@ -57,7 +57,7 @@ describe('_Session', () => {
'refreshToken',
'refreshTokenExpiresAt',
],
context: { isRoot: true, databaseController },
context: { isRoot: true, wibe: { databaseController } },
})
})

Expand All @@ -80,7 +80,7 @@ describe('_Session', () => {

const { sessionId, user } = await session.meFromAccessToken(
'accessToken',
{ isRoot: true, databaseController } as any,
{ isRoot: true, wibe: { databaseController } } as any,
)

expect(mockGetObjects).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('_Session', () => {
const thirtyDays = new Date(Date.now() + 1000 * 60 * 60 * 24 * 30)

const { accessToken, refreshToken } = await session.create('userId', {
databaseController,
wibe: { databaseController },
} as any)

expect(accessToken).not.toBeUndefined()
Expand Down Expand Up @@ -158,13 +158,15 @@ describe('_Session', () => {

await session.delete({
sessionId: 'sessionId',
databaseController,
wibe: {
databaseController,
},
} as any)

expect(mockDeleteObject).toHaveBeenCalledTimes(1)
expect(mockDeleteObject).toHaveBeenCalledWith({
className: '_Session',
context: { sessionId: 'sessionId', databaseController },
context: { sessionId: 'sessionId', wibe: { databaseController } },
id: 'sessionId',
})
})
Expand All @@ -189,7 +191,7 @@ describe('_Session', () => {
const { accessToken, refreshToken } = await session.refresh(
'accessToken',
'refreshToken',
{ databaseController } as any,
{ wibe: { databaseController } } as any,
)

expect(accessToken).not.toBeUndefined()
Expand All @@ -208,7 +210,7 @@ describe('_Session', () => {
expect(mockUpdateObject).toHaveBeenCalledTimes(1)
expect(mockUpdateObject).toHaveBeenCalledWith({
className: '_Session',
context: { databaseController },
context: expect.any(Object),
id: 'sessionId',
data: {
accessToken: expect.any(String),
Expand Down Expand Up @@ -255,7 +257,7 @@ describe('_Session', () => {
const { accessToken, refreshToken } = await session.refresh(
'accessToken',
'refreshToken',
{ databaseController } as any,
{ wibe: { databaseController } } as any,
)

expect(accessToken).toBe('accessToken')
Expand All @@ -281,7 +283,7 @@ describe('_Session', () => {

expect(
session.refresh('accessToken', 'refreshToken', {
databaseController,
wibe: { databaseController },
} as any),
).rejects.toThrow('_Session not found')

Expand Down Expand Up @@ -313,7 +315,7 @@ describe('_Session', () => {

expect(
session.refresh('accessToken', 'refreshToken', {
databaseController,
wibe: { databaseController },
} as any),
).rejects.toThrow('Refresh token expired')
})
Expand All @@ -337,7 +339,7 @@ describe('_Session', () => {

expect(
session.refresh('accessToken', 'wrongRefreshToken', {
databaseController,
wibe: { databaseController },
} as any),
).rejects.toThrow('Invalid refresh token')
})
Expand Down
36 changes: 22 additions & 14 deletions packages/wibe-server/src/authentication/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Session {
accessToken: string,
context: WibeContext<any>,
): Promise<{ sessionId: string; user: User | null }> {
const sessions = await context.databaseController.getObjects({
const sessions = await context.wibe.databaseController.getObjects({
className: '_Session',
where: {
// @ts-expect-error
Expand Down Expand Up @@ -61,7 +61,9 @@ export class Session {
{
userId,
iat: Date.now(),
exp: Date.now() + this.getAccessTokenExpireIn(context.config),
exp:
Date.now() +
this.getAccessTokenExpireIn(context.wibe.config),
},
import.meta.env.JWT_SECRET || 'dev',
)
Expand All @@ -70,22 +72,26 @@ export class Session {
{
userId,
iat: Date.now(),
exp: Date.now() + this.getRefreshTokenExpireIn(context.config),
exp:
Date.now() +
this.getRefreshTokenExpireIn(context.wibe.config),
},
import.meta.env.JWT_SECRET || 'dev',
)

const { id } = await context.databaseController.createObject({
const { id } = await context.wibe.databaseController.createObject({
className: '_Session',
context,
data: {
accessToken: this.accessToken,
accessTokenExpiresAt: new Date(
Date.now() + this.getAccessTokenExpireIn(context.config),
Date.now() +
this.getAccessTokenExpireIn(context.wibe.config),
),
refreshToken: this.refreshToken,
refreshTokenExpiresAt: new Date(
Date.now() + this.getRefreshTokenExpireIn(context.config),
Date.now() +
this.getRefreshTokenExpireIn(context.wibe.config),
),
user: userId,
},
Expand All @@ -101,7 +107,7 @@ export class Session {
async delete(context: WibeContext<any>) {
if (!context.sessionId) return

await context.databaseController.deleteObject({
await context.wibe.databaseController.deleteObject({
className: '_Session',
context,
id: context.sessionId,
Expand All @@ -113,7 +119,7 @@ export class Session {
refreshToken: string,
context: WibeContext<any>,
) {
const session = await context.databaseController.getObjects({
const session = await context.wibe.databaseController.getObjects({
className: '_Session',
where: {
// @ts-expect-error
Expand All @@ -136,7 +142,7 @@ export class Session {
throw new Error('Refresh token expired')

const refreshTokenExpireIn = this.getRefreshTokenExpireIn(
context.config,
context.wibe.config,
)

// We refresh only if the refresh token is about to expire (75% of the time)
Expand All @@ -156,7 +162,7 @@ export class Session {
{
userId: user?.id,
iat: Date.now(),
exp: this.getAccessTokenExpireIn(context.config),
exp: this.getAccessTokenExpireIn(context.wibe.config),
},
import.meta.env.JWT_SECRET || 'dev',
)
Expand All @@ -165,23 +171,25 @@ export class Session {
{
userId: user?.id,
iat: Date.now(),
exp: this.getRefreshTokenExpireIn(context.config),
exp: this.getRefreshTokenExpireIn(context.wibe.config),
},
import.meta.env.JWT_SECRET || 'dev',
)

await context.databaseController.updateObject({
await context.wibe.databaseController.updateObject({
className: '_Session',
context,
id,
data: {
accessToken: newAccessToken,
accessTokenExpiresAt: new Date(
Date.now() + this.getAccessTokenExpireIn(context.config),
Date.now() +
this.getAccessTokenExpireIn(context.wibe.config),
),
refreshToken: newRefreshToken,
refreshTokenExpiresAt: new Date(
Date.now() + this.getRefreshTokenExpireIn(context.config),
Date.now() +
this.getRefreshTokenExpireIn(context.wibe.config),
),
},
})
Expand Down
7 changes: 7 additions & 0 deletions packages/wibe-server/src/authentication/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,13 @@ describe('Authentication', () => {
})

it('should not authorize to access to protected resource if the user is not connected', async () => {
await createUserAndUpdateRole({
anonymousClient: client,
port,
roleName: 'Client3',
rootClient,
})

const userClient = getUserClient(port, 'invalidToken')

expect(
Expand Down
Loading
Loading