-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |