Skip to content

Commit

Permalink
return boolean instead of informations (object)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelzoidl committed May 19, 2017
1 parent d3282d5 commit b283cb7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
48 changes: 15 additions & 33 deletions test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})

Expand All @@ -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)
})
})
})

0 comments on commit b283cb7

Please sign in to comment.