Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
janb87 opened this issue Apr 18, 2018 · 3 comments

Comments

@janb87
Copy link

janb87 commented Apr 18, 2018

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?

@gabrielsroka
Copy link
Contributor

gabrielsroka commented May 14, 2019

@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
Copy link
Contributor

@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);
    }
})();

@oleksandrpravosudko-okta
Copy link
Contributor

Internal ref: OKTA-419414

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants