Skip to content

Commit

Permalink
feat(jwt): allow all numbers in jwt token type names (#511)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gkchestertron authored and benjie committed Jul 14, 2017
1 parent e62d7af commit b3e22d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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')
}))
})
4 changes: 3 additions & 1 deletion src/postgraphql/schema/auth/getPgTokenTypeFromIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`)
Expand Down

0 comments on commit b3e22d6

Please sign in to comment.