~lasall/pidgin-opensteamworks/debian

« back to all changes in this revision

Viewing changes to steam-mobile/libsteam.c

  • Committer: Dominique Lasserre
  • Date: 2013-06-24 22:47:40 UTC
  • mfrom: (36.1.14)
  • Revision ID: lasserre.d@gmail.com-20130624224740-mt1to2tqxqfjjjl8
MergeĀ upstreamĀ changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#if !PURPLE_VERSION_CHECK(3, 0, 0)
7
7
        #define purple_connection_error purple_connection_error_reason
8
8
        #define purple_notify_user_info_add_pair_html purple_notify_user_info_add_pair
9
 
#endif
 
9
#endif
 
10
 
 
11
static const gchar *
 
12
steam_account_get_access_token(SteamAccount *sa) {
 
13
#ifdef TELEPATHY
 
14
        if (sa->cached_access_token)
 
15
                return sa->cached_access_token;
 
16
        return "";
 
17
#else
 
18
        return purple_account_get_string(sa->account, "access_token", "");
 
19
#endif
 
20
}
 
21
 
 
22
static void
 
23
steam_account_set_access_token(SteamAccount *sa, const gchar *access_token) {
 
24
#ifdef TELEPATHY
 
25
        if (access_token != NULL) {
 
26
                g_free(sa->cached_access_token);
 
27
                sa->cached_access_token = g_strdup(access_token);
 
28
                
 
29
                gnome_keyring_store_password(GNOME_KEYRING_NETWORK_PASSWORD,
 
30
                                                                         GNOME_KEYRING_DEFAULT,
 
31
                                                                         _("Steam Mobile OAuth Token"),
 
32
                                                                         access_token,
 
33
                                                                         NULL, NULL, NULL,
 
34
                                                                         "user",                sa->account->username,
 
35
                                                                         "server",              "api.steamcommunity.com",
 
36
                                                                         "protocol",    "steammobile",
 
37
                                                                         "domain",              "libpurple",
 
38
                                                                         NULL);
 
39
        } else {
 
40
                g_free(sa->cached_access_token);
 
41
                sa->cached_access_token = NULL;
 
42
                
 
43
                gnome_keyring_delete_password(GNOME_KEYRING_NETWORK_PASSWORD,
 
44
                                                                          NULL, NULL, NULL,
 
45
                                                                          "user",               sa->account->username,
 
46
                                                                          "server",             "api.steamcommunity.com",
 
47
                                                                          "protocol",   "steammobile",
 
48
                                                                          "domain",             "libpurple",
 
49
                                                                          NULL);
 
50
        }
 
51
#else
 
52
        purple_account_set_string(sa->account, "access_token", access_token);
 
53
#endif
 
54
}
10
55
 
11
56
static const gchar *
12
57
steam_personastate_to_statustype(gint64 state)
104
149
{
105
150
        gchar *steamLogin;
106
151
        
107
 
        steamLogin = g_strconcat(sa->steamid, "||oauth:", purple_account_get_string(sa->account, "access_token", ""), NULL);
 
152
        steamLogin = g_strconcat(sa->steamid, "||oauth:", steam_account_get_access_token(sa), NULL);
108
153
        
109
154
        g_hash_table_replace(sa->cookie_table, g_strdup("forceMobile"), g_strdup("1"));
110
155
        g_hash_table_replace(sa->cookie_table, g_strdup("mobileClient"), g_strdup("ios"));
283
328
        if (userids && userids->str && *userids->str)
284
329
        {
285
330
                GString *url = g_string_new("/ISteamUserOAuth/GetUserSummaries/v0001?");
286
 
                g_string_append_printf(url, "access_token=%s&", purple_url_encode(purple_account_get_string(sa->account, "access_token", "")));
 
331
                g_string_append_printf(url, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
287
332
                g_string_append_printf(url, "steamids=%s", purple_url_encode(userids->str));
288
333
                
289
334
                steam_post_or_get(sa, STEAM_METHOD_GET | STEAM_METHOD_SSL, NULL, url->str, NULL, steam_search_display_results, search_term, TRUE);
302
347
        SteamAccount *sa = user_data;
303
348
        GString *url = g_string_new("/ISteamUserOAuth/Search/v0001?");
304
349
        
305
 
        g_string_append_printf(url, "access_token=%s&", purple_url_encode(purple_account_get_string(sa->account, "access_token", "")));
 
350
        g_string_append_printf(url, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
306
351
        g_string_append_printf(url, "keywords=%s&", purple_url_encode(text));
307
352
        g_string_append(url, "offset=0&");
308
353
        g_string_append(url, "count=50&");
450
495
                method |= STEAM_METHOD_SSL;
451
496
                url = "/ISteamWebUserPresenceOAuth/Poll/v0001";
452
497
                
453
 
                g_string_append_printf(post, "access_token=%s&", purple_url_encode(purple_account_get_string(sa->account, "access_token", "")));
 
498
                g_string_append_printf(post, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
454
499
        } else {
455
500
                g_string_append_printf(post, "steamid=%s&", purple_url_encode(sa->steamid));
456
501
        }
515
560
        g_return_if_fail(sa && who && *who);
516
561
        
517
562
        url = g_string_new("/ISteamUserOAuth/GetUserSummaries/v0001?");
518
 
        g_string_append_printf(url, "access_token=%s&", purple_url_encode(purple_account_get_string(sa->account, "access_token", "")));
 
563
        g_string_append_printf(url, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
519
564
        g_string_append_printf(url, "steamids=%s", purple_url_encode(who));
520
565
        
521
566
        steam_post_or_get(sa, STEAM_METHOD_GET | STEAM_METHOD_SSL, NULL, url->str, NULL, steam_got_friend_summaries, NULL, TRUE);
575
620
{
576
621
        GString *url = g_string_new("/ISteamUserOAuth/GetFriendList/v0001?");
577
622
        
578
 
        g_string_append_printf(url, "access_token=%s&", purple_url_encode(purple_account_get_string(sa->account, "access_token", "")));
 
623
        g_string_append_printf(url, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
579
624
        g_string_append_printf(url, "steamid=%s&", purple_url_encode(sa->steamid));
580
625
        g_string_append(url, "relationship=friend,requestrecipient");
581
626
        
692
737
{
693
738
        gchar *postdata;
694
739
        
695
 
        postdata = g_strdup_printf("access_token=%s", purple_url_encode(purple_account_get_string(sa->account, "access_token", "")));
 
740
        postdata = g_strdup_printf("access_token=%s", purple_url_encode(steam_account_get_access_token(sa)));
696
741
        steam_post_or_get(sa, STEAM_METHOD_POST | STEAM_METHOD_SSL, NULL, "/ISteamWebUserPresenceOAuth/Logon/v0001", postdata, steam_login_access_token_cb, NULL, TRUE);
697
742
        g_free(postdata);
698
743
}
709
754
static void
710
755
steam_login_cb(SteamAccount *sa, JsonObject *obj, gpointer user_data)
711
756
{
712
 
        if(json_object_has_member(obj, "access_token"))
713
 
        {
714
 
                purple_account_set_string(sa->account, "access_token", json_object_get_string_member(obj, "access_token"));
715
 
                steam_login_with_access_token(sa);
716
 
        } else 
717
 
        {
718
 
                const gchar *x_errorcode = json_object_get_string_member(obj, "x_errorcode");
719
 
                const gchar *error_description = json_object_get_string_member(obj, "error_description");
720
 
                if (g_str_equal(x_errorcode, "steamguard_code_required"))
 
757
//{"success":true,"redirect_uri":"steammobile:\/\/mobileloginsucceeded","login_complete":true,"oauth":"{\"steamid\":\"id\",\"oauth_token\":\"oauthtoken\",\"webcookie\":\"webcookie\"}"}
 
758
//{"success":false,"captcha_needed":false,"captcha_gid":-1,"message":"Incorrect login"}
 
759
//{"success":false,"message":"SteamGuard","emailauth_needed":true,"emaildomain":"domain","emailsteamid":"id"}
 
760
//{"success":false,"message":"Error verifying humanity","captcha_needed":true,"captcha_gid":"1587796635006345822"}
 
761
        if(json_object_get_boolean_member(obj, "success"))
 
762
        {
 
763
                JsonParser *parser = json_parser_new();
 
764
                const gchar *oauthjson = json_object_get_string_member(obj, "oauth");
 
765
                
 
766
                if (!json_parser_load_from_data(parser, oauthjson, strlen(oauthjson), NULL))
 
767
                {
 
768
                        purple_debug_error("steam", "Error parsing response: %s\n", oauthjson);
 
769
                        purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, "JSON decoding error");
 
770
                } else {
 
771
                        JsonNode *root = json_parser_get_root(parser);
 
772
                        JsonObject *oauthobj = json_node_get_object(root);
 
773
                        
 
774
                        steam_account_set_access_token(sa, json_object_get_string_member(oauthobj, "oauth_token"));
 
775
                        steam_login_with_access_token(sa);
 
776
                }
 
777
                g_object_unref(parser);
 
778
        } else
 
779
        {
 
780
                const gchar *error_description = json_object_get_string_member(obj, "message");
 
781
                if (json_object_get_boolean_member(obj, "emailauth_needed"))
721
782
                {
722
783
                        purple_request_input(NULL, NULL, _("Set your Steam Guard Code"),
723
784
                                                _("Copy the Steam Guard Code you will have received in your email"), NULL,
724
785
                                                FALSE, FALSE, "Steam Guard Code", _("OK"),
725
786
                                                G_CALLBACK(steam_set_steam_guard_token_cb), _("Cancel"),
726
787
                                                NULL, sa->account, NULL, NULL, sa->account);
 
788
                        purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, error_description);
 
789
                } else if (json_object_get_boolean_member(obj, "captcha_needed"))
 
790
                {
 
791
                        const gchar *captcha_gid = json_object_get_string_member(obj, "captcha_gid");
 
792
                        //https://steamcommunity.com/public/captcha.php?gid=%s captcha_gid
 
793
                        //TODO
727
794
                        purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, error_description);
728
 
                } else if (g_str_equal(x_errorcode, "incorrect_login"))
 
795
                } else
729
796
                {
730
797
                        purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, error_description);
731
 
                } else {
732
 
                        purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, error_description);
733
798
                }
734
799
        }
735
800
}
736
801
 
 
802
#include "steam_rsa.c"
 
803
 
 
804
static void
 
805
steam_login_got_rsakey(SteamAccount *sa, JsonObject *obj, gpointer user_data)
 
806
{
 
807
        //{"success":true,"publickey_mod":"pubkeyhex","publickey_exp":"pubkeyhex","timestamp":"165685150000"}
 
808
        GString *post = NULL;
 
809
        gchar *encrypted_password;
 
810
        PurpleAccount *account;
 
811
        
 
812
        if(!json_object_get_boolean_member(obj, "success"))
 
813
        {
 
814
                purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_INVALID_USERNAME, _("Invalid username"));
 
815
                return;
 
816
        }
 
817
        
 
818
        account = sa->account;
 
819
        encrypted_password = steam_encrypt_password(
 
820
                                                        json_object_get_string_member(obj, "publickey_mod"),
 
821
                                                        json_object_get_string_member(obj, "publickey_exp"),
 
822
                                                        account->password);
 
823
        
 
824
        purple_debug_misc("steam", "Encrypted password is %s\n", encrypted_password);
 
825
        
 
826
        if (!encrypted_password)
 
827
        {
 
828
                purple_connection_error(sa->pc,
 
829
                                                                PURPLE_CONNECTION_ERROR_ENCRYPTION_ERROR,
 
830
                                                                _("Unable to RSA encrypt the password"));
 
831
                return;
 
832
        }
 
833
        
 
834
        post = g_string_new(NULL);
 
835
        g_string_append_printf(post, "password=%s&", purple_url_encode(encrypted_password));
 
836
        g_string_append_printf(post, "username=%s&", purple_url_encode(account->username));
 
837
        g_string_append_printf(post, "emailauth=%s&", purple_url_encode(purple_account_get_string(account, "steam_guard_code", "")));
 
838
        g_string_append(post, "oauth_client_id=3638BFB1&");
 
839
        g_string_append(post, "oauth_scope=read_profile write_profile read_client write_client&");
 
840
        g_string_append(post, "captchagid=-1&");
 
841
        g_string_append_printf(post, "rsatimestamp=%s", purple_url_encode(json_object_get_string_member(obj, "timestamp")));
 
842
        
 
843
        purple_debug_misc("steam", "Postdata: %s\n", post->str);
 
844
        
 
845
        steam_post_or_get(sa, STEAM_METHOD_POST | STEAM_METHOD_SSL, "steamcommunity.com", "/mobilelogin/dologin", post->str, steam_login_cb, NULL, TRUE);
 
846
        g_string_free(post, TRUE);
 
847
        
 
848
        g_free(encrypted_password);
 
849
}
 
850
 
 
851
#ifdef TELEPATHY
 
852
static void
 
853
steam_keyring_got_password(GnomeKeyringResult res, const gchar* access_token, gpointer user_data) {
 
854
        SteamAccount *sa = user_data;
 
855
        
 
856
        if (access_token && *access_token)
 
857
        {
 
858
                sa->cached_access_token = g_strdup(password);
 
859
        
 
860
                steam_login_with_access_token(sa);
 
861
        } else
 
862
        {
 
863
                gchar *url = g_strdup_printf("/mobilelogin/getrsakey?username=%s", purple_url_encode(sa->account->username));
 
864
                steam_post_or_get(sa, STEAM_METHOD_GET | STEAM_METHOD_SSL, "steamcommunity.com", url, NULL, steam_login_got_rsakey, NULL, TRUE);
 
865
                g_free(url);
 
866
        }
 
867
}
 
868
#endif
 
869
 
737
870
static void
738
871
steam_login(PurpleAccount *account)
739
872
{
756
889
        sa->sent_messages_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
757
890
        sa->waiting_conns = g_queue_new();
758
891
 
759
 
        
 
892
#ifdef TELEPATHY
 
893
        gnome_keyring_find_password(GNOME_KEYRING_NETWORK_PASSWORD,
 
894
                                                                  steam_keyring_got_password, sa, NULL,
 
895
                                                                  "user",               account->username,
 
896
                                                                  "server",             "api.steamcommunity.com",
 
897
                                                                  "protocol",   "steammobile",
 
898
                                                                  "domain",             "libpurple",
 
899
                                                                  NULL);
 
900
#else
760
901
        if (purple_account_get_string(account, "access_token", NULL))
761
902
        {
762
903
                steam_login_with_access_token(sa);
763
904
        } else
764
905
        {
765
 
                GString *post = g_string_new(NULL);
766
 
                g_string_append(post, "client_id=3638BFB1&");
767
 
                g_string_append(post, "grant_type=password&");
768
 
                g_string_append_printf(post, "username=%s&", purple_url_encode(account->username));
769
 
                g_string_append_printf(post, "password=%s&", purple_url_encode(account->password));
770
 
                g_string_append_printf(post, "x_emailauthcode=%s&", purple_url_encode(purple_account_get_string(account, "steam_guard_code", "")));
771
 
                g_string_append(post, "x_webcookie=&");
772
 
                g_string_append(post, "scope=read_profile write_profile read_client write_client");
773
 
                steam_post_or_get(sa, STEAM_METHOD_POST | STEAM_METHOD_SSL, NULL, "/ISteamOAuth2/GetTokenWithCredentials/v0001", post->str, steam_login_cb, NULL, TRUE);
774
 
                g_string_free(post, TRUE);
 
906
                gchar *url = g_strdup_printf("/mobilelogin/getrsakey?username=%s", purple_url_encode(account->username));
 
907
                steam_post_or_get(sa, STEAM_METHOD_GET | STEAM_METHOD_SSL, "steamcommunity.com", url, NULL, steam_login_got_rsakey, NULL, TRUE);
 
908
                g_free(url);
775
909
        }
 
910
#endif
776
911
        
777
912
        purple_connection_set_state(pc, PURPLE_CONNECTING);
778
913
        purple_connection_update_progress(pc, _("Connecting"), 1, 3);
790
925
        
791
926
        // Go offline on the website
792
927
        post = g_string_new(NULL);
793
 
        g_string_append_printf(post, "access_token=%s&", purple_url_encode(purple_account_get_string(sa->account, "access_token", "")));
 
928
        g_string_append_printf(post, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
794
929
        g_string_append_printf(post, "umqid=%s&", purple_url_encode(sa->umqid));
795
930
        steam_post_or_get(sa, STEAM_METHOD_POST | STEAM_METHOD_SSL, NULL, "/ISteamWebUserPresenceOAuth/Logoff/v0001", post->str, NULL, NULL, TRUE);
796
931
        g_string_free(post, TRUE);
822
957
        g_hash_table_destroy(sa->cookie_table);
823
958
        g_hash_table_destroy(sa->hostname_ip_cache);
824
959
        
 
960
#ifdef TELEPATHY
 
961
        g_free(sa->cached_access_token);
 
962
#endif
825
963
        g_free(sa->umqid);
826
964
        g_free(sa);
827
965
}
830
968
steam_send_typing(PurpleConnection *pc, const gchar *name, PurpleTypingState state)
831
969
{
832
970
        SteamAccount *sa = pc->proto_data;
833
 
        PurpleAccount *account = sa->account;
834
971
        if (state == PURPLE_TYPING)
835
972
        {
836
973
                GString *post = g_string_new(NULL);
837
974
                
838
 
                g_string_append_printf(post, "access_token=%s&", purple_url_encode(purple_account_get_string(account, "access_token", "")));
 
975
                g_string_append_printf(post, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
839
976
                g_string_append_printf(post, "umqid=%s&", purple_url_encode(sa->umqid));
840
977
                g_string_append(post, "type=typing&");
841
978
                g_string_append_printf(post, "steamid_dst=%s", name);
879
1016
        
880
1017
        post = g_string_new(NULL);
881
1018
        
882
 
        g_string_append_printf(post, "access_token=%s&", purple_url_encode(purple_account_get_string(account, "access_token", "")));
 
1019
        g_string_append_printf(post, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
883
1020
        g_string_append_printf(post, "umqid=%s&", purple_url_encode(sa->umqid));
884
1021
        g_string_append(post, "type=personastate&");
885
1022
        g_string_append_printf(post, "persona_state=%u", state_id);
900
1037
                PurpleMessageFlags flags)
901
1038
{
902
1039
        SteamAccount *sa = pc->proto_data;
903
 
        PurpleAccount *account = sa->account;
904
1040
        GString *post = g_string_new(NULL);
905
1041
        gchar *stripped;
906
1042
        
907
 
        g_string_append_printf(post, "access_token=%s&", purple_url_encode(purple_account_get_string(account, "access_token", "")));
 
1043
        g_string_append_printf(post, "access_token=%s&", purple_url_encode(steam_account_get_access_token(sa)));
908
1044
        g_string_append_printf(post, "umqid=%s&", purple_url_encode(sa->umqid));
909
1045
        
910
1046
        stripped = purple_markup_strip_html(msg);