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
Merged
Changes from 1 commit
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
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,112 @@ | ||
| import type { ICredentialType, INodeProperties } from 'n8n-workflow'; | ||
|
|
||
| export class WordpressOAuth2Api implements ICredentialType { | ||
|
Joffcom marked this conversation as 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.
|
||
| }, | ||
| { | ||
| 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: 'Use Custom Domain', | ||
| name: 'customDomain', | ||
| type: 'boolean', | ||
| default: false, | ||
| description: | ||
| 'Whether your WordPress.com site uses a custom domain instead of a .wordpress.com subdomain', | ||
| }, | ||
| { | ||
| displayName: 'Custom Domain', | ||
| name: 'customDomainUrl', | ||
| type: 'string', | ||
| displayOptions: { | ||
| show: { | ||
| customDomain: [true], | ||
| }, | ||
| }, | ||
| default: '', | ||
| placeholder: 'myblog.com', | ||
| description: | ||
| "Your WordPress.com site's custom domain. Used as the site identifier in API requests — calls 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"}}', | ||
| }, | ||
| ]; | ||
| } | ||
159 changes: 159 additions & 0 deletions
159
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,159 @@ | ||
| import { ClientOAuth2 } from '@n8n/client-oauth2'; | ||
|
Joffcom marked this conversation as resolved.
Outdated
|
||
| import nock from 'nock'; | ||
|
|
||
| import { WordpressOAuth2Api } from '../WordpressOAuth2Api.credentials'; | ||
|
|
||
| describe('WordpressOAuth2Api Credential', () => { | ||
| const credential = new WordpressOAuth2Api(); | ||
|
|
||
| // Shared OAuth2 configuration | ||
| const authorizationUri = 'https://public-api.wordpress.com/oauth2/authorize'; | ||
| const accessTokenUri = 'https://public-api.wordpress.com/oauth2/token'; | ||
| const redirectUri = 'http://localhost:5678/rest/oauth2-credential/callback'; | ||
| const clientId = 'test-client-id'; | ||
| const clientSecret = 'test-client-secret'; | ||
|
|
||
| const createOAuthClient = (scopes: string[]) => | ||
| new ClientOAuth2({ | ||
| clientId, | ||
| clientSecret, | ||
| accessTokenUri, | ||
| authorizationUri, | ||
| redirectUri, | ||
| scopes, | ||
| }); | ||
|
|
||
| const mockTokenEndpoint = (code: string, responseScopes: string[]) => { | ||
| nock('https://public-api.wordpress.com') | ||
| .post('/oauth2/token', (body: Record<string, unknown>) => { | ||
| return ( | ||
| body.code === code && | ||
| body.grant_type === 'authorization_code' && | ||
| body.redirect_uri === redirectUri | ||
| ); | ||
| }) | ||
| .reply(200, { | ||
| access_token: 'test-access-token', | ||
| token_type: 'Bearer', | ||
| expires_in: 64800, | ||
| scope: responseScopes.join(' '), | ||
| blog_id: '12345', | ||
| blog_url: 'https://myblog.wordpress.com', | ||
| }); | ||
| }; | ||
|
|
||
| beforeAll(() => { | ||
| nock.disableNetConnect(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| nock.restore(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| 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 custom domain toggle defaulting to false', () => { | ||
| const customDomainProperty = credential.properties.find((p) => p.name === 'customDomain'); | ||
| expect(customDomainProperty?.default).toBe(false); | ||
| }); | ||
|
|
||
| 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 default scope of global', () => { | ||
| const enabledScopesProperty = credential.properties.find((p) => p.name === 'enabledScopes'); | ||
| expect(enabledScopesProperty?.default).toBe('global'); | ||
| }); | ||
|
|
||
| describe('OAuth2 flow with default scope', () => { | ||
| it('should include global scope in authorization URI', () => { | ||
| const oauthClient = createOAuthClient(['global']); | ||
| const authUri = oauthClient.code.getUri(); | ||
|
|
||
| expect(authUri).toContain('scope='); | ||
| expect(authUri).toContain('global'); | ||
| expect(authUri).toContain(`client_id=${clientId}`); | ||
| expect(authUri).toContain('response_type=code'); | ||
| }); | ||
|
|
||
| it('should retrieve token successfully with global scope', async () => { | ||
| const code = 'test-auth-code'; | ||
| mockTokenEndpoint(code, ['global']); | ||
|
|
||
| const oauthClient = createOAuthClient(['global']); | ||
| const token = await oauthClient.code.getToken(redirectUri + `?code=${code}`); | ||
|
|
||
| expect(token.data.scope).toContain('global'); | ||
| expect(token.data.blog_id).toBe('12345'); | ||
| expect(token.data.blog_url).toBe('https://myblog.wordpress.com'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('OAuth2 flow with custom scopes', () => { | ||
| const customScopes = ['posts', 'media']; | ||
|
|
||
| it('should include custom scopes in authorization URI', () => { | ||
| const oauthClient = createOAuthClient(customScopes); | ||
| const authUri = oauthClient.code.getUri(); | ||
|
|
||
| expect(authUri).toContain('scope='); | ||
| expect(authUri).toContain('posts'); | ||
| expect(authUri).toContain('media'); | ||
| expect(authUri).not.toContain('global'); | ||
| }); | ||
|
|
||
| it('should retrieve token successfully with custom scopes', async () => { | ||
| const code = 'test-auth-code'; | ||
| mockTokenEndpoint(code, customScopes); | ||
|
|
||
| const oauthClient = createOAuthClient(customScopes); | ||
| const token = await oauthClient.code.getToken(redirectUri + `?code=${code}`); | ||
|
|
||
| expect(token.data.scope).toContain('posts'); | ||
| expect(token.data.scope).toContain('media'); | ||
| }); | ||
|
|
||
| it('should handle completely different custom scopes', async () => { | ||
| const differentScopes = ['comments', 'stats']; | ||
| const code = 'test-auth-code'; | ||
| mockTokenEndpoint(code, differentScopes); | ||
|
|
||
| const oauthClient = createOAuthClient(differentScopes); | ||
| const authUri = oauthClient.code.getUri(); | ||
|
|
||
| expect(authUri).toContain('comments'); | ||
| expect(authUri).toContain('stats'); | ||
| expect(authUri).not.toContain('global'); | ||
| expect(authUri).not.toContain('posts'); | ||
|
|
||
| const token = await oauthClient.code.getToken(redirectUri + `?code=${code}`); | ||
|
|
||
| expect(token.data.scope).toContain('comments'); | ||
| expect(token.data.scope).toContain('stats'); | ||
| expect(token.data.scope).not.toContain('global'); | ||
| expect(token.data.scope).not.toContain('posts'); | ||
| }); | ||
| }); | ||
| }); | ||
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
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.