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: add Oauth2 Login #420

Merged
merged 1 commit into from
Aug 18, 2024
Merged

feat: add Oauth2 Login #420

merged 1 commit into from
Aug 18, 2024

Conversation

dreamhunter2333
Copy link
Owner

@dreamhunter2333 dreamhunter2333 commented Aug 18, 2024

PR Type

Enhancement, Documentation


Description

  • Added Oauth2 login functionality, including support for GitHub and Authentik.
  • Created new components for managing and handling Oauth2 settings and login callbacks.
  • Updated admin and user interfaces to include Oauth2 settings and login options.
  • Enhanced backend APIs to support fetching, saving, and processing Oauth2 settings and logins.
  • Updated documentation and changelog to reflect the new Oauth2 login feature.

Changes walkthrough 📝

Relevant files
Enhancement
17 files
Admin.vue
Add Oauth2 settings tab in Admin view                                       

frontend/src/views/Admin.vue

  • Added import for UserOauth2Settings.vue
  • Added new tab pane for Oauth2 settings in the admin view
  • Updated localization strings for Oauth2 settings
  • +6/-0     
    UserOauth2Settings.vue
    Create UserOauth2Settings component for Oauth2 management

    frontend/src/views/admin/UserOauth2Settings.vue

  • Created new component for managing Oauth2 settings
  • Added form and logic for adding and saving Oauth2 settings
  • Included localization for English and Chinese
  • +253/-0 
    UserLogin.vue
    Add Oauth2 login functionality in UserLogin view                 

    frontend/src/views/user/UserLogin.vue

  • Added Oauth2 login button and logic
  • Updated localization strings for Oauth2 login
  • +30/-1   
    UserOauth2Callback.vue
    Create UserOauth2Callback component for Oauth2 login         

    frontend/src/views/user/UserOauth2Callback.vue

  • Created new component for handling Oauth2 callback
  • Implemented logic to handle Oauth2 login callback and JWT generation
  • +65/-0   
    index.ts
    Add common mail domains constant                                                 

    frontend/src/constant/index.ts

    • Added constant for common mail domains
    +8/-0     
    index.ts
    Add UserOauth2Settings type definition                                     

    frontend/src/models/index.ts

    • Added UserOauth2Settings type definition
    +15/-0   
    index.ts
    Add Oauth2 settings routes in admin API                                   

    worker/src/admin_api/index.ts

    • Added routes for fetching and saving Oauth2 settings
    +5/-0     
    oauth2_settings.ts
    Create handlers for Oauth2 settings in admin API                 

    worker/src/admin_api/oauth2_settings.ts

    • Created handlers for fetching and saving Oauth2 settings
    +34/-0   
    constants.ts
    Add Oauth2 settings key constant                                                 

    worker/src/constants.ts

    • Added constant for Oauth2 settings key
    +1/-0     
    index.ts
    Add UserOauth2Settings type definition                                     

    worker/src/models/index.ts

    • Added UserOauth2Settings type definition
    +16/-0   
    index.ts
    Add Oauth2 login and callback routes in user API                 

    worker/src/user_api/index.ts

    • Added routes for Oauth2 login and callback
    +5/-0     
    oauth2.ts
    Create handlers for Oauth2 login and callback                       

    worker/src/user_api/oauth2.ts

    • Created handlers for Oauth2 login and callback
    +105/-0 
    settings.ts
    Update open settings with Oauth2 client IDs                           

    worker/src/user_api/settings.ts

    • Updated open settings to include Oauth2 client IDs
    +14/-1   
    utils.ts
    Enhance getJsonSetting utility with generics                         

    worker/src/utils.ts

    • Updated getJsonSetting utility function to support generic types
    +3/-4     
    worker.ts
    Allow Oauth2 routes without authentication                             

    worker/src/worker.ts

    • Added middleware to allow Oauth2 routes without authentication
    +1/-0     
    index.js
    Add route for Oauth2 callback                                                       

    frontend/src/router/index.js

    • Added route for Oauth2 callback
    +6/-1     
    index.js
    Add state management for Oauth2 session                                   

    frontend/src/store/index.js

    • Added state management for Oauth2 session
    +14/-5   
    Documentation
    3 files
    zh.ts
    Update sidebar with Oauth2 documentation link                       

    vitepress-docs/docs/.vitepress/zh.ts

    • Updated sidebar to include Oauth2 documentation link
    +6/-5     
    CHANGELOG.md
    Update changelog with Oauth2 login feature                             

    CHANGELOG.md

    • Updated changelog to include Oauth2 login feature
    +1/-0     
    user-oauth2.md
    Add documentation for Oauth2 third-party login                     

    vitepress-docs/docs/zh/guide/feature/user-oauth2.md

    • Added documentation for Oauth2 third-party login
    +26/-0   

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

    @github-actions github-actions bot added documentation Improvements or additions to documentation enhancement New feature or request labels Aug 18, 2024
    Copy link

    Preparing review...

    Copy link

    github-actions bot commented Aug 18, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add a check to ensure that the OAuth2 name is not empty before adding a new setting

    Add a check to ensure that newOauth2Name is not empty before pushing a new OAuth2
    setting to userOauth2Settings. This will prevent adding entries with empty names.

    frontend/src/views/admin/UserOauth2Settings.vue [134-148]

    +if (newOauth2Name.value.trim() === '') {
    +    message.error('OAuth2 name cannot be empty');
    +    return;
    +}
     userOauth2Settings.value.push({
         name: newOauth2Name.value,
         clientID: '',
         clientSecret: '',
         authorizationURL: authorizationURL(),
         accessTokenURL: accessTokenURL(),
         accessTokenFormat: accessTokenFormat(),
         userInfoURL: userInfoURL(),
         userEmailKey: userEmailKey(),
         redirectURL: `${window.location.origin}/user/oauth2/callback`,
         logoutURL: '',
         scope: scope(),
         enableMailAllowList: false,
         mailAllowList: constant.COMMOM_MAIL
     } as UserOauth2Settings)
     
    Suggestion importance[1-10]: 7

    Why:

    7
    Add a check to ensure userOpenSettings.oauth2ClientIDs is defined and not empty before iterating

    Add a check to ensure userOpenSettings.oauth2ClientIDs is defined and not empty
    before iterating over it to prevent potential runtime errors.

    frontend/src/views/user/UserLogin.vue [234-237]

    -<n-button @click="oauth2Login(item.clientID)" v-for="item in userOpenSettings.oauth2ClientIDs"
    +<n-button v-if="userOpenSettings.oauth2ClientIDs && userOpenSettings.oauth2ClientIDs.length" 
    +    @click="oauth2Login(item.clientID)" v-for="item in userOpenSettings.oauth2ClientIDs"
         :key="item.clientID" block secondary strong>
         {{ t('loginWith', { provider: item.name }) }}
     </n-button>
     
    Suggestion importance[1-10]: 7

    Why:

    7
    Add a check to ensure the access_token is present in the response before proceeding

    Add a check to ensure the access_token is present in the response before proceeding
    to fetch user information to prevent potential runtime errors.

    worker/src/user_api/oauth2.ts [53-57]

     const { access_token, token_type } = resJson as { access_token: string, token_type?: string };
    +if (!access_token) {
    +    return c.text("Failed to retrieve access token", 400);
    +}
     const user = await fetch(setting.userInfoURL, {
         headers: {
             "Authorization": `${token_type || 'Bearer'} ${access_token}`,
             "Accept": "application/json",
             "User-Agent": "Cloudflare Workers"
         }
     })
     
    Suggestion importance[1-10]: 7

    Why:

    7
    Performance
    Add a debounce mechanism to the save function to prevent multiple rapid submissions

    Add a debounce mechanism to the save function to prevent multiple rapid submissions
    which could lead to race conditions or server overload.

    frontend/src/views/admin/UserOauth2Settings.vue [61-70]

    -const save = async () => {
    +const debounce = (func, wait) => {
    +    let timeout;
    +    return (...args) => {
    +        clearTimeout(timeout);
    +        timeout = setTimeout(() => func.apply(this, args), wait);
    +    };
    +};
    +
    +const save = debounce(async () => {
         try {
             await api.fetch(`/admin/user_oauth2_settings`, {
                 method: 'POST',
                 body: JSON.stringify(userOauth2Settings.value)
             })
             message.success(t('successTip'))
         } catch (error) {
             message.error((error as Error).message || "error");
         }
    -}
    +}, 300);
     
    Suggestion importance[1-10]: 7

    Why:

    7

    @dreamhunter2333 dreamhunter2333 merged commit b5b59ac into main Aug 18, 2024
    @dreamhunter2333 dreamhunter2333 deleted the feature/dev branch August 18, 2024 06:39
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    documentation Improvements or additions to documentation enhancement New feature or request
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant