oidc-client-js icon indicating copy to clipboard operation
oidc-client-js copied to clipboard

Invalid Issuer when using Azure AD common endpoint

Open chaholl opened this issue 8 years ago • 18 comments

When authenticating via Azure AD using the multi-tenant endpoint at: https://login.microsoftonline.com/common/

tokens are returned with a tenant specific issuer: https://sts.windows.net/{some-guid-signifying-the-azure-ad-tenantId}/

The metadata specifies the issuer as: issuer=https://sts.windows.net/{tenantid}/

Of course this fails validation in ResponseValidator.js since the strings don't match.

In server side code we'd handle this by disabling issuer validation or adding a function to override the default comparison. Is it worth considering a similar mechanism here?

chaholl avatar Jul 23 '18 20:07 chaholl

The spec says that issuer validation is mandatory. Microsoft doesn't follow the spec there too :/

brockallen avatar Jul 23 '18 21:07 brockallen

I have a look at this at the end of the month -- I'm busy until then.

brockallen avatar Sep 14 '18 22:09 brockallen

Did you ever come up with a workaround for this?

brockallen avatar Oct 06 '18 11:10 brockallen

Yes, I added an optional delegate to allow user defined validation - https://github.com/IdentityModel/oidc-client-js/pull/626

chaholl avatar Oct 06 '18 14:10 chaholl

Ah, sorry -- I totally missed the link above for that. I'll have a look and think about that... what's an example of what your callback looks like?

What I'm trying to determine is when in your user's login workflow would you determine the right value? I guess since you don't know it before they go to the common endpoint, you'd only know it after the fact during response processing.

The reason I'm going down this path is that I think I'd like to possibly make the setting less of the focus and instead something at processing time the right place for this.

brockallen avatar Oct 06 '18 14:10 brockallen

Sure, I added an example to the PR. It's basically a function that returns a boolean that indicates whether returned issuer is valid

chaholl avatar Oct 06 '18 15:10 chaholl

So having looked at the code again, I think the natural way (given how this all was designed a while ago) is that you should be able to replace the ResponseValidatorCtor on the OidcClientSettings. So perhaps derive from the existing one (ResponseValidator) and build your own that overdies the getIssuer API.

IOW, no changes needed to the existing code since it seems that it can support what you're looking for.

brockallen avatar Oct 06 '18 15:10 brockallen

Hi @brockallen Is it possible for you to elaborate a bit more on the idea of extending ResponseValidator for achieving this. Maybe with a small example?

iamkarthikrana avatar Nov 30 '18 05:11 iamkarthikrana

@chaholl did you get it working the way @brockallen mentioned 'coz I couldn't yet.

iamkarthikrana avatar Jan 03 '19 03:01 iamkarthikrana

@tipsanemonk Nah, I had a look at the code and couldn't see an easy way to do without a whole load of unnecessary overhead. I'm using my fork with the delegate instead at the moment. #626

chaholl avatar Jan 07 '19 10:01 chaholl

@chaholl I see, anyways thank you :- )

iamkarthikrana avatar Jan 08 '19 05:01 iamkarthikrana

@chaholl did you publish your fork on npm ? is it useable by others ? thanks in advance

jonenst avatar Apr 03 '20 13:04 jonenst

@chaholl did you publish your fork on npm ? is it useable by others ? thanks in advance

Why don't you use the proprietary AAD client libraries?

brockallen avatar Apr 03 '20 14:04 brockallen

Hi @brockallen , thanks for answering. I'm not using msal.js to avoid vendor lock-in.

jonenst avatar Apr 03 '20 14:04 jonenst

@chaholl did you publish your fork on npm ? is it useable by others ? thanks in advance

I didn't, sorry. I daresay the core code has moved on quite a bit since then though. FWIW, I also used this library to avoid lock-in - I needed to support multiple providers, not just AAD

chaholl avatar Apr 03 '20 14:04 chaholl

Also came across this issue, and trying to implement @brockallen 's solution, but noticed that it is not possible because we only get to know the actual tenant id after we parse the JWT, and the parsing is done inside of validateJwtAttributes, so just changing the ResponseValidator wouldn't be enough.

How about adding a special case to validateJwtAttributes to only handle Active Directory logins? Active Directory is very unique, and I doubt that any other services would use the same API structure as AzureAD.

Bill13579 avatar Apr 18 '20 18:04 Bill13579

I don't think there should be any azure specific code in this library.

For what it's worth, I hacked my way around this decoding manually the token and dynamically changing the oidc-client-js "authority" configuration (you have to hack the state as well). It seems to work fine but it's still a huge hack :(

/* hack to ignore the iss check. XXX TODO to remove */
const regextoken=/id_token=[^&]*/;
const regexstate=/state=[^&]*/;
let authority;
if (window.location.hash) {
    const matched_id_token=window.location.hash.match(regextoken);
    const matched_state=window.location.hash.match(regexstate);
    if (matched_id_token != null && matched_state != null) {
        const id_token = matched_id_token[0].split('=')[1];
        const state = matched_state[0].split('=')[1];
        const strState = localStorage.getItem("oidc." + state);
        if (strState != null) {
            authority = jwtDecode(id_token).iss
            // here do your own validation
            //...
            const storedState = JSON.parse(strState);
            storedState.authority=authority;
            localStorage.setItem("oidc." + state, JSON.stringify(storedState));
            sessionStorage.setItem("oidc.hack.authority", authority);
        }
    }
}
authority = authority || sessionStorage.getItem(hackauthoritykey) || idpSettings.authority;
let settings = {
    authority, // other stuff
};
new UserManager(settings);

function logout(dispatch, userManagerInstance) {
    sessionStorage.removeItem(hackauthoritykey);//To remove when hack is removed
}

jonenst avatar Apr 22 '20 13:04 jonenst

If anyone wanted to know how to replace the ResponseValidator with a custom one, I've managed to do it like so:

export class ResponseValidatorCtor {
  constructor (settings) {
    this._settings = settings
  }

  validateSigninResponse (state, response) {
    // validation goes here
    return response
  }

  validateSignoutResponse (state, response) {
    // validation goes here
    return Promise.resolve(response)
  }
}

const settings = {
  client_id,
  redirect_uri,
  ...
  ResponseValidatorCtor
}

const mgr = new oidc.UserManager(settings)

Still need to do some testing but seems to do the job so far.

adamwardecki avatar Jun 24 '20 17:06 adamwardecki