Merge pull request #336 from Jeff-Ciesielski/jeffc/cache_creds

Add credential caching
This commit is contained in:
Dominik Picheta 2017-02-22 18:16:34 +01:00 • committed by GitHub
commit 82dcadeb46
2 changed files with 25 additions and 16 deletions

View file

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

View file

@ -6,7 +6,7 @@
import system except TResult import system except TResult
import httpclient, base64, strutils, rdstdin, json, os, browsers, times, uri import httpclient, base64, strutils, rdstdin, json, os, browsers, times, uri
import tools, common, cli import tools, common, cli, config, options
type type
Auth = object Auth = object
@ -14,6 +14,9 @@ type
pw: string pw: string
token: string ## base64 encoding of user:pw token: string ## base64 encoding of user:pw
const
ApiKeyFile = "api_token"
proc userAborted() = proc userAborted() =
raise newException(NimbleError, "User aborted the process.") raise newException(NimbleError, "User aborted the process.")
@ -22,7 +25,11 @@ proc createHeaders(a: Auth): string =
"Content-Type: application/x-www-form-urlencoded\c\L" & "Content-Type: application/x-www-form-urlencoded\c\L" &
"Accept: */*\c\L") "Accept: */*\c\L")
proc getGithubAuth(): Auth = 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" & display("Info:", "Please create a new personal access token on Github in" &
" order to allow Nimble to fork the packages repository.", " order to allow Nimble to fork the packages repository.",
priority = HighPriority) priority = HighPriority)
@ -34,6 +41,8 @@ proc getGithubAuth(): Auth =
sleep(3000) sleep(3000)
openDefaultBrowser("https://github.com/settings/tokens/new") openDefaultBrowser("https://github.com/settings/tokens/new")
result.token = promptCustom("Personal access token?", "").strip() result.token = promptCustom("Personal access token?", "").strip()
writeFile(cfg.nimbleDir / ApiKeyFile, result.token)
let resp = getContent("https://api.github.com/user", let resp = getContent("https://api.github.com/user",
extraHeaders=createHeaders(result)).parseJson() extraHeaders=createHeaders(result)).parseJson()
@ -131,9 +140,9 @@ proc getPackageOriginUrl(a: Auth): string =
result = "https://" & a.user & ':' & a.pw & '@' & result = "https://" & a.user & ':' & a.pw & '@' &
result["https://".len .. ^1] result["https://".len .. ^1]
proc publish*(p: PackageInfo) = proc publish*(p: PackageInfo, o: Options) =
## Publishes the package p. ## Publishes the package p.
let auth = getGithubAuth() let auth = getGithubAuth(o.config)
var pkgsDir = getTempDir() / "nimble-packages-fork" var pkgsDir = getTempDir() / "nimble-packages-fork"
if not forkExists(auth): if not forkExists(auth):
createFork(auth) createFork(auth)