okta-sdk-nodejs icon indicating copy to clipboard operation
okta-sdk-nodejs copied to clipboard

Add option to define a proxy in the configuration or via environment variables

Open janb87 opened this issue 6 years ago • 3 comments

In our setup we needed to communicate with the okta services via a proxy. To achieve this we had to overwrite the Http implementation.

This is how we solved it:

const oktaSDK = require('@okta/okta-sdk-nodejs')
const Http = require('@okta/okta-sdk-nodejs/src/http')
const HttpsProxyAgent = require('https-proxy-agent')

class HttpWithProxy extends Http {
    constructor({cacheStore, cacheMiddleware, defaultHeaders}) {
        super({
            cacheStore,
            cacheMiddleware
        })
        this.defaultHeaders = defaultHeaders

        // Okta is only https
        const proxy = process.env.https_proxy || process.env.HTTPS_PROXY
        if (proxy) {
            logger.info(`Using proxy server ${proxy}`)

            const agent = new HttpsProxyAgent(proxy)
            this.agent = agent
        }
    }

    http(uri, request, context) {
        if (this.agent) {
            request.agent = this.agent
        }
        return super.http(uri, request, context)
    }
}

const oktaClient = new oktaSDK.Client({
        orgUrl: 'https:...',
        token: 'token'
})

oktaClient.http = new HttpWithProxy({defaultHeaders: oktaClient.http.defaultHeaders})

Could we extend the Client class to take in proxy configuration?

janb87 avatar Apr 18 '18 09:04 janb87

@janb87 thanks for the post. I couldn't get this to work with the recent Okta SDK, node v 12.2 on Windows, with the Fiddler proxy, etc.

Does this still work for you? (I know you posted this over a year ago, perhaps things have changed since then)

Thanks!

gabrielsroka avatar May 14 '19 19:05 gabrielsroka

@janb87

it's 1 year later and i'm looking for the same solution again.

but this time, i got it to work:

/*
set https_proxy=http://127.0.0.1:8888
set NODE_TLS_REJECT_UNAUTHORIZED=0
*/

const okta = require('@okta/okta-sdk-nodejs');
const Http = require('@okta/okta-sdk-nodejs/src/http');
const HttpsProxyAgent = require('https-proxy-agent');

class HttpWithProxy extends Http {
    constructor({cacheStore, cacheMiddleware, defaultHeaders}) {
        super({
            cacheStore,
            cacheMiddleware,
            requestExecutor: new okta.RequestExecutor()
        });
        this.defaultHeaders = defaultHeaders;

        // Okta is only https
        const proxy = process.env.https_proxy || process.env.HTTPS_PROXY;
        if (proxy) {
            const agent = new HttpsProxyAgent(proxy);
            this.agent = agent;
        }
    }

    http(uri, request, context) {
        if (this.agent) {
            request.agent = this.agent;
        }
        return super.http(uri, request, context);
    }
}

const client = new okta.Client({
    orgUrl: 'https://XXX.okta.com/',
    token: 'XXX'
});

client.http = new HttpWithProxy({
    defaultHeaders: client.http.defaultHeaders
});

(async function () {
    try {
        const user = await client.getUser('me');
        console.log(user);
    } catch (e) {
        console.log(e);
    }
})();

gabrielsroka avatar Sep 20 '20 04:09 gabrielsroka

Internal ref: OKTA-419414