Oidc-Client - Windows Azure AD and CORS issue
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]
}
};
}
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.
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
You will also need to set the signingKeys, IIRC, which is sibling to the metadata, not a child
That might be it! Thanks Brock! We'll give that a go and see how it goes.
Thanks for spotting the error Brock! It works!
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,
};
jwks_uri: 'http://localhost:8081/jwks.json',
I know you all know, but in prod please use HTTPS URLs.
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 😆