How to validate the Access Token in OAuth 2.0?

Viewed 2218
1 Answers

Here is an example of the data taken from token (the example is from Azure auth - but it does not matter both use OAuth2).

That's how you extract the information from the Token:

in getAuthInteractiveCallback() or getAuthSilentCallback() methods:

private AuthenticationCallback getAuthSilentCallback() {
    return new AuthenticationCallback() {
        @Override
        public void onSuccess(AuthenticationResult authenticationResult) {
            /* Successfully got a token, call api now */
            Log.d(TAG, "Successfully authenticated");
            Log.d(TAG, "ID Token: " + authenticationResult.getIdToken());
            Log.d(TAG, "ID Token: " + authenticationResult.getAccessToken());

            try {
                String token = authenticationResult.getIdToken();
                String token2 = token.substring(token.indexOf('.') + 1, token.lastIndexOf('.'));
                byte[] data = Base64.decode(token2, Base64.DEFAULT);
                String text = new String(data, StandardCharsets.UTF_8);
                JSONObject jsonObject = new JSONObject(text);
                JSONArray jsonArray = new JSONArray(jsonObject.getString("emails"));
                String eMail = jsonArray.get(0).toString();
                Log.d(TAG, "eMail: " + eMail);

            } catch (JSONException ex) { }



            authResult = authenticationResult;
            state.setAuthResult(authResult);
        }

        @Override
        public void onError(MsalException exception) {
        }

        @Override
        public void onCancel() {
        }
    };
}

In addition you can get iod, expiration time (exp), auth time (auth_time), versiov (ver)

Related