Skip to content

Commit

Permalink
fixed typos, documentation & test
Browse files Browse the repository at this point in the history
  • Loading branch information
wildart committed May 18, 2016
1 parent 1e7fa83 commit 2e81564
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
28 changes: 17 additions & 11 deletions base/libgit2/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ end
"""Credentials callback function
Function provides different credential acquisition functionality w.r.t. a connection protocol.
If payload is provided then `payload_ptr` should contain `LibGit2.AbstractPayload` object.
If payload is provided then `payload_ptr` should contain `LibGit2.AbstractCredentials` object.
For `LibGit2.Consts.CREDTYPE_USERPASS_PLAINTEXT` type, if payload contains fields:
`user` & `pass` they are used to create authentication credentials.
In addition, if payload has field `used` it can be set to `true` to indicate that payload was used
and abort callback with error. This behavior is required to avoid repeated authentication calls with incorrect credentials.
`user` & `pass`, they are used to create authentication credentials.
Empty `user` name and `pass`word trigger authentication error.
For `LibGit2.Consts.CREDTYPE_SSH_KEY` type, if payload contains fields:
`user`, `prvkey`, `pubkey` & `pass`, they are used to create authentication credentials.
Empty `user` name triggers authentication error.
Order of credential checks (if supported):
- ssh key pair (ssh-agent if specified in `payload`)
- ssh key pair (ssh-agent if specified in payload's `usesshagent` field)
- plain text
**Note**: Due to the specifics of `libgit2` authentication procedure, when authentication fails,
this functions is called again without any indication whether authentication was successful or not.
this function is called again without any indication whether authentication was successful or not.
To avoid an infinite loop from repeatedly using the same faulty credentials,
use a call counting strategy to counteract this behaviour.
`checkused!` function can be called to test if credentials were used if call returns `true` value.
Used credentials trigger user prompt for (re)entering required information.
`UserPasswordCredentials` and `CachedCredentials` are implemented using a call counting strategy
that prevents repeated usage of faulty credentials.
"""
function credentials_callback(cred::Ptr{Ptr{Void}}, url_ptr::Cstring,
username_ptr::Cstring,
Expand All @@ -55,7 +61,7 @@ function credentials_callback(cred::Ptr{Ptr{Void}}, url_ptr::Cstring,
host = urlparts.captures[5]
schema = schema === nothing ? "" : schema*"://"

# get credentials object form payload ref
# get credentials object from payload pointer
creds = EmptyCredentials()
if payload_ptr != C_NULL
tmpobj = unsafe_pointer_to_objref(payload_ptr)
Expand All @@ -69,8 +75,8 @@ function credentials_callback(cred::Ptr{Ptr{Void}}, url_ptr::Cstring,
if isset(allowed_types, Cuint(Consts.CREDTYPE_SSH_KEY))
credid = "ssh://$host"

# set ssh-agent triger for first use
if creds[:usesshagent, credid] == nothing
# set ssh-agent trigger for first use
if creds[:usesshagent, credid] === nothing
creds[:usesshagent, credid] = "Y"
end

Expand Down Expand Up @@ -147,7 +153,7 @@ function credentials_callback(cred::Ptr{Ptr{Void}}, url_ptr::Cstring,
end
creds[:pass, credid] = passphrase # save credentials

isempty(username) && return Cint(Error.EAUTH)
isempty(username) && return Cint(Error.EAUTH)

err = ccall((:git_cred_ssh_key_new, :libgit2), Cint,
(Ptr{Ptr{Void}}, Cstring, Cstring, Cstring, Cstring),
Expand Down
36 changes: 19 additions & 17 deletions test/libgit2-online.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,30 @@ mktempdir() do dir
finalize(repo)
end
#end
try

#@testset "with incorrect url" begin
repo_path = joinpath(dir, "Example2")
# credential are required because github try authenticate on unknown repo
cred = LibGit2.UserPasswordCredentials("","") # empty credentials cause authentication error
LibGit2.clone(https_prefix*repo_url*randstring(10), repo_path, payload=Nullable(cred))
error("unexpected")
try
repo_path = joinpath(dir, "Example2")
# credentials are required because github tries to authenticate on unknown repo
cred = LibGit2.UserPasswordCredentials("","") # empty credentials cause authentication error
LibGit2.clone(https_prefix*repo_url*randstring(10), repo_path, payload=Nullable(cred))
error("unexpected")
catch ex
@test isa(ex, LibGit2.Error.GitError)
@test ex.code == LibGit2.Error.EAUTH
end
#end
catch ex
@test isa(ex, LibGit2.Error.GitError)
@test ex.code == LibGit2.Error.EAUTH
end

try
#@testset "with 'ssh' protocol (by default is not supported)" begin
#TODO: remove or condition on libgit2 features this test when ssh protocol will be supported
#@testset "with 'ssh' protocol (by default is not supported)" begin
try
repo_path = joinpath(dir, "Example3")
@test_throws LibGit2.Error.GitError LibGit2.clone(ssh_prefix*repo_url, repo_path)
#end
catch ex
# but we cloned succesfully, so check that repo was created
ex.fail == 1 && @test isdir(joinpath(path, ".git"))
end
catch ex
# but we cloned succesfully, so check that repo was created
ex.fail == 1 && @test isdir(joinpath(path, ".git"))
end
#end
#end
end

Expand Down

0 comments on commit 2e81564

Please sign in to comment.