@@ -81,71 +81,76 @@ public static TOTPDevice registerDevice(Main main, String userId,
81
81
}
82
82
}
83
83
84
- public static TOTPDevice createDevice (Main main , AppIdentifierWithStorage appIdentifierWithStorage , TOTPDevice device )
84
+ public static TOTPDevice createDevice (Main main , AppIdentifierWithStorage appIdentifierWithStorage , String userId ,
85
+ String deviceName , int skew , int period , String secretKey , boolean verified ,
86
+ long createdAt )
85
87
throws DeviceAlreadyExistsException , StorageQueryException , FeatureNotEnabledException ,
86
88
TenantOrAppNotFoundException {
87
89
88
90
Mfa .checkForMFAFeature (appIdentifierWithStorage , main );
89
91
90
- TOTPSQLStorage totpStorage = appIdentifierWithStorage .getTOTPStorage ();
91
- try {
92
- return totpStorage .startTransaction (con -> {
93
- try {
94
- TOTPDevice existingDevice = totpStorage .getDeviceByName_Transaction (con , appIdentifierWithStorage , device .userId , device .deviceName );
95
- if (existingDevice == null ) {
96
- return totpStorage .createDevice_Transaction (con , appIdentifierWithStorage , device );
97
- } else if (!existingDevice .verified ) {
98
- totpStorage .deleteDevice_Transaction (con , appIdentifierWithStorage , device .userId , device .deviceName );
99
- return totpStorage .createDevice_Transaction (con , appIdentifierWithStorage , device );
100
- } else {
101
- throw new StorageTransactionLogicException (new DeviceAlreadyExistsException ());
92
+ if (deviceName != null ) {
93
+ TOTPSQLStorage totpStorage = appIdentifierWithStorage .getTOTPStorage ();
94
+ try {
95
+ return totpStorage .startTransaction (con -> {
96
+ try {
97
+ TOTPDevice existingDevice = totpStorage .getDeviceByName_Transaction (con , appIdentifierWithStorage , userId , deviceName );
98
+ if (existingDevice == null ) {
99
+ return totpStorage .createDevice_Transaction (con , appIdentifierWithStorage , new TOTPDevice (
100
+ userId , deviceName , secretKey , period , skew , verified , createdAt
101
+ ));
102
+ } else if (!existingDevice .verified ) {
103
+ totpStorage .deleteDevice_Transaction (con , appIdentifierWithStorage , userId , deviceName );
104
+ return totpStorage .createDevice_Transaction (con , appIdentifierWithStorage , new TOTPDevice (
105
+ userId , deviceName , secretKey , period , skew , verified , createdAt
106
+ ));
107
+ } else {
108
+ throw new StorageTransactionLogicException (new DeviceAlreadyExistsException ());
109
+ }
110
+ } catch (TenantOrAppNotFoundException | DeviceAlreadyExistsException e ) {
111
+ throw new StorageTransactionLogicException (e );
102
112
}
103
- } catch (TenantOrAppNotFoundException | DeviceAlreadyExistsException e ) {
104
- throw new StorageTransactionLogicException (e );
113
+ });
114
+ } catch (StorageTransactionLogicException e ) {
115
+ if (e .actualException instanceof DeviceAlreadyExistsException ) {
116
+ throw (DeviceAlreadyExistsException ) e .actualException ;
105
117
}
106
- });
107
- } catch (StorageTransactionLogicException e ) {
108
- if (e .actualException instanceof DeviceAlreadyExistsException ) {
109
- throw (DeviceAlreadyExistsException ) e .actualException ;
118
+ throw new StorageQueryException (e .actualException );
110
119
}
111
- throw new StorageQueryException (e .actualException );
112
120
}
113
- }
114
121
115
- public static TOTPDevice registerDevice (AppIdentifierWithStorage appIdentifierWithStorage , Main main , String userId ,
116
- String deviceName , int skew , int period )
117
- throws StorageQueryException , DeviceAlreadyExistsException , NoSuchAlgorithmException ,
118
- FeatureNotEnabledException , TenantOrAppNotFoundException , StorageTransactionLogicException {
119
-
120
- String secret = generateSecret ();
121
- TOTPDevice device = new TOTPDevice (userId , deviceName , secret , period , skew , false , System .currentTimeMillis ());
122
122
TOTPSQLStorage totpStorage = appIdentifierWithStorage .getTOTPStorage ();
123
-
124
- if (deviceName != null ) {
125
- return createDevice (main , appIdentifierWithStorage , device );
126
- }
127
-
128
- // Find number of existing devices to set device name
129
123
TOTPDevice [] devices = totpStorage .getDevices (appIdentifierWithStorage , userId );
130
124
int verifiedDevicesCount = Arrays .stream (devices ).filter (d -> d .verified ).toArray ().length ;
131
125
132
126
while (true ) {
133
127
try {
134
- return createDevice (main , appIdentifierWithStorage , new TOTPDevice (
135
- device . userId ,
128
+ return createDevice (main , appIdentifierWithStorage ,
129
+ userId ,
136
130
"TOTP Device " + verifiedDevicesCount ,
137
- device . secretKey ,
138
- device . period ,
139
- device . skew ,
140
- device . verified ,
141
- device . createdAt
142
- )) ;
131
+ skew ,
132
+ period ,
133
+ secretKey ,
134
+ verified ,
135
+ createdAt
136
+ );
143
137
} catch (DeviceAlreadyExistsException e ){
144
138
}
145
139
verifiedDevicesCount ++;
146
140
}
147
141
}
148
142
143
+ public static TOTPDevice registerDevice (AppIdentifierWithStorage appIdentifierWithStorage , Main main , String userId ,
144
+ String deviceName , int skew , int period )
145
+ throws StorageQueryException , DeviceAlreadyExistsException , NoSuchAlgorithmException ,
146
+ FeatureNotEnabledException , TenantOrAppNotFoundException , StorageTransactionLogicException {
147
+
148
+ String secretKey = generateSecret ();
149
+
150
+ return createDevice (main , appIdentifierWithStorage , userId , deviceName , skew , period , secretKey , false ,
151
+ System .currentTimeMillis ());
152
+ }
153
+
149
154
private static void checkAndStoreCode (TenantIdentifierWithStorage tenantIdentifierWithStorage , Main main ,
150
155
String userId , TOTPDevice [] devices ,
151
156
String code )
0 commit comments