Skip to content

Commit

Permalink
introduced SSH credential type for cached credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
wildart committed May 13, 2016
1 parent a15a013 commit 8733e14
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions base/libgit2/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -651,34 +651,47 @@ 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
"Sets specific credential parameter value: first index is a credential parameter name, second index is a host name (with schema)"
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)
Expand Down

0 comments on commit 8733e14

Please sign in to comment.