Get Auth0 access token via command line

Viewed 726

I am force to use an unergonomic web site (let’s call it zzz) that uses auth0 for authentication, and a REST API internally, and I have a strong desire to use the API directly.

Using the browser inspector, I can see how that API works, and if I use the JWT Access Token (transmitted using Authorization: Bearer) that I find there, I can script access to API.

The problem I am facing is getting such a JWT access token via auth0, given my username and password.

When I use the browser-based login, I see that zzz redirects me to

https://zzz.eu.auth0.com/login
   ?state=g6…mo
   &client=uz…6j
   &protocol=oauth2
   &response_type=token%20id_token
   &redirect_uri=https%3A%2F%2Ffoo.zzz.com%2Fcallback
   &scope=openid%20read%3Amore%20scopes…
   &audience=zz-api-prod
   &nonce=0G…L7
   &auth0Client=ey…n0%3D

(line breaks for your convenience)

I can now manually enter username and password, and get logged in, which seems to perform these steps:

  • A POST to

    https://zzz.eu.auth0.com/usernamepassword/login
    

    with a body of

    { "client_id":"uz…6j",
      "redirect_uri":"https://foo.zzz.com/callback",
      "tenant":"zzz",
      "response_type":"token id_token"
      "connection":"zzz-production-users",
      "username":"…",
      "password":"…",
      "nonce":"0G…L7",
      "state":"g6…mo",
      "sso":true,
      "_intstate":"deprecated",
      "_csrf":"fb…KI",
      "audience":"zzz-api-prod",
      "auth0Client":"ey…n0=",
      "scope":"openid read:stores more:scopes …",
      "protocol":"oauth2"
    }
    

    and a response of

    <form method="post" name="hiddenform" action="https://zzz.eu.auth0.com/login/callback">
      <input type="hidden" name="wa" value="wsignin1.0">
      <input type="hidden" 
             name="wresult" 
             value="ey…tE">
      <input type="hidden" name="wctx" value="…">
      <noscript>
          <p>
              Script is disabled. Click Submit to continue.
          </p><input type="submit" value="Submit">
      </noscript>
     </form>
    
  • The JS on the login page seems to press that submit button, causing a POST to

    https://zzz.eu.auth0.com/login/callback
    

    which redirects to

    https://foo.zzz.com/callback
       #access_token=ey…7Q
       &scope=openid%20read%3Amore%20scopes…
       &expires_in=7200
       &token_type=Bearer
       &state=%7B%7D
       &id_token=ey…Sg
    

    … which contains the precious access token that I want.

Trying to script this precise flow, which would involve parsing the returned HTML to extract the wctx and wresult arguments, is quite tedious.

So my question is:

Is there a way to get the access_token some other way that is more convenient to script using just command line curl, or maybe some simple python code/library?

I have tried various things that I found on the auth0 documentation website (e.g. https://auth0.com/docs/api-auth/tutorials/password-grant#realm-support), but could not get them to work; presmably because they need to be explicitly enabled by zzz in their auth0 settings?

1 Answers

The correct approach should be a separation between these:

  • Unergonomic web site (Client A) - which uses the implicit flow
  • Web API
  • Client B (your command line) - which uses a different flow

In Auth0 you should configure a new OAuth Client Entry for Client B, which should probably use a different flow - perhaps the password grant you mention.

You will at least be able to get a token in this manner. Whether the API accepts calls from you may depend on other design aspects.

Related