IdentityModel / oidc-client-js

OpenID Connect (OIDC) and OAuth2 protocol support for browser-based JavaScript applications
Apache License 2.0
2.43k stars 842 forks source link

Example code for node.js #335

Closed AndreasHohn closed 7 years ago

AndreasHohn commented 7 years ago

Hello,

I am trying to get the oidc-client running in node.js enviroment. I did come as far that there are not problems when starting it. But when calling mgr.signinRedirect({**"not sure what to put here"**}).then(function() { log("signinRedirect done"); }).catch(function(err) { log(err); }); });

there is an error thrown: TypeError: n._XMLHttpRequest is not a constructor at t.getJson (c:\Users\AndreasHohn\Documents\Git\beepresence\node_modules\oidc-client\lib\oidc-client.min.js:1:26874) at t.getJson (c:\Users\AndreasHohn\Documents\Git\beepresence\node_modules\oidc-client\lib\oidc-client.min.js:1:26842) at t.getMetadata (c:\Users\AndreasHohn\Documents\Git\beepresence\node_modules\oidc-client\lib\oidc-client.min.js:1:23485) at t._getMetadataProperty (c:\Users\AndreasHohn\Documents\Git\beepresence\node_modules\oidc-client\lib\oidc-client.min.js:1:25004) at t.getAuthorizationEndpoint (c:\Users\AndreasHohn\Documents\Git\beepresence\node_modules\oidc-client\lib\oidc-client.min.js:1:23991) at e.t.createSigninRequest (c:\Users\AndreasHohn\Documents\Git\beepresence\node_modules\oidc-client\lib\oidc-client.min.js:1:4356) at c:\Users\AndreasHohn\Documents\Git\beepresence\node_modules\oidc-client\lib\oidc-client.min.js:74:11990 at process._tickCallback (internal/process/next_tick.js:103:7)

function(t,e){var n=this;if(o.default.debug("JsonService.getJson",t),!t)throw o.default.error("No url passed"),new Error("url");return new Promise(function(r,i){var s=new n._XMLHttpRequest;...

Does anybody has an example or could help me? First time working with node.js and OIDC

This is my index.js: const express = require('express'); global.XMLHttpRequest = require("xmlhttprequest"); if (typeof localStorage === "undefined" || localStorage === null) { var LocalStorage = require('node-localstorage').LocalStorage; localStorage = new LocalStorage('./scratch'); } const app = express(); /////////////////////////////// // OidcClient config /////////////////////////////// const Oidc = require('oidc-client') Oidc.Log.logger = console; Oidc.Log.level = Oidc.Log.ERROR; var config = { authority: "http://<ouruserserviceurl>", client_id: "userWeb", redirect_uri: "http://localhost:4200/callback", response_type: "id_token token", scope: "openid profile userApi", post_logout_redirect_uri: "http://localhost:4200/postlogout", popup_redirect_uri:'http://localhost:4200/popup', popup_post_logout_redirect_uri:'http://localhost:4200/popuplogout', silent_redirect_uri:'http://localhost:4200/silent', automaticSilentRenew:true, //silentRequestTimeout:10000, filterProtocolClaims: true, loadUserInfo: true, userStore: new Oidc.WebStorageStateStore({ store: localStorage }) }; var mgr = new Oidc.UserManager(config) /////////////////////////////// // events /////////////////////////////// mgr.events.addAccessTokenExpiring(function () { console.log("token expiring"); log("token expiring"); }); mgr.events.addAccessTokenExpired(function () { console.log("token expired"); log("token expired"); }); mgr.events.addSilentRenewError(function (e) { console.log("silent renew error", e.message); log("silent renew error", e.message); }); mgr.events.addUserLoaded(function (user) { console.log("user loaded", user); mgr.getUser().then(function(){ console.log("getUser loaded user after userLoaded event fired"); }); }); mgr.events.addUserUnloaded(function (e) { console.log("user unloaded"); }); // Initial page redirecting to Github app.get('/auth', (req, res) => { mgr.signinRedirect({redirect_uri: 'http://localhost:4200/callback'}).then(function() { log("signinRedirect done"); }).catch(function(err) { log(err); }); }); // Callback service parsing the authorization token and asking for the access token app.get('/callback', (req, res) => { new Oidc.UserManager().signinRedirectCallback().then(function(user) { log("signin response success", user); }).catch(function(err) { log(err); }); }); app.get('/success', (req, res) => { res.send(''); }); app.get('/postlogout', (req, res) => { //res.send(''); }); app.get('/popup', (req, res) => { //res.send(''); new Oidc.UserManager().signinPopupCallback(); }); app.get('/popuplogout', (req, res) => { //res.send(''); new Oidc.UserManager().signoutPopupCallback(true); }); app.get('/silent', (req, res) => { //res.send(''); new Oidc.UserManager().signinSilentCallback(); }); app.get('/', (req, res) => { res.send('Hello<br><a href="/auth">Log in with UserService</a>'); mgr.getUser().then(function (user) { if (user) { console.log("User logged in", user.profile); } else { console.log("User not logged in"); } }); }); app.listen(4200, () => { console.log('Express server started on port 4200'); // eslint-disable-line });

brockallen commented 7 years ago

I am trying to get the oidc-client running in node.js enviroment.

Why? This library is designed for browser-based client-side JavaScript.

mribichich commented 7 years ago

Just in case I leave a fork for node that I found:

https://github.com/edwinf/oidc-client-node