Skip to content

Commit

Permalink
added AuthEntry type - references fsprojects#257
Browse files Browse the repository at this point in the history
  • Loading branch information
theimowski committed Oct 14, 2014
1 parent 055275b commit 958da28
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Paket.Core/DependenciesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ type DependenciesFile(fileName,options,packages : PackageRequirement list, remot
| Nuget source ->
match source.Auth with
| None -> yield "source " + source.Url
| Some auth -> yield sprintf "source %s username: \"%s\" password: \"%s\"" source.Url auth.Username auth.Password
| Some auth -> yield sprintf "source %s username: \"%s\" password: \"%s\"" source.Url <| auth.Username.Original <| auth.Password.Original

| LocalNuget source -> yield "source " + source

Expand Down
2 changes: 1 addition & 1 deletion src/Paket.Core/Nuget.fs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ let DownloadPackage(auth, url, name, version, force) =
//client.Credentials <- new NetworkCredential(auth.Username,auth.Password)

//so use THIS instead to send credenatials RIGHT AWAY
let credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth.Username + ":" + auth.Password))
let credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth.Username.Expanded + ":" + auth.Password.Expanded))
request.Headers.[HttpRequestHeader.Authorization] <- String.Format("Basic {0}", credentials)

request.Proxy <- Utils.defaultProxy
Expand Down
4 changes: 2 additions & 2 deletions src/Paket.Core/NugetConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let private readPackageSources(configFile : FileInfo) =
let auth = doc.SelectNodes(sprintf "//packageSourceCredentials/%s" (XmlConvert.EncodeLocalName node.Attributes.["key"].Value))
|> Seq.cast<XmlNode>
|> Seq.firstOrDefault
|> Option.map (fun node -> {Username = node.SelectSingleNode("//add[@key='Username']").Attributes.["value"].Value
Password = node.SelectSingleNode("//add[@key='ClearTextPassword']").Attributes.["value"].Value})
|> Option.map (fun node -> {Username = AuthEntry.Create <| node.SelectSingleNode("//add[@key='Username']").Attributes.["value"].Value
Password = AuthEntry.Create <| node.SelectSingleNode("//add[@key='ClearTextPassword']").Attributes.["value"].Value})
PackageSource.Parse (url, auth)]

let removeFileIfExists file =
Expand Down
4 changes: 2 additions & 2 deletions src/Paket.Core/PackageSourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ let passwordRegex = new Regex("password[:][ ]*[\"]([^\"]+)[\"]", RegexOptions.Ig

let parseAuth(text:string) =
if userNameRegex.IsMatch(text) && passwordRegex.IsMatch(text) then
let expanded = Environment.ExpandEnvironmentVariables(text)
Some { Username = userNameRegex.Match(expanded).Groups.[1].Value; Password = passwordRegex.Match(expanded).Groups.[1].Value }
Some { Username = AuthEntry.Create <| userNameRegex.Match(text).Groups.[1].Value
Password = AuthEntry.Create <| passwordRegex.Match(text).Groups.[1].Value }
else
if text.Contains("username:") || text.Contains("password:") then
failwithf "Could not parse auth in \"%s\"" text
Expand Down
14 changes: 11 additions & 3 deletions src/Paket.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ open System.Net
open System.Xml
open System.Text

type AuthEntry =
{ Original : string
Expanded : string }

static member Create (s : string) =
{ Original = s
Expanded = Environment.ExpandEnvironmentVariables(s) }

type Auth =
{ Username : string
Password : string }
{ Username : AuthEntry
Password : AuthEntry }

/// Creates a directory if it does not exist.
let CreateDir path =
Expand Down Expand Up @@ -63,7 +71,7 @@ let createWebClient(auth:Auth option) =
//client.Credentials <- new NetworkCredential(auth.Username,auth.Password)

//so use THIS instead to send credenatials RIGHT AWAY
let credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth.Username + ":" + auth.Password))
let credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth.Username.Expanded + ":" + auth.Password.Expanded))
client.Headers.[HttpRequestHeader.Authorization] <- String.Format("Basic {0}", credentials)

client.Headers.Add("user-agent", "Paket")
Expand Down
8 changes: 4 additions & 4 deletions tests/Paket.Tests/DependenciesFile/ParserSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ let ``should read config with encapsulated password source``() =
(cfg.Packages |> List.find (fun p -> p.Name = "Rx-Main")).Sources
|> shouldEqual [ PackageSource.Nuget { Url = "http://nuget.org/api/v2"
Auth =
Some { Username = "tatü tata"
Password = "you got hacked!" } } ]
Some { Username = { Original = "tatü tata"; Expanded = "tatü tata" }
Password = { Original = "you got hacked!"; Expanded = "you got hacked!" } } } ]

let configWithPasswordInSingleQuotes = """
source http://nuget.org/api/v2 username: 'tatü tata' password: 'you got hacked!'
Expand Down Expand Up @@ -261,8 +261,8 @@ let ``should read config with password in env variable``() =
(cfg.Packages |> List.find (fun p -> p.Name = "Rx-Main")).Sources
|> shouldEqual [ PackageSource.Nuget { Url = "http://nuget.org/api/v2"
Auth =
Some { Username = "user XYZ"
Password = "pw Love" } } ]
Some { Username = { Original = "%FEED_USERNAME%"; Expanded = "user XYZ"}
Password = { Original = "%FEED_PASSWORD%"; Expanded = "pw Love"} } } ]

let configWithExplicitVersions = """
source "http://nuget.org/api/v2"
Expand Down

0 comments on commit 958da28

Please sign in to comment.