Skip to content

Commit

Permalink
feat: add proxy support to the auth flow [gh-0] (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
umutuzgur authored Jul 5, 2023
1 parent 8737ef5 commit 7f1932b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
8 changes: 6 additions & 2 deletions packages/cli/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as http from 'http'
import * as crypto from 'crypto'
import jwtDecode from 'jwt-decode'
import { getDefaults as getApiDefaults } from '../rest/api'
import { assignProxy } from '../services/util'
import * as fs from 'fs'
import * as path from 'path'

Expand Down Expand Up @@ -240,12 +241,15 @@ export class AuthContext {

get #axiosInstance () {
// Keep axios instance stateless
return axios.create({
baseURL: getApiDefaults().baseURL,
const { baseURL } = getApiDefaults()
const axiosConf = assignProxy(baseURL, {
baseURL,
headers: {
Accept: 'application/json, text/plain, */*',
Authorization: `Bearer ${this.#accessToken}`,
},
})

return axios.create(axiosConf)
}
}
25 changes: 2 additions & 23 deletions packages/cli/src/rest/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import axios, { AxiosInstance, CreateAxiosDefaults } from 'axios'
// @ts-ignore
import { getProxyForUrl } from 'proxy-from-env'
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'
import axios, { AxiosInstance } from 'axios'
import config from '../services/config'
import { assignProxy } from '../services/util'
import Accounts from './accounts'
import Users from './users'
import Projects from './projects'
Expand Down Expand Up @@ -47,25 +45,6 @@ export async function validateAuthentication (): Promise<void> {
}
}

const isHttps = (protocol: string) => protocol.startsWith('https')

function assignProxy (baseURL: string, axiosConfig: CreateAxiosDefaults) {
const proxy = getProxyForUrl(baseURL)
if (!proxy) {
return axiosConfig
}

const isEndpointHttps = isHttps(baseURL)

if (isEndpointHttps) {
axiosConfig.httpsAgent = new HttpsProxyAgent({ proxy })
} else {
axiosConfig.httpAgent = new HttpProxyAgent({ proxy })
}

return axiosConfig
}

function init (): AxiosInstance {
const { baseURL } = getDefaults()
const axiosConf = assignProxy(baseURL, { baseURL })
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/src/services/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { CreateAxiosDefaults } from 'axios'
import * as path from 'path'
import * as fs from 'fs/promises'
import * as fsSync from 'fs'
import { Service } from 'ts-node'
import * as gitRepoInfo from 'git-repo-info'
import { parse } from 'dotenv'
// @ts-ignore
import { getProxyForUrl } from 'proxy-from-env'
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'

// Copied from oclif/core
// eslint-disable-next-line
Expand Down Expand Up @@ -173,3 +177,22 @@ export async function getEnvs (envFile: string|undefined, envArgs: Array<string>
const envsString = `${envArgs.join('\n')}`
return parse(envsString)
}

const isHttps = (protocol: string) => protocol.startsWith('https')

export function assignProxy (baseURL: string, axiosConfig: CreateAxiosDefaults) {
const proxy = getProxyForUrl(baseURL)
if (!proxy) {
return axiosConfig
}

const isEndpointHttps = isHttps(baseURL)

if (isEndpointHttps) {
axiosConfig.httpsAgent = new HttpsProxyAgent({ proxy })
} else {
axiosConfig.httpAgent = new HttpProxyAgent({ proxy })
}

return axiosConfig
}

0 comments on commit 7f1932b

Please sign in to comment.