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(api): Added GitLab OAuth #188

Merged
merged 1 commit into from
Apr 20, 2024
Merged

feat(api): Added GitLab OAuth #188

merged 1 commit into from
Apr 20, 2024

Conversation

rajdip-b
Copy link
Member

@rajdip-b rajdip-b commented Apr 20, 2024

User description

Fixes #155


Type

Enhancement


Description

  • Integrated GitLab OAuth into the authentication system.
  • Added necessary classes and factories for GitLab OAuth strategy.
  • Updated tests and documentation to support GitLab OAuth.
  • Added new endpoints for GitLab OAuth login and callback.
  • Included new environment variables and updated package dependencies.

Changes walkthrough

Relevant files
Enhancement
4 files
auth.module.ts
Integrate GitLab OAuth Strategy into Auth Module                 

apps/api/src/auth/auth.module.ts

  • Added GitlabOAuthStrategyFactory and GitlabStrategy to the
    authentication module.
  • Configured dependency injection for GitLab OAuth strategy.
  • +10/-0   
    auth.controller.ts
    Implement GitLab OAuth Endpoints in Auth Controller           

    apps/api/src/auth/controller/auth.controller.ts

  • Added gitlabOAuthStrategyFactory to the constructor for dependency
    injection.
  • Implemented new endpoints for GitLab OAuth login and callback.
  • +50/-1   
    gitlab-strategy.factory.ts
    Implement GitLab OAuth Strategy Factory                                   

    apps/api/src/config/factory/gitlab/gitlab-strategy.factory.ts

  • Implemented GitlabOAuthStrategyFactory with methods to check if OAuth
    is enabled and to create the strategy.
  • +35/-0   
    gitlab.strategy.ts
    Implement GitLab Strategy for OAuth                                           

    apps/api/src/config/oauth-strategy/gitlab/gitlab.strategy.ts

    • Created GitlabStrategy for handling GitLab OAuth.
    +23/-0   
    Tests
    3 files
    auth.controller.spec.ts
    Update Auth Controller Tests for GitLab Integration           

    apps/api/src/auth/controller/auth.controller.spec.ts

  • Included GitlabOAuthStrategyFactory in the testing setup for
    AuthController.
  • +2/-0     
    gitlab-strategy.factory.spec.ts
    Add Tests for GitLab OAuth Strategy Factory                           

    apps/api/src/config/factory/gitlab/gitlab-strategy.factory.spec.ts

  • Added tests for enabling/disabling GitLab OAuth and creating the
    strategy.
  • +46/-0   
    gitlab.strategy.spec.ts
    Add Tests for GitLab Strategy                                                       

    apps/api/src/config/oauth-strategy/gitlab/gitlab.strategy.spec.ts

    • Added basic tests for the GitlabStrategy.
    +17/-0   
    Configuration changes
    1 files
    .env.example
    Add GitLab OAuth Environment Variables to Example               

    .env.example

    • Added environment variables for GitLab OAuth.
    +4/-0     
    Dependencies
    2 files
    package.json
    Add Passport-GitLab2 Dependency                                                   

    apps/api/package.json

    • Added passport-gitlab2 dependency for GitLab OAuth.
    +1/-0     
    pnpm-lock.yaml
    Update Lock File for New Dependency                                           

    pnpm-lock.yaml

    • Updated lock file with passport-gitlab2 package.
    +10/-0   
    Documentation
    1 files
    environment-variables.md
    Document GitLab OAuth Environment Variables                           

    docs/contributing-to-keyshade/environment-variables.md

  • Updated documentation to include GitLab OAuth environment variables.
  • +18/-16 

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    sonarcloud bot commented Apr 20, 2024

    Quality Gate Passed Quality Gate passed

    Issues
    0 New issues
    0 Accepted issues

    Measures
    0 Security Hotspots
    No data about Coverage
    0.0% Duplication on New Code

    See analysis details on SonarCloud

    @codiumai-pr-agent-free codiumai-pr-agent-free bot added the type: enhancement New feature or request label Apr 20, 2024
    Copy link
    Contributor

    PR Description updated to latest commit (488854d)

    Copy link
    Contributor

    PR Review

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves integration of a new OAuth strategy (GitLab), which includes updates across multiple files including module configuration, controller updates, strategy implementation, and tests. The changes are moderate in size and complexity, requiring a thorough review to ensure security and functionality.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    Possible Bug: The GitLab OAuth endpoints in auth.controller.ts might throw an exception if the OAuth is not enabled, but it does not handle what happens if the user's GitLab account does not provide an email. This should be handled gracefully.

    Configuration Dependency: The system's behavior depends on environment variables (GITLAB_CLIENT_ID, GITLAB_CLIENT_SECRET, GITLAB_CALLBACK_URL). If these are not set correctly, the OAuth functionality will not work, and the error logging in gitlab-strategy.factory.ts might not be sufficient for troubleshooting.

    🔒 Security concerns

    No


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link
    Contributor

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Add GitlabOAuthStrategyFactory to the module providers for dependency injection.

    Consider adding the GitlabOAuthStrategyFactory to the providers array directly in the
    @Module decorator to ensure it is available for dependency injection throughout the
    module.

    apps/api/src/auth/auth.module.ts [44]

    -GitlabOAuthStrategyFactory,
    +providers: [GitlabOAuthStrategyFactory, ...],
     
    Implement error handling in the gitlabOAuthCallback method.

    Add error handling for the gitlabOAuthCallback method to manage exceptions that may occur
    during the OAuth process, such as token expiration or network issues.

    apps/api/src/auth/controller/auth.controller.ts [190-194]

    -return await this.authService.handleOAuthLogin(
    -  email,
    -  name,
    -  profilePictureUrl
    -)
    +try {
    +  return await this.authService.handleOAuthLogin(
    +    email,
    +    name,
    +    profilePictureUrl
    +  )
    +} catch (error) {
    +  throw new HttpException('Failed to handle GitLab OAuth login', HttpStatus.INTERNAL_SERVER_ERROR);
    +}
     
    Best practice
    Standardize the import structure in the @Module decorator.

    Ensure consistent object structure in the module imports by using braces for single
    imports, similar to other imports in the array.

    apps/api/src/auth/auth.module.ts [44]

    -GitlabOAuthStrategyFactory,
    +{ provide: GitlabOAuthStrategyFactory, useClass: GitlabOAuthStrategyFactory },
     
    Use a specific exception for disabled features in the gitlabOAuthLogin method.

    Refactor the gitlabOAuthLogin method to use a more descriptive and specific exception when
    GitLab OAuth is not enabled, such as FeatureNotEnabledException.

    apps/api/src/auth/controller/auth.controller.ts [160-163]

    -throw new HttpException(
    -  'GitLab Auth is not enabled in this environment. Refer to the https://docs.keyshade.xyz/contributing-to-keyshade/environment-variables if you would like to set it up.',
    -  HttpStatus.BAD_REQUEST
    +throw new FeatureNotEnabledException(
    +  'GitLab Auth is not enabled. Please enable it to use this feature.',
    +  'GITLAB_OAUTH'
     )
     
    Specify a return type for the gitlabOAuthCallback method.

    Add a return type to the gitlabOAuthCallback method to improve code readability and
    maintainability.

    apps/api/src/auth/controller/auth.controller.ts [187]

    -async gitlabOAuthCallback(@Req() req) {
    +async gitlabOAuthCallback(@Req() req): Promise<Profile> {
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    Copy link

    codecov bot commented Apr 20, 2024

    Codecov Report

    Attention: Patch coverage is 84.84848% with 5 lines in your changes are missing coverage. Please review.

    Project coverage is 93.02%. Comparing base (7bb3d21) to head (488854d).
    Report is 46 commits behind head on develop.

    Files Patch % Lines
    ...rc/config/oauth-strategy/gitlab/gitlab.strategy.ts 50.00% 4 Missing ⚠️
    ...c/config/factory/gitlab/gitlab-strategy.factory.ts 93.75% 1 Missing ⚠️
    Additional details and impacted files
    @@             Coverage Diff              @@
    ##           develop     #188       +/-   ##
    ============================================
    + Coverage    62.20%   93.02%   +30.81%     
    ============================================
      Files           76       99       +23     
      Lines         1503     2180      +677     
      Branches       260      404      +144     
    ============================================
    + Hits           935     2028     +1093     
    + Misses         568      152      -416     
    Flag Coverage Δ
    api-e2e-tests 93.02% <84.84%> (+30.81%) ⬆️

    Flags with carried forward coverage won't be shown. Click here to find out more.

    ☔ View full report in Codecov by Sentry.
    📢 Have feedback on the report? Share it here.

    @rajdip-b rajdip-b merged commit 4d3bbe4 into develop Apr 20, 2024
    10 of 11 checks passed
    @rajdip-b rajdip-b deleted the feat/gitlab-auth branch April 20, 2024 17:15
    kriptonian1 pushed a commit that referenced this pull request Apr 24, 2024
    rajdip-b pushed a commit that referenced this pull request May 12, 2024
    ## [1.3.0](v1.2.0...v1.3.0) (2024-05-12)
    
    ### 🚀 Features
    
    * Add approval support ([#158](#158)) ([e09ae60](e09ae60))
    * **api:** Add configuration live update support ([#181](#181)) ([f7d6684](f7d6684))
    * **api:** Add feature to export data of a workspace ([#152](#152)) ([46833aa](46833aa))
    * **api:** Add Integration support ([#203](#203)) ([f1ae87e](f1ae87e))
    * **api:** Add note to [secure] and variable ([#151](#151)) ([2e62351](2e62351))
    * **api:** Add OAuth redirection and polished authentication ([#212](#212)) ([d2968bc](d2968bc))
    * **api:** Add support for storing and managing variables ([#149](#149)) ([963a8ae](963a8ae))
    * **api:** Added GitLab OAuth ([#188](#188)) ([4d3bbe4](4d3bbe4))
    * **api:** Added validation for reason field ([#190](#190)) ([90b8ff2](90b8ff2))
    * **api:** Create default workspace on user's creation ([#182](#182)) ([3dc0c4c](3dc0c4c))
    * **api:** Reading `port` Dynamically ([#170](#170)) ([fd46e3e](fd46e3e))
    * **auth:** Add Google OAuth ([#156](#156)) ([cf387ea](cf387ea))
    * **web:** Added waitlist ([#168](#168)) ([1084c77](1084c77))
    * **web:** Landing revamp ([#165](#165)) ([0bc723b](0bc723b))
    
    ### 🐛 Bug Fixes
    
    * **web:** alignment issue in “Collaboration made easy” section ([#178](#178)) ([df5ca75](df5ca75))
    * **workspace:** delete duplicate tailwind config ([99d922a](99d922a))
    
    ### 📚 Documentation
    
    * add contributor list ([f37569a](f37569a))
    * Add integration docs ([#204](#204)) ([406ddb7](406ddb7))
    * Added integration docs to gitbook summary ([ab37530](ab37530))
    * **api:** Add swagger docs of API key controller ([#167](#167)) ([2910476](2910476))
    * **api:** Add swagger docs of User Controller ([#166](#166)) ([fd59522](fd59522))
    * fix typo in environment-variables.md ([#163](#163)) ([48294c9](48294c9))
    * Remove supabase from docs ([#169](#169)) ([eddbce8](eddbce8))
    * **setup:** replace NX with Turbo in setup instructions ([#175](#175)) ([af8a460](af8a460))
    * Update README.md ([b59f16b](b59f16b))
    * Update running-the-api.md ([177dbbf](177dbbf))
    * Update running-the-api.md ([#193](#193)) ([3d5bcac](3d5bcac))
    
    ### 🔧 Miscellaneous Chores
    
    * Added lockfile ([60a3b9b](60a3b9b))
    * Added lockfile ([6bb512c](6bb512c))
    * **api:** Added type inference and runtime validation to `process.env` ([#200](#200)) ([249e07d](249e07d))
    * **api:** Fixed prisma script env errors ([#209](#209)) ([8762354](8762354))
    * **API:** Refactor authority check functions in API ([#189](#189)) ([e9d710d](e9d710d))
    * **api:** Refactor user e2e tests ([b38d45a](b38d45a))
    * **ci:** Disabled api stage release ([97877c4](97877c4))
    * **ci:** Update stage deployment config ([868a6a1](868a6a1))
    * **codecov:** update api-e2e project coverage ([1e90d7e](1e90d7e))
    * **dockerfile:** Fixed web dockerfile ([6134bb2](6134bb2))
    * **docker:** Optimized web Dockerfile to reduct image size ([#173](#173)) ([444286a](444286a))
    * **release:** Downgraded package version ([c173fee](c173fee))
    * **release:** Fix failing release ([#213](#213)) ([40f64f3](40f64f3))
    * **release:** Install pnpm ([1081bea](1081bea))
    * **release:** Updated release commit ([b8958e7](b8958e7))
    * **release:** Updated release commit ([e270eb8](e270eb8))
    * Update deprecated husky Install command ([#202](#202)) ([e61102c](e61102c))
    * Upgrade @million/lint from 0.0.66 to 0.0.73 ([#172](#172)) ([dd43ed9](dd43ed9))
    * **web:** Updated fly memory config ([4debc66](4debc66))
    
    ### 🔨 Code Refactoring
    
    * **api:** Made events central to workspace ([#159](#159)) ([9bc00ae](9bc00ae))
    * **api:** Migrated to cookie based authentication ([#206](#206)) ([ad6911f](ad6911f))
    * **monorepo:** Migrate from nx to turbo ([#153](#153)) ([88b4b00](88b4b00))
    @rajdip-b
    Copy link
    Member Author

    🎉 This PR is included in version 1.3.0 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type: enhancement New feature or request
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    GitLab OAuth
    1 participant