From b283cb7016de809cec29d51e930f1358c1bab320 Mon Sep 17 00:00:00 2001 From: michaelzoidl Date: Fri, 19 May 2017 12:41:06 +0200 Subject: [PATCH] return boolean instead of informations (object) --- index.js | 19 ++++++++++--------- test.js | 48 +++++++++++++++--------------------------------- 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/index.js b/index.js index 3c9c467..f8b6a08 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ export const parseBase64ByPlattform = base64String => { if (!base64String || !isValidBase64(base64String)) throw new Error('No valid base64 passed') - if(typeof window === 'undefined' && (process && process.version !== 'undefined')) { + if (typeof window === 'undefined' && (process && process.version !== 'undefined')) { return Buffer.from(base64String, 'base64').toString() } else { return window.atob(base64String) @@ -16,18 +16,19 @@ export const isValidBase64 = base64String => { } export const parseJwtToken = jwtToken => { - if(!jwtToken) throw new Error('No valid JWT token passed') + if (!jwtToken) return false + if (typeof jwtToken !== 'string') return false const jwtTokenParts = jwtToken.split('.') - if(jwtTokenParts.length !== 3) throw new Error('No valid JWT token passed') + if (jwtTokenParts.length !== 3) return false try { - const header = JSON.parse(parseBase64ByPlattform(jwtTokenParts[0])) - const payload = JSON.parse(parseBase64ByPlattform(jwtTokenParts[1])) - const signature = jwtTokenParts[2] - - return { header, payload, signature } + JSON.parse(parseBase64ByPlattform(jwtTokenParts[0])) + JSON.parse(parseBase64ByPlattform(jwtTokenParts[1])) + return true } catch (err) { - throw new Error('No valid JWT token passed') + return false } } + +export default parseJwtToken diff --git a/test.js b/test.js index 1be080a..7c5f4fb 100644 --- a/test.js +++ b/test.js @@ -1,15 +1,15 @@ -import jwtValid, { parseBase64ByPlattform, isValidBase64, parseJwtToken } from './index' +import { parseBase64ByPlattform, isValidBase64, parseJwtToken } from './index' const jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqd3QiOiJpcyBhd2Vzb21lISJ9.j8Z3twgi5nCrZJXH1JoxfQ8q1u5btwr3vS3wyqfytOo' describe('JWT Valid', () => { describe('isValidBase64', () => { it('returns false if a not valid base64 string is passed', () => { - expect(isValidBase64('No Valid Base64 String')).to.be.false + expect(isValidBase64('No Valid Base64 String')).to.equal(false) }) it('returns true if a valid base64 string is passed', () => { - expect(isValidBase64('SSBhbSB2YWxpZA==')).to.be.true + expect(isValidBase64('SSBhbSB2YWxpZA==')).to.equal(true) }) }) @@ -24,44 +24,26 @@ describe('JWT Valid', () => { }) describe('parseJwtToken', () => { - it('returns error if nothing is passed', () => { - expect(() => parseJwtToken()).to.throw('No valid JWT token passed') + it('returns a boolean', () => { + expect(parseJwtToken()).to.be.a('boolean') }) - it('returns error if no valid token is passed', () => { - expect(() => parseJwtToken('iam-no-jwt-token')).to.throw('No valid JWT token passed') - }) - - it('returns error if no valid token is passed', () => { - expect(() => parseJwtToken('a.b.c')).to.throw('No valid JWT token passed') - }) - - it('returns error if no valid token is passed', () => { - expect(() => parseJwtToken('SGVsbG8gV29ybGQ=.SGVsbG8gV29ybGQ=.foobar')).to.throw('No valid JWT token passed') - }) - - it('returns error if no valid token is passed', () => { - expect(() => parseJwtToken('eyJhbGciOiJpbnZhbGlkIiwidHlwIjoiaW52YWxpZCJ9.eyBub3RoaW5nOiAndHJ1ZScgfQ==.some_signature')).to.throw('No valid JWT token passed') - }) - - it('returns a object', () => { - expect(parseJwtToken(jwtToken)).to.be.a('object') - }) - - it('returns a object with the props header, payload and signature', () => { - expect(parseJwtToken(jwtToken)).to.include.keys('header', 'payload', 'signature') + it('returns false if no token is passed', () => { + expect(parseJwtToken()).to.equal(false) }) - it('returns a object with the header containing the decoded informations', () => { - expect(parseJwtToken(jwtToken).header).to.deep.equal({ alg: 'HS256', typ: 'JWT' }) + it('returns false if wired stuff is passed', () => { + expect(parseJwtToken('iam wired')).to.equal(false) }) - it('returns a object with the payload containing the decoded informations', () => { - expect(parseJwtToken(jwtToken).payload).to.deep.equal({ jwt: 'is awesome!' }) + it('returns false if a non-string type is passed', () => { + expect(parseJwtToken(123)).to.equal(false) + expect(parseJwtToken([])).to.equal(false) + expect(parseJwtToken({})).to.equal(false) }) - it('returns a object with the signature string', () => { - expect(parseJwtToken(jwtToken).signature).to.equal(jwtToken.split('.')[2]) + it('returns true if a valid jwt token is passed', () => { + expect(parseJwtToken(jwtToken)).to.equal(true) }) }) })