Add credential caching

This change stores the user's github API token in ~/.nimble/api_token for
ease of publishing.

closes https://github.com/nim-lang/nimble/issues/307
This commit is contained in:
Jeff Ciesielski 2017-02-21 22:14:08 -05:00
commit c13a1c8977
2 changed files with 25 additions and 16 deletions

View file

@ -1028,7 +1028,7 @@ proc doAction(options: Options) =
init(options)
of actionPublish:
var pkgInfo = getPkgInfo(getCurrentDir(), options)
publish(pkgInfo)
publish(pkgInfo, options)
of actionDump:
dump(options)
of actionTasks:

View file

@ -6,7 +6,7 @@
import system except TResult
import httpclient, base64, strutils, rdstdin, json, os, browsers, times, uri
import tools, common, cli
import tools, common, cli, config, options
type
Auth = object
@ -14,6 +14,9 @@ type
pw: string
token: string ## base64 encoding of user:pw
const
ApiKeyFile = "api_token"
proc userAborted() =
raise newException(NimbleError, "User aborted the process.")
@ -22,18 +25,24 @@ proc createHeaders(a: Auth): string =
"Content-Type: application/x-www-form-urlencoded\c\L" &
"Accept: */*\c\L")
proc getGithubAuth(): Auth =
display("Info:", "Please create a new personal access token on Github in" &
" order to allow Nimble to fork the packages repository.",
priority = HighPriority)
display("Hint:", "Make sure to give the access token access to public repos" &
" (public_repo scope)!", Warning, HighPriority)
sleep(5000)
display("Info:", "Your default browser should open with the following URL: " &
"https://github.com/settings/tokens/new", priority = HighPriority)
sleep(3000)
openDefaultBrowser("https://github.com/settings/tokens/new")
result.token = promptCustom("Personal access token?", "").strip()
proc getGithubAuth(cfg: Config): Auth =
try:
result.token = readFile(cfg.nimbleDir / ApiKeyFile)
except IOError:
display("Info:", "Please create a new personal access token on Github in" &
" order to allow Nimble to fork the packages repository.",
priority = HighPriority)
display("Hint:", "Make sure to give the access token access to public repos" &
" (public_repo scope)!", Warning, HighPriority)
sleep(5000)
display("Info:", "Your default browser should open with the following URL: " &
"https://github.com/settings/tokens/new", priority = HighPriority)
sleep(3000)
openDefaultBrowser("https://github.com/settings/tokens/new")
result.token = promptCustom("Personal access token?", "").strip()
writeFile(cfg.nimbleDir / ApiKeyFile, result.token)
let resp = getContent("https://api.github.com/user",
extraHeaders=createHeaders(result)).parseJson()
@ -131,9 +140,9 @@ proc getPackageOriginUrl(a: Auth): string =
result = "https://" & a.user & ':' & a.pw & '@' &
result["https://".len .. ^1]
proc publish*(p: PackageInfo) =
proc publish*(p: PackageInfo, o: Options) =
## Publishes the package p.
let auth = getGithubAuth()
let auth = getGithubAuth(o.config)
var pkgsDir = getTempDir() / "nimble-packages-fork"
if not forkExists(auth):
createFork(auth)