pkpassmanager.cpp 6 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);

95 96
    std::unique_ptr<KPkPass::Pass> newPass(KPkPass::Pass::fromFile(url.toLocalFile()));
    if (!newPass)
97
        return; // TODO error handling
98
    if (newPass->passTypeIdentifier().isEmpty() || newPass->serialNumber().isEmpty())
99 100 101
        return; // TODO error handling

    QDir dir(basePath);
102 103
    dir.mkdir(newPass->passTypeIdentifier());
    dir.cd(newPass->passTypeIdentifier());
104 105 106

    // serialNumber() can contain percent-encoding or slashes,
    // ie stuff we don't want to have in file names
107
    const auto serNum = QString::fromUtf8(newPass->serialNumber().toUtf8().toBase64(QByteArray::Base64UrlEncoding));
108 109 110 111 112 113 114
    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
    if (oldPass) {
126 127 128 129 130 131 132 133 134 135
        // check for changes and generate change message
        QStringList changes;
        for (const auto &f : newPass->fields()) {
            const auto prevValue = oldPass->field(f.key()).value();
            const auto curValue = f.value();
            if (curValue != prevValue) {
                changes.push_back(f.changeMessage());
            }
        }
        emit passUpdated(passId, changes);
136 137 138 139
        oldPass->deleteLater();
    } else {
        emit passAdded(passId);
    }
140 141 142 143 144 145
}

void PkPassManager::removePass(const QString& passId)
{
    const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes/");
    QFile::remove(basePath + QLatin1Char('/') + passId + QLatin1String(".pkpass"));
Volker Krause's avatar
Volker Krause committed
146
    emit passRemoved(passId);
147
    delete m_passes.take(passId);
148
}
149 150 151 152 153 154

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

158
    QNetworkRequest req(p->passUpdateUrl());
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    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);
}
181 182 183 184 185 186 187 188

QDateTime PkPassManager::relevantDate(KPkPass::Pass *pass)
{
    const auto dt = pass->relevantDate();
    if (dt.isValid())
        return dt;
    return pass->expirationDate();
}