File indexing completed on 2024-04-28 16:01:31

0001 /******************************************************************************
0002  * This file is part of the libqgit2 library
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0017  */
0018 
0019 #include "qgitcredentials.h"
0020 #include "credentials_p.h"
0021 
0022 #include "private/pathcodec.h"
0023 
0024 #include "git2.h"
0025 
0026 namespace LibQGit2
0027 {
0028 
0029 CredentialsPrivate::CredentialsPrivate(unsigned int allowedTypes) :
0030     m_allowed_types(allowedTypes)
0031 {
0032 }
0033 
0034 CredentialsPrivate::~CredentialsPrivate() {}
0035 
0036 int CredentialsPrivate::create(git_credential**, const char*, const char*, unsigned int)
0037 {
0038     return -1;
0039 }
0040 
0041 int CredentialsPrivate::create(Credentials &credentials, git_credential **cred, const char *url, const char *usernameFromUrl, unsigned int allowedTypes)
0042 {
0043     CredentialsPrivate *d = credentials.d_func();
0044 
0045     int result = -1;
0046     if ((allowedTypes & d->m_allowed_types)) {
0047         result = d->create(cred, url, usernameFromUrl, allowedTypes);
0048     }
0049 
0050     return result;
0051 }
0052 
0053 
0054 struct SSHCredentialsPrivate : public CredentialsPrivate {
0055     SSHCredentialsPrivate(const QString &privateKeyPath, const QString &publicKeyPath, const QByteArray &userName, const QByteArray &passphrase) :
0056         CredentialsPrivate(GIT_CREDTYPE_SSH_KEY | GIT_CREDTYPE_USERNAME),
0057         m_private_key_path(PathCodec::toLibGit2(privateKeyPath)),
0058         m_public_key_path(PathCodec::toLibGit2(publicKeyPath)),
0059         m_user_name(userName),
0060         m_passphrase(passphrase)
0061     {
0062     }
0063 
0064 protected:
0065     int create(git_credential **cred, const char*, const char*, unsigned int allowedTypes)
0066     {
0067         if (allowedTypes & GIT_CREDTYPE_USERNAME) {
0068             return git_cred_username_new(cred, m_user_name.data());
0069         }
0070 
0071         if (allowedTypes & GIT_CREDTYPE_SSH_KEY) {
0072             return git_cred_ssh_key_new(cred, m_user_name.data(), m_public_key_path.data(), m_private_key_path.data(), m_passphrase.data());
0073         }
0074 
0075         return -1;
0076     }
0077 
0078 private:
0079     const QByteArray m_private_key_path;
0080     const QByteArray m_public_key_path;
0081     const QByteArray m_user_name;
0082     const QByteArray m_passphrase;
0083 };
0084 
0085 
0086 Credentials::Credentials() :
0087     d_ptr(new CredentialsPrivate(0))
0088 {
0089 }
0090 
0091 Credentials::Credentials(CredentialsPrivate &p) :
0092     d_ptr(&p)
0093 {
0094 }
0095 
0096 bool Credentials::isEmpty() const
0097 {
0098     return d_ptr.isNull();
0099 }
0100 
0101 Credentials Credentials::ssh(const QString &privateKeyPath, const QString &publicKeyPath, const QByteArray &userName, const QByteArray &passphrase)
0102 {
0103     return Credentials(*new SSHCredentialsPrivate(privateKeyPath, publicKeyPath, userName, passphrase));
0104 }
0105 
0106 }