Commit ba0ede92ab8c75f5fe28d5f59b30caee62c0cf64

Authored by Matti Virkkunen
1 parent 191d6034

Store auth token to avoid having to log in every time.

constants.hpp
... ... @@ -16,3 +16,4 @@
16 16 #define LINE_APPLICATION "DESKTOPWIN\t3.2.1.83\tWINDOWS\t5.1.2600-XP-x64"
17 17  
18 18 #define LINE_ACCOUNT_CERTIFICATE "line-certificate"
  19 +#define LINE_ACCOUNT_AUTH_TOKEN "line-auth-token"
... ...
linehttptransport.cpp
... ... @@ -273,6 +273,8 @@ void LineHttpTransport::ssl_input(PurpleSslConnection *, PurpleInputCondition co
273 273 msg += err.reason;
274 274  
275 275 if (err.code == line::ErrorCode::NOT_AUTHORIZED_DEVICE) {
  276 + purple_account_remove_setting(acct, LINE_ACCOUNT_AUTH_TOKEN);
  277 +
276 278 if (err.reason == "AUTHENTICATION_DIVESTED_BY_OTHER_DEVICE") {
277 279 msg = "LINE: You have been logged out because "
278 280 "you logged in from another device.";
... ...
purpleline.hpp
... ... @@ -160,7 +160,8 @@ private:
160 160 // Login process methods, executed in this this order
161 161 void login_start();
162 162  
163   - void got_auth_token(std::string auth_token);
  163 + void get_auth_token();
  164 + void set_auth_token(std::string auth_token);
164 165 void get_last_op_revision();
165 166 void get_profile();
166 167 void get_contacts();
... ...
purpleline_login.cpp
... ... @@ -4,8 +4,57 @@ void PurpleLine::login_start() {
4 4 purple_connection_set_state(conn, PURPLE_CONNECTING);
5 5 purple_connection_update_progress(conn, "Logging in", 0, 3);
6 6  
  7 + std::string auth_token(purple_account_get_string(acct, LINE_ACCOUNT_AUTH_TOKEN, ""));
  8 +
  9 + if (auth_token.size()) {
  10 + // There's a stored authentication token, see if it works
  11 +
  12 + set_auth_token(auth_token);
  13 +
  14 + c_out->send_getLastOpRevision();
  15 + c_out->send([this, auth_token]() {
  16 + int64_t local_rev;
  17 +
  18 + try {
  19 + local_rev = c_out->recv_getLastOpRevision();
  20 + } catch (line::TalkException &err) {
  21 + if (err.code == line::ErrorCode::AUTHENTICATION_FAILED
  22 + || err.code == line::ErrorCode::NOT_AUTHORIZED_DEVICE)
  23 + {
  24 + // Auth token expired, get a new one
  25 +
  26 + purple_debug_info("line", "Existing auth token expired.\n");
  27 +
  28 + purple_account_remove_setting(acct, LINE_ACCOUNT_AUTH_TOKEN);
  29 +
  30 + get_auth_token();
  31 + return;
  32 + }
  33 +
  34 + // Unknown error
  35 + throw;
  36 + }
  37 +
  38 + poller.set_local_rev(local_rev);
  39 +
  40 + set_auth_token(auth_token);
  41 +
  42 + // Already got the last op revision, no need to call get_last_op_revision()
  43 +
  44 + get_profile();
  45 + });
  46 + } else {
  47 + // Get a new auth token
  48 +
  49 + get_auth_token();
  50 + }
  51 +}
  52 +
  53 +void PurpleLine::get_auth_token() {
7 54 std::string certificate(purple_account_get_string(acct, LINE_ACCOUNT_CERTIFICATE, ""));
8 55  
  56 + purple_debug_info("line", "Logging in with credentials to get new auth token.\n");
  57 +
9 58 c_out->send_loginWithIdentityCredentialForCertificate(
10 59 line::IdentityProvider::LINE,
11 60 purple_account_get_username(acct),
... ... @@ -31,10 +80,14 @@ void PurpleLine::login_start() {
31 80  
32 81 if (result.type == line::LoginResultType::SUCCESS && result.authToken != "")
33 82 {
34   - got_auth_token(result.authToken);
  83 + set_auth_token(result.authToken);
  84 +
  85 + get_last_op_revision();
35 86 }
36 87 else if (result.type == line::LoginResultType::REQUIRE_DEVICE_CONFIRM)
37 88 {
  89 + purple_debug_info("line", "Starting PIN verification.\n");
  90 +
38 91 pin_verifier.verify(result, [this](std::string auth_token, std::string certificate) {
39 92 if (certificate != "") {
40 93 purple_account_set_string(
... ... @@ -43,7 +96,9 @@ void PurpleLine::login_start() {
43 96 certificate.c_str());
44 97 }
45 98  
46   - got_auth_token(auth_token);
  99 + set_auth_token(auth_token);
  100 +
  101 + get_last_op_revision();
47 102 });
48 103 }
49 104 else
... ... @@ -59,7 +114,9 @@ void PurpleLine::login_start() {
59 114 });
60 115 }
61 116  
62   -void PurpleLine::got_auth_token(std::string auth_token) {
  117 +void PurpleLine::set_auth_token(std::string auth_token) {
  118 + purple_account_set_string(acct, LINE_ACCOUNT_AUTH_TOKEN, auth_token.c_str());
  119 +
63 120 // Re-open output client to update persistent headers
64 121 c_out->close();
65 122 c_out->set_auto_reconnect(true);
... ... @@ -68,8 +125,6 @@ void PurpleLine::got_auth_token(std::string auth_token) {
68 125 c_out->set_auth_token(auth_token);
69 126 poller.set_auth_token(auth_token);
70 127 http.set_auth_token(auth_token);
71   -
72   - get_last_op_revision();
73 128 }
74 129  
75 130 void PurpleLine::get_last_op_revision() {
... ...