From 8733e1438b48022cc505ab3e180429efb6c5c49f Mon Sep 17 00:00:00 2001 From: wildart Date: Fri, 13 May 2016 14:41:15 -0400 Subject: [PATCH] introduced SSH credential type for cached credentials --- base/libgit2/types.jl | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/base/libgit2/types.jl b/base/libgit2/types.jl index 9bf806b7a3a36e..c384c2397a6fc4 100644 --- a/base/libgit2/types.jl +++ b/base/libgit2/types.jl @@ -69,7 +69,7 @@ function Base.setindex!(p::AbstractCredentials, val, keys...) ks = Symbol(k) isdefined(p, ks) && setfield!(p, ks, val) end - return nothing + return p end "Checks if credentials were used" checkused!(p::AbstractCredentials) = true @@ -651,24 +651,34 @@ end "Resets authentication failure protection count" reset!(p::UserPasswordCredentials, cnt::Int=3) = (p.count = cnt) -"Field map for cached credentials type" -const CachedCredentialsFieldMap = - Dict(:user=>1, :pass=>2, :pubkey=>3, :prvkey=>4, :usesshagent=>5) +"SSH credentials type" +type SSHCredentials + user::AbstractString + pass::AbstractString + pubkey::AbstractString + prvkey::AbstractString + usesshagent::AbstractString # used for ssh-agent authentication + + SSHCredentials(u::AbstractString,p::AbstractString) = new(u,p,"","","Y") + SSHCredentials() = SSHCredentials("","") +end "Credentials that support caching" type CachedCredentials <: AbstractCredentials - cred::Dict{AbstractString,Vector{AbstractString}} + cred::Dict{AbstractString,SSHCredentials} count::Int # authentication failure protection count - CachedCredentials() = new(Dict{AbstractString,Vector{AbstractString}}(),3) + CachedCredentials() = new(Dict{AbstractString,SSHCredentials}(),3) end "Returns specific credential parameter value: first index is a credential parameter name, second index is a host name (with schema)" function Base.getindex(p::CachedCredentials, keys...) length(keys) != 2 && return nothing key, host = keys - if haskey(p.cred, host) && haskey(CachedCredentialsFieldMap, key) + if haskey(p.cred, host) creds = p.cred[host] - idx = CachedCredentialsFieldMap[key] - isdefined(creds,idx) && return creds[idx] + if isdefined(creds,key) + kval = getfield(creds, key) + !isempty(kval) && return kval + end end return nothing end @@ -676,9 +686,12 @@ end function Base.setindex!(p::CachedCredentials, val, keys...) length(keys) != 2 && return nothing key, host = keys - !haskey(p.cred, host) && (p.cred[host] = Array(AbstractString, length(LibGit2.CachedCredentialsFieldMap))) - haskey(CachedCredentialsFieldMap, key) && return setindex!(p.cred[host], val, CachedCredentialsFieldMap[key]) - return nothing + if !haskey(p.cred, host) + p.cred[host] = SSHCredentials() + end + creds = p.cred[host] + isdefined(creds,key) && setfield!(creds, key, val) + return p end "Checks if credentials were used or failed authentication, see `LibGit2.credentials_callback`" function checkused!(p::CachedCredentials)