Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from cofacts/auth-api
@auth directive and context API
- Loading branch information
Showing
10 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,60 @@ | ||
import { gql } from '../testUtils'; | ||
|
||
it('context rejects anonymous users', async () => { | ||
const result = await gql` | ||
{ | ||
context { | ||
state | ||
} | ||
} | ||
`(); | ||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"data": Object { | ||
"context": null, | ||
}, | ||
"errors": Array [ | ||
[GraphQLError: Invalid authentication header], | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it('Returns user context', async () => { | ||
const result = await gql` | ||
{ | ||
context { | ||
state | ||
issuedAt | ||
data { | ||
searchedText | ||
} | ||
} | ||
} | ||
`( | ||
{}, | ||
{ | ||
userId: 'U12345678', | ||
userContext: { | ||
state: 'CHOOSING_ARTICLE', | ||
issuedAt: 1586013070089, | ||
data: { | ||
searchedText: 'Foo', | ||
}, | ||
}, | ||
} | ||
); | ||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"data": Object { | ||
"context": Object { | ||
"data": Object { | ||
"searchedText": "Foo", | ||
}, | ||
"issuedAt": 1586013070089, | ||
"state": "CHOOSING_ARTICLE", | ||
}, | ||
}, | ||
} | ||
`); | ||
}); |
This file contains 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,75 @@ | ||
jest.mock('src/lib/redisClient'); | ||
|
||
import { getContext } from '../'; | ||
import redis from 'src/lib/redisClient'; | ||
|
||
beforeEach(() => { | ||
redis.get.mockClear(); | ||
}); | ||
|
||
describe('getContext', () => { | ||
it('generates null context for anonymous requests', async () => { | ||
const context = await getContext({ ctx: { req: { headers: {} } } }); | ||
|
||
expect(context).toMatchInlineSnapshot(` | ||
Object { | ||
"userContext": null, | ||
"userId": "", | ||
} | ||
`); | ||
}); | ||
|
||
it('generates null context for wrong credentials', async () => { | ||
redis.get.mockImplementationOnce(() => ({ | ||
nonce: 'correctpass', | ||
})); | ||
|
||
const context = await getContext({ | ||
ctx: { | ||
req: { | ||
headers: { | ||
authorization: `basic ${Buffer.from('user1:wrongpass').toString( | ||
'base64' | ||
)}`, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(context).toMatchInlineSnapshot(` | ||
Object { | ||
"userContext": null, | ||
"userId": "user1", | ||
} | ||
`); | ||
}); | ||
|
||
it('reads userContext for logged-in requests', async () => { | ||
redis.get.mockImplementationOnce(() => ({ | ||
nonce: 'correctpass', | ||
foo: 'bar', | ||
})); | ||
|
||
const context = await getContext({ | ||
ctx: { | ||
req: { | ||
headers: { | ||
authorization: `basic ${Buffer.from('user1:correctpass').toString( | ||
'base64' | ||
)}`, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(context).toMatchInlineSnapshot(` | ||
Object { | ||
"userContext": Object { | ||
"foo": "bar", | ||
"nonce": "correctpass", | ||
}, | ||
"userId": "user1", | ||
} | ||
`); | ||
}); | ||
}); |
This file contains 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,4 +1,3 @@ | ||
jest.mock('@line/bot-sdk'); | ||
import { | ||
mockedGetNumberOfMessageDeliveries, | ||
mockedGetNumberOfFollowers, | ||
|
This file contains 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,23 @@ | ||
import { AuthenticationError } from 'apollo-server-koa'; | ||
import { SchemaDirectiveVisitor } from 'graphql-tools'; | ||
|
||
/** | ||
* When field with @auth is accessed, make sure the user is logged in (i.e. `userContext` in context). | ||
*/ | ||
class AuthDirective extends SchemaDirectiveVisitor { | ||
visitFieldDefinition(field) { | ||
const { resolve } = field; | ||
|
||
field.resolve = (...args) => { | ||
const [, , context] = args; | ||
|
||
if (!context.userId || !context.userContext) { | ||
throw new AuthenticationError('Invalid authentication header'); | ||
} | ||
|
||
return resolve(...args); | ||
}; | ||
} | ||
} | ||
|
||
export default AuthDirective; |
This file contains 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 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 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 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 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,3 @@ | ||
export default { | ||
get: jest.fn(), | ||
}; |