This repository has been archived by the owner on Oct 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate socket server from the HTTP API. (#241)
* Separate socket server from the HTTP API. This changes the API so users have to set up both the HTTP API and the socket server. * Update docs * lint fixes
- Loading branch information
1 parent
f9a15b3
commit f123c41
Showing
10 changed files
with
200 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import crypto from 'crypto'; | ||
import { promisify } from 'util'; | ||
|
||
const randomBytes = promisify(crypto.randomBytes); | ||
|
||
export default class AuthRegistry { | ||
constructor(redis) { | ||
this.redis = redis; | ||
} | ||
|
||
async createAuthToken(user) { | ||
const token = (await randomBytes(64)).toString('hex'); | ||
await this.redis.set(`http-api:socketAuth:${token}`, user.id, 'EX', 60); | ||
return token; | ||
} | ||
|
||
async getTokenUser(token) { | ||
if (token.length !== 128) { | ||
throw new Error('Invalid token'); | ||
} | ||
const [userID] = await this.redis | ||
.multi() | ||
.get(`http-api:socketAuth:${token}`) | ||
.del(`http-api:socketAuth:${token}`) | ||
.exec(); | ||
|
||
return userID; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.