-
Notifications
You must be signed in to change notification settings - Fork 16
feat: oauth support in variant & personalize #1598
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
Merged
aman19K
merged 6 commits into
next-feature-update
from
feat/DX-1501-variant-personalize-oauth
Oct 1, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9de2947
feat: oauth support in variant & personalize
aman19K 0244801
version bump
aman19K 96cab56
seed plugin version bump
aman19K 3616fc9
added dotenv in utilities plugin
aman19K ccd7bda
clone version bump
aman19K cba5767
refactor: auth handler message
aman19K File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
packages/contentstack-utilities/src/authentication-handler.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { cliux as ux, authHandler, configHandler } from './index'; | ||
|
|
||
| class AuthenticationHandler { | ||
| private authType: string; | ||
| private isOAuth: boolean; | ||
| private token: string | null = null; | ||
|
|
||
| constructor() { | ||
| this.authType = configHandler.get('authorisationType'); | ||
| this.isOAuth = this.authType === 'OAUTH'; | ||
| } | ||
|
|
||
| async getAuthDetails(): Promise<void> { | ||
| try { | ||
| switch (this.authType) { | ||
| case 'BASIC': | ||
| this.token = configHandler.get('authtoken'); | ||
| break; | ||
| case 'OAUTH': | ||
| await authHandler.compareOAuthExpiry(); | ||
| this.token = `Bearer ${configHandler.get('oauthAccessToken')}`; | ||
| break; | ||
| default: | ||
| const authToken = configHandler.get('authtoken'); | ||
| if (authToken) { | ||
| this.token = authToken; | ||
| } else { | ||
| ux.print('Session timed out, please login to proceed', { | ||
| color: 'yellow', | ||
| }); | ||
| process.exit(1); | ||
| } | ||
| break; | ||
| } | ||
| } catch (error) { | ||
| ux.print(`Error occurred while fetching auth details: ${error.message}`, { | ||
| color: 'red', | ||
| }); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| get isOauthEnabled(): boolean { | ||
| return this.isOAuth; | ||
| } | ||
|
|
||
| get accessToken(): string { | ||
| if (!this.token) { | ||
| throw new Error('Token is not available. Please authenticate first.'); | ||
| } | ||
| return this.token; | ||
| } | ||
|
|
||
| async refreshAccessToken(error: any, maxRetryCount = 1): Promise<void> { | ||
| if (error.response && error.response.status) { | ||
| if (maxRetryCount >= 3) { | ||
| ux.print('Max retry count reached, please login to proceed', { | ||
| color: 'yellow', | ||
| }); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| maxRetryCount++; // Increment for the next retry attempt | ||
|
|
||
| switch (error.response.status) { | ||
| case 401: | ||
| // NOTE: Refresh the token if the type is OAuth. | ||
| const region: { cma: string; name: string; cda: string } = configHandler.get('region') || {}; | ||
| if (region?.cma) { | ||
| let hostName: string = ''; | ||
| if (region.cma.startsWith('http')) { | ||
| const u = new URL(region.cma); | ||
| if (u.host) hostName = u.host; | ||
| } | ||
| hostName = hostName || region.cma; | ||
| await this.refreshToken(hostName); | ||
| return this.refreshAccessToken(error, maxRetryCount); // Retry after refreshing the token | ||
| } | ||
|
|
||
| case 429: | ||
| case 408: | ||
| // These cases require a wait, adding a delay before retrying | ||
| await new Promise((resolve) => setTimeout(resolve, 1000)); // wait for 1 second | ||
| return this.refreshAccessToken(error, maxRetryCount); // Retry | ||
|
|
||
| default: | ||
| return; // Handle other cases if necessary | ||
| } | ||
| } | ||
| } | ||
|
|
||
| refreshToken(hostName: string): Promise<boolean> { | ||
| return new Promise<boolean>((resolve) => { | ||
| if (this.authType === 'BASIC') { | ||
| // NOTE Handle basic auth 401 here | ||
| resolve(false); | ||
| ux.print('Session timed out, please login to proceed', { | ||
| color: 'yellow', | ||
| }); | ||
| process.exit(); | ||
| } else if (this.authType === 'OAUTH') { | ||
| authHandler.host = hostName; | ||
| // NOTE Handle OAuth refresh token | ||
| authHandler | ||
| .compareOAuthExpiry(true) | ||
| .then(() => { | ||
| this.token = `Bearer ${configHandler.get('oauthAccessToken')}`; | ||
| resolve(true); | ||
| }) | ||
| .catch((error: any) => { | ||
| console.log(error); | ||
| resolve(false); | ||
| }); | ||
| } else { | ||
| resolve(false); | ||
| ux.print('You do not have the permissions to perform this action, please login to proceed', { | ||
| color: 'yellow', | ||
| }); | ||
| process.exit(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const authenticationHandler = new AuthenticationHandler(); | ||
| export default authenticationHandler; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.