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

Implemented qrcode and added hash to encryption #48

Merged
merged 4 commits into from
Sep 12, 2024
Merged

Conversation

godchiSymbionix
Copy link
Collaborator

@godchiSymbionix godchiSymbionix commented Sep 11, 2024

Summary by CodeRabbit

  • New Features

    • Introduced QR code generation functionality for SHLinks, accessible via a new API endpoint.
    • Added a new dependency for QR code generation to enhance user interaction.
  • Security Enhancements

    • Implemented hashed storage for passcodes and client secrets to improve security measures.
  • Tests

    • Added comprehensive unit tests for the new QR code generation API endpoint to ensure functionality and error handling.
    • Introduced unit tests for the QR code generation use case to validate behavior under various scenarios.
  • Documentation

    • Introduced a new Data Transfer Object (DTO) for QR code requests to streamline data handling.

Copy link

coderabbitai bot commented Sep 11, 2024

Walkthrough

The changes introduce a new QR code generation feature in the application, along with enhancements to security by adding hashed fields for sensitive data. A new API endpoint is created to generate QR codes for specific SHLinks, supported by a corresponding DTO for request data. The database schema is updated with new fields to store hashed passcodes, and unit tests are added to ensure the functionality works as expected. Additionally, a utility function is made accessible for broader use within the application.

Changes

File(s) Change Summary
package.json Added dependency "qrcode": "^1.5.4" for QR code generation.
prisma/migrations/.../migration.sql Added column config_passcode_hash of type TEXT to the "shlink" table for secure passcode storage.
prisma/schema.prisma, prisma/schema.test.prisma Added optional fields config_passcode_hash in shlink and config_client_secret_hash in server_config models.
src/app/api/v1/share-links/[id]/qrcode/route.ts Introduced an API endpoint for generating QR codes associated with SHLinks.
src/app/api/v1/share-links/[id]/qrcode/route.test.ts Added unit tests for the QR code generation API endpoint to validate functionality and error handling.
src/domain/dtos/shlink-qrcode.ts Created SHLinkQRCodeRequestDto class for encapsulating request data for QR code generation.
src/mappers/shlink-mapper.ts Changed encodeSHLink function from private to exported for broader accessibility.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant Service
    participant Database

    Client->>API: POST /api/v1/share-links/:id/qrcode
    API->>Service: Validate ID and Token
    Service->>Database: Retrieve SHLink by ID
    Database-->>Service: Return SHLink
    Service->>API: Generate QR Code
    API-->>Client: Return QR Code Image
Loading

🐇 In a world of links and codes,
A QR tale now unfolds.
With hashes safe and tokens bright,
We share our links in pure delight.
Hopping through the digital space,
A QR code brings a smiling face! 🌟


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 422bc7e and 943e835.

Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
Files selected for processing (1)
  • package.json (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • package.json

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (2)
package.json (1)

30-30: LGTM! The addition of the qrcode library aligns with the PR objectives.

The qrcode library is a good choice for implementing QR code generation functionality in the application. Its addition aligns with the stated PR objectives and the AI-generated summary.

Please ensure that the QR code generation feature is thoroughly tested, covering various scenarios and edge cases.

As an additional suggestion, consider using a more strict version constraint like ~1.5.4 instead of ^1.5.4 to prevent unintended updates that might introduce breaking changes. However, this is a minor nitpick and the current version constraint is acceptable.

src/mappers/shlink-mapper.ts (1)

Line range hint 99-108: LGTM, but consider the implications of exporting the function.

The code changes are approved.

However, please note that exporting the encodeSHLink function makes it part of the module's public API. This means that any future changes to the function could potentially break other parts of the codebase that depend on it.

Consider the following:

  • Carefully evaluate if exporting the function is necessary. Only export it if you intend it to be used by other modules.
  • If you do export the function, make sure to document its usage and contract (input/output types, expected behavior, edge cases, etc.) so that other developers know how to use it correctly.
  • Be cautious when making changes to exported functions in the future, as they could be breaking changes. If necessary, consider versioning your module's API to handle breaking changes gracefully.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7bb87cb and 36bca2a.

Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
Files selected for processing (8)
  • package.json (1 hunks)
  • prisma/migrations/20240911113019_test/migration.sql (1 hunks)
  • prisma/schema.prisma (2 hunks)
  • prisma/schema.test.prisma (1 hunks)
  • src/app/api/v1/share-links/[id]/qrcode/route.test.ts (1 hunks)
  • src/app/api/v1/share-links/[id]/qrcode/route.ts (1 hunks)
  • src/domain/dtos/shlink-qrcode.ts (1 hunks)
  • src/mappers/shlink-mapper.ts (1 hunks)
Additional comments not posted (10)
src/domain/dtos/shlink-qrcode.ts (1)

1-3: LGTM!

The code changes are approved.

prisma/migrations/20240911113019_test/migration.sql (1)

1-2: LGTM!

The migration looks good. It adds a new column config_passcode_hash of type TEXT to the shlink table, which aligns with the PR objective of adding hashed fields for sensitive data.

src/app/api/v1/share-links/[id]/qrcode/route.ts (1)

18-47: LGTM!

The GET function is well-structured and follows good practices:

  1. It handles the case when the id is not provided by returning a 404 response.
  2. It uses the getSingleSHLinkUseCase function to retrieve the SHLink, separating the use case logic from the API route handler.
  3. It returns a 404 response if the SHLink is not found, which is the appropriate status code for a not found resource.
  4. It uses the encodeSHLink function to encode the SHLink data before generating the QR code, keeping the encoding logic separate from the route handler.
  5. It generates the QR code using the QRCode.toDataURL function, which is a commonly used library for generating QR codes.
  6. It returns the QR code data URL in the JSON response with a 200 status code, which is the appropriate status code for a successful response.
  7. It catches any errors and passes them to the handleApiValidationError function, handling errors centrally.

The code changes are approved.

src/app/api/v1/share-links/[id]/qrcode/route.test.ts (4)

30-53: LGTM!

The test case for the happy path scenario looks good. It mocks the required dependencies, makes the API call with valid inputs, and verifies the response status and body.


55-64: LGTM!

The test case for the missing ID scenario looks good. It makes the API call without the ID and verifies the 404 response status and message.


66-76: LGTM!

The test case for the scenario where the SHLink is not found looks good. It mocks the getSingleSHLinkUseCase to return null, makes the API call, and verifies the 404 response status and message.


78-92: LGTM!

The test case for the error handling scenario looks good. It mocks the getSingleSHLinkUseCase to throw an error, makes the API call, and verifies that the handleApiValidationError is called with the error. It also verifies the 500 response status.

prisma/schema.test.prisma (1)

48-48: LGTM!

The addition of the config_passcode_hash field to store the hashed version of the config_passcode is a good security practice. The @encryption:hash(config_passcode) annotation ensures that the passcode is hashed before storing it. Marking the field as optional is also correct as not all shlinks may have a passcode.

The code changes are approved.

prisma/schema.prisma (2)

48-48: LGTM!

The addition of the config_passcode_hash field to store a hashed version of the config_passcode is a good security practice. It reduces the risk of exposure in case of a data breach.

The @encryption:hash attribute ensures that the value is automatically hashed when stored in the database.


82-82: LGTM!

The addition of the config_client_secret_hash field to store a hashed version of the config_client_secret is a good security practice. It reduces the risk of exposure in case of a data breach.

The @encryption:hash attribute ensures that the value is automatically hashed when stored in the database.

Copy link
Collaborator

@jacob-khoza-symb jacob-khoza-symb Sep 11, 2024

Choose a reason for hiding this comment

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

your migrations only show one change when you have two tables changed

@godchiSymbionix godchiSymbionix merged commit 18a7141 into main Sep 12, 2024
2 checks passed
This was referenced Sep 12, 2024
@coderabbitai coderabbitai bot mentioned this pull request Sep 23, 2024
@godchiSymbionix godchiSymbionix deleted the feat/qr_code branch September 25, 2024 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants