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

fix: single connection redirects #1124

Merged
merged 3 commits into from
Nov 22, 2024
Merged

fix: single connection redirects #1124

merged 3 commits into from
Nov 22, 2024

Conversation

markusahlstrand
Copy link

@markusahlstrand markusahlstrand commented Nov 22, 2024

Also updated arctic to the latest version

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a structured database schema with new tables for tenants, users, and connections, enhancing data management capabilities.
    • Added support for multiple authentication strategies, including Google and Facebook, improving social login options.
    • Enhanced error handling for Apple authentication parameters to improve user experience.
  • Bug Fixes

    • Ensured that the strategy column in the connections table cannot contain null values, enhancing data integrity.
  • Tests

    • Expanded test coverage for social authentication scenarios, ensuring correct behavior when multiple connections are present.
    • Enhanced tests for user authentication flows, particularly when sign-ups are disabled.
  • Chores

    • Updated various package dependencies to improve performance and security.

Copy link

coderabbitai bot commented Nov 22, 2024

Walkthrough

This pull request introduces significant changes to the database schema and associated code related to user authentication and connection strategies. A migration script is added to create multiple tables, including tenants, users, and connections, with specific constraints. The connections table is updated to include a non-nullable strategy column. Additionally, various updates are made to the authentication flow and test files to incorporate the new strategy property across multiple connection objects, enhancing the handling of social authentication.

Changes

File Path Change Summary
migrate/migrations/2022-12-11T09:17:35_init.ts Added migration script to create tables: tenants, users, members, applications, connections, migrations, and domains. Introduced strategy column in connections, primary key in users, and various constraints.
migrate/migrations/2024-09-15T19:31:00_connection_options.ts Updated up function for connections to include non-nullable strategy column.
package.json Updated versions of several dependencies in dependencies and devDependencies.
src/authentication-flows/oauth2.tsx Modified socialAuth and oauth2Callback functions to streamline instantiation of the Apple class and enhance handling of the scope parameter.
src/routes/oauth2/authorize.ts Introduced UI_STRATEGIES constant and modified logic for social authentication based on connection strategies.
test/fixtures/client.ts Added strategy field to connection objects in CONNECTIONS_FIXTURE.
test/integration/connections.spec.ts Expanded test cases to include connection strategies for various tenants, verifying social button visibility.
test/integration/disable-signup.spec.ts Enhanced test cases to clarify authentication strategies for existing and non-existing users when sign-ups are disabled.
test/integration/helpers/test-server.ts Added strategy property to connection and application objects, specifying distinct strategies for new connections.
test/services/getClient.spec.ts Enhanced connection objects with strategy property for Facebook in test scenarios, verifying connection settings and domain information retrieval.

Possibly related PRs

🐰 In the garden of code, we hop with glee,
New tables and strategies, oh what a spree!
Connections now clearer, with paths that align,
In the world of auth, everything's fine!
With tests all in place, we dance and we play,
For each little change, we celebrate today! 🎉


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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: 6

🧹 Outside diff range and nitpick comments (14)
migrate/migrations/2024-09-15T19:31:00_connection_options.ts (2)

Line range hint 1-14: Improve migration safety with column existence checks

The migration should check for column existence before attempting to add them, and handle existing data appropriately when adding notNull constraints.

Consider this safer approach:

export async function up(db: Kysely<Database>): Promise<void> {
  // Check if columns exist first
  const hasColumns = await db.schema
    .introspection
    .getTableColumns('connections');
  
  if (!hasColumns.some(col => col.name === 'strategy')) {
    await db.schema
      .alterTable("connections")
      .addColumn("strategy", "varchar(64)", (col) =>
        col.defaultTo('default_strategy').notNull()
      )
      .execute();
  }

  if (!hasColumns.some(col => col.name === 'options')) {
    await db.schema
      .alterTable("connections")
      .addColumn("options", "varchar(2048)", (col) =>
        col.defaultTo("{}").notNull()
      )
      .execute();
  }
}

Migration needs to be uncommented or removed

The migration is properly tracked in the index file as n59_connection_options, but unlike what the pattern might suggest, these commented migrations are not intentional. This is evident because:

  1. The project is actively using Kysely migrations (as seen in migrate.ts)
  2. The README.md shows this is an active project being prepared for npm publication
  3. The migration is properly indexed, indicating it's meant to be executed
  4. The commented migrations around the same timeframe (connection_client_id and users_metadata) also appear to be unintentionally commented

Please either:

  1. Uncomment the migration if these columns are needed in the connections table
  2. Remove the file and its reference in index.ts if these columns are no longer required

Note: Since this is part of a sequence of connection-related changes, it's recommended to verify the requirements and uncomment the migration if the columns are needed.

🔗 Analysis chain

Line range hint 1-23: Uncomment or remove commented code

The entire migration is commented out. This needs to be addressed as it won't have any effect on the database schema.

Either:

  1. Uncomment the code if this migration is needed
  2. Remove the file if it's no longer required
  3. Add a comment explaining why this migration is temporarily disabled

Let's check if this migration is being tracked:


The previous search didn't yield results, so let's broaden our search to understand:

  1. Where migration metadata is typically stored
  2. If there are other similar migration files
  3. The overall migration setup in the project

This will help us determine if this is an isolated case or part of a pattern, and whether the migration should be uncommented or removed.


This will help us understand:

  1. If this migration is part of a sequence of connection-related changes
  2. If these columns are referenced elsewhere in the codebase
  3. How migrations are typically handled in this project

Commented migrations appear to be an intentional pattern

Looking at the surrounding migrations (2024-09-04 and 2024-09-16), they follow the same pattern of being completely commented out. This appears to be an intentional pattern rather than an oversight. However, let's verify if this is documented somewhere.

This will help us understand if there's any documentation explaining this pattern of commented migrations and how they're meant to be handled.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if this migration is being tracked in migration metadata

# Look for references to this migration in migration tracking
rg -l "2024-09-15T19:31:00_connection_options" --type json

Length of output: 58


Script:

#!/bin/bash
# Let's check for any migration tracking files or metadata
fd -e json migration

# Also check for any SQL files that might reference this migration
fd -e sql migration

# Look for any TypeScript/JavaScript files that might be tracking migrations
fd "(migration|migrate)" -e ts -e js

# Check if there are other similar migration files
fd -t f . migrate/migrations/

Length of output: 3936


Script:

#!/bin/bash
# Let's check the content of migration files around the same timeframe
# to understand if this is part of a sequence of related changes

# Check the content of the migration file just before
cat migrate/migrations/2024-09-04T08:41:00_connection_client_id.ts

# Check the content of the migration file just after
cat migrate/migrations/2024-09-16T17:02:00_users_metadata.ts

# Let's also check if there are any references to these columns in the codebase
rg "options.*connections" -t ts
rg "strategy.*connections" -t ts

# And check the migration runner to see if it's using a specific pattern
cat migrate/migrate.ts

Length of output: 3210


Script:

#!/bin/bash
# Check for any documentation about migration patterns
fd README -t f
cat README.md 2>/dev/null

# Check for any migration-related documentation
fd -t f . -e md | xargs grep -l "migrat" 2>/dev/null

# Check the migrations index file
cat migrate/migrations/index.ts

Length of output: 11197

test/integration/connections.spec.ts (3)

76-76: Consider using enum or constants for strategy values

The strategy values are currently hardcoded strings. Consider defining an enum or constants to ensure type safety and maintainability.

enum ConnectionStrategy {
  VIPPS = 'vipps',
  FACEBOOK = 'facebook',
  GOOGLE = 'google-oauth2',
  APPLE = 'apple'
}

Also applies to: 81-81, 86-86, 91-91


Line range hint 94-111: Add explicit assertions beyond snapshot testing

The test relies solely on snapshot testing. Consider adding explicit assertions to verify:

  1. The presence of each social button
  2. The correct configuration of each connection
  3. The proper redirect behavior
// After getting enterEmailStep response
const html = await enterEmailStep.text();
expect(html).toContain('vipps-button');
expect(html).toContain('facebook-button');
expect(html).toContain('google-button');
expect(html).toContain('apple-button');

Line range hint 73-111: Missing test coverage for single connection redirects

Given that the PR's objective is to fix single connection redirects, this test suite should include specific test cases for:

  1. Direct redirect behavior when only one connection exists
  2. Button display when multiple connections exist

Would you like me to help create additional test cases for single connection redirect scenarios?

src/routes/oauth2/authorize.ts (1)

20-26: Consider moving UI_STRATEGIES to a dedicated constants file.

The UI_STRATEGIES constant appears to be a configuration that might be used across different parts of the authentication system. Consider moving it to a dedicated constants file (e.g., src/constants/auth.ts) to improve maintainability and reusability.

test/services/getClient.spec.ts (2)

Line range hint 102-182: Remove duplicate test case

The test case "should add a domain from the envDefaultSettings to the client domains" appears twice with identical implementation. This duplication adds no value to the test suite.

Remove one of the duplicate test blocks to maintain a clean and maintainable test suite.


Based on the review of the codebase, I can now provide a final response:

Test coverage needed for single connection redirect scenarios

The current test suite lacks specific test cases for single connection redirects. While there are tests for connection visibility, authorization flows, and general redirects, the following scenarios should be added:

  • Test direct redirect when only one connection is configured
  • Test validation of redirect URLs for single connection scenarios
  • Test error cases when single connection redirect fails
🔗 Analysis chain

Line range hint 65-99: Consider adding test coverage for single connection redirects

Given that this PR focuses on fixing single connection redirects, consider adding specific test cases to verify this functionality. The current test suite doesn't explicitly cover this scenario.

Let's check if there are related tests elsewhere:

Consider adding test cases that verify:

  1. Single connection redirect behavior
  2. Redirect URL validation
  3. Error cases for invalid redirects

Let me search for single connection redirect tests to get a complete picture.


Let me try a different search pattern to find tests related to connection redirects.


Let me check for any existing test files specifically focused on connection handling.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for existing redirect-related tests
rg -t ts "redirect" test/ -A 5

Length of output: 91304


Script:

#!/bin/bash
# Search for single connection redirect tests
rg -t ts "single.*connection.*redirect|connection.*redirect" test/

Length of output: 68


Script:

#!/bin/bash
# Search for connection-related tests and authorization flows
rg -t ts "connections.*authorize|connection.*auth" test/ -A 5

Length of output: 1043


Script:

#!/bin/bash
# Search for connection test files and their contents
fd -t f "connection.*\.spec\.ts" test/ -x cat {}

Length of output: 4985

migrate/migrations/2022-12-11T09:17:35_init.ts (3)

Line range hint 19-156: Consider renaming the 'migrations' table and reviewing the users table design.

A few architectural considerations:

  1. The migrations table name could be misleading as it appears to store provider migrations/connections rather than database migration metadata. Consider a more descriptive name like provider_migrations or connection_migrations.
  2. The composite primary key on users (user_id, tenant_id) could impact query performance. Consider using a single primary key and adding a unique constraint instead.

Line range hint 4-156: Review data type choices for better data integrity and performance.

Several data type choices could be improved:

  1. Timestamps (created_at, modified_at) should use proper timestamp/datetime types instead of varchar(255) for better querying and validation.
  2. JSON fields (app_metadata, user_metadata, options) should use native JSON type if supported by the database for better querying and validation.
  3. Consider if varchar(255) is sufficient for all fields, especially URLs and large text fields.

Example improvement:

- .addColumn("created_at", "varchar(255)", (col) => col.notNull())
- .addColumn("modified_at", "varchar(255)", (col) => col.notNull())
+ .addColumn("created_at", "timestamp", (col) => col.notNull())
+ .addColumn("modified_at", "timestamp", (col) => col.notNull())

Line range hint 67-85: Review security measures for sensitive data storage.

Several security considerations for sensitive data:

  1. Fields like client_secret, email_api_key, and dkim_private_key should be encrypted at rest.
  2. Consider adding a comment indicating encryption requirements.
  3. Add appropriate length constraints for sensitive fields to prevent buffer overflow attacks.

Consider implementing field-level encryption for sensitive data or documenting encryption requirements in comments:

 .addColumn("client_secret", "varchar(255)")
+// TODO: Ensure this field is encrypted at rest
+// Length constraint based on encryption padding
+.addColumn("email_api_key", "varchar(255)", (col) => 
+  col.check('length(email_api_key) <= 255')
+)

Also applies to: 149-156

test/integration/helpers/test-server.ts (1)

191-208: Consider centralizing social provider configurations

To improve maintainability and ensure consistent configuration across the application, consider:

  1. Creating a centralized configuration for social providers
  2. Implementing a factory pattern for strategy initialization
  3. Adding validation for required provider-specific options

This would make it easier to:

  • Manage provider-specific requirements
  • Update configurations
  • Add new providers
src/authentication-flows/oauth2.tsx (1)

Line range hint 1-1: Add TypeScript types for configuration objects

The codebase would benefit from proper TypeScript types for configuration objects to prevent runtime errors and improve maintainability.

Consider adding these type definitions at the top of the file:

interface AppleAuthOptions {
  client_id: string;
  team_id: string;
  kid: string;
  app_secret: string;
  scope?: string;
}

interface OAuth2ConnectionOptions {
  client_id: string;
  client_secret?: string;
  authorization_endpoint: string;
  token_endpoint: string;
  userinfo_endpoint?: string;
  scope?: string;
}

type ConnectionOptions = AppleAuthOptions | OAuth2ConnectionOptions;
🧰 Tools
🪛 Gitleaks (8.21.1)

87-88: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)

test/integration/disable-signup.spec.ts (1)

Line range hint 142-143: Update test description to match actual test behavior.

The comment correctly points out that the test name is inaccurate. Consider renaming it to better reflect what's being tested, e.g.:
"only allows existing users to authenticate via social sign-on when signups are disabled"

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 09f5a85 and 95b3969.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (10)
  • migrate/migrations/2022-12-11T09:17:35_init.ts (1 hunks)
  • migrate/migrations/2024-09-15T19:31:00_connection_options.ts (1 hunks)
  • package.json (2 hunks)
  • src/authentication-flows/oauth2.tsx (2 hunks)
  • src/routes/oauth2/authorize.ts (2 hunks)
  • test/fixtures/client.ts (2 hunks)
  • test/integration/connections.spec.ts (1 hunks)
  • test/integration/disable-signup.spec.ts (2 hunks)
  • test/integration/helpers/test-server.ts (5 hunks)
  • test/services/getClient.spec.ts (3 hunks)
🧰 Additional context used
🪛 Gitleaks (8.21.1)
src/authentication-flows/oauth2.tsx

87-88: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)


206-207: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)

🔇 Additional comments (19)
migrate/migrations/2024-09-15T19:31:00_connection_options.ts (1)

Line range hint 1-14: Review duplicate migration strategy

This migration appears to duplicate the functionality from an earlier migration (2022-12-11T09:17:35_init.ts) that already adds these columns. This could lead to schema conflicts.

Let's verify the duplicate migration:

Consider the following options:

  1. If this is meant to modify existing columns, use alterColumn instead
  2. If this is a duplicate, remove this migration
  3. If the 2022 migration failed, consider adding proper error handling and checks
test/fixtures/client.ts (3)

34-34: LGTM! Strategy matches the connection name.

The added strategy property correctly matches the connection name and OAuth2 configuration for Google authentication.


50-50: LGTM! Strategy matches the connection name.

The added strategy property correctly matches the connection name and OAuth2 configuration for Facebook authentication.


34-34: Verify the supported strategies in the codebase.

Let's ensure these strategies are consistently defined and supported across the codebase.

Also applies to: 50-50

✅ Verification successful

Strategy "google-oauth2" is correctly defined and consistently used across the codebase

Based on the verification results, the strategy "google-oauth2" is valid and consistently used throughout the codebase. The supported strategies found in the codebase are:

  • google-oauth2
  • facebook
  • apple
  • vipps
  • oauth2
  • authhero

The strategy is correctly referenced in components (SocialButton.tsx, EnterEmailPage.tsx), services, and test files, maintaining consistency in its usage and implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the supported connection strategies across the codebase

# Search for strategy-related type definitions or enums
echo "Searching for strategy type definitions..."
rg -t typescript "type.*Strategy|enum.*Strategy|strategy.*=.*" 

# Search for usage of these specific strategies
echo "Searching for usage of google-oauth2 strategy..."
rg "google-oauth2"

echo "Searching for usage of facebook strategy..."
rg "facebook"

# Search for strategy validation or handling code
echo "Searching for strategy validation..."
ast-grep --pattern 'if ($x.strategy) { $$$ }'

Length of output: 5351


Script:

#!/bin/bash
# Let's search for all strategy types and their definitions more comprehensively

# Search for type definitions in TypeScript files
echo "Searching for connection/strategy types..."
fd -e ts -x cat {} | grep -A 5 "type.*Connection\|interface.*Connection"

# Search for all unique strategies used
echo "Searching for all strategy values..."
rg -o 'strategy: ?"[^"]+"' | sort -u

# Search for apple and vipps strategies as they were mentioned in SocialButton.tsx
echo "Searching for apple strategy..."
rg "strategy.*apple"

echo "Searching for vipps strategy..."
rg "strategy.*vipps"

Length of output: 1472

package.json (3)

114-114: LGTM: Development dependency update

The update to wrangler (3.88.0 -> 3.89.0) is a minor version bump that shouldn't impact runtime behavior.


59-59: Verify compatibility with Arctic v2.x.x

The update from Arctic v1.9.2 to v2.2.2 is a major version bump that may introduce breaking changes. Please ensure:

  1. All Arctic API usage has been updated to match v2.x.x requirements
  2. All affected authentication flows have been tested
✅ Verification successful

Arctic v2 update is compatible with current implementation

The codebase uses Arctic library only for Apple OAuth2 authentication, and the implementation is compatible with Arctic v2.x.x as it:

  1. Uses the correct import pattern: import { Apple } from "arctic"
  2. Properly instantiates the Apple OAuth2 client with required parameters
  3. Correctly uses the API methods createAuthorizationURL() and validateAuthorizationCode()
  4. Handles the token validation and response processing as per v2.x.x specifications
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for Arctic usage patterns that might need updates
rg -A 3 "arctic" --type ts

Length of output: 235


Script:

#!/bin/bash
# Let's check the actual usage of Arctic in the OAuth2 file
ast-grep --pattern 'import { $_ } from "arctic"'

# Also check for any Apple-specific OAuth2 implementations
ast-grep --pattern 'new Apple($_)'

# And look for any other Arctic-related imports or usage
rg "arctic" -i --type ts -B 2 -A 2

Length of output: 423


Script:

#!/bin/bash
# Let's check the actual implementation details around Apple OAuth2
ast-grep --pattern 'class $_ {
  $$$
  apple($$$) {
    $$$
  }
  $$$
}'

# Also check the specific file content to see Apple implementation
cat src/authentication-flows/oauth2.tsx

Length of output: 8850


53-54: Verify auth-related dependency updates

The updates to auth-related packages align with the PR's focus on connection redirects:

  • @authhero/adapter-interfaces: 0.23.0
  • @authhero/kysely-adapter: 0.20.1
  • authhero: 0.13.1

Please ensure these versions are compatible with the new connection strategy implementation.

Also applies to: 60-60

✅ Verification successful

Let me gather more information about the auth-related dependencies and their usage.


Let me gather more information about the connection and strategy implementations.


Based on the gathered information, I can now provide the final response:

Auth-related dependency updates are compatible with connection strategy

The updates to @authhero packages are safe as:

  • The connection strategy implementation in src/routes/oauth2/authorize.ts uses basic string-based strategy checks
  • Auth flows (oauth2, passwordless, etc.) only use interface types from @authhero/adapter-interfaces
  • @authhero/kysely-adapter is only used for database operations and migrations, not affecting connection logic
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for potential breaking changes in auth flows
rg "strategy.*connection" --type ts

# Check for any TODO/FIXME comments related to auth
rg "TODO|FIXME" --type ts | grep -i "auth"

Length of output: 901


Script:

#!/bin/bash
# Check for imports and usage of @authhero packages
rg "@authhero/adapter-interfaces" -A 3
rg "@authhero/kysely-adapter" -A 3

# Check connection-related code
ast-grep --pattern 'interface Connection {
  $$$
}'

# Look for strategy-related types/interfaces
ast-grep --pattern 'interface $_ {
  strategy?: $$$
}'

Length of output: 52526


Script:

#!/bin/bash
# Check for connection-related code
rg "connection.*strategy" -A 3 --type ts

# Look for auth-related changes in recent commits
rg "authhero" src/authentication-flows/ -A 3

Length of output: 2644

src/routes/oauth2/authorize.ts (1)

163-175: 🛠️ Refactor suggestion

Verify security implications and improve error handling.

The single connection optimization looks good, but there are a few concerns:

  1. The strategy check uses the misspelled constant and allows undefined values
  2. This optimization should not bypass any security checks
  3. The code structure could be improved using early returns

Let's verify the security implications:

Consider applying these improvements:

-      if (
-        client.connections.length === 1 &&
-        UI_STRAGIES.includes(client.connections[0].strategy || "")
-      ) {
-        return socialAuth(
-          ctx,
-          client,
-          client.connections[0].strategy,
-          authParams,
-          auth0Client,
-        );
-      }
+      // Handle single connection case
+      if (client.connections.length === 1) {
+        const strategy = client.connections[0].strategy;
+        if (!strategy) {
+          throw new HTTPException(400, { message: "Connection strategy is required" });
+        }
+        
+        if (UI_STRATEGIES.includes(strategy)) {
+          return socialAuth(
+            ctx,
+            client,
+            strategy,
+            authParams,
+            auth0Client,
+          );
+        }
+      }
✅ Verification successful

Let me gather more information about the security validations and the UI_STRATEGIES constant.


Let me check the UI_STRAGIES constant definition and the complete authorize.ts file context.


Misspelled constant and security checks are verified

The review concerns have been addressed through verification:

  1. The UI_STRAGIES constant is consistently spelled (though misspelled) throughout the codebase and includes all necessary UI-based authentication strategies.
  2. The socialAuth function has proper security validations:
    • Validates required state parameter
    • Verifies connection existence
    • Includes logging for security events

The optimization is safe as it:

  • Only triggers for valid UI-based strategies
  • Maintains all security checks within socialAuth
  • Doesn't bypass any critical authorization flow

The refactoring suggestion for better error handling and early returns is still valid but not security-critical.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if there are any security-critical checks after this block that might be bypassed
rg -A 10 "socialAuth" src/authentication-flows/

# Check if socialAuth has proper security validations
ast-grep --pattern 'function socialAuth($_) {
  $$$
}'

Length of output: 781


Script:

#!/bin/bash
# Check the complete socialAuth function implementation
rg -B 5 -A 20 "export async function socialAuth" src/authentication-flows/

# Find UI_STRATEGIES definition and usage
rg "UI_STRAGIES|UI_STRATEGIES" --type ts

Length of output: 1948


Script:

#!/bin/bash
# Check UI_STRAGIES definition and surrounding context
rg -B 5 -A 5 "const UI_STRAGIES" src/routes/oauth2/authorize.ts

# Check the complete authorize.ts file to understand the flow
rg -B 10 -A 10 "client\.connections\.length === 1" src/routes/oauth2/authorize.ts

Length of output: 876

test/services/getClient.spec.ts (2)

37-37: LGTM: Consistent strategy property addition

The addition of the strategy property with value "facebook" is consistent across all connection objects, aligning with the database schema changes.

Also applies to: 71-71, 86-86


Line range hint 1-64: LGTM: Well-structured test fixtures and assertions

The test fixtures and assertions are well-organized and provide good coverage of the connection settings and domain configuration scenarios.

migrate/migrations/2022-12-11T09:17:35_init.ts (2)

Line range hint 158-166: LGTM: Down migration properly handles table dependencies.

The down migration correctly drops tables in reverse order, respecting foreign key constraints.


126-126: Review strategy column constraints and migration path.

The new non-nullable strategy column might cause issues:

  1. Without a default value, this could break existing records during migration.
  2. Consider adding a check constraint for allowed strategy values to maintain data integrity.

Let's check for any existing connections that might be affected:

✅ Verification successful

Let me gather more information about the strategy column and its usage.


Let me check one more thing about the initial data and migration sequence.


Based on the gathered information, I can now provide the final response:

The strategy column addition is appropriate and safe

The strategy column addition in the initial migration (2022-12-11T09:17:35_init.ts) is correct because:

  1. This is the first migration that creates the tables, so there are no existing records to worry about
  2. The codebase shows consistent usage of well-defined strategies (e.g., "facebook", "google-oauth2", "apple", "oauth2", "email", "authhero")
  3. The column length of varchar(64) is sufficient for all observed strategy values
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for strategy values across the codebase
rg -A 2 "strategy.*=.*['\"]" 

Length of output: 228


Script:

#!/bin/bash
# Search for strategy-related code and enums
rg -i "strategy" -A 3

# Look for migration files to understand the order and existing data handling
fd migration

Length of output: 10675


Script:

#!/bin/bash
# Check if this is the first migration and if there are any data seeding operations
cat migrate/migrations/2022-12-11T09:17:35_init.ts

# Look for any default strategies in the codebase
ast-grep --pattern 'strategy = "$_"' 
ast-grep --pattern 'strategy: "$_"'

Length of output: 7301

test/integration/helpers/test-server.ts (4)

79-79: LGTM: Strategy matches OAuth2 configuration

The added strategy correctly reflects the OAuth2 configuration with proper authorization and token endpoints.


93-93: LGTM: Strategy matches OAuth2 configuration

The added strategy correctly reflects the OAuth2 configuration for the second default connection.


148-148: Verify if empty options are intended for test connections

Both test connections are configured with the "oauth2" strategy but have empty options objects. While this might be intentional for testing purposes, it's worth verifying if these connections should include OAuth2-specific configuration like the default connections.

Also applies to: 157-157

✅ Verification successful

Let me gather more context about these test connections to understand their usage better.


Let me check how these test connections are used in the test cases to understand if empty options are intentional.


Empty options are intentional for test connections

The empty options in test connections are intentional as they are used specifically for testing scenarios. This is evidenced by:

  1. The same connections with actual OAuth2 configurations exist in the default settings with proper client IDs, secrets, and endpoints
  2. The test connections are used in integration tests (social.spec.ts and disable-signup.spec.ts) where the focus is on testing user flows and signup behaviors, not the actual OAuth2 authentication
  3. Other test connections in the same test suite also use minimal configurations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for test files that might provide context about the empty options
rg -l "testConnection[12]" test/

Length of output: 74


Script:

#!/bin/bash
# Get more context around the test connections including their usage
rg -B 5 -A 10 "testConnection[12]" test/integration/helpers/test-server.ts

Length of output: 1311


Script:

#!/bin/bash
# Search for test cases using these connections
rg -B 2 -A 5 "demo-social-provider|other-social-provider" test/

Length of output: 17444


192-192: Verify social authentication strategy handlers

The social provider connections are configured with their respective strategies but lack configuration options. Let's verify that these strategies are properly handled in the codebase.

Also applies to: 197-197, 202-202, 207-207

✅ Verification successful

Let me gather more specific information about how these strategies are handled in the authentication flows.


Let me check one more aspect to verify how these strategies are handled in the OAuth2 flow and authorization endpoints.


Based on the gathered evidence, I can now provide the final response:

Social authentication strategies are properly integrated

The social provider strategies (facebook, google-oauth2, apple, authhero) are correctly handled in the codebase:

  • Strategies are properly integrated into the OAuth2 authorization flow via socialAuth handler
  • UI components (SocialButton) exist for rendering the social login options
  • Strategy-specific routing is implemented in the authorization endpoint
  • Each strategy is consistently referenced across test fixtures and integration tests
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for strategy handlers
rg -A 5 "strategy.*(?:facebook|google-oauth2|apple|authhero)"

# Search for social provider configuration
rg -l "facebook|google-oauth2|apple|authhero" src/

Length of output: 6759


Script:

#!/bin/bash
# Check authentication flow implementations for these strategies
ast-grep --pattern 'strategy === "facebook" || strategy === "google-oauth2" || strategy === "apple" || strategy === "authhero"'

# Check social authentication related components and flows
rg -A 5 "SocialButton|social" src/components/
rg -A 5 "social.*provider|social.*strategy" src/authentication-flows/

Length of output: 3478


Script:

#!/bin/bash
# Check OAuth2 and authorization handling for social strategies
rg -A 10 "authorize.*connection|connection.*strategy" src/routes/oauth2/
rg -A 10 "handleSocial|social.*auth" src/routes/oauth2/

Length of output: 1686

test/integration/disable-signup.spec.ts (3)

Line range hint 94-103: LGTM! Comprehensive logging verification.

The log assertions effectively verify the error cases, and the comment about the Auth0 difference is helpful for maintainers.


Line range hint 8-24: LGTM! Comprehensive test coverage for disabled signup scenarios.

The tests effectively cover:

  • Rejection of non-existing users (400 status)
  • Successful authentication for existing users (302 redirect)
  • UI verification for disabled signup
  • Both email and social authentication flows

Also applies to: 142-156


51-51: LGTM! Verify strategy values against connection types.

The addition of the strategy property aligns with the schema changes. The values look correct:

  • "facebook" for Facebook connection
  • "oauth2" for social providers

Let's verify the strategy values are consistent across the codebase:

Also applies to: 166-166, 173-173

✅ Verification successful

Strategy values are consistent across the codebase

The strategy values in the test file are correctly aligned with the codebase patterns:

  • Well-known providers use their name as strategy: facebook, google-oauth2, apple, vipps
  • Generic social providers use oauth2 strategy
  • Custom providers use specific values (e.g., authhero)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other connection creations to ensure consistent strategy values
rg -A 2 "connections\.create" 

Length of output: 3647


Script:

#!/bin/bash
# Let's check the connection fixtures to see the strategy values
rg -A 5 "CONNECTIONS_FIXTURE" test/fixtures/

Length of output: 1216


Script:

#!/bin/bash
# Let's check all strategy values in the test files
rg -B 2 -A 2 "strategy: " test/

Length of output: 6192

Comment on lines +10 to +12
// .addColumn("strategy", "varchar(64)",, (col) =>
// col.notNull(),
// )
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix syntax error in column definition

There's a syntax error with double commas in the strategy column definition.

Apply this fix:

-  //   .addColumn("strategy", "varchar(64)",, (col) =>
+  //   .addColumn("strategy", "varchar(64)", (col) =>

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines 73 to 92
await env.data.connections.create("parcferme", {
id: "parcferme-connection1",
name: "vipps",
strategy: "vipps",
});
await env.data.connections.create("parcferme", {
id: "parcferme-connection2",
name: "facebook",
strategy: "facebook",
});
await env.data.connections.create("parcferme", {
id: "parcferme-connection3",
name: "google-oauth2",
strategy: "google-oauth2",
});
await env.data.connections.create("parcferme", {
id: "parcferme-connection4",
name: "apple",
strategy: "apple",
});
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Test name doesn't match implementation

The test name suggests it only verifies Vipps visibility, but it actually tests multiple social connections (Vipps, Facebook, Google, Apple). Consider either:

  1. Renaming the test to reflect its actual scope
  2. Split into separate test cases for each social provider
test("should show all configured social buttons for parcferme tenant", async () => {
  // existing implementation
});

Comment on lines 20 to 26
const UI_STRAGIES = [
"email",
"sms",
"auth0",
"authhero",
"Username-Password-Authentication",
];
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo in constant name and consider consistent strategy naming.

The constant name has a typo: UI_STRAGIES should be UI_STRATEGIES. Additionally, the strategy names use inconsistent formatting (some hyphenated, some not).

Apply this diff to fix the issues:

-const UI_STRAGIES = [
+const UI_STRATEGIES = [
   "email",
   "sms",
   "auth0",
   "authhero",
-  "Username-Password-Authentication",
+  "username-password-authentication",
];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const UI_STRAGIES = [
"email",
"sms",
"auth0",
"authhero",
"Username-Password-Authentication",
];
const UI_STRATEGIES = [
"email",
"sms",
"auth0",
"authhero",
"username-password-authentication",
];

Comment on lines 97 to 98
auth2State.code_id,
options.scope?.split(" ") || [],
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Validate and sanitize OAuth scope values

The current implementation defaults to an empty scope array without validation, which might not be appropriate for all authentication scenarios.

Consider adding scope validation:

+ const defaultScope = ['name', 'email'];  // Add minimum required scopes
+ const requestedScope = options.scope?.split(" ") || defaultScope;
+ 
+ // Validate scopes against allowed values
+ const allowedScopes = new Set(['name', 'email', 'openid']);
+ const validScopes = requestedScope.filter(scope => allowedScopes.has(scope));
+ 
  const appleAuthorizatioUrl = await apple.createAuthorizationURL(
    auth2State.code_id,
-   options.scope?.split(" ") || [],
+   validScopes,
  );

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines 82 to 92
options.client_id!,
options.team_id!,
options.kid!,
new Uint8Array(
options
.app_secret!.replace(/^-----BEGIN PRIVATE KEY-----/, "")
.replace(/-----END PRIVATE KEY-----/, "")
.replace(/\s/g, ""),
},
.replace(/\s/g, "")
.split("")
.map((char) => char.charCodeAt(0)),
),
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Enhance security of private key handling and add parameter validation

The current implementation has potential security concerns in how it handles the private key:

  1. String manipulation of private key material could leave sensitive data in string pool
  2. No validation of required parameters before processing

Consider these improvements:

+ if (!options.client_id || !options.team_id || !options.kid || !options.app_secret) {
+   throw new Error('Missing required Apple authentication parameters');
+ }
+
+ // Use a secure buffer to handle private key
+ const privateKeyBuffer = Buffer.from(options.app_secret, 'utf-8');
+ const cleanedKey = privateKeyBuffer.toString().replace(
+   /-----BEGIN PRIVATE KEY-----|-----END PRIVATE KEY-----|\s/g,
+   ''
+ );
+ const keyArray = Uint8Array.from(Buffer.from(cleanedKey, 'base64'));
+ // Clear sensitive data from memory
+ privateKeyBuffer.fill(0);
+
  const apple = new Apple(
    options.client_id!,
    options.team_id!,
    options.kid!,
-   new Uint8Array(
-     options
-       .app_secret!.replace(/^-----BEGIN PRIVATE KEY-----/, "")
-       .replace(/-----END PRIVATE KEY-----/, "")
-       .replace(/\s/g, "")
-       .split("")
-       .map((char) => char.charCodeAt(0)),
-   ),
+   keyArray,
    `${ctx.env.ISSUER}callback`,
  );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
options.client_id!,
options.team_id!,
options.kid!,
new Uint8Array(
options
.app_secret!.replace(/^-----BEGIN PRIVATE KEY-----/, "")
.replace(/-----END PRIVATE KEY-----/, "")
.replace(/\s/g, ""),
},
.replace(/\s/g, "")
.split("")
.map((char) => char.charCodeAt(0)),
),
if (!options.client_id || !options.team_id || !options.kid || !options.app_secret) {
throw new Error('Missing required Apple authentication parameters');
}
// Use a secure buffer to handle private key
const privateKeyBuffer = Buffer.from(options.app_secret, 'utf-8');
const cleanedKey = privateKeyBuffer.toString().replace(
/-----BEGIN PRIVATE KEY-----|-----END PRIVATE KEY-----|\s/g,
''
);
const keyArray = Uint8Array.from(Buffer.from(cleanedKey, 'base64'));
// Clear sensitive data from memory
privateKeyBuffer.fill(0);
const apple = new Apple(
options.client_id!,
options.team_id!,
options.kid!,
keyArray,
`${ctx.env.ISSUER}callback`,
);
🧰 Tools
🪛 Gitleaks (8.21.1)

87-88: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)

Comment on lines +201 to +216
options.client_id!,
options.team_id!,
options.kid!,
new Uint8Array(
options
.app_secret!.replace(/^-----BEGIN PRIVATE KEY-----/, "")
.replace(/-----END PRIVATE KEY-----/, "")
.replace(/\s/g, ""),
},
`${env.ISSUER}callback`,
.replace(/\s/g, "")
.split("")
.map((char) => char.charCodeAt(0)),
),
`${ctx.env.ISSUER}callback`,
);

const tokens = await apple.validateAuthorizationCode(code);
userinfo = parseJwt(tokens.idToken);
userinfo = parseJwt(tokens.idToken());
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor duplicate code and add error handling

The code has several issues that need attention:

  1. Duplicate private key handling code
  2. Missing error handling for token validation
  3. No type checking for token response

Consider these improvements:

+ // Extract to a shared utility function
+ function createAppleClient(options: any, callbackUrl: string) {
+   if (!options.client_id || !options.team_id || !options.kid || !options.app_secret) {
+     throw new Error('Missing required Apple authentication parameters');
+   }
+   
+   const privateKeyBuffer = Buffer.from(options.app_secret, 'utf-8');
+   const cleanedKey = privateKeyBuffer.toString().replace(
+     /-----BEGIN PRIVATE KEY-----|-----END PRIVATE KEY-----|\s/g,
+     ''
+   );
+   const keyArray = Uint8Array.from(Buffer.from(cleanedKey, 'base64'));
+   privateKeyBuffer.fill(0);
+   
+   return new Apple(
+     options.client_id,
+     options.team_id,
+     options.kid,
+     keyArray,
+     callbackUrl
+   );
+ }

  if (connection.name === "apple") {
-   const apple = new Apple(
-     options.client_id!,
-     options.team_id!,
-     options.kid!,
-     new Uint8Array(
-       options
-         .app_secret!.replace(/^-----BEGIN PRIVATE KEY-----/, "")
-         .replace(/-----END PRIVATE KEY-----/, "")
-         .replace(/\s/g, "")
-         .split("")
-         .map((char) => char.charCodeAt(0)),
-     ),
-     `${ctx.env.ISSUER}callback`,
-   );
+   const apple = createAppleClient(options, `${ctx.env.ISSUER}callback`);

    try {
      const tokens = await apple.validateAuthorizationCode(code);
+     if (!tokens.idToken) {
+       throw new Error('ID token not found in response');
+     }
      userinfo = parseJwt(tokens.idToken());
+   } catch (error) {
+     const log = createLogMessage(ctx, {
+       type: LogTypes.FAILED_LOGIN,
+       description: `Apple authentication failed: ${error.message}`,
+     });
+     await ctx.env.data.logs.create(client.tenant.id, log);
+     throw new HTTPException(401, { message: "Authentication failed" });
    }
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
options.client_id!,
options.team_id!,
options.kid!,
new Uint8Array(
options
.app_secret!.replace(/^-----BEGIN PRIVATE KEY-----/, "")
.replace(/-----END PRIVATE KEY-----/, "")
.replace(/\s/g, ""),
},
`${env.ISSUER}callback`,
.replace(/\s/g, "")
.split("")
.map((char) => char.charCodeAt(0)),
),
`${ctx.env.ISSUER}callback`,
);
const tokens = await apple.validateAuthorizationCode(code);
userinfo = parseJwt(tokens.idToken);
userinfo = parseJwt(tokens.idToken());
const apple = createAppleClient(options, `${ctx.env.ISSUER}callback`);
try {
const tokens = await apple.validateAuthorizationCode(code);
if (!tokens.idToken) {
throw new Error('ID token not found in response');
}
userinfo = parseJwt(tokens.idToken());
} catch (error) {
const log = createLogMessage(ctx, {
type: LogTypes.FAILED_LOGIN,
description: `Apple authentication failed: ${error.message}`,
});
await ctx.env.data.logs.create(client.tenant.id, log);
throw new HTTPException(401, { message: "Authentication failed" });
}
🧰 Tools
🪛 Gitleaks (8.21.1)

206-207: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)

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: 1

🧹 Outside diff range and nitpick comments (1)
src/authentication-flows/oauth2.tsx (1)

81-88: Enhance error message specificity

While the validation is good, the error message could be more helpful by indicating which specific parameter is missing.

-      throw new Error("Missing required Apple authentication parameters");
+      const missingParams = [
+        ['client_id', options.client_id],
+        ['team_id', options.team_id],
+        ['kid', options.kid],
+        ['app_secret', options.app_secret]
+      ].filter(([key, value]) => !value).map(([key]) => key);
+      throw new Error(`Missing required Apple authentication parameters: ${missingParams.join(', ')}`);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 95b3969 and 8abd0c7.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (2)
  • src/authentication-flows/oauth2.tsx (2 hunks)
  • src/routes/oauth2/authorize.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/routes/oauth2/authorize.ts
🧰 Additional context used
🪛 Gitleaks (8.21.1)
src/authentication-flows/oauth2.tsx

94-94: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)


217-218: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)

🔇 Additional comments (3)
src/authentication-flows/oauth2.tsx (3)

90-98: LGTM! Secure private key handling implementation

The implementation follows security best practices:

  • Uses Buffer for secure key handling
  • Properly cleans up sensitive data
  • Safely processes the private key
🧰 Tools
🪛 Gitleaks (8.21.1)

94-94: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)


99-104: LGTM! Proper Apple client initialization

The initialization is correct and safe due to the prior parameter validation.


109-109: Past review comment about scope validation still applies

Comment on lines 211 to +227
const apple = new Apple(
{
clientId: options.client_id!,
teamId: options.team_id!,
keyId: options.kid!,
certificate: options
options.client_id!,
options.team_id!,
options.kid!,
new Uint8Array(
options
.app_secret!.replace(/^-----BEGIN PRIVATE KEY-----/, "")
.replace(/-----END PRIVATE KEY-----/, "")
.replace(/\s/g, ""),
},
`${env.ISSUER}callback`,
.replace(/\s/g, "")
.split("")
.map((char) => char.charCodeAt(0)),
),
`${ctx.env.ISSUER}callback`,
);

const tokens = await apple.validateAuthorizationCode(code);
userinfo = parseJwt(tokens.idToken);
userinfo = parseJwt(tokens.idToken());
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling for token validation

The token validation lacks proper error handling which could lead to uncaught exceptions.

-    const tokens = await apple.validateAuthorizationCode(code);
-    userinfo = parseJwt(tokens.idToken());
+    try {
+      const tokens = await apple.validateAuthorizationCode(code);
+      if (!tokens.idToken) {
+        throw new Error('ID token not found in response');
+      }
+      userinfo = parseJwt(tokens.idToken());
+    } catch (error) {
+      const log = createLogMessage(ctx, {
+        type: LogTypes.FAILED_LOGIN,
+        description: `Apple authentication failed: ${error.message}`,
+      });
+      await ctx.env.data.logs.create(client.tenant.id, log);
+      throw new HTTPException(401, { message: "Authentication failed" });
+    }

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Gitleaks (8.21.1)

217-218: Identified a Private Key, which may compromise cryptographic security and sensitive data encryption.

(private-key)

@markusahlstrand markusahlstrand merged commit 1857395 into main Nov 22, 2024
1 of 2 checks passed
@markusahlstrand markusahlstrand deleted the ma/single-connection branch November 22, 2024 08:43
This was referenced Dec 17, 2024
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.

1 participant