forked from c0re100/go-tdlib
/
passwordState.go
180 lines (151 loc) · 7.64 KB
/
passwordState.go
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package tdlib
import (
"encoding/json"
"fmt"
)
// PasswordState Represents the current state of 2-step verification
type PasswordState struct {
tdCommon
HasPassword bool `json:"has_password"` // True, if a 2-step verification password is set
PasswordHint string `json:"password_hint"` // Hint for the password; may be empty
HasRecoveryEmailAddress bool `json:"has_recovery_email_address"` // True, if a recovery email is set
HasPassportData bool `json:"has_passport_data"` // True, if some Telegram Passport elements were saved
RecoveryEmailAddressCodeInfo *EmailAddressAuthenticationCodeInfo `json:"recovery_email_address_code_info"` // Information about the recovery email address to which the confirmation email was sent; may be null
PendingResetDate int32 `json:"pending_reset_date"` // If not 0, point in time (Unix timestamp) after which the password can be reset immediately using resetPassword
}
// MessageType return the string telegram-type of PasswordState
func (passwordState *PasswordState) MessageType() string {
return "passwordState"
}
// NewPasswordState creates a new PasswordState
//
// @param hasPassword True, if a 2-step verification password is set
// @param passwordHint Hint for the password; may be empty
// @param hasRecoveryEmailAddress True, if a recovery email is set
// @param hasPassportData True, if some Telegram Passport elements were saved
// @param recoveryEmailAddressCodeInfo Information about the recovery email address to which the confirmation email was sent; may be null
// @param pendingResetDate If not 0, point in time (Unix timestamp) after which the password can be reset immediately using resetPassword
func NewPasswordState(hasPassword bool, passwordHint string, hasRecoveryEmailAddress bool, hasPassportData bool, recoveryEmailAddressCodeInfo *EmailAddressAuthenticationCodeInfo, pendingResetDate int32) *PasswordState {
passwordStateTemp := PasswordState{
tdCommon: tdCommon{Type: "passwordState"},
HasPassword: hasPassword,
PasswordHint: passwordHint,
HasRecoveryEmailAddress: hasRecoveryEmailAddress,
HasPassportData: hasPassportData,
RecoveryEmailAddressCodeInfo: recoveryEmailAddressCodeInfo,
PendingResetDate: pendingResetDate,
}
return &passwordStateTemp
}
// GetPasswordState Returns the current state of 2-step verification
func (client *Client) GetPasswordState() (*PasswordState, error) {
result, err := client.SendAndCatch(UpdateData{
"@type": "getPasswordState",
})
if err != nil {
return nil, err
}
if result.Data["@type"].(string) == "error" {
return nil, fmt.Errorf("error! code: %v msg: %s", result.Data["code"], result.Data["message"])
}
var passwordState PasswordState
err = json.Unmarshal(result.Raw, &passwordState)
return &passwordState, err
}
// SetPassword Changes the password for the current user. If a new recovery email address is specified, then the change will not be applied until the new recovery email address is confirmed
// @param oldPassword Previous password of the user
// @param newPassword New password of the user; may be empty to remove the password
// @param newHint New password hint; may be empty
// @param setRecoveryEmailAddress Pass true if the recovery email address must be changed
// @param newRecoveryEmailAddress New recovery email address; may be empty
func (client *Client) SetPassword(oldPassword string, newPassword string, newHint string, setRecoveryEmailAddress bool, newRecoveryEmailAddress string) (*PasswordState, error) {
result, err := client.SendAndCatch(UpdateData{
"@type": "setPassword",
"old_password": oldPassword,
"new_password": newPassword,
"new_hint": newHint,
"set_recovery_email_address": setRecoveryEmailAddress,
"new_recovery_email_address": newRecoveryEmailAddress,
})
if err != nil {
return nil, err
}
if result.Data["@type"].(string) == "error" {
return nil, fmt.Errorf("error! code: %v msg: %s", result.Data["code"], result.Data["message"])
}
var passwordState PasswordState
err = json.Unmarshal(result.Raw, &passwordState)
return &passwordState, err
}
// SetRecoveryEmailAddress Changes the 2-step verification recovery email address of the user. If a new recovery email address is specified, then the change will not be applied until the new recovery email address is confirmed. If new_recovery_email_address is the same as the email address that is currently set up, this call succeeds immediately and aborts all other requests waiting for an email confirmation
// @param password Password of the current user
// @param newRecoveryEmailAddress New recovery email address
func (client *Client) SetRecoveryEmailAddress(password string, newRecoveryEmailAddress string) (*PasswordState, error) {
result, err := client.SendAndCatch(UpdateData{
"@type": "setRecoveryEmailAddress",
"password": password,
"new_recovery_email_address": newRecoveryEmailAddress,
})
if err != nil {
return nil, err
}
if result.Data["@type"].(string) == "error" {
return nil, fmt.Errorf("error! code: %v msg: %s", result.Data["code"], result.Data["message"])
}
var passwordState PasswordState
err = json.Unmarshal(result.Raw, &passwordState)
return &passwordState, err
}
// CheckRecoveryEmailAddressCode Checks the 2-step verification recovery email address verification code
// @param code Verification code to check
func (client *Client) CheckRecoveryEmailAddressCode(code string) (*PasswordState, error) {
result, err := client.SendAndCatch(UpdateData{
"@type": "checkRecoveryEmailAddressCode",
"code": code,
})
if err != nil {
return nil, err
}
if result.Data["@type"].(string) == "error" {
return nil, fmt.Errorf("error! code: %v msg: %s", result.Data["code"], result.Data["message"])
}
var passwordState PasswordState
err = json.Unmarshal(result.Raw, &passwordState)
return &passwordState, err
}
// ResendRecoveryEmailAddressCode Resends the 2-step verification recovery email address verification code
func (client *Client) ResendRecoveryEmailAddressCode() (*PasswordState, error) {
result, err := client.SendAndCatch(UpdateData{
"@type": "resendRecoveryEmailAddressCode",
})
if err != nil {
return nil, err
}
if result.Data["@type"].(string) == "error" {
return nil, fmt.Errorf("error! code: %v msg: %s", result.Data["code"], result.Data["message"])
}
var passwordState PasswordState
err = json.Unmarshal(result.Raw, &passwordState)
return &passwordState, err
}
// RecoverPassword Recovers the 2-step verification password using a recovery code sent to an email address that was previously set up
// @param recoveryCode Recovery code to check
// @param newPassword New password of the user; may be empty to remove the password
// @param newHint New password hint; may be empty
func (client *Client) RecoverPassword(recoveryCode string, newPassword string, newHint string) (*PasswordState, error) {
result, err := client.SendAndCatch(UpdateData{
"@type": "recoverPassword",
"recovery_code": recoveryCode,
"new_password": newPassword,
"new_hint": newHint,
})
if err != nil {
return nil, err
}
if result.Data["@type"].(string) == "error" {
return nil, fmt.Errorf("error! code: %v msg: %s", result.Data["code"], result.Data["message"])
}
var passwordState PasswordState
err = json.Unmarshal(result.Raw, &passwordState)
return &passwordState, err
}