root/src/anilistdotcocredentials.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef ANILISTDOTCOCREDENTIALS_H
#define ANILISTDOTCOCREDENTIALS_H

#include "onlinecredentials.h"
#include "nwutils.h"
#include "config.h"

class AnilistDotCoCredentials : public OnlineCredentials
{
public:
    class AuthToken {
    public:
        QString accessToken;
        QString tokenType; // I don't know what to do with this, yet
        QDateTime expires;
        unsigned int expiresInAsSeconds; // redundant
        QString refreshToken;

        bool isValid();

        void describeAuthenticate(nw::Describer& d);
        void describeRefresh(nw::Describer &d);
    };

    AnilistDotCoCredentials(const BaseConfig& config);

    //////////////////////////////////////
    /// There are 3 Tokens/steps: ("never used the API before" / "using the API right now" / "paused using")
    /// 1) "code" (received from /authorize redirecting to localhost)
    /// 2) "authorization_code" (received from giving the "code" and api-secret to anilist.co)
    ///         You need to send this to authorize every API request you make.
    ///         It runs out after 1 hour, go to step 3.
    /// 3) "refresh_code" comes with the first "authorization_code".
    ///         you must send it every hour to get a new ""authorization_code".
    ///         this brings you back to step 2.
    ///         You should store it to avoid going to step 1 again.
    /////////////////////////////////////////
    /// The first step happens outside in the web-ui.
    /// We must provide a href for the web-ui that takes the user to
    /// anilist.co and asks them to accept our client.
    /// We set /api/online-confirmation/anilist.co/ as redirect-uri to
    /// witch anilist.co brings us back and define an API that calls the next method.
    /////////////////////////////////////////
    virtual const QString connectUri() const; // this should for once not have any side effects
    /////////////////////////////////////////
    /// This method is called by our localhost:8082 server, when anilist.co
    /// redirects the user with the confirmation "code".
    /// We request the actual auth token and our refresh token for the future.
    /////////////////////////////////////////
    virtual bool fetchFirstAuthorizeToken(QString code);
    /////////////////////////////////////////
    /// Once the client is already registered and our first auth token has run out
    /// We must call this method to request a fresh auth token using our stored refresh_key
    /////////////////////////////////////////
    bool refreshAuthorizationToken();

    bool login() { return verifyCredentials(); }
    bool verifyCredentials();
    void setCredentialsForHandle(CurlResult& userdata, CURL* handle) const;

    virtual bool isFresh() { return this->token.isValid(); }
    virtual bool refresh();

    virtual const QString identifierKey() const;
    static const QString IDENTIFIER_KEY;

private:
    AuthToken token;
    QString secret;
    QString redirectUri;
    const BaseConfig& config;

    void writeToken();
    void updateAuthHeader();

    static QDebug log();
    static QDebug err();
};

#endif // ANILISTDOTCOCREDENTIALS_H