pkpassmanager.cpp 5.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
    Copyright (C) 2018 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pkpassmanager.h"
19
#include "logging.h"
20

Volker Krause's avatar
Volker Krause committed
21
#include <KPkPass/Pass>
22 23 24 25 26

#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QFile>
27 28
#include <QNetworkAccessManager>
#include <QNetworkReply>
29
#include <QStandardPaths>
30
#include <QTemporaryFile>
31 32 33 34 35
#include <QUrl>
#include <QVector>

PkPassManager::PkPassManager(QObject* parent)
    : QObject(parent)
36
    , m_nam(new QNetworkAccessManager(this))
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
{
}

PkPassManager::~PkPassManager() = default;

QVector<QString> PkPassManager::passes() const
{
    const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes");
    QDir::root().mkpath(basePath);

    QVector<QString> passIds;
    for (QDirIterator topIt(basePath, QDir::NoDotAndDotDot | QDir::Dirs); topIt.hasNext();) {
        for (QDirIterator subIt(topIt.next(), QDir::Files); subIt.hasNext();) {
            QFileInfo fi(subIt.next());
            passIds.push_back(fi.dir().dirName() + QLatin1Char('/') + fi.baseName());
        }
    }

    return passIds;
}

Volker Krause's avatar
Volker Krause committed
58
KPkPass::Pass* PkPassManager::pass(const QString& passId)
59 60
{
    const auto it = m_passes.constFind(passId);
Volker Krause's avatar
Volker Krause committed
61
    if (it != m_passes.constEnd() && it.value()) {
62
        return it.value();
Volker Krause's avatar
Volker Krause committed
63 64 65 66 67 68
    }

    const QString passPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes/") + passId + QLatin1String(".pkpass");
    if (!QFile::exists(passPath)) {
        return nullptr;
    }
69

Volker Krause's avatar
Volker Krause committed
70
    auto file = KPkPass::Pass::fromFile(passPath, this);
71 72 73 74 75 76 77
    // TODO error handling
    m_passes.insert(passId, file);
    return file;
}

void PkPassManager::importPass(const QUrl& url)
{
78 79 80 81 82 83 84 85 86 87 88
    doImportPass(url, Copy);
}

void PkPassManager::importPassFromTempFile(const QString& tmpFile)
{
    doImportPass(QUrl::fromLocalFile(tmpFile), Move);
}

void PkPassManager::doImportPass(const QUrl& url, PkPassManager::ImportMode mode)
{
    qCDebug(Log) << url << mode;
89 90 91 92 93 94
    if (!url.isLocalFile())
        return; // TODO

    const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes");
    QDir::root().mkpath(basePath);

Volker Krause's avatar
Volker Krause committed
95
    std::unique_ptr<KPkPass::Pass> file(KPkPass::Pass::fromFile(url.toLocalFile()));
96 97 98 99 100 101 102 103 104 105 106
    if (!file)
        return; // TODO error handling
    if (file->passTypeIdentifier().isEmpty() || file->serialNumber().isEmpty())
        return; // TODO error handling

    QDir dir(basePath);
    dir.mkdir(file->passTypeIdentifier());
    dir.cd(file->passTypeIdentifier());

    // serialNumber() can contain percent-encoding or slashes,
    // ie stuff we don't want to have in file names
107 108 109 110 111 112 113 114
    const auto serNum = QString::fromUtf8(file->serialNumber().toUtf8().toBase64(QByteArray::Base64UrlEncoding));
    const QString passId = dir.dirName() + QLatin1Char('/') + serNum;

    auto oldPass = pass(passId);
    if (oldPass) {
        QFile::remove(dir.absoluteFilePath(serNum + QLatin1String(".pkpass")));
        m_passes.remove(passId);
    }
115 116 117

    switch (mode) {
        case Move:
118
            QFile::rename(url.toLocalFile(), dir.absoluteFilePath(serNum + QLatin1String(".pkpass")));
119 120
            break;
        case Copy:
121
            QFile::copy(url.toLocalFile(), dir.absoluteFilePath(serNum + QLatin1String(".pkpass")));
122 123 124
            break;
    }

125 126 127 128 129 130 131
    if (oldPass) {
        // TODO check for changes and generate change message
        emit passUpdated(passId);
        oldPass->deleteLater();
    } else {
        emit passAdded(passId);
    }
132 133 134 135 136 137 138 139
}

void PkPassManager::removePass(const QString& passId)
{
    const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes/");
    QFile::remove(basePath + QLatin1Char('/') + passId + QLatin1String(".pkpass"));
    // TODO change signal
}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

void PkPassManager::updatePass(const QString& passId)
{
    auto p = pass(passId);
    if (!p || p->webServiceUrl().isEmpty() || p->authenticationToken().isEmpty())
        return;
    if (p->relevantDate() < QDateTime::currentDateTimeUtc()) // TODO check expiration date and voided property
        return;

    QUrl url(p->webServiceUrl());
    url.setPath(url.path() + QLatin1String("/v1/passes/") + p->passTypeIdentifier() + QLatin1Char('/') + p->serialNumber());
    qCDebug(Log) << "GET" << url;
    QNetworkRequest req(url);
    req.setRawHeader("Authorization", "ApplePass " + p->authenticationToken().toUtf8());
    req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
    auto reply = m_nam->get(req);
    connect(reply, &QNetworkReply::finished, this, [this, reply]() {
        if (reply->error() != QNetworkReply::NoError) {
            qCWarning(Log) << "Failed to download pass:" << reply->errorString();
            return;
        }

        QTemporaryFile tmp;
        tmp.open();
        tmp.write(reply->readAll());
        tmp.close();
        importPassFromTempFile(tmp.fileName());
    });
}

void PkPassManager::updatePasses()
{
    for (const auto &passId : passes())
        updatePass(passId);
}