mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
test(credentials): add more test cases
This commit is contained in:
parent
26812ffe9c
commit
4948bba773
4 changed files with 162 additions and 120 deletions
|
@ -1,34 +1,9 @@
|
|||
import 'dart:ffi';
|
||||
import 'package:ffi/ffi.dart';
|
||||
import '../error.dart';
|
||||
import '../util.dart';
|
||||
import 'libgit2_bindings.dart';
|
||||
|
||||
/// Create a credential to specify a username.
|
||||
///
|
||||
/// This is used with ssh authentication to query for the username if none is
|
||||
/// specified in the url.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_credential> username(String username) {
|
||||
final out = calloc<Pointer<git_credential>>();
|
||||
final usernameC = username.toNativeUtf8().cast<Int8>();
|
||||
|
||||
final error = libgit2.git_credential_username_new(out, usernameC);
|
||||
|
||||
calloc.free(usernameC);
|
||||
|
||||
if (error < 0) {
|
||||
calloc.free(out);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out.value;
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new plain-text username and password credential object.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_credential> userPass({
|
||||
required String username,
|
||||
required String password,
|
||||
|
@ -37,26 +12,15 @@ Pointer<git_credential> userPass({
|
|||
final usernameC = username.toNativeUtf8().cast<Int8>();
|
||||
final passwordC = password.toNativeUtf8().cast<Int8>();
|
||||
|
||||
final error = libgit2.git_credential_userpass_plaintext_new(
|
||||
out,
|
||||
usernameC,
|
||||
passwordC,
|
||||
);
|
||||
libgit2.git_credential_userpass_plaintext_new(out, usernameC, passwordC);
|
||||
|
||||
calloc.free(usernameC);
|
||||
calloc.free(passwordC);
|
||||
|
||||
if (error < 0) {
|
||||
calloc.free(out);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out.value;
|
||||
}
|
||||
return out.value;
|
||||
}
|
||||
|
||||
/// Create a new passphrase-protected ssh key credential object.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_credential> sshKey({
|
||||
required String username,
|
||||
required String publicKey,
|
||||
|
@ -69,7 +33,7 @@ Pointer<git_credential> sshKey({
|
|||
final privateKeyC = privateKey.toNativeUtf8().cast<Int8>();
|
||||
final passPhraseC = passPhrase.toNativeUtf8().cast<Int8>();
|
||||
|
||||
final error = libgit2.git_credential_ssh_key_new(
|
||||
libgit2.git_credential_ssh_key_new(
|
||||
out,
|
||||
usernameC,
|
||||
publicKeyC,
|
||||
|
@ -82,36 +46,22 @@ Pointer<git_credential> sshKey({
|
|||
calloc.free(privateKeyC);
|
||||
calloc.free(passPhraseC);
|
||||
|
||||
if (error < 0) {
|
||||
calloc.free(out);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out.value;
|
||||
}
|
||||
return out.value;
|
||||
}
|
||||
|
||||
/// Create a new ssh key credential object used for querying an ssh-agent.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_credential> sshKeyFromAgent(String username) {
|
||||
final out = calloc<Pointer<git_credential>>();
|
||||
final usernameC = username.toNativeUtf8().cast<Int8>();
|
||||
|
||||
final error = libgit2.git_credential_ssh_key_from_agent(out, usernameC);
|
||||
libgit2.git_credential_ssh_key_from_agent(out, usernameC);
|
||||
|
||||
calloc.free(usernameC);
|
||||
|
||||
if (error < 0) {
|
||||
calloc.free(out);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out.value;
|
||||
}
|
||||
return out.value;
|
||||
}
|
||||
|
||||
/// Create a new ssh key credential object reading the keys from memory.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_credential> sshKeyFromMemory({
|
||||
required String username,
|
||||
required String publicKey,
|
||||
|
@ -124,7 +74,7 @@ Pointer<git_credential> sshKeyFromMemory({
|
|||
final privateKeyC = privateKey.toNativeUtf8().cast<Int8>();
|
||||
final passPhraseC = passPhrase.toNativeUtf8().cast<Int8>();
|
||||
|
||||
final error = libgit2.git_credential_ssh_key_memory_new(
|
||||
libgit2.git_credential_ssh_key_memory_new(
|
||||
out,
|
||||
usernameC,
|
||||
publicKeyC,
|
||||
|
@ -137,10 +87,5 @@ Pointer<git_credential> sshKeyFromMemory({
|
|||
calloc.free(privateKeyC);
|
||||
calloc.free(passPhraseC);
|
||||
|
||||
if (error < 0) {
|
||||
calloc.free(out);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out.value;
|
||||
}
|
||||
return out.value;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import 'dart:ffi';
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:libgit2dart/libgit2dart.dart';
|
||||
import 'package:libgit2dart/src/util.dart';
|
||||
import '../credentials.dart';
|
||||
import '../callbacks.dart';
|
||||
import '../repository.dart';
|
||||
|
@ -120,21 +121,34 @@ class RemoteCallbacks {
|
|||
int allowedTypes,
|
||||
Pointer<Void> payload,
|
||||
) {
|
||||
final credentialType = credentials!.credentialType;
|
||||
if (allowedTypes & credentialType.value != credentialType.value) {
|
||||
throw ArgumentError('Invalid credential type $credentialType');
|
||||
if (payload.cast<Int8>().value == 2) {
|
||||
libgit2.git_error_set_str(
|
||||
git_error_t.GIT_ERROR_INVALID,
|
||||
'Incorrect credentials.'.toNativeUtf8().cast<Int8>(),
|
||||
);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
}
|
||||
|
||||
if (credentials is Username) {
|
||||
final cred = credentials as Username;
|
||||
credPointer[0] = credentials_bindings.username(cred.username);
|
||||
} else if (credentials is UserPass) {
|
||||
final credentialType = credentials!.credentialType;
|
||||
|
||||
if (allowedTypes & credentialType.value != credentialType.value) {
|
||||
libgit2.git_error_set_str(
|
||||
git_error_t.GIT_ERROR_INVALID,
|
||||
'Invalid credential type $credentialType'.toNativeUtf8().cast<Int8>(),
|
||||
);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
}
|
||||
|
||||
if (credentials is UserPass) {
|
||||
final cred = credentials as UserPass;
|
||||
credPointer[0] = credentials_bindings.userPass(
|
||||
username: cred.username,
|
||||
password: cred.password,
|
||||
);
|
||||
} else if (credentials is Keypair) {
|
||||
payload.cast<Int8>().value++;
|
||||
}
|
||||
|
||||
if (credentials is Keypair) {
|
||||
final cred = credentials as Keypair;
|
||||
credPointer[0] = credentials_bindings.sshKey(
|
||||
username: cred.username,
|
||||
|
@ -142,10 +156,16 @@ class RemoteCallbacks {
|
|||
privateKey: cred.privateKey,
|
||||
passPhrase: cred.passPhrase,
|
||||
);
|
||||
} else if (credentials is KeypairFromAgent) {
|
||||
payload.cast<Int8>().value++;
|
||||
}
|
||||
|
||||
if (credentials is KeypairFromAgent) {
|
||||
final cred = credentials as KeypairFromAgent;
|
||||
credPointer[0] = credentials_bindings.sshKeyFromAgent(cred.username);
|
||||
} else if (credentials is KeypairFromMemory) {
|
||||
payload.cast<Int8>().value++;
|
||||
}
|
||||
|
||||
if (credentials is KeypairFromMemory) {
|
||||
final cred = credentials as KeypairFromMemory;
|
||||
credPointer[0] = credentials_bindings.sshKeyFromMemory(
|
||||
username: cred.username,
|
||||
|
@ -153,6 +173,7 @@ class RemoteCallbacks {
|
|||
privateKey: cred.privateKey,
|
||||
passPhrase: cred.passPhrase,
|
||||
);
|
||||
payload.cast<Int8>().value++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -199,6 +220,8 @@ class RemoteCallbacks {
|
|||
|
||||
if (callbacks.credentials != null) {
|
||||
credentials = callbacks.credentials;
|
||||
final payload = calloc<Int8>()..value = 1;
|
||||
callbacksOptions.payload = payload.cast();
|
||||
callbacksOptions.credentials = Pointer.fromFunction(
|
||||
credentialsCb,
|
||||
except,
|
||||
|
|
|
@ -5,20 +5,6 @@ abstract class Credentials {
|
|||
GitCredential get credentialType;
|
||||
}
|
||||
|
||||
/// Credential with specific username.
|
||||
class Username implements Credentials {
|
||||
const Username(this.username);
|
||||
|
||||
/// The username to authenticate with.
|
||||
final String username;
|
||||
|
||||
@override
|
||||
GitCredential get credentialType => GitCredential.username;
|
||||
|
||||
@override
|
||||
String toString() => 'Username{username: $username}';
|
||||
}
|
||||
|
||||
/// Plain-text username and password credential.
|
||||
class UserPass implements Credentials {
|
||||
const UserPass({required this.username, required this.password});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue