Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No check needed. Parsing will throw an error if base64 is not valid #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
# JWT Valid
# Is JWT Valid
> Pass a JWT token and get a boolean if the token is valid, the requirements for a valid token are defined by the official [RFC 7519 standard](https://tools.ietf.org/html/rfc7519)

[![Zero Dependencies](https://img.shields.io/badge/zero-dependencies-brightgreen.svg)]()
[![Coverage Status](https://coveralls.io/repos/github/entwicklerstube/jwt-valid/badge.svg?branch=master)](https://coveralls.io/github/entwicklerstube/jwt-valid?branch=master)
[![Build Status](https://travis-ci.org/entwicklerstube/jwt-valid.svg?branch=master)](https://travis-ci.org/entwicklerstube/jwt-valid)
[![devDependencies Status](https://david-dm.org/entwicklerstube/jwt-valid/dev-status.svg)](https://david-dm.org/entwicklerstube/jwt-valid?type=dev)

### Install
**npm**
```
npm install jwt-valid
npm install is-jwt-valid
```

**yarn**
```
yarn add jwt-valid
yarn add is-jwt-valid
```

### Usage
```js
jwtValid(JWT_TOKEN<STRING>)
isJwtValid(JWT_TOKEN<STRING>)
```

### Example
```js
jwtValid('this is no valid token') // returns false
jwtValid('some.valid.token') // returns true
isJwtValid('this is no valid token') // returns false
isJwtValid('some.valid.token') // returns true
```

### Support
Expand Down
9 changes: 1 addition & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const parseBase64ByPlattform = base64String => {
if (!base64String || !isValidBase64(base64String)) throw new Error('No valid base64 passed')
if (!base64String) throw new Error('No valid base64 passed')

if (typeof window === 'undefined' && (process && process.version !== 'undefined')) {
return Buffer.from(base64String, 'base64').toString()
Expand All @@ -8,13 +8,6 @@ export const parseBase64ByPlattform = base64String => {
}
}

export const isValidBase64 = base64String => {
if (!base64String) return false
if (!(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(base64String))) return false

return true
}

export const parseJwtToken = jwtToken => {
if (!jwtToken) return false
if (typeof jwtToken !== 'string') return false
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{
"name": "jwt-valid",
"version": "1.1.0",
"name": "is-jwt-valid",
"version": "1.0.2",
"description": "Pass a jwt token and get a boolean if the token is valid",
"keywords": "jwt, jwt valid, jwt still valid, valid, valid",
"keywords": ["jwt", "jwt valid", "jwt still valid", "valid"],
"main": "build/index.js",
"repository": "https://github.com/entwicklerstube/jwt-valid",
"repository": "https://github.com/levinunnink/jwt-valid",
"author": "Michael J. Zoidl",
"license": "MIT",
"scripts": {
"build": "rimraf build && mkdir build && babel index.js --out-dir build",
"prepublish": "yarn run standard && yarn run test && yarn run build",
"prepublish": "npm run-script standard && npm run-script test && npm run-script build",
"test": "mocha test.js --require mocha --compilers js:babel-core/register",
"standard": "standard --env mocha",
"nyc": "nyc --require babel-core/register --require './mocha.js' mocha test.js",
"coverage": "yarn run nyc && nyc report --reporter=text-lcov | coveralls",
"prepublish": "yarn run test && yarn run build"
"coverage": "npm run-script nyc && nyc report --reporter=text-lcov | coveralls"
},
"files": [
"build"
Expand Down
12 changes: 1 addition & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { parseBase64ByPlattform, isValidBase64, parseJwtToken } from './index'
import { parseBase64ByPlattform, 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.equal(false)
})

it('returns true if a valid base64 string is passed', () => {
expect(isValidBase64('SSBhbSB2YWxpZA==')).to.equal(true)
})
})

describe('parseBase64ByPlattform', () => {
it('returns error if no valid token is passed', () => {
expect(() => parseBase64ByPlattform()).to.throw('No valid base64 passed')
Expand Down