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

Oidc-Client - Windows Azure AD and CORS issue

Open nzdjigit opened this issue 9 years ago • 8 comments

Hi,

We are having issues with CORS and our oidc-client implementation. Here is the error we are getting:

XMLHttpRequest cannot load https://login.microsoftonline.com/common/discovery/keys. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:44384' is therefore not allowed access.

We've already looked at the following issues: https://github.com/IdentityModel/oidc-client-js/issues/275 https://github.com/IdentityModel/oidc-client-js/issues/44

But unfortunately none of the solutions worked for us and it is getting very frustrating.

From what we understood if metadata were explicitly provided, it shouldn't try and hit the .../discovery/keys endpoint for the metadata, but for some reason it keeps doing it.

Could anyone please shed some light on what we could be possibly doing wrong?

Here is our implementation of the auth-service:

import { Injectable } from '@angular/core';
import { UserManager, UserManagerSettings, User } from 'oidc-client';

@Injectable()
export class AuthService {
    private manager: UserManager = new UserManager(getClientSettings());
    private user: User = null;

    constructor() {
        this.manager.getUser()
            .then(user => { this.user = user; });
    }

    isLoggedIn(): boolean {
        return this.user != null && !this.user.expired;
    }

    getClaims(): any {
        return this.user.profile;
    }

    getAuthorizationHeaderValue(): string {
        return `${this.user.token_type} ${this.user.access_token}`;
    }

    startAuthentication(): Promise<void> {
        return this.manager.signinRedirect();
    }

    completeAuthentication(): Promise<void> {
        return this.manager.signinRedirectCallback()
            .then(user => { this.user = user; });
    }
}

export function getClientSettings(): UserManagerSettings {
    return {
        authority: 'https://login.microsoftonline.com/{tenantId}',
        client_id: {appClientId},
        redirect_uri: 'https://localhost:44384/auth-callback',
        post_logout_redirect_uri: 'https://localhost:44384/',
        response_type: "id_token",
        scope: 'openid',
        loadUserInfo: false,
        metadata: {
            issuer: "https://sts.windows.net/{tenantId}/",
            authorization_endpoint: "https://login.microsoftonline.com/{tenantId}/oauth2/authorize",
            token_endpoint: "https://login.microsoftonline.com/{tenantId}/oauth2/token",
            jwks_uri: "https://login.microsoftonline.com/common/discovery/keys",
            signingKeys: [keys go in here]
        }
    };
}

nzdjigit avatar Sep 08 '17 07:09 nzdjigit

They don't support CORS, so you can either configure the matdata manually or you should use their proprietary JS library for their proprietary token service.

brockallen avatar Sep 08 '17 12:09 brockallen

Hi Brock,

Thanks for your reply.

Yes I did figure that much, that's why we tried to configure the metadata manually, but for some reason it still tries to browse to Microsoft's metadata endpoint, which is giving us an error.

I've posted the code snippet from our auth-service that shows how we're manually setting the metadata, but we're clearly doing something wrong or missing something. That's why we were hoping someone could help us identify the issue.

Thanks

nzdjigit avatar Sep 08 '17 12:09 nzdjigit

You will also need to set the signingKeys, IIRC, which is sibling to the metadata, not a child

brockallen avatar Sep 08 '17 12:09 brockallen

That might be it! Thanks Brock! We'll give that a go and see how it goes.

nzdjigit avatar Sep 08 '17 12:09 nzdjigit

Thanks for spotting the error Brock! It works!

nzdjigit avatar Sep 08 '17 22:09 nzdjigit

WARNING: Hacky idea alert!... Manually fetch https://login.microsoftonline.com/common/discovery/keys and save to your local server. Then make a request to your own server by editing your oidc configuration. Ours looks like this:

export default {
  authority: 'https://login.microsoftonline.com/YOUR_TENANT_ID',
  client_id: 'MY_CLIENT_ID',
  resource: 'MY_CLIENT_ID',
  redirect_uri: 'http://localhost:8081/signin-oidc',
  response_type: 'id_token token',
  metadata: {
    // magic happens here!!
    jwks_uri: 'http://localhost:8081/jwks.json',
    authorization_endpoint: "https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/authorize",
    issuer: "https://sts.windows.net/YOUR_TENANT_ID/",
  },
  scope: 'openid',
  loadUserInfo: false,
  silent_redirect_uri: 'http://localhost:8081/silent-refresh-oidc.html',
  automaticSilentRenew: true,
  accessTokenExpiringNotificationTime: 30,
};

tommed avatar Nov 01 '18 14:11 tommed

jwks_uri: 'http://localhost:8081/jwks.json',

I know you all know, but in prod please use HTTPS URLs.

brockallen avatar Nov 02 '18 00:11 brockallen

jwks_uri: 'http://localhost:8081/jwks.json',

I know you all know, but in prod please use HTTPS URLs.

and as this is information for the client, probably best not redirect them to localhost either lol 😆

tommed avatar Nov 02 '18 09:11 tommed