* Generates a new challenge * @param {ChallengeConfig} [conf] - Challenge configuration * @returns {Promise<{ challenge: {c: number, s: number, d: number}, token?: string, expires: number }>} Challenge data
(conf)
| 266 | * @returns {Promise<{ challenge: {c: number, s: number, d: number}, token?: string, expires: number }>} Challenge data |
| 267 | */ |
| 268 | async createChallenge(conf) { |
| 269 | await this._lazyCleanup(); |
| 270 | |
| 271 | /** @type {{c: number, s: number, d: number}} */ |
| 272 | const challenge = { |
| 273 | c: conf?.challengeCount ?? 50, |
| 274 | s: conf?.challengeSize ?? 32, |
| 275 | d: conf?.challengeDifficulty ?? 4, |
| 276 | }; |
| 277 | |
| 278 | const token = await randomHex(25); |
| 279 | const expires = Date.now() + (conf?.expiresMs ?? 600000); |
| 280 | |
| 281 | if (conf && conf.store === false) { |
| 282 | return { challenge, expires }; |
| 283 | } |
| 284 | |
| 285 | const challengeData = { expires, challenge }; |
| 286 | |
| 287 | if (this.config.storage?.challenges?.store) { |
| 288 | await this.config.storage.challenges.store(token, challengeData); |
| 289 | } else { |
| 290 | this.config.state.challengesList[token] = challengeData; |
| 291 | } |
| 292 | |
| 293 | return { challenge, token, expires }; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Redeems a challenge solution in exchange for a token |
no test coverage detected