~ruther/NosTale-Gfless

NosTale-Gfless/NostaleAuth/Api/AuthorizedGameforgeApi.cs -rw-r--r-- 1.9 KiB
4f5eef52 — František Boháček chore: add license 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
61
62
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using NostaleAuth.Extensions;
using NostaleAuth.Models;

namespace NostaleAuth.Api
{
    public class AuthorizedGameforgeApi
    {
        private List<GameforgeAccount> _accounts;
        
        public AuthorizedGameforgeApi(string authToken, Guid installationId)
        {
            AuthToken = authToken;
            InstallationId = installationId;
        }
        
        public Guid InstallationId { get; set; }
        
        public string AuthToken { get; }
        
        public async Task<IEnumerable<GameforgeAccount>> GetAccounts(bool cache = true)
        {
            if (cache && _accounts != null)
            {
                return _accounts;
            }
            
            var request = new GameforgeRequest<GameforgeAccount>(HttpMethod.Get, "/user/accounts", InstallationId, AuthToken);
            Dictionary<string, GameforgeAccount> response = await request.Send();

            if (response == null)
            {
                return new List<GameforgeAccount>();
            }
            
            _accounts = new List<GameforgeAccount>(response.Values);
            return _accounts;
        }

        public async Task<string> GetSessionToken(GameforgeAccount gameforgeAccount, bool raw = false)
        {
            var request = new GameforgeRequest<SessionRequest, string>(HttpMethod.Post, "/auth/thin/codes", InstallationId, AuthToken);
            var sessionRequest = new SessionRequest
            {
                PlatformGameAccountId = gameforgeAccount.Id
            };
            
            Dictionary<string, string> response = await request.Send(sessionRequest);

            if (response == null)
            {
                return string.Empty;
            }

            string data = (response.GetValueOrDefault("code") ?? string.Empty);
            return raw ? data : data.ToHex();
        }
    }
}
Do not follow this link