Class: MTProto::Type::RPC::Auth::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/type/rpc/auth/authorization.rb

Constant Summary collapse

CONSTRUCTOR =
0x2ea2c0d4
CONSTRUCTOR_SIGN_UP_REQUIRED =
0x44747e9a

Class Method Summary collapse

Class Method Details

.parse(response) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mtproto/type/rpc/auth/authorization.rb', line 14

def self.parse(response)
  constructor = response[0, 4].unpack1('L<')

  if constructor == Type::GzipPacked::CONSTRUCTOR
    response = Type::GzipPacked.unpack(response)
    constructor = response[0, 4].unpack1('L<')
  end

  if constructor == Type::RpcError::CONSTRUCTOR
    error = Type::RpcError.deserialize(response)
    raise MTProto::RpcError.new(error.error_code, error.error_message)
  end

  if constructor == 
    return { authorization: nil, sign_up_required: true }
  end

  if constructor == CONSTRUCTOR
    offset = 4

    # Parse flags
    flags = response[offset, 4].unpack1('L<')
    offset += 4

    # tmp_sessions:flags.0?int
    if (flags & (1 << 0)) != 0
      offset += 4
    end

    # otherwise_relogin_days:flags.1?int
    if (flags & (1 << 1)) != 0
      offset += 4
    end

    # future_auth_token:flags.2?bytes
    if (flags & (1 << 2)) != 0
      first_byte = response[offset].unpack1('C')

      if first_byte < 254
        # Short format: 1 byte length + data + padding
        token_len = first_byte
        offset += 1 + token_len
        padding = (4 - ((1 + token_len) % 4)) % 4
        offset += padding
      else
        # Long format: 0xfe + 3 bytes length + data + padding
        offset += 1
        token_len = response[offset, 3].unpack('CCC').inject(0) {|sum, b| (sum << 8) + b}
        offset += 3 + token_len
        padding = (4 - ((4 + token_len) % 4)) % 4
        offset += padding
      end
    end

    # Parse User object
    # user#20b1422 flags:# flags2:# id:long access_hash:flags.0?long ...
    user_constructor = response[offset, 4].unpack1('L<')
    offset += 4

    user_flags = response[offset, 4].unpack1('L<')
    offset += 4

    user_flags2 = response[offset, 4].unpack1('L<')
    offset += 4

    # id:long (always present)
    user_id = response[offset, 8].unpack1('Q<')
    offset += 8

    # access_hash:flags.0?long (conditional on flags.0)
    access_hash = nil
    if (user_flags & (1 << 0)) != 0
      access_hash = response[offset, 8].unpack1('Q<')
      offset += 8
    end

    # We have what we need, skip the rest of User fields

    return {
      authorization: true,
      flags: flags,
      sign_up_required: false,
      user_id: user_id,
      access_hash: access_hash
    }
  end

  raise UnexpectedConstructorError.new(constructor)
end