Repository navigation
feat(Wordpress Node): Add support for OAuth2 #27113
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
+326
−4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
100 changes: 100 additions & 0 deletions
100
packages/nodes-base/credentials/WordpressOAuth2Api.credentials.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,100 @@ | ||
| import type { ICredentialType, INodeProperties } from 'n8n-workflow'; | ||
|
|
||
| export class WordpressOAuth2Api implements ICredentialType { | ||
|
Joffcom marked this conversation as resolved.
Resolved
|
||
| name = 'wordpressOAuth2Api'; | ||
|
|
||
| displayName = 'WordPress OAuth2 API'; | ||
|
|
||
| extends = ['oAuth2Api']; | ||
|
|
||
| documentationUrl = 'wordpress'; | ||
|
|
||
| properties: INodeProperties[] = [ | ||
| { | ||
| displayName: | ||
| 'OAuth2 authentication works with WordPress.com-hosted sites only. For self-hosted WordPress, use the WordPress API (Basic Auth) credential instead.', | ||
| name: 'wordpressComNotice', | ||
| type: 'notice', | ||
| default: '', | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
| }, | ||
| { | ||
| displayName: 'Grant Type', | ||
| name: 'grantType', | ||
| type: 'hidden', | ||
| default: 'authorizationCode', | ||
| }, | ||
| { | ||
| displayName: 'Authorization URL', | ||
| name: 'authUrl', | ||
| type: 'hidden', | ||
| default: 'https://public-api.wordpress.com/oauth2/authorize', | ||
| required: true, | ||
| }, | ||
| { | ||
| displayName: 'Access Token URL', | ||
| name: 'accessTokenUrl', | ||
| type: 'hidden', | ||
| default: 'https://public-api.wordpress.com/oauth2/token', | ||
| required: true, | ||
| }, | ||
| { | ||
| displayName: 'Auth URI Query Parameters', | ||
| name: 'authQueryParameters', | ||
| type: 'hidden', | ||
| default: '', | ||
| }, | ||
| { | ||
| displayName: 'Authentication', | ||
| name: 'authentication', | ||
| type: 'hidden', | ||
| default: 'header', | ||
| }, | ||
| { | ||
| displayName: 'WordPress.com Site', | ||
| name: 'wordpressSite', | ||
| type: 'string', | ||
| default: '', | ||
| required: true, | ||
|
Joffcom marked this conversation as resolved.
Resolved
|
||
| placeholder: 'myblog.wordpress.com', | ||
| description: | ||
| 'Your WordPress.com site identifier — either a .wordpress.com subdomain (e.g. myblog.wordpress.com) or a custom domain (e.g. myblog.com). Used in API requests that still route through public-api.wordpress.com.', | ||
| }, | ||
| { | ||
| displayName: 'Custom Scopes', | ||
| name: 'customScopes', | ||
| type: 'boolean', | ||
| default: false, | ||
| description: 'Whether to define custom OAuth2 scopes instead of the defaults', | ||
| }, | ||
| { | ||
| displayName: | ||
| 'The default scopes needed for the node to work are already set. If you change these the node may not function correctly.', | ||
| name: 'customScopesNotice', | ||
| type: 'notice', | ||
| default: '', | ||
| displayOptions: { | ||
| show: { | ||
| customScopes: [true], | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| displayName: 'Enabled Scopes', | ||
| name: 'enabledScopes', | ||
| type: 'string', | ||
| displayOptions: { | ||
| show: { | ||
| customScopes: [true], | ||
| }, | ||
| }, | ||
| default: 'global', | ||
| description: 'Space-separated list of OAuth2 scopes to request', | ||
| }, | ||
| { | ||
| displayName: 'Scope', | ||
| name: 'scope', | ||
| type: 'hidden', | ||
| default: '={{$self["customScopes"] ? $self["enabledScopes"] : "global"}}', | ||
| }, | ||
| ]; | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
packages/nodes-base/credentials/test/WordpressOAuth2Api.credentials.test.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,48 @@ | ||
| import { WordpressOAuth2Api } from '../WordpressOAuth2Api.credentials'; | ||
|
|
||
| describe('WordpressOAuth2Api Credential', () => { | ||
| const credential = new WordpressOAuth2Api(); | ||
|
|
||
| it('should have correct credential metadata', () => { | ||
| expect(credential.name).toBe('wordpressOAuth2Api'); | ||
| expect(credential.extends).toEqual(['oAuth2Api']); | ||
|
|
||
| const authUrlProperty = credential.properties.find((p) => p.name === 'authUrl'); | ||
| expect(authUrlProperty?.default).toBe('https://public-api.wordpress.com/oauth2/authorize'); | ||
|
|
||
| const accessTokenUrlProperty = credential.properties.find((p) => p.name === 'accessTokenUrl'); | ||
| expect(accessTokenUrlProperty?.default).toBe('https://public-api.wordpress.com/oauth2/token'); | ||
| }); | ||
|
|
||
| it('should have a notice about WordPress.com-only support', () => { | ||
| const noticeProperty = credential.properties.find((p) => p.name === 'wordpressComNotice'); | ||
| expect(noticeProperty).toBeDefined(); | ||
| expect(noticeProperty?.type).toBe('notice'); | ||
| expect(noticeProperty?.default).toBe(''); | ||
| }); | ||
|
|
||
| it('should have a required wordpressSite field with no default', () => { | ||
| const siteProperty = credential.properties.find((p) => p.name === 'wordpressSite'); | ||
| expect(siteProperty).toBeDefined(); | ||
| expect(siteProperty?.type).toBe('string'); | ||
| expect(siteProperty?.required).toBe(true); | ||
| expect(siteProperty?.default).toBe(''); | ||
| }); | ||
|
|
||
| it('should have custom scopes toggle defaulting to false', () => { | ||
| const customScopesProperty = credential.properties.find((p) => p.name === 'customScopes'); | ||
| expect(customScopesProperty?.default).toBe(false); | ||
| }); | ||
|
|
||
| it('should have enabledScopes defaulting to "global"', () => { | ||
| const enabledScopesProperty = credential.properties.find((p) => p.name === 'enabledScopes'); | ||
| expect(enabledScopesProperty?.default).toBe('global'); | ||
| }); | ||
|
|
||
| it('should use enabledScopes when customScopes is true, otherwise fall back to "global"', () => { | ||
| const scopeProperty = credential.properties.find((p) => p.name === 'scope'); | ||
| expect(scopeProperty?.default).toBe( | ||
| '={{$self["customScopes"] ? $self["enabledScopes"] : "global"}}', | ||
| ); | ||
| }); | ||
| }); |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,29 @@ export async function wordpressApiRequest( | |||||
| uri?: string, | ||||||
| option: IDataObject = {}, | ||||||
| ): Promise<any> { | ||||||
| const credentials = await this.getCredentials('wordpressApi'); | ||||||
| const authType = this.getNodeParameter('authType', 0, 'basicAuth') as string; | ||||||
| const isOAuth2 = authType === 'oAuth2'; | ||||||
|
|
||||||
| let baseUri: string; | ||||||
| let credentialType: string; | ||||||
| let rejectUnauthorized: boolean | undefined; | ||||||
|
|
||||||
| if (isOAuth2) { | ||||||
| const credentials = await this.getCredentials('wordpressOAuth2Api'); | ||||||
| credentialType = 'wordpressOAuth2Api'; | ||||||
| let site = credentials.wordpressSite as string; | ||||||
| try { | ||||||
| site = new URL(site).hostname; | ||||||
| } catch { | ||||||
| site = site.split('/')[0]; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Fallback site normalization should strip query/hash too; otherwise protocol-less inputs like Prompt for AI agents
Suggested change
|
||||||
| } | ||||||
| baseUri = `https://public-api.wordpress.com/wp/v2/sites/${site}`; | ||||||
| } else { | ||||||
| const credentials = await this.getCredentials('wordpressApi'); | ||||||
| credentialType = 'wordpressApi'; | ||||||
| baseUri = `${credentials.url as string}/wp-json/wp/v2`; | ||||||
| rejectUnauthorized = !(credentials.allowUnauthorizedCerts as boolean); | ||||||
| } | ||||||
|
|
||||||
| let options: IRequestOptions = { | ||||||
| headers: { | ||||||
|
|
@@ -29,16 +51,15 @@ export async function wordpressApiRequest( | |||||
| method, | ||||||
| qs, | ||||||
| body, | ||||||
| uri: uri || `${credentials.url}/wp-json/wp/v2${resource}`, | ||||||
| rejectUnauthorized: !credentials.allowUnauthorizedCerts, | ||||||
| uri: uri ?? `${baseUri}${resource}`, | ||||||
| ...(rejectUnauthorized !== undefined ? { rejectUnauthorized } : {}), | ||||||
| json: true, | ||||||
| }; | ||||||
| options = Object.assign({}, options, option); | ||||||
| if (Object.keys(options.body as IDataObject).length === 0) { | ||||||
| delete options.body; | ||||||
| } | ||||||
| try { | ||||||
| const credentialType = 'wordpressApi'; | ||||||
| return await this.helpers.requestWithAuthentication.call(this, credentialType, options); | ||||||
| } catch (error) { | ||||||
| throw new NodeApiError(this.getNode(), error as JsonObject); | ||||||
|
|
||||||
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
Oops, something went wrong.
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.