@@ -27,11 +27,13 @@ import androidx.test.platform.app.InstrumentationRegistry
2727import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_EXPIRATION_DATE
2828import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_ID
2929import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_SECRET
30+ import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD
3031import com.owncloud.android.data.authentication.KEY_OAUTH2_REFRESH_TOKEN
3132import com.owncloud.android.data.authentication.KEY_OAUTH2_SCOPE
3233import com.owncloud.android.data.authentication.SELECTED_ACCOUNT
3334import com.owncloud.android.data.providers.SharedPreferencesProvider
3435import com.owncloud.android.domain.authentication.oauth.model.ClientRegistrationInfo
36+ import com.owncloud.android.domain.authentication.oauth.model.OIDCServerConfiguration
3537import com.owncloud.android.domain.exceptions.AccountNotFoundException
3638import com.owncloud.android.domain.exceptions.AccountNotNewException
3739import com.owncloud.android.domain.exceptions.AccountNotTheSameException
@@ -290,6 +292,159 @@ class OCLocalAuthenticationDataSourceTest {
290292 }
291293 }
292294
295+ @Test
296+ fun addOAuthAccountStoresPostAuthMethod () {
297+ mockRegularAccountCreationFlow()
298+ mockSelectedAccountNameInPreferences()
299+
300+ val postRegistration = OC_CLIENT_REGISTRATION .copy(
301+ tokenEndpointAuthMethod = OIDCServerConfiguration .CLIENT_SECRET_POST
302+ )
303+ val newAccountName = ocLocalAuthenticationDataSource.addOAuthAccount(
304+ OC_ACCOUNT_ID ,
305+ OC_REDIRECTION_PATH .lastPermanentLocation,
306+ OC_AUTH_TOKEN_TYPE ,
307+ OC_ACCESS_TOKEN ,
308+ OC_SECURE_SERVER_INFO_BASIC_AUTH ,
309+ OC_USER_INFO ,
310+ OC_REFRESH_TOKEN ,
311+ OC_SCOPE ,
312+ null ,
313+ postRegistration
314+ )
315+
316+ val newAccount = Account (OC_ACCOUNT_NAME , OC_ACCOUNT .type)
317+ assertEquals(newAccount.name, newAccountName)
318+ verifyOAuthParamsAreUpdated(
319+ newAccount,
320+ OC_ACCESS_TOKEN ,
321+ OC_OAUTH_SUPPORTED_TRUE ,
322+ OC_REFRESH_TOKEN ,
323+ OC_SCOPE ,
324+ postRegistration,
325+ 1
326+ )
327+ }
328+
329+ @Test
330+ fun addOAuthAccountReauthorizationReplacesAuthMethod () {
331+ mockSelectedAccountNameInPreferences()
332+
333+ val postRegistration = OC_CLIENT_REGISTRATION .copy(
334+ tokenEndpointAuthMethod = OIDCServerConfiguration .CLIENT_SECRET_POST
335+ )
336+ val accountName = ocLocalAuthenticationDataSource.addOAuthAccount(
337+ OC_ACCOUNT_ID ,
338+ OC_REDIRECTION_PATH .lastPermanentLocation,
339+ OC_AUTH_TOKEN_TYPE ,
340+ OC_ACCESS_TOKEN ,
341+ OC_SECURE_SERVER_INFO_BASIC_AUTH ,
342+ OC_USER_INFO .copy(id = OC_ACCOUNT_ID ),
343+ OC_REFRESH_TOKEN ,
344+ OC_SCOPE ,
345+ OC_ACCOUNT_NAME ,
346+ postRegistration
347+ )
348+
349+ assertEquals(OC_ACCOUNT_NAME , accountName)
350+ verifyOAuthParamsAreUpdated(
351+ OC_ACCOUNT ,
352+ OC_ACCESS_TOKEN ,
353+ OC_OAUTH_SUPPORTED_TRUE ,
354+ OC_REFRESH_TOKEN ,
355+ OC_SCOPE ,
356+ postRegistration,
357+ 1
358+ )
359+ }
360+
361+ @Test
362+ fun addOAuthAccountIsolatesAuthMethodBetweenAccounts () {
363+ mockRegularAccountCreationFlow()
364+ mockSelectedAccountNameInPreferences()
365+
366+ val postRegistration = OC_CLIENT_REGISTRATION .copy(
367+ tokenEndpointAuthMethod = OIDCServerConfiguration .CLIENT_SECRET_POST
368+ )
369+ ocLocalAuthenticationDataSource.addOAuthAccount(
370+ OC_ACCOUNT_ID ,
371+ OC_REDIRECTION_PATH .lastPermanentLocation,
372+ OC_AUTH_TOKEN_TYPE ,
373+ OC_ACCESS_TOKEN ,
374+ OC_SECURE_SERVER_INFO_BASIC_AUTH ,
375+ OC_USER_INFO ,
376+ OC_REFRESH_TOKEN ,
377+ OC_SCOPE ,
378+ null ,
379+ postRegistration
380+ )
381+
382+ val firstAccount = Account (OC_ACCOUNT_NAME , OC_ACCOUNT .type)
383+ val secondAccount = Account (" other@demo.owncloud.com" , OC_ACCOUNT .type)
384+ mockGetAccountsByType(OC_ACCOUNT .type, arrayOf(firstAccount))
385+
386+ ocLocalAuthenticationDataSource.addOAuthAccount(
387+ " other" ,
388+ OC_REDIRECTION_PATH .lastPermanentLocation,
389+ OC_AUTH_TOKEN_TYPE ,
390+ OC_ACCESS_TOKEN ,
391+ OC_SECURE_SERVER_INFO_BASIC_AUTH ,
392+ OC_USER_INFO ,
393+ OC_REFRESH_TOKEN ,
394+ OC_SCOPE ,
395+ null ,
396+ OC_CLIENT_REGISTRATION
397+ )
398+
399+ verify(exactly = 1 ) {
400+ accountManager.setUserData(
401+ firstAccount,
402+ KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD ,
403+ OIDCServerConfiguration .CLIENT_SECRET_POST
404+ )
405+ }
406+ verify(exactly = 0 ) {
407+ accountManager.setUserData(
408+ firstAccount,
409+ KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD ,
410+ OIDCServerConfiguration .CLIENT_SECRET_BASIC
411+ )
412+ }
413+ verify(exactly = 1 ) {
414+ accountManager.setUserData(
415+ secondAccount,
416+ KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD ,
417+ OC_CLIENT_REGISTRATION .tokenEndpointAuthMethod
418+ )
419+ }
420+ }
421+
422+ @Test
423+ fun addOAuthAccountWithoutRegistrationDoesNotStoreAuthMethod () {
424+ mockRegularAccountCreationFlow()
425+ mockSelectedAccountNameInPreferences()
426+
427+ ocLocalAuthenticationDataSource.addOAuthAccount(
428+ OC_ACCOUNT_ID ,
429+ OC_REDIRECTION_PATH .lastPermanentLocation,
430+ OC_AUTH_TOKEN_TYPE ,
431+ OC_ACCESS_TOKEN ,
432+ OC_SECURE_SERVER_INFO_BASIC_AUTH ,
433+ OC_USER_INFO ,
434+ OC_REFRESH_TOKEN ,
435+ OC_SCOPE ,
436+ null ,
437+ null
438+ )
439+
440+ verify(exactly = 0 ) {
441+ accountManager.setUserData(any(), KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD , any())
442+ accountManager.setUserData(any(), KEY_CLIENT_REGISTRATION_CLIENT_ID , any())
443+ accountManager.setUserData(any(), KEY_CLIENT_REGISTRATION_CLIENT_SECRET , any())
444+ accountManager.setUserData(any(), KEY_CLIENT_REGISTRATION_CLIENT_EXPIRATION_DATE , any())
445+ }
446+ }
447+
293448 @Test
294449 fun supportsOAuth2Ok () {
295450
@@ -409,6 +564,11 @@ class OCLocalAuthenticationDataSourceTest {
409564 accountManager.setUserData(account, KEY_CLIENT_REGISTRATION_CLIENT_SECRET , clientInfo.clientSecret)
410565 accountManager.setUserData(account, KEY_CLIENT_REGISTRATION_CLIENT_ID , clientInfo.clientId)
411566 accountManager.setUserData(account, KEY_CLIENT_REGISTRATION_CLIENT_EXPIRATION_DATE , clientInfo.clientSecretExpiration.toString())
567+ accountManager.setUserData(
568+ account,
569+ KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD ,
570+ clientInfo.tokenEndpointAuthMethod
571+ )
412572 }
413573 }
414574
0 commit comments