Skip to content

Instantly share code, notes, and snippets.

@khle
Created October 23, 2016 06:40
Show Gist options
  • Select an option

  • Save khle/3063b94cb179d17abc29c506fbb981c2 to your computer and use it in GitHub Desktop.

Select an option

Save khle/3063b94cb179d17abc29c506fbb981c2 to your computer and use it in GitHub Desktop.
JWT Node Azure AD
import {Observable} from 'rx';
import http from 'http';
import restler from 'restler'
import * as jwtaadUtils from './jwtaadUtils';
function get(url) {
return Observable.create(observer => {
restler.get(url).on('complete', (result) => {
if (result instanceof Error) {
observer.onError(result);
} else {
observer.onNext(result);
observer.onCompleted();
}
});
});
}
function requestOpenIdConfig(tenantId, jwt) {
const tenantOpenIdconfig = 'https://login.windows.net/' + tenantId + '/.well-known/openid-configuration';
const source = get(tenantOpenIdconfig)
.map(result => result.jwks_uri)
.flatMap(jwtSigningKeysLocation => get(jwtSigningKeysLocation))
.map(result => result.keys)
.flatMap(publicKeys => Observable.from(publicKeys))
.flatMap(keys => Observable.from(keys.x5c))
.map(certificate => jwtaadUtils.convertCertificateToBeOpenSSLCompatible(certificate))
.map(compatCertificate => jwtaadUtils.verify(jwt, compatCertificate));
source.subscribe(
response => {
console.log(response)
},
error => console.error(error),
() => console.log('done'));
};
const jwt = 'eyJ0eXAiOiJ...'
const tenant = jwtaadUtils.getTenantId(jwt);
console.log(tenant);
requestOpenIdConfig(tenant, jwt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment