-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: new jwt middlware * test: new auth test * feat: unauthorized error
- Loading branch information
Showing
9 changed files
with
95 additions
and
71 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
import axios from 'axios' | ||
import each from 'jest-each' | ||
import { config } from '@wix-velo/velo-external-db-core' | ||
import { authOwnerWithoutJwt, authOwnerWithWrongJwtPublicKey, authVisitor, authOwnerWithWrongAppId } from '@wix-velo/external-db-testkit' | ||
import { initApp, teardownApp, dbTeardown, setupDb, currentDbImplementationName } from '../resources/e2e_resources' | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: 'http://localhost:8080' | ||
}) | ||
|
||
|
||
describe(`Velo External DB authorization: ${currentDbImplementationName()}`, () => { | ||
beforeAll(async() => { | ||
await setupDb() | ||
await initApp() | ||
}, 20000) | ||
|
||
afterAll(async() => { | ||
await dbTeardown() | ||
}, 20000) | ||
|
||
describe('Role authorization', () => { | ||
each(config.map(i => i.pathPrefix)).test('should throw 401 on a request to %s without the appropriate role', async(api) => { | ||
return expect(axiosInstance.post(api, {}, authVisitor)).rejects.toThrow('401') | ||
}) | ||
}) | ||
|
||
describe('JWT authorization', () => { | ||
test('should throw if the request is not singed with JWT token', async() => { | ||
return expect(axiosInstance.post('data/insert', {}, authOwnerWithoutJwt)).rejects.toThrow('401') | ||
}) | ||
|
||
test('should throw if the request is singed with the wrong public key', async() => { | ||
return expect(axiosInstance.post('data/insert', {}, authOwnerWithWrongJwtPublicKey)).rejects.toThrow('401') | ||
}) | ||
|
||
test('should throw if the request is signed with wrong app id', async() => { | ||
return expect(axiosInstance.post('data/insert', {}, authOwnerWithWrongAppId)).rejects.toThrow('401') | ||
}) | ||
}) | ||
|
||
afterAll(async() => await teardownApp()) | ||
}) |
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,53 +1,61 @@ | ||
import * as Chance from 'chance' | ||
import { AxiosRequestHeaders } from 'axios' | ||
import * as jwt from 'jsonwebtoken' | ||
import { authConfig } from '@wix-velo/test-commons' | ||
|
||
const chance = Chance() | ||
const axios = require('axios').create({ | ||
baseURL: 'http://localhost:8080', | ||
}) | ||
|
||
const allowedMetasite = chance.word() | ||
const externalDatabaseId = chance.word() | ||
const TOKEN_ISSUER = 'wix.com' | ||
|
||
export const authInit = () => { | ||
process.env['ALLOWED_METASITES'] = allowedMetasite | ||
process.env['EXTERNAL_DATABASE_ID'] = externalDatabaseId | ||
process.env['JWT_PUBLIC_KEY'] = authConfig.authPublicKey | ||
process.env['APP_DEF_ID'] = authConfig.kid | ||
} | ||
|
||
const appendRoleToRequest = (role: string) => (dataRaw: string) => { | ||
export const appendRoleToRequest = (role: string) => (dataRaw: string) => { | ||
const data = JSON.parse( dataRaw ) | ||
return JSON.stringify({ ...data, ...{ requestContext: { ...data.requestContext, role } } }) | ||
return JSON.stringify({ request: data, metadata: { requestContext: { ...data.requestContext, role } } }) | ||
} | ||
|
||
const appendJWTHeaderToRequest = (dataRaw: string, headers: AxiosRequestHeaders) => { | ||
headers['Authorization'] = createJwtHeader() | ||
export const signTokenWith = (privateKey: string, appId: string, options = { algorithm: 'RS256' }) => (dataRaw: string, headers: AxiosRequestHeaders) => { | ||
headers['Content-Type'] = 'text/plain' | ||
const data = JSON.parse( dataRaw ) | ||
return JSON.stringify({ ...data } ) | ||
const payload = { | ||
data, | ||
iss: TOKEN_ISSUER, | ||
aud: appId, | ||
} | ||
const signedData = jwt.sign(payload, privateKey, options as jwt.SignOptions) | ||
return signedData | ||
} | ||
|
||
const TOKEN_ISSUER = 'wix-data.wix.com' | ||
|
||
const createJwtHeader = () => { | ||
const token = jwt.sign({ iss: TOKEN_ISSUER, siteId: allowedMetasite, aud: externalDatabaseId }, authConfig.authPrivateKey, { algorithm: 'ES256', keyid: authConfig.kid }) | ||
return `Bearer ${token}` | ||
} | ||
|
||
export const authAdmin = { transformRequest: axios.defaults | ||
.transformRequest | ||
.concat( appendJWTHeaderToRequest, appendRoleToRequest('BACKEND_CODE') ) } | ||
.concat(appendRoleToRequest('BACKEND_CODE'), signTokenWith(authConfig.authPrivateKey, authConfig.kid)) } | ||
|
||
export const authOwner = { transformRequest: axios.defaults | ||
.transformRequest | ||
.concat( appendJWTHeaderToRequest, appendRoleToRequest('OWNER' ) ) } | ||
.concat(appendRoleToRequest('OWNER'), signTokenWith(authConfig.authPrivateKey, authConfig.kid)) } | ||
|
||
export const authVisitor = { transformRequest: axios.defaults | ||
.transformRequest | ||
.concat( appendJWTHeaderToRequest, appendRoleToRequest('VISITOR' ) ) } | ||
.concat(appendRoleToRequest('VISITOR'), signTokenWith(authConfig.authPrivateKey, authConfig.kid)) } | ||
|
||
export const authOwnerWithoutJwt = { transformRequest: axios.defaults | ||
.transformRequest | ||
.concat( appendRoleToRequest('OWNER' ) ) } | ||
.concat( appendRoleToRequest('OWNER') ) } | ||
|
||
export const authOwnerWithWrongJwtPublicKey = { transformRequest: axios.defaults | ||
.transformRequest | ||
.concat( appendRoleToRequest('OWNER'), signTokenWith(authConfig.otherAuthPrivateKey, authConfig.kid) ) } | ||
|
||
|
||
export const authOwnerWithWrongAppId= { transformRequest: axios.defaults | ||
.transformRequest | ||
.concat( appendRoleToRequest('OWNER'), signTokenWith(authConfig.authPrivateKey, 'wrong-app-id') ) } | ||
|
||
|
||
|
||
export const errorResponseWith = (status: any, message: string) => ({ response: { data: { description: expect.stringContaining(message) }, status } }) |
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
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