Express app running on http://localhost:6001 receiving requests from electron application on http://localhost:5000.
Authentication process:
Authorization Code Flow with PKCE - Authorizing the user(native app) and receive the authorization code in the url callback.
Decode it and then use it in the POST /oauth/token
.
However, I’m receiving 404 error "Request failed with status code 400"
when I call the GET /authorize
endpoint.
const express = require("express");
const cors = require("cors");
import * as crypto from 'crypto';
import { nanoid } from 'nanoid'
import axios from 'axios';
const bodyParser = require ('body-parser');
const app = express();
var port = 6001;
app.use(cors());
app.use(bodyParser());
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
app.get("/authorize", async (req: any, res: any, next:any) => {
// Creates verifier
function base64URLEncode(str: Buffer) {
return str.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
var verifier = base64URLEncode(crypto.randomBytes(32));
// Generate a code_challenge
function sha256(buffer: string) {
return crypto.createHash('sha256').update(buffer).digest();
}
var challenge = base64URLEncode(sha256(verifier));
var nonce = nanoid();
await axios.get(`https://dev-49v8whrc.us.auth0.com/authorize?response_type=code&
client_id=----------------------&
prompt=none&
connection=Username-Password-Authentication&
redirect_uri=http://localhost:6001/authorize&
nonce=${nonce}&
code_challenge=${challenge}&
code_challenge_method=S256`).then((authCode: any) => res.send(authCode) ).catch((err) => res.send(err))
});