From f4644836d3c5c442c260f7132cdbb9e1e580999a Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Sun, 2 Jan 2022 10:27:40 -0800 Subject: [PATCH] Connect UserGenPage to API. Base 32 string seems to pass quality checks, shorter do not. --- api/views.py | 2 +- frontend/src/components/UserGenPage.js | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/api/views.py b/api/views.py index a900a42b..5321d517 100644 --- a/api/views.py +++ b/api/views.py @@ -117,7 +117,7 @@ class UserGenerator(APIView): # Deny user gen if entropy below 128 bits or 0.7 shannon heterogeneity if bits_entropy < 128 or shannon_entropy < 0.7: - context['Bad Request'] = 'The token does not have enough entropy' + context['bad_request'] = 'The token does not have enough entropy' return Response(context, status=status.HTTP_400_BAD_REQUEST) # Hashes the token, only 1 iteration. Maybe more is better. diff --git a/frontend/src/components/UserGenPage.js b/frontend/src/components/UserGenPage.js index 9cf8abfa..69f22298 100644 --- a/frontend/src/components/UserGenPage.js +++ b/frontend/src/components/UserGenPage.js @@ -4,10 +4,11 @@ export default class UserGenPage extends Component { constructor(props) { super(props); this.state = { - token: this.genBase62Token(30), + token: this.genBase62Token(32), }; - this.token = this.genBase62Token(30); + this.getGeneratedUser(); } + // sort of cryptographically strong function to generate Base62 token client-side genBase62Token(length) { @@ -19,11 +20,29 @@ export default class UserGenPage extends Component { .substring(0, length); } + getGeneratedUser() { + fetch('/api/usergen' + '?token=' + this.state.token) + .then((response) => response.json()) + .then((data) => { + this.setState({ + nickname: data.nickname, + bit_entropy: data.token_bits_entropy, + shannon_entropy: data.token_shannon_entropy, + bad_request: data.bad_request, + }); + }); + } + + render() { return (

This is the landing and user generator page

Have a token of appreciation {this.state.token}

+

Username is {this.state.nickname}

+

Shannon entropy is {this.state.shannon_entropy}

+

Entropy depth is {this.state.bit_entropy} bits

+

Bad request: {this.state.bad_request}

); }