Skip to content

Commit

Permalink
feat: OAuth時のproxy認証、クライアント証明書認証に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
the-red committed Sep 9, 2022
1 parent 158a046 commit 35a97ed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
23 changes: 11 additions & 12 deletions src/generator/dts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ export const run: Runner = async (config) => {
const distDir = 'dts'
mkdirSync(distDir, { recursive: true })

let connection: Record<string, string | undefined> = { 'base-url': `https://${context.host}` }
const connection: Record<string, string | undefined> = { '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 || []
Expand Down
6 changes: 5 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { ProxyOption, PfxOption } from 'gyuma'

type _Context<Env> = {
env: Env
host: string
appId: Record<string, number>
oauth?: boolean
oauth?: boolean | { scope: string }
proxy?: ProxyOption
pfx?: PfxOption
}

export type Config<Env extends string = string, Context extends _Context<Env> = _Context<Env>> = {
Expand Down
35 changes: 22 additions & 13 deletions src/oauth.ts
Original file line number Diff line number Diff line change
@@ -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<string> => {
// プロキシサーバーの認証情報が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
}

0 comments on commit 35a97ed

Please sign in to comment.