Skip to content

Commit ce81d26

Browse files
author
Abimbola Idowu
committed
feat(middleware-auth): add method for anonymous session flow
affects: @commercetools/sdk-middleware-auth
1 parent 655c6ad commit ce81d26

3 files changed

Lines changed: 135 additions & 2 deletions

File tree

docs/sdk/api/sdkMiddlewareAuth.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,40 @@ const client = createClient({
129129
],
130130
})
131131
```
132+
133+
## `createAuthMiddlewareForAnonymousSessionFlow(options)`
134+
135+
Creates a [middleware](/sdk/Glossary.md#middleware) to handle authentication for the [Anonymous Session Flow](http://dev.commercetools.com/http-api-authorization.html#tokens-for-anonymous-sessions) of the commercetools platform API.
136+
137+
#### Named arguments (options)
138+
139+
1. `host` *(String)*: the host of the OAuth API service
140+
2. `projectKey` *(String)*: the key of the project to assign the default scope to
141+
3. `credentials` *(Object)*: the client credentials for authentication (`clientId`, `clientSecret`, `anonymousId`)
142+
4. `scopes` *(Array)*: a list of [scopes](http://dev.commercetools.com/http-api-authorization.html#scopes) (default `manage_project:{projectKey}`) to assign to the OAuth token
143+
144+
145+
#### Usage example
146+
147+
```js
148+
import { createClient } from '@commercetools/sdk-client'
149+
import { createAuthMiddlewareForAnonymousSessionFlow } from '@commercetools/sdk-middleware-auth'
150+
151+
const client = createClient({
152+
middlewares: [
153+
createAuthMiddlewareForAnonymousSessionFlow({
154+
host: 'https://auth.commercetools.com',
155+
projectKey: 'test',
156+
credentials: {
157+
clientId: '123',
158+
clientSecret: 'secret',
159+
anonymousId: 'unique-id-of-customer-not-required',
160+
},
161+
scopes: [
162+
'view_products:test',
163+
'manage_orders:test',
164+
],
165+
}),
166+
],
167+
})
168+
```
Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1-
export default function createAuthMiddlewareForAnonymousSessionFlow () {
2-
throw new Error('Middleware not implemented yet')
1+
/* @flow */
2+
import type {
3+
AuthMiddlewareOptions,
4+
Middleware,
5+
MiddlewareRequest,
6+
MiddlewareResponse,
7+
Next,
8+
Task,
9+
} from 'types/sdk'
10+
11+
import { buildRequestForAnonymousSessionFlow } from './build-requests'
12+
import authMiddlewareBase from './base-auth-flow'
13+
import store from './utils'
14+
15+
export default function createAuthMiddlewareForAnonymousSessionFlow (
16+
options: AuthMiddlewareOptions,
17+
): Middleware {
18+
const tokenCache = store({})
19+
const pendingTasks: Array<Task> = []
20+
21+
const requestState = store(false)
22+
return (next: Next) => (
23+
request: MiddlewareRequest,
24+
response: MiddlewareResponse,
25+
) => {
26+
const params = {
27+
request,
28+
response,
29+
...buildRequestForAnonymousSessionFlow(options),
30+
pendingTasks,
31+
requestState,
32+
tokenCache,
33+
}
34+
authMiddlewareBase(params, next)
35+
}
336
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
createAuthMiddlewareForAnonymousSessionFlow,
3+
} from '../src'
4+
5+
import authMiddlewareBase from '../src/base-auth-flow'
6+
7+
jest.mock('../src/base-auth-flow')
8+
9+
function createTestRequest (options) {
10+
return {
11+
url: '',
12+
method: 'GET',
13+
body: null,
14+
headers: {},
15+
...options,
16+
}
17+
}
18+
19+
function createTestMiddlewareOptions (options) {
20+
return {
21+
host: 'https://auth.commercetools.co',
22+
projectKey: 'foo',
23+
credentials: {
24+
clientId: '123',
25+
clientSecret: 'secret',
26+
anonymousId: 'secretme',
27+
},
28+
...options,
29+
}
30+
}
31+
32+
describe('Anonymous Session Flow', () => {
33+
it('should call the base-auth-flow method with the right params', () =>
34+
new Promise((resolve, reject) => {
35+
authMiddlewareBase.mockImplementation((params, next) => {
36+
next(params) // makes it easy to test what was passed in
37+
})
38+
const request = createTestRequest()
39+
const response = {
40+
resolve,
41+
reject,
42+
}
43+
const next = (actualParams) => {
44+
expect(actualParams.request).toEqual(actualParams.request)
45+
expect(actualParams.response).toEqual(actualParams.response)
46+
expect(actualParams.pendingTasks).toEqual([])
47+
expect(actualParams.url).toBe(
48+
'https://auth.commercetools.co/oauth/foo/anonymous/token',
49+
)
50+
expect('MTIzOnNlY3JldA==').toBe(actualParams.basicAuth)
51+
expect(authMiddlewareBase).toHaveBeenCalledTimes(1)
52+
jest.unmock('../src/base-auth-flow')
53+
resolve()
54+
}
55+
const middlewareOptions = createTestMiddlewareOptions()
56+
const authMiddleware = createAuthMiddlewareForAnonymousSessionFlow(
57+
middlewareOptions,
58+
)
59+
60+
authMiddleware(next)(request, response)
61+
}),
62+
)
63+
})

0 commit comments

Comments
 (0)