diff --git a/src/generator/dts/index.ts b/src/generator/dts/index.ts index 4678e24..4fa0a72 100644 --- a/src/generator/dts/index.ts +++ b/src/generator/dts/index.ts @@ -19,20 +19,19 @@ export const run: Runner = async (config) => { const distDir = 'dts' mkdirSync(distDir, { recursive: true }) - let connection: Record = { 'base-url': `https://${context.host}` } + const connection: Record = { 'base-url': `https://${context.host}` } if (context.oauth) { - connection = { - ...connection, - 'oauth-token': await getOauthToken(context.host, {} /* context.agentOptions */), - } + connection['oauth-token'] = await getOauthToken({ + domain: context.host, + scope: typeof context.oauth === 'object' ? context.oauth.scope : undefined, + proxy: context.proxy, + pfx: context.pfx, + }) } else { - connection = { - ...connection, - 'username': process.env.GOQOO_USERNAME, - 'password': process.env.GOQOO_PASSWORD, - 'basic-auth-username': process.env.GOQOO_BASICAUTH_USERNAME, - 'basic-auth-password': process.env.GOQOO_BASICAUTH_PASSWORD, - } + connection['username'] = process.env.GOQOO_USERNAME + connection['password'] = process.env.GOQOO_PASSWORD + connection['basic-auth-username'] = process.env.GOQOO_BASICAUTH_USERNAME + connection['basic-auth-password'] = process.env.GOQOO_BASICAUTH_PASSWORD } const skipApps = dtsGen?.skip || [] diff --git a/src/lib/types.ts b/src/lib/types.ts index b1aa75f..347d847 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,8 +1,12 @@ +import type { ProxyOption, PfxOption } from 'gyuma' + type _Context = { env: Env host: string appId: Record - oauth?: boolean + oauth?: boolean | { scope: string } + proxy?: ProxyOption + pfx?: PfxOption } export type Config = _Context> = { diff --git a/src/oauth.ts b/src/oauth.ts index 934099a..f26e5c0 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -1,18 +1,27 @@ -import gyuma from 'gyuma' +import netrc from 'netrc-parser' +import { gyuma, PfxOption, ProxyOption } from 'gyuma' -type ProxyOption = - | string - | { - protocol: string - auth: string - hostname: string - port: number +export const getOauthToken = async ({ + domain, + scope = 'k:app_settings:read', + proxy, + pfx, +}: { + domain: string + scope?: string + proxy?: ProxyOption + pfx?: PfxOption +}): Promise => { + // プロキシサーバーの認証情報がnetrcにある場合は読み取る + // 認証はオプションなので、認証情報が見つからなくても標準入力はさせない + if (proxy instanceof Object && !proxy.auth) { + netrc.loadSync() + const netrcProxyProps = netrc.machines[proxy.hostname] + if (netrcProxyProps) { + proxy.auth = `${netrcProxyProps.login}:${netrcProxyProps.password}` } -type PfxOption = { filepath: string; password: string } -type AgentOptions = { proxy?: ProxyOption; pfx?: PfxOption } + } -export const getOauthToken = async (domain: string, agentOptions: AgentOptions) => { - const scope = 'k:app_settings:read' - const token = await gyuma({ domain, scope, ...agentOptions }, true) + const token = await gyuma({ domain, scope, proxy, pfx }, true) return token }