-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
fix(auth): Treat authData[provider]=null as unlink; skip provider validation for unlink #9856
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
Open
SNtGog
wants to merge
4
commits into
parse-community:alpha
Choose a base branch
from
SNtGog:refactor-restwrite-authdata-handling
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7669bd9
Optimize authData handling logic and integrate delta-based updates
SNtGog 7db1f8e
Revert "Optimize authData handling logic and integrate delta-based up…
SNtGog 2979264
Fix authData handling to exclude unlinked providers and add tests for…
SNtGog 4e3ac64
remove redundant tests
SNtGog 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
describe('AuthData Delta Behavior', () => { | ||
const MOCK_USER_ID = 'mockUserId'; | ||
const MOCK_ACCESS_TOKEN = 'mockAccessToken123'; | ||
|
||
const createMockUser = () => ({ | ||
id: MOCK_USER_ID, | ||
code: 'C1' | ||
}); | ||
|
||
const mockGooglePlayGamesAPI = () => { | ||
mockFetch([ | ||
{ | ||
url: 'https://oauth2.googleapis.com/token', | ||
method: 'POST', | ||
response: { | ||
ok: true, | ||
json: () => Promise.resolve({ access_token: MOCK_ACCESS_TOKEN }), | ||
}, | ||
}, | ||
{ | ||
url: `https://www.googleapis.com/games/v1/players/${MOCK_USER_ID}`, | ||
method: 'GET', | ||
response: { | ||
ok: true, | ||
json: () => Promise.resolve({ playerId: MOCK_USER_ID }), | ||
}, | ||
}, | ||
]); | ||
}; | ||
|
||
const setupAuthConfig = (additionalProviders = {}) => { | ||
return reconfigureServer({ | ||
auth: { | ||
gpgames: { | ||
clientId: 'validClientId', | ||
clientSecret: 'validClientSecret', | ||
}, | ||
someAdapter1: { | ||
validateAuthData: () => Promise.resolve(), | ||
validateAppId: () => Promise.resolve(), | ||
validateOptions: () => {}, | ||
}, | ||
someAdapter2: { | ||
validateAuthData: () => Promise.resolve(), | ||
validateAppId: () => Promise.resolve(), | ||
validateOptions: () => {}, | ||
}, | ||
...additionalProviders, | ||
}, | ||
}); | ||
}; | ||
|
||
beforeEach(async () => { | ||
await setupAuthConfig(); | ||
}); | ||
|
||
describe('Provider Linking', () => { | ||
it('should link someAdapter1 without affecting unchanged Google Play Games auth', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = createMockUser(); | ||
const user = await Parse.User.logInWith('gpgames', { authData }); | ||
const sessionToken = user.getSessionToken(); | ||
|
||
await user.fetch({ sessionToken }); | ||
const currentAuthData = user.get('authData') || {}; | ||
|
||
user.set('authData', { | ||
...currentAuthData, | ||
someAdapter1: { id: 'T1', access_token: 'token123' }, | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData'); | ||
|
||
expect(finalAuthData.gpgames?.id).toBe(MOCK_USER_ID); | ||
expect(finalAuthData.someAdapter1?.id).toBe('T1'); | ||
}); | ||
|
||
it('should handle multiple providers correctly', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = { | ||
gpgames: { id: MOCK_USER_ID, code: 'C4' }, | ||
someAdapter2: { id: 'F1', access_token: 'fb_token' }, | ||
}; | ||
|
||
const user = new Parse.User(); | ||
user.set('authData', authData); | ||
await user.save(); | ||
|
||
const sessionToken = user.getSessionToken(); | ||
|
||
await user.fetch({ sessionToken }); | ||
const currentAuthData = user.get('authData') || {}; | ||
|
||
user.set('authData', { | ||
someAdapter2: currentAuthData.someAdapter2, | ||
someAdapter1: { id: 'T2', access_token: 'tw_token' }, | ||
gpgames: null, // Unlink Google Play Games | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData') || {}; | ||
|
||
expect(finalAuthData.gpgames).toBeUndefined(); | ||
expect(finalAuthData.someAdapter2?.id).toBe('F1'); | ||
expect(finalAuthData.someAdapter1?.id).toBe('T2'); | ||
}); | ||
}); | ||
|
||
describe('Provider Unlinking', () => { | ||
it('should unlink provider via null', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = createMockUser(); | ||
const user = await Parse.User.logInWith('gpgames', { authData }); | ||
const sessionToken = user.getSessionToken(); | ||
|
||
await user.fetch({ sessionToken }); | ||
const currentAuthData = user.get('authData') || {}; | ||
|
||
user.set('authData', { | ||
...currentAuthData, | ||
gpgames: null, | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData') || {}; | ||
|
||
expect(finalAuthData.gpgames).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('Data Validation Optimization', () => { | ||
it('should skip revalidation when authData is identical', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = createMockUser(); | ||
const user = await Parse.User.logInWith('gpgames', { authData }); | ||
const sessionToken = user.getSessionToken(); | ||
|
||
await user.fetch({ sessionToken }); | ||
const existingAuthData = user.get('authData'); | ||
|
||
// Small delay to ensure timestamp differences don't affect comparison | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
||
user.set('authData', JSON.parse(JSON.stringify(existingAuthData))); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData') || {}; | ||
|
||
expect(finalAuthData.gpgames?.id).toBe(MOCK_USER_ID); | ||
}); | ||
|
||
it('should handle empty authData gracefully', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const user = await Parse.User.signUp('test', 'password123'); | ||
|
||
const sessionToken = user.getSessionToken(); | ||
await user.fetch({ sessionToken }); | ||
|
||
user.set('authData', { | ||
someAdapter1: { id: 'T3', access_token: 'token456' }, | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData'); | ||
|
||
expect(finalAuthData).toBeDefined(); | ||
expect(finalAuthData.someAdapter1?.id).toBe('T3'); | ||
}); | ||
}); | ||
|
||
describe('Partial Data Updates', () => { | ||
it('should handle partial provider data updates correctly', async () => { | ||
mockGooglePlayGamesAPI() | ||
|
||
const authData = createMockUser(); | ||
const user = await Parse.User.logInWith('gpgames', { authData }); | ||
|
||
const sessionToken = user.getSessionToken(); | ||
|
||
await user.fetch({ sessionToken }); | ||
|
||
const currentAuthData = user.get('authData') || {}; | ||
user.set('authData', { | ||
...currentAuthData, | ||
gpgames: { | ||
...currentAuthData.gpgames, | ||
code: 'new', | ||
}, | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData'); | ||
|
||
expect(finalAuthData.gpgames.id).toBe(MOCK_USER_ID); | ||
}); | ||
}); | ||
|
||
describe('API Call Optimization', () => { | ||
beforeEach(async () => { | ||
await setupAuthConfig(); | ||
}); | ||
|
||
it('should not call getAccessTokenFromCode for unchanged authData', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = createMockUser(); | ||
const user = await Parse.User.logInWith('gpgames', { authData }); | ||
const sessionToken = user.getSessionToken(); | ||
|
||
const initialCallCount = global.fetch.calls.count(); | ||
|
||
const freshUser = await new Parse.Query(Parse.User).get(user.id, { sessionToken }); | ||
const currentAuthData = freshUser.get('authData'); | ||
|
||
freshUser.set('authData', JSON.parse(JSON.stringify(currentAuthData))); | ||
await freshUser.save(null, { sessionToken }); | ||
|
||
expect(global.fetch.calls.count()).toBe(initialCallCount); | ||
}); | ||
|
||
it('should handle mixed authData operations without redundant API calls', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = createMockUser(); | ||
const user = await Parse.User.logInWith('gpgames', { authData }); | ||
const sessionToken = user.getSessionToken(); | ||
|
||
const initialCallCount = global.fetch.calls.count(); | ||
|
||
const freshUser = await new Parse.Query(Parse.User).get(user.id, { sessionToken }); | ||
const currentAuthData = freshUser.get('authData') || {}; | ||
|
||
freshUser.set('authData', { | ||
...currentAuthData, | ||
someAdapter2: { id: 'fb123', access_token: 'fb_token' } | ||
}); | ||
await freshUser.save(null, { sessionToken }); | ||
|
||
expect(global.fetch.calls.count()).toBe(initialCallCount); | ||
|
||
const finalUser = await new Parse.Query(Parse.User).get(user.id, { sessionToken }); | ||
const finalAuthData = finalUser.get('authData') || {}; | ||
|
||
expect(finalAuthData.gpgames?.id).toBe(MOCK_USER_ID); | ||
expect(finalAuthData.someAdapter2?.id).toBe('fb123'); | ||
}); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: diff and equality system should not be coded from scratch and if not done correctly could be an attack vector, popular libs are native implementation should be used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback on diff/equality — agreed. I overreached trying to fix too much at once. I’ve reduced this PR to minimal changes only:
Before findUsersWithAuthData, I filter out providers where authData[provider] is null (or undefined), so lookup/validation runs only on non-null providers.
Unlink via authData[provider] = null is applied without invoking the adapter.
If we revisit partial update semantics later, I’ll rely on native or well-vetted utilities and propose that in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh thanks @SNtGog diffs looks much better 🚀