~ruther/NosTale-Gfless

ref: fc51a68e949a505ac95fe9b5e7363245d2b539d8 NosTale-Gfless/NostaleAuth/Api/GameforgeApi.cs -rw-r--r-- 1.9 KiB
fc51a68e — František Boháček Initial commit 4 years ago
                                                                                
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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using NostaleAuth.Extensions;
using NostaleAuth.Models;
using NostaleAuth.Utils;

namespace NostaleAuth.Api
{
    public class GameforgeApi
    {
        private readonly HttpClient _httpClient;

        public GameforgeApi()
        {
            _httpClient = new HttpClient();
        }

        public Guid GenerateIntallationId(string email, string password)
        {
            return Guid.Parse(Cryptography.ToMd5(email + password));
        }

        public async Task<int> GetLatestVersion()
        {
            var request = new GameforgeRequest<int>(HttpMethod.Get, "/patching/download/nostale/default?branchToken");
            Dictionary<string, int> response = await request.Send();

            return response.GetValueOrDefault("latest");
        }

        public async Task<AuthorizedGameforgeApi> Login(string email, string password, Locales locale, Guid installationId)
        {
            var request = new GameforgeRequest<AuthRequest, string>(HttpMethod.Post, "/auth/sessions", installationId);
            
            var authRequest = new AuthRequest
            {
                Locale = locale.Value,
                Email = email,
                Password = password
            };

            Dictionary<string, string> response = await request.Send(authRequest);
            if (response == null)
            {
                return null;
            }
            
            string authToken = response.GetValueOrDefault("token") ?? string.Empty;
            
            return new AuthorizedGameforgeApi(authToken, installationId);
        }

        public Task<AuthorizedGameforgeApi> Login(string email, string password, Locales locale)
        {
            return Login(email, password, locale, GenerateIntallationId(email, password));
        }
    }
}
Do not follow this link