1
1
package com .instagram .api .user ;
2
2
3
+ import com .instagram .api .APIException ;
3
4
import com .instagram .api .Constants ;
4
5
5
6
import javax .crypto .Cipher ;
6
7
import javax .crypto .spec .GCMParameterSpec ;
7
8
import javax .crypto .spec .SecretKeySpec ;
8
9
import javax .net .ssl .HttpsURLConnection ;
10
+ import java .io .IOException ;
9
11
import java .nio .ByteBuffer ;
10
12
import java .security .GeneralSecurityException ;
11
13
import java .security .KeyFactory ;
12
14
import java .security .PublicKey ;
13
15
import java .security .SecureRandom ;
14
16
import java .security .spec .X509EncodedKeySpec ;
15
17
import java .util .Base64 ;
16
- import java .util .List ;
18
+ import java .util .HashMap ;
17
19
import java .util .Map ;
20
+ import java .util .logging .Level ;
21
+
22
+ import static osintgram4j .commons .AppConstants .log_net ;
18
23
19
24
public class UserManager {
20
25
26
+ /*
27
+ private static final String FILTER_PLACEHOLDER_NAME = "__name__";
28
+ private static final String FILTER_PLACEHOLDER_ID = "__userID__";
29
+ private static final String FILTER_PLACEHOLDER_SESSION_ID = "__sessionID__";
30
+ */
31
+
21
32
/*
22
33
TODO: Steps on encrypting the password, and logging in
23
34
1. Retrieve the header values (see below comment from TypeScript/JavaScript)
@@ -33,7 +44,30 @@ public class UserManager {
33
44
'ig-set-password-encryption-pub-key': pwPubKey,
34
45
*/
35
46
36
- public static User login (String username , String password ) {
47
+ public static User login (String username , String password ) throws IOException , APIException {
48
+ if (Constants .Privates .PASS_ENC_KEY_ID == null )
49
+ throw new NullPointerException ("Password Encryption Key ID has not been initialized" );
50
+
51
+ if (Constants .Privates .PASS_ENC_PUB_KEY == null )
52
+ throw new NullPointerException ("Password Encryption Public Key has not been initialized" );
53
+
54
+ if (Constants .Privates .IG_AUTH_HEADER == null )
55
+ throw new NullPointerException ("Instagram Authentication Header has not been initialized" );
56
+
57
+ if (Constants .Privates .WWW_CLAIM == null )
58
+ throw new NullPointerException ("Instagram WWW Claim has not been initialized" );
59
+
60
+ String encPass ;
61
+ try {
62
+ encPass = PasswordEncryption .toEncryptedPassword (password .toCharArray ());
63
+ } catch (GeneralSecurityException ex ) {
64
+ log_net .log (Level .SEVERE , "Failed to encrypt password" , ex );
65
+ throw new APIException (ex );
66
+ }
67
+
68
+ Map <String , String > loginHeaders = new HashMap <>(Constants .putDefaultHeaders ());
69
+ loginHeaders .put ("enc_password" , PasswordEncryption .writePassword (encPass ));
70
+
37
71
//https://www.instagram.com/api/v1/accounts/login/ajax
38
72
//https://www.instagram.com/api/v1/accounts/login/ajax?force_classic_login
39
73
@@ -167,7 +201,7 @@ public static String writePassword(String encryptedPassword) {
167
201
return String .format ("#PWD_INSTAGRAM:4:%d:%s" , timestamp , encryptedPassword );
168
202
}
169
203
170
- public static String toEncryptedPassword (String password ) throws GeneralSecurityException {
204
+ public static String toEncryptedPassword (char [] password ) throws GeneralSecurityException {
171
205
KeyFactory keyFact = KeyFactory .getInstance ("RSA" );
172
206
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec (Base64 .getDecoder ().decode (Constants .Privates .PASS_ENC_PUB_KEY ));
173
207
PublicKey publicKey = keyFact .generatePublic (pubKeySpec );
@@ -189,7 +223,8 @@ public static String toEncryptedPassword(String password) throws GeneralSecurity
189
223
long time = System .currentTimeMillis () / 1000L ;
190
224
aesCipher .updateAAD (String .valueOf (time ).getBytes ());
191
225
192
- byte [] aesEncrypted = aesCipher .doFinal (password .getBytes ());
226
+ String _pass = new String (password );
227
+ byte [] aesEncrypted = aesCipher .doFinal (_pass .getBytes ());
193
228
byte [] sizeBuffer = ByteBuffer .allocate (2 ).putShort ((short ) rsaEncrypted .length ).array ();
194
229
byte [] authTag = aesCipher .getIV ();
195
230
0 commit comments