nativescript-auth0 icon indicating copy to clipboard operation
nativescript-auth0 copied to clipboard

ReferenceError: Can't find variable: A0SHA256ChallengeGenerator

Open JimLynchCodes opened this issue 5 years ago • 3 comments
trafficstars

Hi! I was excited to add this plugin to my nativescript app.

When I run it in my laptop simulator "tns run ios" then everything works great.

When I run it on my phone though "tns preview" then I get an error message when calling the "webAuthentication" function:

ReferenceError: Can't find variable: A0SHA256ChallengeGenerator

Which platform(s) does your issue occur on?

  • iOS (only deplying to iOS right now)

Please, provide the following version numbers that your issue occurs with:

  • CLI: (run tns --version to fetch it) : 6.7.8

  • package.json:

{
  "nativescript": {
    "id": "enrolled-agent.study-app",
    "tns-android": {
      "version": "*"
    },
    "tns-ios": {
      "version": "*"
    }
  },
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "repository": "<fill-your-repository-here>",
  "scripts": {
    "start": "tns run ios --env.environment=\"enrolled-agent.dev\" --device=\"E36E2FEF-8EDB-45E7-9A51-E60C6EA3EE74\"",
    "pre-prepare": "ts-node ./pre-prepare/pre-prepare-app.ts",
    "pre": "echo foo",
    "lint": "tslint \"src/**/*.ts\""
  },
  "dependencies": {
    "@angular/animations": "~8.2.0",
    "@angular/common": "~8.2.0",
    "@angular/compiler": "~8.2.0",
    "@angular/core": "~8.2.0",
    "@angular/forms": "~8.2.0",
    "@angular/platform-browser": "~8.2.0",
    "@angular/platform-browser-dynamic": "~8.2.0",
    "@angular/router": "~8.2.0",
    "@nativescript/theme": "~2.3.0",
    "nativescript-angular": "~8.21.0",
    "nativescript-auth0": "^3.0.2",
    "nativescript-ui-sidedrawer": "~8.0.0",
    "reflect-metadata": "~0.1.12",
    "rxjs": "^6.4.0",
    "tns-core-modules": "~6.5.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~8.2.0",
    "@nativescript/schematics": "^9.0.0",
    "@ngtools/webpack": "~8.2.0",
    "codelyzer": "~4.5.0",
    "nativescript-angular-cli": "^0.1.9",
    "nativescript-dev-webpack": "~1.5.0",
    "node-sass": "^4.7.1",
    "tslint": "~5.19.0",
    "typescript": "~3.5.3"
  },
  "gitHead": "20a65d338ae8f8911087ab6615b89363f864b07b",
  "readme": "NativeScript Application"
}

Please, tell us how to recreate the issue in as much detail as possible.

  • Add this library to a fresh nativescript project

  • run tns preview and see the app crash with this error when it tries to call "webAuthentication".

JimLynchCodes avatar Sep 18 '20 16:09 JimLynchCodes

Bump! 🙃

JimLynchCodes avatar Sep 26 '20 18:09 JimLynchCodes

I am also having this problem... my esteemed colleague @Vishwa52 has figured a workaround, but its unclear why this isnt working. Here's his solution: in safariWebAuth.js, replace the handler with this:

handler(redirectURL) {
        if (this.responseType.indexOf(ResponseType.code) > -1) {
            const authentication = new Auth0Authentication(this.clientId, this.url, this.telemetry);
            authentication.logger = this.logger;
            const verifier = this.getRandomValues(32)
            const challenge = Base64.stringify(sha256(verifier)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');

            return new PKCE(authentication, redirectURL, verifier, challenge, 'S256', this.responseType, this.nonce);
            // return PKCE.init(authentication, redirectURL, this.responseType, this.nonce);
        }
        else {
            return new ImplicitGrant(this.responseType, this.nonce);
        }
    }

It just skips the A0SHA256ChallengeGenerator and generates one manually. @NathanWalker any ideas?

davecoffin avatar Jan 18 '23 15:01 davecoffin

In addition to the solution @davecoffin mentioned, Here is the function and imports that generate a verifier to create a challenge:

Install crypto-es with the following command: npm i crypto-es

// crypto-es to use base64 and sha256 encryption.
import CryptoES from 'crypto-es';
const Base64 = CryptoES.enc.Base64;
const sha256 = CryptoES.SHA256;

getRandomValues(size) {
    size = size >= 43 && size <= 128 ? size : 128;

    let benchStr = '';
    const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    for (let i = 0; i < size; i++) {
      benchStr += possible.charAt(Math.floor(Math.random() * possible.length));
    }

    return benchStr;
}

Vishwa52 avatar Jan 18 '23 17:01 Vishwa52