-
Notifications
You must be signed in to change notification settings - Fork 70
276 anonymous session #283
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 19 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c41ecc7
feat(password-flow): add method to build valid password auth uri
5674403
feat(sdk-middleware-auth): implement password flow
a9e5418
refactor(base-auth-flow): refactor fetching token logic
8049663
test(base-auth-flow): add tests
8282a59
chore(base-auth-flow): add comments to tests
3b2d9f6
docs(password-flow): add docs about new auth flow
ac0cf35
docs(password-flow): update docs
a8ecfd2
refactor(middleware-auth): remove duplicate tests
30d96da
refactor(middleware-auth): update base on PR review
22c93b5
refactor(middleware-auth): improve code
186d927
test(integration): add integration tests for customer login
c2ce612
refactor(auth): refactor code base on PR review
655c6ad
feat(middleware-auth): add anonymous token implementation
ce81d26
feat(middleware-auth): add method for anonymous session flow
0fea194
refactor(test): update code
ed81854
refactor(buildRequest): ensure projectKey is present
d1c25cb
test(anonymouse-session): improve test coverage
c95bbdb
docs(auth): improve docs
2792091
refactor(tests): improve readability
e256305
test(integration): add test for anonymouse session flow
42077a6
docs(sdk-middleware-auth): update documentation
wizzy25 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
37 changes: 35 additions & 2 deletions
37
packages/sdk-middleware-auth/src/anonymous-session-flow.js
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 |
|---|---|---|
| @@ -1,3 +1,36 @@ | ||
| export default function createAuthMiddlewareForAnonymousSessionFlow () { | ||
| throw new Error('Middleware not implemented yet') | ||
| /* @flow */ | ||
| import type { | ||
| AuthMiddlewareOptions, | ||
| Middleware, | ||
| MiddlewareRequest, | ||
| MiddlewareResponse, | ||
| Next, | ||
| Task, | ||
| } from 'types/sdk' | ||
|
|
||
| import { buildRequestForAnonymousSessionFlow } from './build-requests' | ||
| import authMiddlewareBase from './base-auth-flow' | ||
| import store from './utils' | ||
|
|
||
| export default function createAuthMiddlewareForAnonymousSessionFlow ( | ||
| options: AuthMiddlewareOptions, | ||
| ): Middleware { | ||
| const tokenCache = store({}) | ||
| const pendingTasks: Array<Task> = [] | ||
|
|
||
| const requestState = store(false) | ||
| return (next: Next) => ( | ||
| request: MiddlewareRequest, | ||
| response: MiddlewareResponse, | ||
| ) => { | ||
| const params = { | ||
| request, | ||
| response, | ||
| ...buildRequestForAnonymousSessionFlow(options), | ||
| pendingTasks, | ||
| requestState, | ||
| tokenCache, | ||
| } | ||
| authMiddlewareBase(params, next) | ||
| } | ||
| } |
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
63 changes: 63 additions & 0 deletions
63
packages/sdk-middleware-auth/test/anonymous-session-flow.spec.js
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,63 @@ | ||
| import { | ||
| createAuthMiddlewareForAnonymousSessionFlow, | ||
| } from '../src' | ||
|
|
||
| import authMiddlewareBase from '../src/base-auth-flow' | ||
|
|
||
| jest.mock('../src/base-auth-flow') | ||
|
|
||
| function createTestRequest (options) { | ||
| return { | ||
| url: '', | ||
| method: 'GET', | ||
| body: null, | ||
| headers: {}, | ||
| ...options, | ||
| } | ||
| } | ||
|
|
||
| function createTestMiddlewareOptions (options) { | ||
| return { | ||
| host: 'https://auth.commercetools.co', | ||
| projectKey: 'foo', | ||
| credentials: { | ||
| clientId: '123', | ||
| clientSecret: 'secret', | ||
| anonymousId: 'secretme', | ||
| }, | ||
| ...options, | ||
| } | ||
| } | ||
|
|
||
| describe('Anonymous Session Flow', () => { | ||
| it('should call the base-auth-flow method with the right params', () => | ||
| new Promise((resolve, reject) => { | ||
| authMiddlewareBase.mockImplementation((params, next) => { | ||
| next(params) // makes it easy to test what was passed in | ||
| }) | ||
| const request = createTestRequest() | ||
| const response = { | ||
| resolve, | ||
| reject, | ||
| } | ||
| const next = (actualParams) => { | ||
| expect(actualParams.request).toEqual(request) | ||
| expect(actualParams.response).toEqual(response) | ||
| expect(actualParams.pendingTasks).toEqual([]) | ||
| expect(actualParams.url).toBe( | ||
| 'https://auth.commercetools.co/oauth/foo/anonymous/token', | ||
| ) | ||
| expect(actualParams.basicAuth).toBe('MTIzOnNlY3JldA==') | ||
| expect(authMiddlewareBase).toHaveBeenCalledTimes(1) | ||
| jest.unmock('../src/base-auth-flow') | ||
| resolve() | ||
| } | ||
| const middlewareOptions = createTestMiddlewareOptions() | ||
| const authMiddleware = createAuthMiddlewareForAnonymousSessionFlow( | ||
| middlewareOptions, | ||
| ) | ||
|
|
||
| authMiddleware(next)(request, response) | ||
| }), | ||
| ) | ||
| }) |
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
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.