Compare commits
32 Commits
Author | SHA1 | Date | |
---|---|---|---|
ad86800d8c | |||
a14d4e937b | |||
4ba3e3bccb | |||
907b82fca9 | |||
4c469169f4 | |||
d5f402df86 | |||
d01bcc29fa | |||
55a5f71b6e | |||
7ccdb8f9a0 | |||
b61ddd1872 | |||
ac3b5177e5 | |||
0e8630d145 | |||
bb1daa2e28 | |||
17d7f264a4 | |||
7f1c25ed98 | |||
d9fa92cb1c | |||
b08354f2b2 | |||
06141ec89d | |||
113df6f8f7 | |||
a761725e6b | |||
f5a13c01b0 | |||
5e3f5c1871 | |||
af7ff4e8d9 | |||
959652b3d3 | |||
c1a9213a52 | |||
4d73584e4e | |||
486aab1ded | |||
6d593ff804 | |||
5fc5d73752 | |||
a38975ca49 | |||
cb2cc8c074 | |||
b7e1fcde6d |
1
.commitlintrc.json
Normal file
1
.commitlintrc.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{ "extends": ["@commitlint/config-conventional"] }
|
1
.github/ISSUE_TEMPLATE/BUG.md
vendored
1
.github/ISSUE_TEMPLATE/BUG.md
vendored
@ -1,6 +1,7 @@
|
|||||||
---
|
---
|
||||||
name: '🐛 Bug Report'
|
name: '🐛 Bug Report'
|
||||||
about: 'Report an unexpected problem or unintended behavior.'
|
about: 'Report an unexpected problem or unintended behavior.'
|
||||||
|
title: '[Bug]'
|
||||||
labels: 'bug'
|
labels: 'bug'
|
||||||
---
|
---
|
||||||
|
|
||||||
|
14
.github/ISSUE_TEMPLATE/DOCUMENTATION.md
vendored
14
.github/ISSUE_TEMPLATE/DOCUMENTATION.md
vendored
@ -1,6 +1,7 @@
|
|||||||
---
|
---
|
||||||
name: '📜 Documentation'
|
name: '📜 Documentation'
|
||||||
about: 'Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...).'
|
about: 'Correct spelling errors, improvements or additions to documentation files (README, CONTRIBUTING...).'
|
||||||
|
title: '[Documentation]'
|
||||||
labels: 'documentation'
|
labels: 'documentation'
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -10,11 +11,12 @@ Please make sure your issue has not already been fixed.
|
|||||||
Please place an x (no spaces - [x]) in all [ ] that apply.
|
Please place an x (no spaces - [x]) in all [ ] that apply.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
### Documentation :
|
## Documentation
|
||||||
|
|
||||||
- [ ] Is Missing
|
<!-- Please uncomment the type of documentation problem this issue address -->
|
||||||
- [ ] Is Confusing
|
|
||||||
- [ ] Has Typo errors
|
|
||||||
- [ ] Not Sure?
|
|
||||||
|
|
||||||
### Proposal
|
<!-- Documentation is Missing -->
|
||||||
|
<!-- Documentation is Confusing -->
|
||||||
|
<!-- Documentation has Typo errors -->
|
||||||
|
|
||||||
|
## Proposal
|
||||||
|
7
.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md
vendored
7
.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md
vendored
@ -1,19 +1,20 @@
|
|||||||
---
|
---
|
||||||
name: '✨ Feature Request'
|
name: '✨ Feature Request'
|
||||||
about: 'Suggest a new feature idea.'
|
about: 'Suggest a new feature idea.'
|
||||||
|
title: '[Feature]'
|
||||||
labels: 'feature request'
|
labels: 'feature request'
|
||||||
---
|
---
|
||||||
|
|
||||||
<!-- Please make sure your issue has not already been fixed. -->
|
<!-- Please make sure your issue has not already been fixed. -->
|
||||||
|
|
||||||
### Description
|
## Description
|
||||||
|
|
||||||
<!-- A clear and concise description of the problem or missing capability... -->
|
<!-- A clear and concise description of the problem or missing capability... -->
|
||||||
|
|
||||||
### Describe the solution you'd like
|
## Describe the solution you'd like
|
||||||
|
|
||||||
<!-- If you have a solution in mind, please describe it. -->
|
<!-- If you have a solution in mind, please describe it. -->
|
||||||
|
|
||||||
### Describe alternatives you've considered
|
## Describe alternatives you've considered
|
||||||
|
|
||||||
<!-- Have you considered any alternative solutions or workarounds? -->
|
<!-- Have you considered any alternative solutions or workarounds? -->
|
||||||
|
17
.github/ISSUE_TEMPLATE/IMPROVEMENT.md
vendored
17
.github/ISSUE_TEMPLATE/IMPROVEMENT.md
vendored
@ -1,6 +1,7 @@
|
|||||||
---
|
---
|
||||||
name: '🔧 Improvement'
|
name: '🔧 Improvement'
|
||||||
about: 'Improve structure/format/performance/refactor/tests of the code.'
|
about: 'Improve structure/format/performance/refactor/tests of the code.'
|
||||||
|
title: '[Improvement]'
|
||||||
labels: 'improvement'
|
labels: 'improvement'
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -10,12 +11,14 @@ Please make sure your issue has not already been fixed.
|
|||||||
Please place an x (no spaces - [x]) in all [ ] that apply.
|
Please place an x (no spaces - [x]) in all [ ] that apply.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
### Type of Improvement :
|
## Type of Improvement
|
||||||
|
|
||||||
- [ ] Files and Folders Structure
|
<!-- Please uncomment the type of improvements this issue address -->
|
||||||
- [ ] Performance
|
|
||||||
- [ ] Refactoring code
|
|
||||||
- [ ] Tests
|
|
||||||
- [ ] Not Sure?
|
|
||||||
|
|
||||||
### Proposal
|
<!-- Files and Folders Structure -->
|
||||||
|
<!-- Performance -->
|
||||||
|
<!-- Refactoring code -->
|
||||||
|
<!-- Tests -->
|
||||||
|
<!-- Not Sure? -->
|
||||||
|
|
||||||
|
## Proposal
|
||||||
|
1
.github/ISSUE_TEMPLATE/QUESTION.md
vendored
1
.github/ISSUE_TEMPLATE/QUESTION.md
vendored
@ -1,6 +1,7 @@
|
|||||||
---
|
---
|
||||||
name: '🙋 Question'
|
name: '🙋 Question'
|
||||||
about: 'Further information is requested.'
|
about: 'Further information is requested.'
|
||||||
|
title: '[Question]'
|
||||||
labels: 'question'
|
labels: 'question'
|
||||||
---
|
---
|
||||||
|
|
||||||
|
17
.github/PULL_REQUEST_TEMPLATE.md
vendored
17
.github/PULL_REQUEST_TEMPLATE.md
vendored
@ -9,19 +9,8 @@ Please place an x (no spaces - [x]) in all [ ] that apply.
|
|||||||
|
|
||||||
-->
|
-->
|
||||||
|
|
||||||
### What type of change does this PR introduce?
|
## What changes this PR introduce?
|
||||||
|
|
||||||
- [ ] Bugfix
|
## List any relevant issue numbers
|
||||||
- [ ] Feature
|
|
||||||
- [ ] Refactor
|
|
||||||
- [ ] Documentation
|
|
||||||
- [ ] Not Sure?
|
|
||||||
|
|
||||||
### Does this PR introduce breaking changes?
|
## Is there anything you'd like reviewers to focus on?
|
||||||
|
|
||||||
- [ ] Yes
|
|
||||||
- [ ] No
|
|
||||||
|
|
||||||
### List any relevant issue numbers:
|
|
||||||
|
|
||||||
### Description:
|
|
||||||
|
2
.github/workflows/commitlint.yml
vendored
2
.github/workflows/commitlint.yml
vendored
@ -12,7 +12,7 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
commitlint:
|
commitlint:
|
||||||
if: ${{ (github.event_name == 'push') || (github.event_name == 'pull_request_review' && github.event.review.state == 'approved' && (github.event.review.author_association == 'COLLABORATOR' || github.event.review.author_association == 'MEMBER' || github.event.review.author_association == 'OWNER') && !github.event.pull_request.draft && github.event.pull_request.state == 'open') }}
|
if: ${{ (github.event_name == 'push') || (github.event_name == 'pull_request_review' && github.event.review.state == 'approved' && (github.event.review.author_association == 'COLLABORATOR' || github.event.review.author_association == 'MEMBER' || github.event.review.author_association == 'OWNER') && !github.event.pull_request.draft && github.event.pull_request.state == 'open') }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: 'ubuntu-latest'
|
||||||
steps:
|
steps:
|
||||||
- uses: 'actions/checkout@v2'
|
- uses: 'actions/checkout@v2'
|
||||||
with:
|
with:
|
||||||
|
10
.github/workflows/nodejs.yml
vendored
10
.github/workflows/nodejs.yml
vendored
@ -12,7 +12,7 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
ci_app:
|
ci_app:
|
||||||
if: ${{ (github.event_name == 'push') || (github.event_name == 'pull_request_review' && github.event.review.state == 'approved' && (github.event.review.author_association == 'COLLABORATOR' || github.event.review.author_association == 'MEMBER' || github.event.review.author_association == 'OWNER') && !github.event.pull_request.draft && github.event.pull_request.state == 'open') }}
|
if: ${{ (github.event_name == 'push') || (github.event_name == 'pull_request_review' && github.event.review.state == 'approved' && (github.event.review.author_association == 'COLLABORATOR' || github.event.review.author_association == 'MEMBER' || github.event.review.author_association == 'OWNER') && !github.event.pull_request.draft && github.event.pull_request.state == 'open') }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: 'ubuntu-latest'
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
node-version: [14.x]
|
node-version: [14.x]
|
||||||
@ -25,10 +25,9 @@ jobs:
|
|||||||
node-version: ${{ matrix.node-version }}
|
node-version: ${{ matrix.node-version }}
|
||||||
|
|
||||||
- name: 'Cache dependencies'
|
- name: 'Cache dependencies'
|
||||||
uses: 'actions/cache@v2'
|
uses: 'actions/cache@v2.1.4'
|
||||||
with:
|
with:
|
||||||
path: |
|
path: '**/node_modules'
|
||||||
**/node_modules
|
|
||||||
key: ${{ runner.os }}-${{ hashFiles('**/package.json') }}
|
key: ${{ runner.os }}-${{ hashFiles('**/package.json') }}
|
||||||
|
|
||||||
- name: 'Install dependencies'
|
- name: 'Install dependencies'
|
||||||
@ -37,6 +36,9 @@ jobs:
|
|||||||
- name: 'Lint'
|
- name: 'Lint'
|
||||||
run: 'npm run lint'
|
run: 'npm run lint'
|
||||||
|
|
||||||
|
- name: 'MarkdownLint'
|
||||||
|
run: 'npm run markdownlint'
|
||||||
|
|
||||||
- name: 'Build'
|
- name: 'Build'
|
||||||
run: 'npm run build'
|
run: 'npm run build'
|
||||||
|
|
||||||
|
2
.github/workflows/npm-publish.yml
vendored
2
.github/workflows/npm-publish.yml
vendored
@ -13,7 +13,7 @@ jobs:
|
|||||||
- uses: 'actions/checkout@v2'
|
- uses: 'actions/checkout@v2'
|
||||||
|
|
||||||
- name: 'Cache dependencies'
|
- name: 'Cache dependencies'
|
||||||
uses: 'actions/cache@v2'
|
uses: 'actions/cache@v2.1.4'
|
||||||
with:
|
with:
|
||||||
path: '**/node_modules'
|
path: '**/node_modules'
|
||||||
key: ${{ runner.os }}-${{ hashFiles('**/package.json') }}
|
key: ${{ runner.os }}-${{ hashFiles('**/package.json') }}
|
||||||
|
1
.husky/.gitignore
vendored
Normal file
1
.husky/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
_
|
4
.husky/commit-msg
Executable file
4
.husky/commit-msg
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
. "$(dirname "$0")/_/husky.sh"
|
||||||
|
|
||||||
|
npx --no-install commitlint --edit
|
5
.husky/pre-commit
Executable file
5
.husky/pre-commit
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
. "$(dirname "$0")/_/husky.sh"
|
||||||
|
|
||||||
|
npm run lint
|
||||||
|
npm run markdownlint
|
7
.markdownlint.json
Normal file
7
.markdownlint.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD013": false,
|
||||||
|
"MD024": false,
|
||||||
|
"MD033": false,
|
||||||
|
"MD041": false
|
||||||
|
}
|
2
.yarnrc
2
.yarnrc
@ -1,2 +1,2 @@
|
|||||||
install.no-lockfile true
|
--install.no-lockfile true
|
||||||
save-exact true
|
save-exact true
|
||||||
|
46
CHANGELOG.md
46
CHANGELOG.md
@ -1,5 +1,51 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [2.0.0](https://github.com/Thream/socketio-jwt/compare/v1.1.1...v2.0.0) (2021-02-22)
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- usage of auth option to send credentials ([a14d4e9](https://github.com/Thream/socketio-jwt/commit/a14d4e937b764fdf4fb6b173c55b6f49688766dd))
|
||||||
|
|
||||||
|
See: <https://socket.io/docs/v3/middlewares/#Sending-credentials>
|
||||||
|
|
||||||
|
### BREAKING CHANGES
|
||||||
|
|
||||||
|
- `extraHeaders` with `Authorization` doesn't work anymore
|
||||||
|
|
||||||
|
### Migration
|
||||||
|
|
||||||
|
You need to change the way to connect client side.
|
||||||
|
|
||||||
|
Before :
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { io } from 'socket.io-client'
|
||||||
|
|
||||||
|
const socket = io('http://localhost:9000', {
|
||||||
|
extraHeaders: { Authorization: `Bearer ${yourJWT}` }
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
After :
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { io } from 'socket.io-client'
|
||||||
|
|
||||||
|
const socket = io('http://localhost:9000', {
|
||||||
|
auth: { token: `Bearer ${yourJWT}` }
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## [1.1.1](https://github.com/Thream/socketio-jwt/compare/v1.1.0...v1.1.1) (2021-01-28)
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
- **types:** decodedToken in secret callback ([c1a9213](https://github.com/Thream/socketio-jwt/commit/c1a9213a527e4c6188328221372e1f40191a790e)), closes [#21](https://github.com/Thream/socketio-jwt/issues/21)
|
||||||
|
|
||||||
|
### Documentation
|
||||||
|
|
||||||
|
- update server side usage with `jwks-rsa` : get the secret with `key.getPublicKey()` instead of `key.rsaPublicKey`
|
||||||
|
|
||||||
## [1.1.0](https://github.com/Thream/socketio-jwt/compare/v1.0.1...v1.1.0) (2021-01-07)
|
## [1.1.0](https://github.com/Thream/socketio-jwt/compare/v1.0.1...v1.1.0) (2021-01-07)
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
11
README.md
11
README.md
@ -75,7 +75,7 @@ io.use(
|
|||||||
authorize({
|
authorize({
|
||||||
secret: async (decodedToken) => {
|
secret: async (decodedToken) => {
|
||||||
const key = await client.getSigningKeyAsync(decodedToken.header.kid)
|
const key = await client.getSigningKeyAsync(decodedToken.header.kid)
|
||||||
return key.rsaPublicKey
|
return key.getPublicKey()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@ -87,14 +87,19 @@ io.on('connection', async (socket) => {
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `authorize` options
|
||||||
|
|
||||||
|
- `secret` is a string containing the secret for HMAC algorithms, or a function that should fetch the secret or public key as shown in the example with `jwks-rsa`.
|
||||||
|
- `algorithms` (default: `HS256`)
|
||||||
|
|
||||||
### Client side
|
### Client side
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { io } from 'socket.io-client'
|
import { io } from 'socket.io-client'
|
||||||
|
|
||||||
// Require Bearer Tokens to be passed in as an Authorization Header
|
// Require Bearer Token
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: `Bearer ${yourJWT}` }
|
auth: { token: `Bearer ${yourJWT}` }
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handling token expiration
|
// Handling token expiration
|
||||||
|
@ -1 +0,0 @@
|
|||||||
module.exports = { extends: ['@commitlint/config-conventional'] }
|
|
38
package.json
38
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@thream/socketio-jwt",
|
"name": "@thream/socketio-jwt",
|
||||||
"version": "1.1.0",
|
"version": "2.0.0",
|
||||||
"description": "Authenticate socket.io incoming connections with JWTs.",
|
"description": "Authenticate socket.io incoming connections with JWTs.",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
@ -25,15 +25,11 @@
|
|||||||
"url": "https://github.com/Thream/socketio-jwt/issues"
|
"url": "https://github.com/Thream/socketio-jwt/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/Thream/socketio-jwt#readme",
|
"homepage": "https://github.com/Thream/socketio-jwt#readme",
|
||||||
"husky": {
|
|
||||||
"hooks": {
|
|
||||||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
|
|
||||||
"pre-commit": "npm run lint"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"release-it": {
|
"release-it": {
|
||||||
"git": {
|
"git": {
|
||||||
"commitMessage": "chore(release): v${version}"
|
"commit": false,
|
||||||
|
"push": false,
|
||||||
|
"tag": false
|
||||||
},
|
},
|
||||||
"github": {
|
"github": {
|
||||||
"release": false
|
"release": false
|
||||||
@ -44,6 +40,8 @@
|
|||||||
"hooks": {
|
"hooks": {
|
||||||
"before:init": [
|
"before:init": [
|
||||||
"npm run lint",
|
"npm run lint",
|
||||||
|
"npm run markdownlint",
|
||||||
|
"npm run build",
|
||||||
"npm run test"
|
"npm run test"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -72,12 +70,16 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rimraf ./build && tsc",
|
"build": "rimraf ./build && tsc",
|
||||||
|
"markdownlint": "markdownlint '**/*.md' --ignore node_modules",
|
||||||
"lint": "ts-standard | snazzy",
|
"lint": "ts-standard | snazzy",
|
||||||
"format": "ts-standard --fix | snazzy",
|
"format": "ts-standard --fix | snazzy",
|
||||||
"release": "release-it",
|
"release": "release-it",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"test:watchAll": "jest --watchAll",
|
"test:watchAll": "jest --watchAll",
|
||||||
"test:clearCache": "jest --clearCache"
|
"test:clearCache": "jest --clearCache",
|
||||||
|
"postinstall": "husky install",
|
||||||
|
"prepublishOnly": "pinst --disable",
|
||||||
|
"postpublish": "pinst --enable"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"socket.io": ">=3.0.0"
|
"socket.io": ">=3.0.0"
|
||||||
@ -89,23 +91,25 @@
|
|||||||
"@commitlint/cli": "11.0.0",
|
"@commitlint/cli": "11.0.0",
|
||||||
"@commitlint/config-conventional": "11.0.0",
|
"@commitlint/config-conventional": "11.0.0",
|
||||||
"@release-it/conventional-changelog": "2.0.0",
|
"@release-it/conventional-changelog": "2.0.0",
|
||||||
"@types/express": "4.17.9",
|
"@types/express": "4.17.11",
|
||||||
"@types/jest": "26.0.20",
|
"@types/jest": "26.0.20",
|
||||||
"@types/jsonwebtoken": "8.5.0",
|
"@types/jsonwebtoken": "8.5.0",
|
||||||
"@types/node": "14.14.20",
|
"@types/node": "14.14.31",
|
||||||
"@types/server-destroy": "1.0.1",
|
"@types/server-destroy": "1.0.1",
|
||||||
"axios": "0.21.1",
|
"axios": "0.21.1",
|
||||||
"express": "4.17.1",
|
"express": "4.17.1",
|
||||||
"husky": "4.3.7",
|
"husky": "5.1.0",
|
||||||
"jest": "26.6.3",
|
"jest": "26.6.3",
|
||||||
"release-it": "14.2.2",
|
"markdownlint-cli": "0.26.0",
|
||||||
|
"pinst": "2.1.4",
|
||||||
|
"release-it": "14.4.1",
|
||||||
"rimraf": "3.0.2",
|
"rimraf": "3.0.2",
|
||||||
"server-destroy": "1.0.1",
|
"server-destroy": "1.0.1",
|
||||||
"snazzy": "9.0.0",
|
"snazzy": "9.0.0",
|
||||||
"socket.io": "3.0.5",
|
"socket.io": "3.1.1",
|
||||||
"socket.io-client": "3.0.5",
|
"socket.io-client": "3.1.1",
|
||||||
"ts-jest": "26.4.4",
|
"ts-jest": "26.5.1",
|
||||||
"ts-standard": "10.0.0",
|
"ts-standard": "10.0.0",
|
||||||
"typescript": "4.1.3"
|
"typescript": "4.1.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ describe('authorize - with secret as string in options', () => {
|
|||||||
|
|
||||||
it('should emit error with bad token format', (done) => {
|
it('should emit error with bad token format', (done) => {
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: 'testing' }
|
auth: { token: 'testing' }
|
||||||
})
|
})
|
||||||
socket.on('connect_error', (err: any) => {
|
socket.on('connect_error', (err: any) => {
|
||||||
expect(err.data.message).toEqual(
|
expect(err.data.message).toEqual(
|
||||||
@ -45,7 +45,7 @@ describe('authorize - with secret as string in options', () => {
|
|||||||
|
|
||||||
it('should emit error with unauthorized handshake', (done) => {
|
it('should emit error with unauthorized handshake', (done) => {
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: 'Bearer testing' }
|
auth: { token: 'Bearer testing' }
|
||||||
})
|
})
|
||||||
socket.on('connect_error', (err: any) => {
|
socket.on('connect_error', (err: any) => {
|
||||||
expect(err.data.message).toEqual(
|
expect(err.data.message).toEqual(
|
||||||
@ -59,7 +59,7 @@ describe('authorize - with secret as string in options', () => {
|
|||||||
|
|
||||||
it('should connect the user', (done) => {
|
it('should connect the user', (done) => {
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: `Bearer ${token}` }
|
auth: { token: `Bearer ${token}` }
|
||||||
})
|
})
|
||||||
socket.on('connect', () => {
|
socket.on('connect', () => {
|
||||||
socket.close()
|
socket.close()
|
||||||
@ -93,7 +93,7 @@ describe('authorize - with secret as callback in options', () => {
|
|||||||
|
|
||||||
it('should connect the user', (done) => {
|
it('should connect the user', (done) => {
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: `Bearer ${token}` }
|
auth: { token: `Bearer ${token}` }
|
||||||
})
|
})
|
||||||
socket.on('connect', () => {
|
socket.on('connect', () => {
|
||||||
socket.close()
|
socket.close()
|
||||||
|
@ -31,7 +31,7 @@ export const fixtureStart = async (
|
|||||||
if (typeof options.secret === 'string') {
|
if (typeof options.secret === 'string') {
|
||||||
keySecret = options.secret
|
keySecret = options.secret
|
||||||
} else {
|
} else {
|
||||||
keySecret = await options.secret(() => {})
|
keySecret = await options.secret({ header: { alg: 'RS256' }, payload: '' })
|
||||||
}
|
}
|
||||||
app.post('/login', (_req, res) => {
|
app.post('/login', (_req, res) => {
|
||||||
const profile = {
|
const profile = {
|
||||||
|
@ -21,7 +21,15 @@ type SocketIOMiddleware = (
|
|||||||
next: (err?: ExtendedError) => void
|
next: (err?: ExtendedError) => void
|
||||||
) => void
|
) => void
|
||||||
|
|
||||||
type SecretCallback = (decodedToken: null | { [key: string]: any } | string) => Promise<string>
|
interface CompleteDecodedToken {
|
||||||
|
header: {
|
||||||
|
alg: Algorithm
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
payload: any
|
||||||
|
}
|
||||||
|
|
||||||
|
type SecretCallback = (decodedToken: CompleteDecodedToken) => Promise<string>
|
||||||
|
|
||||||
export interface AuthorizeOptions {
|
export interface AuthorizeOptions {
|
||||||
secret: string | SecretCallback
|
secret: string | SecretCallback
|
||||||
@ -32,9 +40,9 @@ export const authorize = (options: AuthorizeOptions): SocketIOMiddleware => {
|
|||||||
const { secret, algorithms = ['HS256'] } = options
|
const { secret, algorithms = ['HS256'] } = options
|
||||||
return async (socket, next) => {
|
return async (socket, next) => {
|
||||||
let encodedToken: string | null = null
|
let encodedToken: string | null = null
|
||||||
const authorizationHeader = socket.request.headers.authorization
|
const { token } = socket.handshake.auth
|
||||||
if (authorizationHeader != null) {
|
if (token != null) {
|
||||||
const tokenSplitted = authorizationHeader.split(' ')
|
const tokenSplitted = token.split(' ')
|
||||||
if (tokenSplitted.length !== 2 || tokenSplitted[0] !== 'Bearer') {
|
if (tokenSplitted.length !== 2 || tokenSplitted[0] !== 'Bearer') {
|
||||||
return next(
|
return next(
|
||||||
new UnauthorizedError('credentials_bad_format', {
|
new UnauthorizedError('credentials_bad_format', {
|
||||||
@ -51,15 +59,14 @@ export const authorize = (options: AuthorizeOptions): SocketIOMiddleware => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// Store encoded JWT
|
|
||||||
socket.encodedToken = encodedToken
|
socket.encodedToken = encodedToken
|
||||||
let keySecret: string | null = null
|
let keySecret: string | null = null
|
||||||
let decodedToken: any
|
let decodedToken: any
|
||||||
if (typeof secret === 'string') {
|
if (typeof secret === 'string') {
|
||||||
keySecret = secret
|
keySecret = secret
|
||||||
} else {
|
} else {
|
||||||
decodedToken = jwt.decode(encodedToken, { complete: true })
|
const completeDecodedToken = jwt.decode(encodedToken, { complete: true })
|
||||||
keySecret = await secret(decodedToken)
|
keySecret = await secret(completeDecodedToken as CompleteDecodedToken)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
decodedToken = jwt.verify(encodedToken, keySecret, { algorithms })
|
decodedToken = jwt.verify(encodedToken, keySecret, { algorithms })
|
||||||
@ -70,7 +77,6 @@ export const authorize = (options: AuthorizeOptions): SocketIOMiddleware => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// Store decoded JWT
|
|
||||||
socket.decodedToken = decodedToken
|
socket.decodedToken = decodedToken
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user