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

feat: generate secret function and replaced few instances #7810

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
} from 'src/engine/core-modules/workspace/workspace.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
import { generateSecret } from 'src/utils/generate-secret';

@Injectable()
export class TokenService {
Expand Down Expand Up @@ -232,7 +233,7 @@ export class TokenService {
userId: string,
workspaceId: string,
): Promise<AuthToken> {
const secret = this.environmentService.get('LOGIN_TOKEN_SECRET');
const secret = generateSecret(workspaceId, 'LOGIN');
FelixMalfait marked this conversation as resolved.
Show resolved Hide resolved
const expiresIn = this.environmentService.get(
'SHORT_TERM_TOKEN_EXPIRES_IN',
);
Expand Down Expand Up @@ -271,7 +272,7 @@ export class TokenService {
const jwtPayload = {
sub: workspaceId,
};
const secret = this.environmentService.get('ACCESS_TOKEN_SECRET');
const secret = generateSecret(workspaceId, 'ACCESS');
let expiresIn: string | number;

if (expiresAt) {
Expand Down
14 changes: 14 additions & 0 deletions packages/twenty-server/src/utils/generate-secret.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createHash } from 'crypto';

if (!process.env.APP_SECRET) {
Copy link
Member

@FelixMalfait FelixMalfait Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should access env var through the dedicate service not directly through process.env ; you should probably do this in TokenService

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I'll see that.

throw new Error('APP_SECRET is not set');
}

export const generateSecret = (
workspaceId: string,
type: 'ACCESS' | 'LOGIN' | 'REFRESH' | 'FILE',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: consider using an enum for type parameter

): string => {
return createHash('sha256')
.update(`${process.env.APP_SECRET}${workspaceId}${type}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: no separator between APP_SECRET, workspaceId, and type could lead to collisions

.digest('hex');
};
Loading