From b3e22d66e0aeb6212a672de07080107b7ef6e01f Mon Sep 17 00:00:00 2001 From: John Fellman Date: Fri, 14 Jul 2017 12:35:22 -0700 Subject: [PATCH] feat(jwt): allow all numbers in jwt token type names (#511) * allow all numbers in jwt token type names * fix(postgraphql): improve jwt token type check regex and add tests for it * fix(postgraphql): clean up bad style * fix(postgraphql) more style fixes * fix(postgraphql) removes unnecessary tests and adds double-quote escaping to testing of names starting with a number --- .../getPgTokenTypeFromIdentifier-test.js | 28 +++++++++++++++++++ .../auth/getPgTokenTypeFromIdentifier.ts | 4 ++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/postgraphql/schema/auth/__tests__/getPgTokenTypeFromIdentifier-test.js diff --git a/src/postgraphql/schema/auth/__tests__/getPgTokenTypeFromIdentifier-test.js b/src/postgraphql/schema/auth/__tests__/getPgTokenTypeFromIdentifier-test.js new file mode 100644 index 0000000000..ed7c4e37e4 --- /dev/null +++ b/src/postgraphql/schema/auth/__tests__/getPgTokenTypeFromIdentifier-test.js @@ -0,0 +1,28 @@ +import withPgClient from '../../../../__tests__/utils/withPgClient' +import introspectDatabase from '../../../../postgres/introspection/introspectDatabase' +import getPgTokenTypeFromIdentifier from '../getPgTokenTypeFromIdentifier' + +// good names start with a letter or underscore and may also contain numbers after position 0 +// note: names that start with a number must be escaped with double quotes +const validNames = [ + '_test', '0_test', 'test_0', '9_test', 'test_9', +] + +validNames.forEach(validName => { + test(`Valid schema name ${validName} and its jwt_token type should create without error and pass validation`, withPgClient(async client => { + let query = 'not an error' + + try { + query = await client.query(`create schema "${validName}"; create type "${validName}"."jwt_token" as (role text, exp integer, a integer, b integer, c integer)`) + } catch (e) { + query = e + } + + expect(query).not.toBeInstanceOf(Error) + + let catalog = await introspectDatabase(client, [validName]) + let tokenType = getPgTokenTypeFromIdentifier(catalog, `"${validName}"."jwt_token"`) + + expect(tokenType.name).toBe('jwt_token') + })) +}) diff --git a/src/postgraphql/schema/auth/getPgTokenTypeFromIdentifier.ts b/src/postgraphql/schema/auth/getPgTokenTypeFromIdentifier.ts index 38a55ba51c..6ed145b784 100644 --- a/src/postgraphql/schema/auth/getPgTokenTypeFromIdentifier.ts +++ b/src/postgraphql/schema/auth/getPgTokenTypeFromIdentifier.ts @@ -43,7 +43,9 @@ export default function getPgTokenTypeFromIdentifier ( * @private */ function parseTypeIdentifier (typeIdentifier: string): { namespaceName: string, typeName: string } { - const match = typeIdentifier.match(/^(?:([a-zA-Z1-2_]+)|"([^"]*)")\.(?:([a-zA-Z1-2_]+)|"([^"]*)")$/) + // schema and type names can begin with a letter or underscore, but not a number + // numbers are allowed in the rest of the name + const match = typeIdentifier.match(/^(?:([a-zA-Z_][a-zA-Z0-9_]*)|"([^"]*)")\.(?:([a-zA-Z_][a-zA-Z0-9_]*)|"([^"]*)")$/) if (!match) throw new Error(`Type identifier '${typeIdentifier}' is of the incorrect form.`)